|
| 1 | +/* |
| 2 | +Copyright 2009-2016 Igor Polevoy |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package org.javalite.common; |
| 18 | + |
| 19 | + |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import static org.javalite.common.Util.readResource; |
| 23 | + |
| 24 | +/** |
| 25 | + * Simple implementation of small quick templates filled with dynamic data.<br/> |
| 26 | + * An example of a template: |
| 27 | + * |
| 28 | + * |
| 29 | + * <pre> |
| 30 | + * Hello, {{name}}! |
| 31 | + * </pre> |
| 32 | + * |
| 33 | + * |
| 34 | + * Usage: |
| 35 | + * |
| 36 | + * <pre> |
| 37 | + * Templator templator = new Templator("/hello_template.txt"); |
| 38 | + * System.out.println(templator.merge(map("name", "Kyla"))); // prints: Hello, Kyla! |
| 39 | + * </pre> |
| 40 | + * |
| 41 | + * <p></p> |
| 42 | + * Note: Yes, I'm aware of existence of Mustache, Freemarker, Velocity, etc. |
| 43 | + * This implementation is for quickly loading small snippets of text merged with data. |
| 44 | + * The goal is to eliminate code pollution. If you need more power, use bigger frameworks. |
| 45 | + * |
| 46 | + * @author Igor Polevoy on 9/30/16. |
| 47 | + */ |
| 48 | +public class Templator { |
| 49 | + |
| 50 | + private String template; |
| 51 | + |
| 52 | + /** |
| 53 | + * @param templatePath path to a template on classpath |
| 54 | + */ |
| 55 | + public Templator(String templatePath){ |
| 56 | + template = readResource(templatePath); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * This method is used in repeated operations, since it will load a resource once. |
| 61 | + * |
| 62 | + * @param values values to merge into a template |
| 63 | + * @return result of merging |
| 64 | + */ |
| 65 | + public String merge(Map<String, ?> values){ |
| 66 | + return mergeFromTemplate(template, values); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * This method is used in one-off operations, where it is OK to load a template every time. |
| 71 | + * |
| 72 | + * Example: |
| 73 | + * <code> |
| 74 | + * String result = Templator.mergeFromPath(readResource("/message_template.txt", valuesMap)); |
| 75 | + * </code> |
| 76 | + * |
| 77 | + * @param templatePath template to merge |
| 78 | + * @param values values to merge into a template |
| 79 | + * @return result of merging |
| 80 | + */ |
| 81 | + public static String mergeFromPath(String templatePath, Map<String, ?> values) { |
| 82 | + return mergeFromTemplate(readResource(templatePath), values); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Merges from string as template. |
| 87 | + * |
| 88 | + * @param template template content, with placeholders like: {{name}} |
| 89 | + * @param values map with values to merge |
| 90 | + */ |
| 91 | + public static String mergeFromTemplate(String template, Map<String, ?> values) { |
| 92 | + for (String param : values.keySet()) { |
| 93 | + template = template.replace("{{" + param + "}}", values.get(param) == null ? "" : values.get(param).toString()); |
| 94 | + } |
| 95 | + return template.replaceAll("\n|\r| ", ""); |
| 96 | + } |
| 97 | +} |
0 commit comments