1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  package org.apache.tiles.definition.pattern;
23  
24  import java.text.MessageFormat;
25  import java.util.ArrayList;
26  import java.util.LinkedHashMap;
27  import java.util.List;
28  import java.util.Locale;
29  import java.util.Map;
30  import java.util.Set;
31  import java.util.regex.Matcher;
32  import java.util.regex.Pattern;
33  
34  import org.apache.tiles.Attribute;
35  import org.apache.tiles.Definition;
36  import org.apache.tiles.Expression;
37  import org.apache.tiles.ListAttribute;
38  
39  
40  
41  
42  
43  
44  
45  public final class PatternUtil {
46  
47      
48  
49  
50  
51      private static final Locale ROOT_LOCALE = new Locale("", "");
52  
53      
54  
55      private static final Pattern INVALID_FORMAT_ELEMENT = Pattern.compile("\\{[^}0-9]+\\}");
56  
57      
58  
59  
60      private PatternUtil() {
61      }
62  
63      
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74      public static Definition replacePlaceholders(Definition d, String name,
75              Object... varsOrig) {
76  
77          Object[] vars = replaceNullsWithBlank(varsOrig);
78  
79          Definition nudef = new Definition();
80  
81          nudef.setExtends(replace(d.getExtends(), vars));
82          nudef.setName(name);
83          nudef.setPreparer(replace(d.getPreparer(), vars));
84          Attribute templateAttribute = d.getTemplateAttribute();
85          if (templateAttribute != null) {
86              nudef.setTemplateAttribute(replaceVarsInAttribute(
87                      templateAttribute, vars));
88          }
89  
90          Set<String> attributeNames = d.getLocalAttributeNames();
91          if (attributeNames != null && !attributeNames.isEmpty()) {
92              for (String attributeName : attributeNames) {
93                  Attribute attr = d.getLocalAttribute(attributeName);
94                  Attribute nuattr = replaceVarsInAttribute(attr, vars);
95  
96                  nudef.putAttribute(replace(attributeName, vars), nuattr);
97              }
98          }
99  
100         attributeNames = d.getCascadedAttributeNames();
101         if (attributeNames != null && !attributeNames.isEmpty()) {
102             for (String attributeName : attributeNames) {
103                 Attribute attr = d.getCascadedAttribute(attributeName);
104                 Attribute nuattr = replaceVarsInAttribute(attr, vars);
105 
106                 nudef.putAttribute(replace(attributeName, vars), nuattr, true);
107             }
108         }
109 
110         return nudef;
111     }
112 
113     
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124     public static <K, V> Map<K, V> createExtractedMap(Map<K, V> map, Set<K> keys) {
125         Map<K, V> retValue = new LinkedHashMap<K, V>();
126         for (K key : keys) {
127             retValue.put(key, map.get(key));
128         }
129         return retValue;
130     }
131 
132     
133 
134 
135 
136 
137 
138 
139 
140 
141     private static Attribute replaceVarsInAttribute(Attribute attr,
142             Object... vars) {
143         Attribute nuattr;
144         if (attr instanceof ListAttribute) {
145             nuattr = replaceVarsInListAttribute((ListAttribute) attr, vars);
146         } else {
147             nuattr = replaceVarsInSimpleAttribute(attr, vars);
148         }
149         return nuattr;
150     }
151 
152     
153 
154 
155 
156 
157 
158 
159 
160 
161     private static Attribute replaceVarsInSimpleAttribute(Attribute attr,
162             Object... vars) {
163         Attribute nuattr;
164         nuattr = new Attribute();
165 
166         nuattr.setRole(replace(attr.getRole(), vars));
167         nuattr.setRenderer(attr.getRenderer());
168         Expression expressionObject = attr.getExpressionObject();
169         if (expressionObject != null) {
170             Expression newExpressionObject = Expression
171                     .createExpression(replace(expressionObject.getExpression(), vars), expressionObject.getLanguage());
172             nuattr.setExpressionObject(newExpressionObject);
173         }
174 
175         Object value = attr.getValue();
176         if (value instanceof String) {
177             value = replace((String) value, vars);
178         }
179         nuattr.setValue(value);
180         return nuattr;
181     }
182 
183     
184 
185 
186 
187 
188 
189 
190 
191 
192     private static Attribute replaceVarsInListAttribute(ListAttribute listAttr,
193             Object... vars) {
194         Attribute nuattr;
195         ListAttribute nuListAttr = new ListAttribute();
196         nuListAttr.setInherit(listAttr.isInherit());
197         List<Attribute> nuItems = nuListAttr.getValue();
198         for (Object item : listAttr.getValue()) {
199             Attribute child = (Attribute) item;
200             child = replaceVarsInAttribute(child, vars);
201             nuItems.add(child);
202         }
203         nuattr = nuListAttr;
204         return nuattr;
205     }
206 
207     
208 
209 
210 
211 
212 
213 
214     private static String replace(String st, Object... vars) {
215         if (st != null && st.indexOf('{') >= 0) {
216 
217             
218             List<String> originals = new ArrayList<String>();
219             for(Matcher m = INVALID_FORMAT_ELEMENT.matcher(st); m.find() ; m = INVALID_FORMAT_ELEMENT.matcher(st)) {
220                 originals.add(m.group());
221                 st = m.replaceFirst("INVALID_FORMAT_ELEMENT");
222             }
223 
224             
225             st = new MessageFormat(st.replaceAll("'", "'''"), ROOT_LOCALE)
226                     .format(vars, new StringBuffer(), null).toString();
227 
228             
229             for (String original : originals) {
230                 st = st.replaceFirst("INVALID_FORMAT_ELEMENT", original);
231             }
232         }
233         return st;
234     }
235 
236     private static Object[] replaceNullsWithBlank(Object[] varsOrig) {
237         Object[] vars = new Object[varsOrig.length];
238         for(int i = 0; i < varsOrig.length; ++i) {
239             vars[i] = null != varsOrig[i] ? varsOrig[i] : "";
240         }
241         return vars;
242     }
243 }