├── .gitignore ├── LICENSE.txt ├── README.md ├── phrase-commons ├── phrase-commons.iml ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── pddstudio │ │ │ └── phrase │ │ │ └── java │ │ │ └── commons │ │ │ ├── config │ │ │ └── Config.java │ │ │ ├── log │ │ │ └── Logger.java │ │ │ └── tag │ │ │ ├── BaseTag.java │ │ │ ├── ITag.java │ │ │ ├── Pair.java │ │ │ ├── Tag.java │ │ │ ├── TagFinder.java │ │ │ ├── TagProcessor.java │ │ │ └── TagResult.java │ └── resources │ │ ├── debug │ │ └── configuration.properties │ │ └── release │ │ └── configuration.properties │ └── test │ └── java │ └── com │ └── pddstudio │ └── phrase │ └── java │ └── commons │ ├── BaseUtilityTest.java │ └── TagFinderTest.java ├── phrase-java ├── phrase-java.iml ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── pddstudio │ │ └── phrase │ │ └── java │ │ ├── Pair.java │ │ └── Phrase.java │ └── test │ └── java │ └── com │ └── pddstudio │ └── phrase │ └── java │ ├── BaseUtilityTest.java │ └── PhraseTest.java ├── phrases-java.iml └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | target/ 3 | phrase-java/target 4 | phrase-common/target 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Phrase - Java and Android String Template *Engine* 2 | ================================== 3 | 4 | Based upon [Phrase by Square](https://github.com/square/phrase). 5 | 6 | A small String Template Library (10k) for Android and Java. 7 | 8 | [![license](http://img.shields.io/badge/license-apache_2.0-red.svg?style=flat)](https://raw.githubusercontent.com/pddstudio/phrase-java/master/LICENSE.txt) [![](https://jitpack.io/v/PDDStudio/phrase-java.svg)](https://jitpack.io/#PDDStudio/phrase-java) 9 | 10 | API 11 | --- 12 | 13 | The usage is as simple as shown in the snippet below: 14 | 15 | ```java 16 | CharSequence formatted = Phrase.from("Hi {first_name}, you are {age} years old.") 17 | .put("first_name", firstName) 18 | .put("age", age) 19 | .format(); 20 | ``` 21 | 22 | Download 23 | -------- 24 | 25 | You can download [the latest jar here](https://github.com/PDDStudio/phrase-java/releases). 26 | 27 | You can also depend on the .jar through JitPack: 28 | 29 | ```xml 30 | 31 | 32 | 33 | jitpack.io 34 | https://jitpack.io 35 | 36 | 37 | 38 | 39 | 40 | com.github.pddstudio 41 | phrase 42 | (insert latest version) 43 | 44 | ``` 45 | 46 | or through Gradle: 47 | 48 | ```groovy 49 | 50 | //top level build.gradle 51 | allprojects { 52 | repositories { 53 | ... 54 | maven { url 'https://jitpack.io' } 55 | } 56 | } 57 | 58 | //module dependencies 59 | dependencies { 60 | compile 'com.github.pddstudio:phrase-java:(latest version)' 61 | } 62 | ``` 63 | 64 | License 65 | ------- 66 | 67 | Copyright 2017 Patrick Jung 68 | Copyright 2013 Square, Inc. 69 | 70 | Licensed under the Apache License, Version 2.0 (the "License"); 71 | you may not use this file except in compliance with the License. 72 | You may obtain a copy of the License at 73 | 74 | http://www.apache.org/licenses/LICENSE-2.0 75 | 76 | Unless required by applicable law or agreed to in writing, software 77 | distributed under the License is distributed on an "AS IS" BASIS, 78 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 79 | See the License for the specific language governing permissions and 80 | limitations under the License. 81 | -------------------------------------------------------------------------------- /phrase-commons/phrase-commons.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /phrase-commons/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | phrase-java-root 6 | com.pddstudio 7 | 1.0.0 8 | 9 | 4.0.0 10 | 11 | phrase-commons 12 | ${phrase.commons.version} 13 | jar 14 | 15 | 16 | 17 | src/main/resources/debug 18 | src/main/resources/release 19 | 20 | 3.4 21 | 2.1 22 | 1.9.2 23 | 1.10 24 | 25 | 26 | 27 | 28 | 29 | debug 30 | 31 | 32 | 33 | ${properties.configuration.debug} 34 | 35 | 36 | ${project.artifactId}-${project.version}-DEBUG 37 | 38 | 39 | 40 | release 41 | 42 | true 43 | 44 | 45 | 46 | 47 | ${properties.configuration.release} 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | org.apache.commons 59 | commons-lang3 60 | ${dependency.apache.commons.lang3} 61 | 62 | 63 | org.apache.commons 64 | commons-configuration2 65 | ${dependency.apache.commons.configuration2} 66 | 67 | 68 | commons-beanutils 69 | commons-beanutils 70 | ${dependency.apache.commons.beanutils} 71 | 72 | 73 | commons-codec 74 | commons-codec 75 | ${dependency.apache.commons.codec} 76 | true 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /phrase-commons/src/main/java/com/pddstudio/phrase/java/commons/config/Config.java: -------------------------------------------------------------------------------- 1 | package com.pddstudio.phrase.java.commons.config; 2 | 3 | import org.apache.commons.configuration2.Configuration; 4 | import org.apache.commons.configuration2.builder.fluent.Configurations; 5 | import org.apache.commons.configuration2.ex.ConfigurationException; 6 | 7 | import java.io.File; 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | /** 12 | * Created by pddstudio on 17/10/2016. 13 | */ 14 | public final class Config { 15 | 16 | private static Config config; 17 | 18 | private static final Map defaultConfig; 19 | 20 | static { 21 | //create the fallback default config 22 | defaultConfig = new HashMap<>(); 23 | //set the default values 24 | defaultConfig.put("logging.enabled", false); 25 | defaultConfig.put("build.profile", "???"); 26 | } 27 | 28 | private static Config getConfig() { 29 | if(config == null) { 30 | config = new Config(); 31 | } 32 | return config; 33 | } 34 | 35 | private final Configuration properties; 36 | private final boolean initSuccess; 37 | 38 | private Config() { 39 | Configuration configuration; 40 | try { 41 | Configurations configurations = new Configurations(); 42 | 43 | configuration = configurations.properties(new File("configuration.properties")); 44 | } catch (ConfigurationException ce) { 45 | ce.printStackTrace(); 46 | configuration = null; 47 | } 48 | this.properties = configuration; 49 | this.initSuccess = configuration != null; 50 | } 51 | 52 | private T getProperty(String propertyName, Class objClass) { 53 | if(initSuccess) { 54 | return properties.get(objClass, propertyName); 55 | } else { 56 | Object property = defaultConfig.get(propertyName); 57 | return objClass.cast(property); 58 | } 59 | } 60 | 61 | public static boolean loggingEnabled() { 62 | return getConfig().getProperty("logging.enabled", Boolean.class); 63 | } 64 | 65 | public static String getBuildProfile() { 66 | return getConfig().getProperty("build.profile", String.class); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /phrase-commons/src/main/java/com/pddstudio/phrase/java/commons/log/Logger.java: -------------------------------------------------------------------------------- 1 | package com.pddstudio.phrase.java.commons.log; 2 | 3 | import com.pddstudio.phrase.java.commons.config.Config; 4 | 5 | /** 6 | * Created by pddstudio on 16/10/2016. 7 | */ 8 | public class Logger { 9 | 10 | private static final Object LOCK = new Object(); 11 | 12 | public static Logger getLogger() { 13 | return new Logger(null); 14 | } 15 | 16 | public static Logger getLogger(CharSequence tagPrefix) { 17 | return new Logger(tagPrefix); 18 | } 19 | 20 | public static Logger getLogger(Class clazz) { 21 | return getLogger(clazz.getSimpleName()); 22 | } 23 | 24 | private final CharSequence tag; 25 | private final boolean enabled; 26 | 27 | private Logger(CharSequence tagPrefix) { 28 | if(tagPrefix == null || tagPrefix.length() == 0) { 29 | this.tag = null; 30 | } else { 31 | this.tag = tagPrefix; 32 | } 33 | this.enabled = Config.loggingEnabled(); 34 | System.out.println("Logger initialized! Status: [buildProfile=" + Config.getBuildProfile() + "][enabled=" + enabled + "]"); 35 | } 36 | 37 | private void logMsg(String message) { 38 | synchronized (LOCK) { 39 | if(enabled) { 40 | System.out.println(message); 41 | } 42 | } 43 | } 44 | 45 | private String addPrefix(String toMessage) { 46 | if(tag != null) { 47 | toMessage = "[" + tag + "] " + toMessage; 48 | } 49 | return toMessage; 50 | } 51 | 52 | public void log(String message, Object... args) { 53 | String msg = String.format(message, args); 54 | logMsg(addPrefix(msg)); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /phrase-commons/src/main/java/com/pddstudio/phrase/java/commons/tag/BaseTag.java: -------------------------------------------------------------------------------- 1 | package com.pddstudio.phrase.java.commons.tag; 2 | 3 | /** 4 | * Created by pddstudio on 16/10/2016. 5 | */ 6 | public abstract class BaseTag implements ITag { 7 | 8 | private final CharSequence tagId; 9 | 10 | public BaseTag(String tagName) { 11 | this.tagId = tagName; 12 | } 13 | 14 | @Override 15 | public CharSequence getTagName() { 16 | return tagId; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /phrase-commons/src/main/java/com/pddstudio/phrase/java/commons/tag/ITag.java: -------------------------------------------------------------------------------- 1 | package com.pddstudio.phrase.java.commons.tag; 2 | 3 | /** 4 | * Created by pddstudio on 16/10/2016. 5 | */ 6 | public interface ITag { 7 | CharSequence getTagName(); 8 | Pair getTagTypes(); 9 | CharSequence getStartTag(); 10 | CharSequence getEndTag(); 11 | } 12 | -------------------------------------------------------------------------------- /phrase-commons/src/main/java/com/pddstudio/phrase/java/commons/tag/Pair.java: -------------------------------------------------------------------------------- 1 | package com.pddstudio.phrase.java.commons.tag; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Created by pddstudio on 29/11/2016. 7 | */ 8 | public class Pair implements Serializable { 9 | 10 | private final K key; 11 | private final V value; 12 | 13 | public Pair(K key, V value) { 14 | this.key = key; 15 | this.value = value; 16 | } 17 | 18 | public K getKey() { 19 | return key; 20 | } 21 | 22 | public V getValue() { 23 | return value; 24 | } 25 | 26 | @Override 27 | public boolean equals(Object o) { 28 | if (this == o) return true; 29 | if (o instanceof Pair) { 30 | Pair pair = (Pair) o; 31 | if (key != null ? !key.equals(pair.key) : pair.key != null) return false; 32 | if (value != null ? !value.equals(pair.value) : pair.value != null) return false; 33 | return true; 34 | } 35 | return false; 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | return key + "=" + value; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /phrase-commons/src/main/java/com/pddstudio/phrase/java/commons/tag/Tag.java: -------------------------------------------------------------------------------- 1 | package com.pddstudio.phrase.java.commons.tag; 2 | 3 | /** 4 | * Created by pddstudio on 16/10/2016. 5 | */ 6 | public class Tag extends BaseTag { 7 | 8 | private final CharSequence tagStart; 9 | private final CharSequence tagEnd; 10 | 11 | public Tag(String identifier, String startTag, String endTag) { 12 | super(identifier); 13 | this.tagStart = startTag; 14 | this.tagEnd = endTag; 15 | } 16 | 17 | @Override 18 | public Pair getTagTypes() { 19 | return new Pair<>(tagStart, tagEnd); 20 | } 21 | 22 | @Override 23 | public CharSequence getStartTag() { 24 | return tagStart; 25 | } 26 | 27 | @Override 28 | public CharSequence getEndTag() { 29 | return tagEnd; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /phrase-commons/src/main/java/com/pddstudio/phrase/java/commons/tag/TagFinder.java: -------------------------------------------------------------------------------- 1 | package com.pddstudio.phrase.java.commons.tag; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | /** 10 | * Created by pddstudio on 16/10/2016. 11 | */ 12 | public class TagFinder { 13 | 14 | public static TagFinder in(CharSequence targetCharSequence) { 15 | return new TagFinder(targetCharSequence); 16 | } 17 | 18 | private final CharSequence target; 19 | private final List tagList; 20 | private final Map resultMap; 21 | private TagProcessor tagProcessor; 22 | 23 | private TagFinder(CharSequence target) { 24 | this.target = target; 25 | this.tagList = new ArrayList<>(); 26 | this.resultMap = new HashMap<>(); 27 | } 28 | 29 | public TagFinder find(ITag... tags) { 30 | tagList.addAll(Arrays.asList(tags)); 31 | return this; 32 | } 33 | 34 | public TagResult getResult(ITag forTag) { 35 | return getResult(forTag.getTagName()); 36 | } 37 | 38 | public TagResult getResult(CharSequence forTagName) { 39 | return resultMap.get(forTagName); 40 | } 41 | 42 | public void execute() { 43 | for(ITag tag : tagList) { 44 | tagProcessor = TagProcessor.create(tag); 45 | TagResult result = tagProcessor.execute(target); 46 | resultMap.put(tag.getTagName(), result); 47 | } 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /phrase-commons/src/main/java/com/pddstudio/phrase/java/commons/tag/TagProcessor.java: -------------------------------------------------------------------------------- 1 | package com.pddstudio.phrase.java.commons.tag; 2 | 3 | import com.pddstudio.phrase.java.commons.log.Logger; 4 | 5 | import org.apache.commons.lang3.StringUtils; 6 | 7 | /** 8 | * Created by pddstudio on 16/10/2016. 9 | */ 10 | public class TagProcessor { 11 | 12 | static TagProcessor create(ITag tag) { 13 | return new TagProcessor(tag); 14 | } 15 | 16 | private final ITag tag; 17 | private final TagResult tagResult; 18 | private final Logger logger; 19 | 20 | private CharSequence target; 21 | 22 | private TagProcessor(ITag tag) { 23 | this.tag = tag; 24 | this.tagResult = new TagResult(tag); 25 | this.logger = Logger.getLogger(TagProcessor.class); 26 | } 27 | 28 | public TagResult execute(CharSequence target) { 29 | logger.log("execute() called => target : %s", target); 30 | this.target = target; 31 | findTagContent(); 32 | return tagResult; 33 | } 34 | 35 | private void findTagContent() { 36 | int startTags = StringUtils.countMatches(target, tag.getStartTag()); 37 | int endTags = StringUtils.countMatches(target, tag.getEndTag()); 38 | //make sure the amount of start and end tags are equal 39 | if(startTags == endTags) { 40 | String[] results = StringUtils.substringsBetween(target.toString(), tag.getStartTag().toString(), tag.getEndTag().toString()); 41 | for(String result : results) { 42 | tagResult.addResult(result); 43 | logger.log("Found Tag: %s", result); 44 | } 45 | } else { 46 | //TODO: throw exception or something 47 | logger.log("Tag counts don't match. Found %s start tags and %s end tags!", startTags, endTags); 48 | } 49 | } 50 | 51 | 52 | } 53 | -------------------------------------------------------------------------------- /phrase-commons/src/main/java/com/pddstudio/phrase/java/commons/tag/TagResult.java: -------------------------------------------------------------------------------- 1 | package com.pddstudio.phrase.java.commons.tag; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by pddstudio on 16/10/2016. 8 | */ 9 | public class TagResult { 10 | 11 | private final ITag targetTag; 12 | private final List resultTexts; 13 | 14 | TagResult(ITag target) { 15 | this.targetTag = target; 16 | this.resultTexts = new ArrayList<>(); 17 | } 18 | 19 | void addResult(CharSequence result) { 20 | resultTexts.add(result); 21 | } 22 | 23 | public boolean isPresent() { 24 | return !resultTexts.isEmpty(); 25 | } 26 | 27 | public List getResults() { 28 | return resultTexts; 29 | } 30 | 31 | public ITag getTargetTag() { 32 | return targetTag; 33 | } 34 | 35 | public int getResultCount() { 36 | return resultTexts.size(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /phrase-commons/src/main/resources/debug/configuration.properties: -------------------------------------------------------------------------------- 1 | build.profile = debug 2 | logging.enabled = true -------------------------------------------------------------------------------- /phrase-commons/src/main/resources/release/configuration.properties: -------------------------------------------------------------------------------- 1 | build.profile = release 2 | logging.enabled = false -------------------------------------------------------------------------------- /phrase-commons/src/test/java/com/pddstudio/phrase/java/commons/BaseUtilityTest.java: -------------------------------------------------------------------------------- 1 | package com.pddstudio.phrase.java.commons; 2 | 3 | /** 4 | * Created by pddstudio on 17/10/2016. 5 | */ 6 | public abstract class BaseUtilityTest {} 7 | -------------------------------------------------------------------------------- /phrase-commons/src/test/java/com/pddstudio/phrase/java/commons/TagFinderTest.java: -------------------------------------------------------------------------------- 1 | package com.pddstudio.phrase.java.commons; 2 | 3 | import com.pddstudio.phrase.java.commons.tag.Tag; 4 | import com.pddstudio.phrase.java.commons.tag.TagResult; 5 | import com.pddstudio.phrase.java.commons.tag.TagFinder; 6 | 7 | import org.junit.Test; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | import static org.junit.Assert.assertTrue; 11 | 12 | /** 13 | * Created by pddstudio on 16/10/2016. 14 | */ 15 | public class TagFinderTest extends BaseUtilityTest { 16 | 17 | @Test 18 | public void simpleSingleTagTest() { 19 | //prepare dummy string and tag 20 | String targetString = "Hello, this is a simple test String"; 21 | Tag nameTag = new Tag("nameTag", "", ""); 22 | 23 | //execute the request 24 | TagFinder tagFinder = TagFinder.in(targetString).find(nameTag); 25 | tagFinder.execute(); 26 | TagResult result = tagFinder.getResult(nameTag); 27 | 28 | assertTrue(result.isPresent()); 29 | assertEquals(result.getResults().get(0), "String"); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /phrase-java/phrase-java.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /phrase-java/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | phrase-java-root 6 | com.pddstudio 7 | 1.0.0 8 | 9 | 4.0.0 10 | 11 | phrase-java 12 | ${phrase.java.version} 13 | jar 14 | 15 | phrase-java 16 | A ported - Java based only - fork of Phrase for Android 17 | https://github.com/pddstudio/phrase-java 18 | 19 | -------------------------------------------------------------------------------- /phrase-java/src/main/java/com/pddstudio/phrase/java/Pair.java: -------------------------------------------------------------------------------- 1 | package com.pddstudio.phrase.java; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Created by pddstudio on 29/11/2016. 7 | */ 8 | public class Pair implements Serializable { 9 | 10 | private final K key; 11 | private final V value; 12 | 13 | public Pair(K key, V value) { 14 | this.key = key; 15 | this.value = value; 16 | } 17 | 18 | public K getKey() { 19 | return key; 20 | } 21 | 22 | public V getValue() { 23 | return value; 24 | } 25 | 26 | @Override 27 | public boolean equals(Object o) { 28 | if (this == o) return true; 29 | if (o instanceof Pair) { 30 | Pair pair = (Pair) o; 31 | if (key != null ? !key.equals(pair.key) : pair.key != null) return false; 32 | if (value != null ? !value.equals(pair.value) : pair.value != null) return false; 33 | return true; 34 | } 35 | return false; 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | return key + "=" + value; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /phrase-java/src/main/java/com/pddstudio/phrase/java/Phrase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Patrick J 3 | * Copyright (C) 2013 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.pddstudio.phrase.java; 18 | 19 | import java.lang.reflect.Array; 20 | import java.util.Arrays; 21 | import java.util.HashMap; 22 | import java.util.HashSet; 23 | import java.util.Iterator; 24 | import java.util.List; 25 | import java.util.Map; 26 | import java.util.Set; 27 | 28 | /** 29 | * A fluent API for formatting Strings. Canonical usage: 30 | *
 31 |  *   CharSequence formatted = Phrase.from("Hi {first_name}, you are {age} years old.")
 32 |  *       .put("first_name", firstName)
 33 |  *       .put("age", age)
 34 |  *       .format();
 35 |  * 
36 | *
    37 | *
  • Surround keys with curly braces; use two {{ to escape.
  • 38 | *
  • Keys start with lowercase letters followed by lowercase letters and underscores.
  • 39 | *
  • Spans are preserved, such as simple HTML tags found in strings.xml.
  • 40 | *
  • Fails fast on any mismatched keys.
  • 41 | *
42 | * The constructor parses the original pattern into a doubly-linked list of {@link Token}s. 43 | * These tokens do not modify the original pattern, thus preserving any spans. 44 | *

45 | * The {@link #format()} method iterates over the tokens, replacing text as it iterates. The 46 | * doubly-linked list allows each token to ask its predecessor for the expanded length. 47 | */ 48 | public final class Phrase { 49 | 50 | private static final String DEFAULT_SEPARATOR = " "; 51 | 52 | /** The unmodified original pattern. */ 53 | private final CharSequence pattern; 54 | 55 | /** The identification type for keys */ 56 | private final KeyIdentifier keyIdentifier; 57 | 58 | /** All keys parsed from the original pattern, sans braces. */ 59 | private final Set keys = new HashSet(); 60 | private final Map keysToValues = new HashMap(); 61 | 62 | /** Cached result after replacing all keys with corresponding values. */ 63 | private CharSequence formatted; 64 | 65 | /** The constructor parses the original pattern into this doubly-linked list of tokens. */ 66 | private Token head; 67 | 68 | /** When parsing, this is the current character. */ 69 | private char curChar; 70 | private int curCharIndex; 71 | 72 | /** Indicates parsing is complete. */ 73 | private static final int EOF = 0; 74 | 75 | /** 76 | * Entry point into this API; pattern must be non-null. 77 | * 78 | * @throws IllegalArgumentException if pattern contains any syntax errors. 79 | */ 80 | public static Phrase from(CharSequence pattern) { 81 | return new Phrase(pattern, KeyIdentifier.CURLY_BRACKETS); 82 | } 83 | 84 | /** 85 | * Entry point into this API; pattern must be non-null. 86 | * 87 | * @throws IllegalArgumentException if pattern contains any syntax errors. 88 | */ 89 | public static Phrase from(CharSequence pattern, KeyIdentifier keyIdentifier) { 90 | return new Phrase(pattern, keyIdentifier); 91 | } 92 | 93 | /** 94 | * Replaces the given key with a non-null value. You may reuse Phrase instances and replace 95 | * keys with new values. 96 | * 97 | * @throws IllegalArgumentException if the key is not in the pattern. 98 | */ 99 | public Phrase put(String key, CharSequence value) { 100 | if (!keys.contains(key)) { 101 | throw new IllegalArgumentException("Invalid key: " + key); 102 | } 103 | if (value == null) { 104 | throw new IllegalArgumentException("Null value for '" + key + "'"); 105 | } 106 | keysToValues.put(key, value); 107 | 108 | // Invalidate the cached formatted text. 109 | formatted = null; 110 | return this; 111 | } 112 | 113 | /** 114 | * Replaces the given key with the {@link Integer#toString(int)} value for the given int. 115 | * 116 | * @see #put(String, CharSequence) 117 | */ 118 | public Phrase put(String key, int value) { 119 | return put(key, Integer.toString(value)); 120 | } 121 | 122 | /** 123 | * Replaces the given key with the {@link Boolean#toString(boolean)} value for the given boolean. 124 | * 125 | * @see #put(String, CharSequence) 126 | */ 127 | public Phrase put(String key, boolean value) { 128 | return put(key, Boolean.toString(value)); 129 | } 130 | 131 | /** 132 | * Replaces the given key with the {@link String#valueOf(double)} value for the given double. 133 | * 134 | * @see #put(String, CharSequence) 135 | */ 136 | public Phrase put(String key, double value) { 137 | return put(key, String.valueOf(value)); 138 | } 139 | 140 | /** 141 | * Replaces the given key with the {@link String#valueOf(float)} value for the given float. 142 | * 143 | * @see #put(String, CharSequence) 144 | */ 145 | public Phrase put(String key, float value) { 146 | return put(key, String.valueOf(value)); 147 | } 148 | 149 | /** 150 | * Replaces the given key with the {@link String#valueOf(Object)} value for the given object. 151 | * 152 | * @see #put(String, CharSequence) 153 | */ 154 | public Phrase put(String key, T value) { 155 | return put(key, String.valueOf(value)); 156 | } 157 | 158 | /** 159 | * Replaces the given key with the created String of each item's {@link String#valueOf(Object)} 160 | * value for the given array item - chained with the provided separator. 161 | * 162 | * @param key - The key for which should be replaced with this array. 163 | * @param values - The array which should be used for this replacement. 164 | * @param separator - The separator which should be used to chain several items together. 165 | * 166 | * @see #putArray(String, Object[], String) 167 | */ 168 | public Phrase putArray(String key, int[] values, String separator) { 169 | Integer[] integers = new Integer[values.length]; 170 | for(int i = 0; i < integers.length; i++) { 171 | integers[i] = values[i]; 172 | } 173 | return putArray(key, integers, separator); 174 | } 175 | 176 | /** 177 | * Replaces the given key with the created String of each item's {@link String#valueOf(Object)} 178 | * value for the given array item - chained with the provided separator. 179 | * 180 | * @param key - The key for which should be replaced with this array. 181 | * @param values - The array which should be used for this replacement. 182 | * @param separator - The separator which should be used to chain several items together. 183 | * 184 | * @see #putArray(String, Object[], String) 185 | */ 186 | public Phrase putArray(String key, boolean[] values, String separator) { 187 | Boolean[] bools = new Boolean[values.length]; 188 | for(int i = 0; i < bools.length; i++) { 189 | bools[i] = values[i]; 190 | } 191 | return putArray(key, bools, separator); 192 | } 193 | 194 | /** 195 | * Replaces the given key with the created String of each item's {@link String#valueOf(Object)} 196 | * value for the given array item - chained with the provided separator. 197 | * 198 | * @param key - The key for which should be replaced with this array. 199 | * @param values - The array which should be used for this replacement. 200 | * @param separator - The separator which should be used to chain several items together. 201 | * 202 | * @see #putArray(String, Object[], String) 203 | */ 204 | public Phrase putArray(String key, float[] values, String separator) { 205 | Float[] floats = new Float[values.length]; 206 | for(int i = 0; i < floats.length; i++) { 207 | floats[i] = values[i]; 208 | } 209 | return putArray(key, floats, separator); 210 | } 211 | 212 | /** 213 | * Replaces the given key with the created String of each item's {@link String#valueOf(Object)} 214 | * value for the given array item - chained with the provided separator. 215 | * 216 | * @param key - The key for which should be replaced with this array. 217 | * @param values - The array which should be used for this replacement. 218 | * @param separator - The separator which should be used to chain several items together. 219 | * 220 | * @see #putArray(String, Object[], String) 221 | */ 222 | public Phrase putArray(String key, double[] values, String separator) { 223 | Double[] doubles = new Double[values.length]; 224 | for(int i = 0; i < doubles.length; i++) { 225 | doubles[i] = values[i]; 226 | } 227 | return putArray(key, doubles, separator); 228 | } 229 | 230 | /** 231 | * Replaces the given key with the created String of each item's {@link String#valueOf(Object)} 232 | * value for the given array item - chained with the provided separator. 233 | * 234 | * @param key - The key for which should be replaced with this array. 235 | * @param values - The array which should be used for this replacement. 236 | * @param separator - The separator which should be used to chain several items together. 237 | * 238 | * @see #put(String, CharSequence) 239 | */ 240 | public Phrase putArray(String key, T[] values, String separator) { 241 | separator = validateSeparator(separator); 242 | StringBuilder chainedValues = new StringBuilder(); 243 | List typeList = Arrays.asList(values); 244 | Iterator iterator = typeList.iterator(); 245 | while (iterator.hasNext()) { 246 | T object = iterator.next(); 247 | chainedValues.append(String.valueOf(object)); 248 | if(iterator.hasNext()) { 249 | chainedValues.append(separator); 250 | } 251 | } 252 | return put(key, chainedValues.toString()); 253 | } 254 | 255 | /** 256 | * Replaces the given key with the created String of each item's {@link String#valueOf(Object)} 257 | * value for the given list item - chained with the provided separator. 258 | * 259 | * @param key - The key which should be replaced with this list 260 | * @param values - The list which should be used for this replacement. 261 | * @param separator - The separator which should be used to chain several items together. 262 | * 263 | * @see #putArray(String, Object[], String) 264 | */ 265 | @SuppressWarnings("unchecked") 266 | public Phrase putList(String key, List values, String separator) { 267 | if(values.isEmpty()) { 268 | throw new IllegalArgumentException("List must not be empty!"); 269 | } 270 | T[] objects = (T[]) Array.newInstance(values.get(0).getClass(), values.size()); 271 | for(int i = 0; i < objects.length; i++) { 272 | objects[i] = values.get(i); 273 | } 274 | return putArray(key, objects, separator); 275 | } 276 | 277 | /** 278 | * Silently ignored if the key is not in the pattern. 279 | * 280 | * @see #put(String, CharSequence) 281 | */ 282 | public Phrase putOptional(String key, CharSequence value) { 283 | return keys.contains(key) ? put(key, value) : this; 284 | } 285 | 286 | /** 287 | * Replaces the given key, if it exists, with the {@link Integer#toString(int)} value 288 | * for the given int. 289 | * 290 | * @see #putOptional(String, CharSequence) 291 | */ 292 | public Phrase putOptional(String key, int value) { 293 | return keys.contains(key) ? put(key, value) : this; 294 | } 295 | 296 | public Phrase putOptional(String key, boolean value) { 297 | return keys.contains(key) ? put(key, value) : this; 298 | } 299 | 300 | public Phrase putOptional(String key, float value) { 301 | return keys.contains(key) ? put(key, value) : this; 302 | } 303 | 304 | public Phrase putOptional(String key, double value) { 305 | return keys.contains(key) ? put(key, value) : this; 306 | } 307 | 308 | public Phrase putOptional(String key, T value) { 309 | return keys.contains(key) ? put(key, value) : this; 310 | } 311 | 312 | /** 313 | * Returns the text after replacing all keys with values. 314 | * 315 | * @throws IllegalArgumentException if any keys are not replaced. 316 | */ 317 | public CharSequence format() { 318 | if (formatted == null) { 319 | if (!keysToValues.keySet().containsAll(keys)) { 320 | Set missingKeys = new HashSet(keys); 321 | missingKeys.removeAll(keysToValues.keySet()); 322 | throw new IllegalArgumentException("Missing keys: " + missingKeys); 323 | } 324 | 325 | // Copy the original pattern to preserve all spans, such as bold, italic, etc. 326 | StringBuilder sb = new StringBuilder(pattern); 327 | for (Token t = head; t != null; t = t.next) { 328 | t.expand(sb, keysToValues); 329 | } 330 | 331 | formatted = sb; 332 | } 333 | return formatted; 334 | } 335 | 336 | /** 337 | * Returns the text after replacing all keys with values. 338 | * 339 | * @throws IllegalArgumentException if any keys are not replaced. 340 | */ 341 | public String formatString() { 342 | return format().toString(); 343 | } 344 | 345 | /** 346 | * Returns the raw pattern without expanding keys; only useful for debugging. Does not pass 347 | * through to {@link #format()} because doing so would drop all spans. 348 | */ 349 | @Override 350 | public String toString() { 351 | return pattern.toString(); 352 | } 353 | 354 | private Phrase(CharSequence pattern, KeyIdentifier keyIdentifier) { 355 | curChar = (pattern.length() > 0) ? pattern.charAt(0) : EOF; 356 | 357 | this.pattern = pattern; 358 | this.keyIdentifier = keyIdentifier; 359 | 360 | // A hand-coded lexer based on the idioms in "Building Recognizers By Hand". 361 | // http://www.antlr2.org/book/byhand.pdf. 362 | Token prev = null; 363 | Token next; 364 | while ((next = token(prev)) != null) { 365 | // Creates a doubly-linked list of tokens starting with head. 366 | if (head == null) head = next; 367 | prev = next; 368 | } 369 | } 370 | 371 | /** Makes sure that the given separator is not null or empty - if so the default separator is used. **/ 372 | private String validateSeparator(String separator) { 373 | if(separator == null || separator.length() == 0) { 374 | separator = DEFAULT_SEPARATOR; 375 | } 376 | return separator; 377 | } 378 | 379 | /** Returns the next token from the input pattern, or null when finished parsing. */ 380 | private Token token(Token prev) { 381 | if (curChar == EOF) { 382 | return null; 383 | } 384 | if (curChar == keyIdentifier.getOpenCharacter()) { 385 | char nextChar = lookahead(); 386 | if (nextChar == keyIdentifier.getOpenCharacter()) { 387 | return leftCurlyBracket(prev); 388 | } else if (nextChar >= 'a' && nextChar <= 'z') { 389 | return key(prev); 390 | } else { 391 | throw new IllegalArgumentException( 392 | "Unexpected character '" + nextChar + "'; expected key."); 393 | } 394 | } 395 | return text(prev); 396 | } 397 | 398 | /** Parses a key: "{some_key}". */ 399 | private KeyToken key(Token prev) { 400 | 401 | // Store keys as normal Strings; we don't want keys to contain spans. 402 | StringBuilder sb = new StringBuilder(); 403 | 404 | // Consume the opening '{'. 405 | consume(); 406 | while ((curChar >= 'a' && curChar <= 'z') || curChar == '_') { 407 | sb.append(curChar); 408 | consume(); 409 | } 410 | 411 | // Consume the closing '}'. 412 | if (curChar != keyIdentifier.getCloseCharacter()) { 413 | throw new IllegalArgumentException("Missing closing brace: " + keyIdentifier.getCloseCharString()); 414 | } 415 | consume(); 416 | 417 | // Disallow empty keys: {}. 418 | if (sb.length() == 0) { 419 | throw new IllegalArgumentException("Empty key: " + keyIdentifier.getOpenCharString() + keyIdentifier.getCloseCharString()); 420 | } 421 | 422 | String key = sb.toString(); 423 | keys.add(key); 424 | return new KeyToken(prev, key); 425 | } 426 | 427 | /** Consumes and returns a token for a sequence of text. */ 428 | private TextToken text(Token prev) { 429 | int startIndex = curCharIndex; 430 | 431 | while (curChar != keyIdentifier.getOpenCharacter() && curChar != EOF) { 432 | consume(); 433 | } 434 | return new TextToken(prev, curCharIndex - startIndex); 435 | } 436 | 437 | /** Consumes and returns a token representing two consecutive curly brackets. */ 438 | private LeftCurlyBracketToken leftCurlyBracket(Token prev) { 439 | consume(); 440 | consume(); 441 | return new LeftCurlyBracketToken(prev); 442 | } 443 | 444 | /** Returns the next character in the input pattern without advancing. */ 445 | private char lookahead() { 446 | return curCharIndex < pattern.length() - 1 ? pattern.charAt(curCharIndex + 1) : EOF; 447 | } 448 | 449 | /** 450 | * Advances the current character position without any error checking. Consuming beyond the 451 | * end of the string can only happen if this parser contains a bug. 452 | */ 453 | private void consume() { 454 | curCharIndex++; 455 | curChar = (curCharIndex == pattern.length()) ? EOF : pattern.charAt(curCharIndex); 456 | } 457 | 458 | private abstract static class Token { 459 | private final Token prev; 460 | private Token next; 461 | 462 | protected Token(Token prev) { 463 | this.prev = prev; 464 | if (prev != null) prev.next = this; 465 | } 466 | 467 | /** Replace text in {@code target} with this token's associated value. */ 468 | abstract void expand(StringBuilder target, Map data); 469 | 470 | /** Returns the number of characters after expansion. */ 471 | abstract int getFormattedLength(); 472 | 473 | /** Returns the character index after expansion. */ 474 | final int getFormattedStart() { 475 | if (prev == null) { 476 | // The first token. 477 | return 0; 478 | } else { 479 | // Recursively ask the predecessor node for the starting index. 480 | return prev.getFormattedStart() + prev.getFormattedLength(); 481 | } 482 | } 483 | } 484 | 485 | /** Ordinary text between tokens. */ 486 | private static class TextToken extends Token { 487 | private final int textLength; 488 | 489 | TextToken(Token prev, int textLength) { 490 | super(prev); 491 | this.textLength = textLength; 492 | } 493 | 494 | @Override 495 | void expand(StringBuilder target, Map data) { 496 | // Don't alter spans in the target. 497 | } 498 | 499 | @Override 500 | int getFormattedLength() { 501 | return textLength; 502 | } 503 | } 504 | 505 | /** A sequence of two curly brackets. */ 506 | private class LeftCurlyBracketToken extends Token { 507 | LeftCurlyBracketToken(Token prev) { 508 | super(prev); 509 | } 510 | 511 | @Override 512 | void expand(StringBuilder target, Map data) { 513 | int start = getFormattedStart(); 514 | target.replace(start, start + 2, keyIdentifier.getOpenCharString()); 515 | } 516 | 517 | @Override 518 | int getFormattedLength() { 519 | // Replace {{ with {. 520 | return 1; 521 | } 522 | } 523 | 524 | private static class KeyToken extends Token { 525 | /** The key without { and }. */ 526 | private final String key; 527 | 528 | private CharSequence value; 529 | 530 | KeyToken(Token prev, String key) { 531 | super(prev); 532 | this.key = key; 533 | } 534 | 535 | @Override 536 | void expand(StringBuilder target, Map data) { 537 | value = data.get(key); 538 | 539 | int replaceFrom = getFormattedStart(); 540 | // Add 2 to account for the opening and closing brackets. 541 | int replaceTo = replaceFrom + key.length() + 2; 542 | target.replace(replaceFrom, replaceTo, value.toString()); 543 | } 544 | 545 | @Override 546 | int getFormattedLength() { 547 | // Note that value is only present after expand. Don't error check because this is all 548 | // private code. 549 | return value.length(); 550 | } 551 | } 552 | 553 | public enum KeyIdentifier { 554 | CURLY_BRACKETS('{', '}'), 555 | ROUND_BRACKETS('(', ')'), 556 | ANGLE_BRACKETS('<', '>'), 557 | SQUARE_BRACKETS('[', ']'); 558 | 559 | private final Pair identifiers; 560 | 561 | KeyIdentifier(char open, char close) { 562 | this.identifiers = new Pair<>(open, close); 563 | } 564 | 565 | public char getOpenCharacter() { 566 | return identifiers.getKey(); 567 | } 568 | 569 | public char getCloseCharacter() { 570 | return identifiers.getValue(); 571 | } 572 | 573 | public String getOpenCharString() { 574 | return String.valueOf(getOpenCharacter()); 575 | } 576 | 577 | public String getCloseCharString() { 578 | return String.valueOf(getCloseCharacter()); 579 | } 580 | 581 | } 582 | 583 | } -------------------------------------------------------------------------------- /phrase-java/src/test/java/com/pddstudio/phrase/java/BaseUtilityTest.java: -------------------------------------------------------------------------------- 1 | package com.pddstudio.phrase.java; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.Iterator; 6 | import java.util.List; 7 | import java.util.Random; 8 | 9 | /** 10 | * Created by pddstudio on 15/10/2016. 11 | */ 12 | public abstract class BaseUtilityTest { 13 | 14 | private int methodNameCount = 20; 15 | private Random random = new Random(); 16 | 17 | protected final Phrase.KeyIdentifier phraseKeyIdentifier; 18 | 19 | private static final String[] LETTERS = new String[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", 20 | "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; 21 | 22 | public BaseUtilityTest(Phrase.KeyIdentifier keyIdentifier) { 23 | this.phraseKeyIdentifier = keyIdentifier; 24 | } 25 | 26 | protected void printResult(String methodName, String result, String expected) { 27 | System.out.println("==== Test Method: " + methodName + " Test KeyIdentifier: " + 28 | phraseKeyIdentifier.name() + " " + 29 | phraseKeyIdentifier.getOpenCharString() + 30 | phraseKeyIdentifier.getCloseCharString() + " ===="); 31 | StringBuilder message = new StringBuilder(); 32 | int remainingWhitespaces = methodNameCount - methodName.length(); 33 | if(remainingWhitespaces < 0) { 34 | remainingWhitespaces = 0; 35 | message.append("[EXPECTED] : ").append(methodName.substring(0, methodNameCount -3)).append("..."); 36 | } else { 37 | message.append("[EXPECTED] : ").append(methodName); 38 | } 39 | for(int i = 0; i < remainingWhitespaces; i++) { 40 | message.append(" "); 41 | } 42 | message.append(" > ").append(expected); 43 | System.out.println(message.toString()); 44 | //print the actual result too 45 | printResult(methodName, result); 46 | } 47 | 48 | private void printResult(String methodName, String result) { 49 | StringBuilder message = new StringBuilder(); 50 | int remainingWhitespaces = methodNameCount - methodName.length(); 51 | if(remainingWhitespaces < 0) { 52 | remainingWhitespaces = 0; 53 | message.append("[RESULT ] : ").append(methodName.substring(0, methodNameCount -3)).append("..."); 54 | } else { 55 | message.append("[RESULT ] : ").append(methodName); 56 | } 57 | for(int i = 0; i < remainingWhitespaces; i++) { 58 | message.append(" "); 59 | } 60 | message.append(" > ").append(result); 61 | System.out.println(message.toString()); 62 | } 63 | 64 | private String getExpectedArrayResult(T[] values, String separator) { 65 | //falling back to the default separator 66 | if (separator == null || separator.length() == 0) { 67 | separator = " "; 68 | } 69 | StringBuilder sb = new StringBuilder(); 70 | List list = Arrays.asList(values); 71 | Iterator iterator = list.iterator(); 72 | while(iterator.hasNext()) { 73 | sb.append(String.valueOf(iterator.next())); 74 | if(iterator.hasNext()) { 75 | sb.append(separator); 76 | } 77 | } 78 | return sb.toString(); 79 | } 80 | 81 | private String getRandomString(int length) { 82 | String s = ""; 83 | if(length < 0 ) { 84 | return s; 85 | } else { 86 | for(int i = 0; i < length; i++) { 87 | s += LETTERS[random.nextInt(LETTERS.length -1)]; 88 | } 89 | return s; 90 | } 91 | } 92 | 93 | protected int[] getRandomIntArray() { 94 | int size = random.nextInt(100); 95 | int[] ints = new int[size]; 96 | for(int i = 0; i < ints.length; i++) { 97 | ints[i] = random.nextInt(Integer.MAX_VALUE); 98 | } 99 | return ints; 100 | } 101 | 102 | protected float[] getRandomFloatArray() { 103 | int size = random.nextInt(100); 104 | float[] floats = new float[size]; 105 | for(int i = 0; i < floats.length; i++) { 106 | floats[i] = random.nextFloat(); 107 | } 108 | return floats; 109 | } 110 | 111 | protected double[] getRandomDoubleArray() { 112 | int size = random.nextInt(100); 113 | double[] doubles = new double[size]; 114 | for(int i = 0; i < doubles.length; i++) { 115 | doubles[i] = random.nextDouble(); 116 | } 117 | return doubles; 118 | } 119 | 120 | protected boolean[] getRandomBooleanArray() { 121 | int size = random.nextInt(100); 122 | boolean[] bools = new boolean[size]; 123 | for(int i = 0; i < bools.length; i++) { 124 | bools[i] = random.nextBoolean(); 125 | } 126 | return bools; 127 | } 128 | 129 | protected String getExpectedIntArrayResult(int[] values, String separator) { 130 | Integer[] integers = new Integer[values.length]; 131 | for(int i = 0; i < integers.length; i++) { 132 | integers[i] = values[i]; 133 | } 134 | return getExpectedArrayResult(integers, separator); 135 | } 136 | 137 | protected String getExpectedFloatArrayResult(float[] values, String separator) { 138 | Float[] floats = new Float[values.length]; 139 | for(int i = 0; i < floats.length; i++) { 140 | floats[i] = values[i]; 141 | } 142 | return getExpectedArrayResult(floats, separator); 143 | } 144 | 145 | protected String getExpectedDoubleArrayResult(double[] values, String separator) { 146 | Double[] doubles = new Double[values.length]; 147 | for(int i = 0; i < doubles.length; i++) { 148 | doubles[i] = values[i]; 149 | } 150 | return getExpectedArrayResult(doubles, separator); 151 | } 152 | 153 | protected String getExpectedBooleanArrayResult(boolean[] values, String separator) { 154 | Boolean[] bools = new Boolean[values.length]; 155 | for(int i = 0; i < bools.length; i++) { 156 | bools[i] = values[i]; 157 | } 158 | return getExpectedArrayResult(bools, separator); 159 | } 160 | 161 | protected String getExpectedStringListResult(List strings, String separator) { 162 | String[] values = strings.toArray(new String[strings.size()]); 163 | return getExpectedArrayResult(values, separator); 164 | } 165 | 166 | protected String[] getRandomStringArray() { 167 | int size = random.nextInt(100); 168 | String[] strings = new String[size]; 169 | for(int i = 0; i < strings.length; i++) { 170 | int textLength = random.nextInt(20); 171 | strings[i] = getRandomString(textLength); 172 | } 173 | return strings; 174 | } 175 | 176 | protected List toList(T[] array) { 177 | List list = new ArrayList<>(); 178 | for(T item : array) { 179 | list.add(item); 180 | } 181 | return list; 182 | } 183 | 184 | } 185 | -------------------------------------------------------------------------------- /phrase-java/src/test/java/com/pddstudio/phrase/java/PhraseTest.java: -------------------------------------------------------------------------------- 1 | package com.pddstudio.phrase.java; 2 | 3 | import com.pddstudio.phrase.java.Phrase.KeyIdentifier; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.junit.runners.Parameterized; 8 | import org.junit.runners.Parameterized.Parameters; 9 | 10 | import java.math.BigDecimal; 11 | import java.util.ArrayList; 12 | import java.util.Arrays; 13 | import java.util.Collection; 14 | import java.util.Iterator; 15 | import java.util.List; 16 | import java.util.Random; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | 20 | /** 21 | * Created by pddstudio on 15/10/2016. 22 | */ 23 | @RunWith(Parameterized.class) 24 | public class PhraseTest extends BaseUtilityTest { 25 | 26 | @Parameters 27 | public static Collection data() { 28 | return Arrays.asList(KeyIdentifier.values()); 29 | } 30 | 31 | private final String separator = " | "; 32 | 33 | public PhraseTest(KeyIdentifier keyIdentifier) { 34 | super(keyIdentifier); 35 | } 36 | 37 | private String getPhraseForIdentifier(String phrase) { 38 | return phrase.replaceAll("\\{", phraseKeyIdentifier.getOpenCharString()).replaceAll("\\}", phraseKeyIdentifier.getCloseCharString()); 39 | } 40 | 41 | private Phrase phraseFrom(String phrase) { 42 | return Phrase.from(getPhraseForIdentifier(phrase), phraseKeyIdentifier); 43 | } 44 | 45 | @Test 46 | public void helloWorldTest() { 47 | String greeting = "Hello"; 48 | String greetingTarget = "World"; 49 | String expected = "Hello World!"; 50 | String phrase = phraseFrom("{greeting} {who}!").put("greeting", greeting).put("who", greetingTarget).formatString(); 51 | printResult("helloWorldTest()", phrase, expected); 52 | assertEquals(phrase, expected); 53 | } 54 | 55 | @Test 56 | public void bigDecimalTest() { 57 | BigDecimal firstNumber = new BigDecimal(500); 58 | BigDecimal secondNumber = new BigDecimal(3); 59 | String expected = "500 / 3 = 167"; 60 | String phrase = phraseFrom("{first} / {second} = {result}") 61 | .put("first", firstNumber) 62 | .put("second", secondNumber) 63 | .put("result", firstNumber.divide(secondNumber, BigDecimal.ROUND_HALF_DOWN)).formatString(); 64 | printResult("bigDecimalTest()", phrase, expected); 65 | assertEquals(phrase, expected); 66 | } 67 | 68 | @Test 69 | public void dummyRestUrlTest() { 70 | String host = "https://github.com"; 71 | String name = "pddstudio"; 72 | String action = "isAwesome"; 73 | boolean actionValue = true; 74 | String expected = "https://github.com/pddstudio/?isAwesome=true"; 75 | String phrase = phraseFrom("{host}/{name}/?{action}={action_value}") 76 | .put("host", host) 77 | .put("name", name) 78 | .put("action", action) 79 | .put("action_value", actionValue).formatString(); 80 | printResult("dummyRestUrlTest()", phrase, expected); 81 | assertEquals(phrase, expected); 82 | } 83 | 84 | @Test 85 | public void digitsTest() { 86 | int intValue = 471; 87 | float floatValue = 21.9F; 88 | double doubleValue = 1921.2; 89 | String expected = "Integer: 471 | Float: 21.9 | Double: 1921.2"; 90 | String phrase = phraseFrom("Integer: {integer} | Float: {float} | Double: {double}") 91 | .put("integer", intValue) 92 | .put("float", floatValue) 93 | .put("double", doubleValue) 94 | .formatString(); 95 | printResult("digitsTest()", phrase, expected); 96 | assertEquals(phrase, expected); 97 | } 98 | 99 | @Test 100 | public void intArrayTest() { 101 | int[] ints = getRandomIntArray(); 102 | String expected = getExpectedIntArrayResult(ints, separator); 103 | String phrase = phraseFrom("{integer_array}").putArray("integer_array", ints, separator).formatString(); 104 | printResult("intArrayTest()", phrase, expected); 105 | assertEquals(phrase, expected); 106 | } 107 | 108 | @Test 109 | public void booleanArrayTest() { 110 | boolean[] bools = getRandomBooleanArray(); 111 | String expected = getExpectedBooleanArrayResult(bools, separator); 112 | String phrase = phraseFrom("{boolean_array}").putArray("boolean_array", bools, separator).formatString(); 113 | printResult("booleanArrayTest()", phrase, expected); 114 | assertEquals(phrase, expected); 115 | } 116 | 117 | @Test 118 | public void floatArrayTest() { 119 | float[] floats = getRandomFloatArray(); 120 | String expected = getExpectedFloatArrayResult(floats, separator); 121 | String phrase = phraseFrom("{float_array}").putArray("float_array", floats, separator).formatString(); 122 | printResult("floatArrayTest()", phrase, expected); 123 | assertEquals(phrase, expected); 124 | } 125 | 126 | @Test 127 | public void doubleArrayTest() { 128 | double[] doubles = getRandomDoubleArray(); 129 | String expected = getExpectedDoubleArrayResult(doubles, separator); 130 | String phrase = phraseFrom("{double_array}").putArray("double_array", doubles, separator).formatString(); 131 | printResult("doubleArrayTest()", phrase, expected); 132 | assertEquals(phrase, expected); 133 | } 134 | 135 | @Test 136 | public void stringListTest() { 137 | List stringList = toList(getRandomStringArray()); 138 | String expected = getExpectedStringListResult(stringList, separator); 139 | String phrase = phraseFrom("{string_list}").putList("string_list", stringList, separator).formatString(); 140 | printResult("stringListTest()", phrase, expected); 141 | assertEquals(phrase, expected); 142 | } 143 | 144 | } 145 | -------------------------------------------------------------------------------- /phrases-java.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.pddstudio 7 | phrase-java-root 8 | 1.0.0 9 | 10 | phrase-commons 11 | phrase-java 12 | 13 | pom 14 | 15 | 16 | 17 | central-release 18 | plugins-releases 19 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 20 | 21 | 22 | snapshots 23 | plugins-snapshot 24 | https://oss.sonatype.org/content/repositories/snapshots/ 25 | 26 | 27 | 28 | 29 | 30 | Apache License, Version 2.0 31 | http://www.apache.org/licenses/LICENSE-2.0.txt 32 | repo 33 | 34 | 35 | 36 | 37 | https://github.com/PDDStudio/phrase-java 38 | 39 | 40 | 41 | 42 | 1.0.1 43 | 1.0.0-SNAPSHOT 44 | 45 | UTF-8 46 | 1.7 47 | 1.7 48 | 3.5.1 49 | 1.3 50 | 51 | 4.12 52 | 53 | 54 | 55 | 56 | 57 | org.apache.maven.plugins 58 | maven-compiler-plugin 59 | ${plugin.maven.compiler.version} 60 | 61 | ${compile.source.version} 62 | ${compile.target.version} 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | junit 71 | junit 72 | ${dependency.junit.version} 73 | test 74 | 75 | 76 | 77 | --------------------------------------------------------------------------------