Changeset 13003 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2017-10-16T13:26:41+02:00 (7 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/tools/template_engine
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/template_engine/CompoundTemplateEntry.java
r12798 r13003 2 2 package org.openstreetmap.josm.tools.template_engine; 3 3 4 /** 5 * {@link TemplateEntry} that concatenates several templates. 6 */ 4 7 public final class CompoundTemplateEntry implements TemplateEntry { 5 8 6 public static TemplateEntry fromArray(TemplateEntry... entry) { 7 if (entry.length == 0) 9 /** 10 * Factory method to concatenate several {@code TemplateEntry}s. 11 * 12 * If the number of entries is 0 or 1, the result may not be a {@code CompoundTemplateEntry}, 13 * but optimized to a static text or the single entry itself. 14 * @param entries the {@code TemplateEntry}s to concatenate 15 * @return a {@link TemplateEntry} that concatenates all the entries 16 */ 17 public static TemplateEntry fromArray(TemplateEntry... entries) { 18 if (entries.length == 0) 8 19 return new StaticText(""); 9 else if (entr y.length == 1)10 return entr y[0];20 else if (entries.length == 1) 21 return entries[0]; 11 22 else 12 return new CompoundTemplateEntry(entr y);23 return new CompoundTemplateEntry(entries); 13 24 } 14 25 -
trunk/src/org/openstreetmap/josm/tools/template_engine/Condition.java
r11057 r13003 3 3 4 4 import java.util.ArrayList; 5 import java.util.Collection; 5 6 import java.util.List; 6 7 8 /** 9 * {@link TemplateEntry} that applies other templates based on conditions. 10 * <p> 11 * It goes through a number of template entries and executes the first one that is valid. 12 */ 7 13 public class Condition implements TemplateEntry { 8 14 9 private final List<TemplateEntry> entries = new ArrayList<>();15 private final List<TemplateEntry> entries; 10 16 17 public Condition(Collection<TemplateEntry> entries) { 18 this.entries = new ArrayList<>(entries); 19 } 20 21 public Condition() { 22 this.entries = new ArrayList<>(); 23 } 24 25 /** 26 * @deprecated (since 13003) use constructor {@link #Condition(java.util.Collection)} to set the entries 27 */ 28 @Deprecated 11 29 public List<TemplateEntry> getEntries() { 12 30 return entries; -
trunk/src/org/openstreetmap/josm/tools/template_engine/ParseError.java
r12656 r13003 8 8 import org.openstreetmap.josm.tools.template_engine.Tokenizer.TokenType; 9 9 10 /** 11 * Exception thrown in case of an error during template parsing. 12 * 13 * Usually caused by invalid user input. 14 */ 10 15 public class ParseError extends Exception { 11 16 -
trunk/src/org/openstreetmap/josm/tools/template_engine/SearchExpressionCondition.java
r12656 r13003 4 4 import org.openstreetmap.josm.data.osm.search.SearchCompiler.Match; 5 5 6 /** 7 * Conditional {@link TemplateEntry} that executes another template in case a search expression applies 8 * to the given data provider. 9 */ 6 10 public class SearchExpressionCondition implements TemplateEntry { 7 11 … … 9 13 private final TemplateEntry text; 10 14 15 /** 16 * Creates a new {@link SearchExpressionCondition}. 17 * @param condition the match condition that is checked before applying the child template 18 * @param text the child template to execute in case the condition is fulfilled 19 */ 11 20 public SearchExpressionCondition(Match condition, TemplateEntry text) { 12 21 this.condition = condition; -
trunk/src/org/openstreetmap/josm/tools/template_engine/StaticText.java
r7937 r13003 2 2 package org.openstreetmap.josm.tools.template_engine; 3 3 4 /** 5 * {@link TemplateEntry} representing a static string. 6 * <p> 7 * When compiling the template result, the given string will simply be inserted at the current position. 8 */ 4 9 public class StaticText implements TemplateEntry { 5 10 6 11 private final String staticText; 7 12 13 /** 14 * Create a new {@code StaticText}. 15 * @param staticText the text to insert verbatim 16 */ 8 17 public StaticText(String staticText) { 9 18 this.staticText = staticText; -
trunk/src/org/openstreetmap/josm/tools/template_engine/TemplateEngineDataProvider.java
r12656 r13003 6 6 import org.openstreetmap.josm.data.osm.search.SearchCompiler.Match; 7 7 8 /** 9 * Interface for objects that can be used with a template to generate a string. 10 * <p> 11 * Provides the necessary information for the template to be applied. 12 */ 8 13 public interface TemplateEngineDataProvider { 14 /** 15 * Get the collection of all keys that can be mapped to values. 16 * @return all keys that can be mapped to values 17 */ 9 18 Collection<String> getTemplateKeys(); 10 19 11 Object getTemplateValue(String name, boolean special); 20 /** 21 * Map a key to a value given the properties of the object. 22 * @param key the key to map 23 * @param special if the key is a "special:*" keyword that is used 24 * to get certain information or automated behavior 25 * @return a value that the key is mapped to or "special" information in case {@code special} is true 26 */ 27 Object getTemplateValue(String key, boolean special); 12 28 29 /** 30 * Check if a condition holds for the object represented by this {@link TemplateEngineDataProvider}. 31 * @param condition the condition to check (which is a search expression) 32 * @return true if the condition holds 33 */ 13 34 boolean evaluateCondition(Match condition); 14 35 } -
trunk/src/org/openstreetmap/josm/tools/template_engine/TemplateEntry.java
r8510 r13003 2 2 package org.openstreetmap.josm.tools.template_engine; 3 3 4 /** 5 * Interface for one node in the abstract syntax tree that is the result of parsing a template 6 * string with {@link TemplateParser}. 7 * 8 * The node can either be branching (condition, context switch) or a leaf node (variable, static text). 9 * The root node, representing the entire template is also a {@code TemplateEntry}. 10 */ 4 11 public interface TemplateEntry { 12 /** 13 * Execute this template by generating text for a given data provider. 14 * @param result the {@link StringBuilder} to append the text to 15 * @param dataProvider the data provider from which information should be compiled to a string 16 */ 5 17 void appendText(StringBuilder result, TemplateEngineDataProvider dataProvider); 6 18 19 /** 20 * Check if this template is applicable to the given data provider. 21 * 22 * @param dataProvider the data provider to check 23 * @return true if all conditions are fulfilled to apply the template (for instance all 24 * required key=value mappings are present), false otherwise 25 */ 7 26 boolean isValid(TemplateEngineDataProvider dataProvider); 8 27 } -
trunk/src/org/openstreetmap/josm/tools/template_engine/TemplateParser.java
r12656 r13003 26 26 /** 27 27 * Constructs a new {@code TemplateParser}. 28 * @param template template to parse28 * @param template template string to parse 29 29 */ 30 30 public TemplateParser(String template) { … … 88 88 private TemplateEntry parseCondition() throws ParseError { 89 89 check(TokenType.CONDITION_START); 90 Co ndition result = new Condition();90 Collection<TemplateEntry> conditionEntries = new ArrayList<>(); 91 91 while (true) { 92 92 … … 98 98 String searchText = searchExpression.getText().trim(); 99 99 if (searchText.isEmpty()) { 100 result.getEntries().add(condition);100 conditionEntries.add(condition); 101 101 } else { 102 102 try { 103 result.getEntries().add(new SearchExpressionCondition(103 conditionEntries.add(new SearchExpressionCondition( 104 104 SearchCompiler.compile(searchText), condition)); 105 105 } catch (SearchParseError e) { … … 112 112 if (token.getType() == TokenType.END) { 113 113 tokenizer.nextToken(); 114 return result;114 return new Condition(conditionEntries); 115 115 } else { 116 116 check(TokenType.PIPE); -
trunk/src/org/openstreetmap/josm/tools/template_engine/Tokenizer.java
r12279 r13003 6 6 import java.util.Set; 7 7 8 /** 9 * This class converts a template string (stream of characters) into a stream of tokens. 10 * 11 * The result of the tokenization (also called lexical analysis) serves as input for the 12 * parser {@link TemplateParser}. 13 */ 8 14 public class Tokenizer { 9 15 … … 52 58 private final StringBuilder text = new StringBuilder(); 53 59 60 /** 61 * Creates a new {@link Tokenizer} 62 * @param template the template as a user input string 63 */ 54 64 public Tokenizer(String template) { 55 65 this.template = template; -
trunk/src/org/openstreetmap/josm/tools/template_engine/Variable.java
r11057 r13003 5 5 import java.util.Locale; 6 6 7 /** 8 * {@link TemplateEntry} that inserts the value of a variable. 9 * <p> 10 * Variables starting with "special:" form a separate namespace and 11 * provide actions other than simple key-value lookup. 12 * <p> 13 * A variable with no mapping for a given data provider will be considered not "valid" 14 * (see {@link TemplateEntry#isValid(TemplateEngineDataProvider)}). 15 */ 7 16 public class Variable implements TemplateEntry { 8 17 … … 10 19 private static final String SPECIAL_VALUE_EVERYTHING = "everything"; 11 20 12 13 21 private final String variableName; 14 22 private final boolean special; 15 23 24 /** 25 * Constructs a new {@code Variable}. 26 * @param variableName the variable name (i.e. the key in the data provider key-value mapping); 27 * will be considered "special" if the variable name starts with {@link SPECIAL_VARIABLE_PREFIX} 28 */ 16 29 public Variable(String variableName) { 17 30 if (variableName.toLowerCase(Locale.ENGLISH).startsWith(SPECIAL_VARIABLE_PREFIX)) { … … 59 72 } 60 73 74 /** 75 * Check if this variable is special. 76 * 77 * @return true if this variable is special 78 */ 61 79 public boolean isSpecial() { 62 80 return special;
Note:
See TracChangeset
for help on using the changeset viewer.