1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.tapfx.easyinsert;
17
18 import java.util.List;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.hivemind.Resource;
23 import org.apache.tapestry.parse.ITemplateParserDelegate;
24 import org.apache.tapestry.parse.TemplateParseException;
25 import org.apache.tapestry.parse.TemplateParser;
26 import org.apache.tapestry.parse.TemplateToken;
27
28 /***
29 * For replacing ${something} into
30 * <span jwcid="@Insert" value="ognl:something"/>
31 * @author andyhot
32 * @version $Id: EasyInsertTemplateParser.java,v 1.3 2005/11/04 23:42:43 andyhot Exp $
33 */
34 public class EasyInsertTemplateParser extends TemplateParser
35 {
36 private static final Log log = LogFactory.getLog(EasyInsertTemplateParser.class);
37
38 private String _prefix = "$";
39
40 public EasyInsertTemplateParser()
41 {
42 super();
43 }
44
45 public void setPrefix(String prefix)
46 {
47 System.out.println("#### Setting prefix to: " + prefix);
48 _prefix = prefix;
49 }
50
51 /***
52 * Sets the prefix from a list. Only the first element is used (for now).
53 * This is needed so that hivemind can inject gathered configurations.
54 * @param list
55 */
56 public void setPrefixes(List list)
57 {
58 System.out.println("#### Setting prefixes to: " + list);
59 if (list != null && list.size() > 0)
60 {
61 EasyInsertConfiguration conf = (EasyInsertConfiguration) list.get(0);
62 setPrefix(conf.getPrefix());
63 }
64 }
65
66 public TemplateToken[] parse(char[] templateData,
67 ITemplateParserDelegate delegate,
68 Resource resourceLocation)
69 throws TemplateParseException
70 {
71 String template = new String(templateData);
72
73 boolean needsChange = EasyInsertTemplateParser.needsProcessing(template, _prefix);
74
75 if (needsChange == true)
76 {
77 template = EasyInsertTemplateParser.process(template, _prefix);
78 log.debug("Expanded: " + resourceLocation.getPath());
79 }
80
81 TemplateToken[] tokens = super.parse(template.toCharArray(), delegate, resourceLocation);
82 return tokens;
83 }
84
85 protected static String process(String template)
86 {
87 return process(template, "$");
88 }
89
90 protected static String process(String template, String prefix)
91 {
92 int pos = 0;
93 do
94 {
95 pos = template.indexOf(prefix + "{", pos);
96 if (pos >=0)
97 {
98 int pos2=template.indexOf("}", pos);
99 if (pos2>=0)
100 {
101 String oldValue = template.substring(pos + 2, pos2);
102 String newValue = EasyInsertTemplateParser.applyBinding(oldValue);
103 int inside = insideTag(template, pos, pos2);
104 if (inside == 0)
105 {
106 template = template.substring(0, pos)
107 + "<span jwcid=\"@Insert\" value=\"" + newValue + "\"/>"
108 + template.substring(pos2 + 1);
109 }
110 else if (inside==1)
111 {
112 template = template.substring(0, pos)
113 + newValue
114 + template.substring(pos2 + 1);
115 }
116 else
117 {
118 template = template.substring(0, pos)
119 + newValue
120 +"\" jwcid=\"@Any\""
121 + template.substring(pos2 + 2);
122 }
123 }
124 }
125 }
126 while (pos>=0);
127 return template;
128 }
129
130 /***
131 *
132 * @return 0 for no, 1 for yes and the tag has a jwcid, 2 for yes and the tag
133 * does NOT have a jwcid.
134 */
135 protected static int insideTag(String template, int start, int end)
136 {
137 if (start<=0 || end>=template.length() - 1)
138 return 0;
139
140 int beforeClose = template.lastIndexOf(">", start);
141 int beforeOpen = template.lastIndexOf("<", start);
142
143 if (beforeClose > beforeOpen)
144 return 0;
145
146 int afterClose = template.indexOf(">", end);
147
148 int jwcid = template.indexOf("jwcid", beforeOpen);
149
150 if (jwcid<0 || jwcid > afterClose)
151 return 2;
152 else
153 return 1;
154 }
155
156 /***
157 *
158 * @return 0 for no, 1 for yes and the tag has a jwcid, 2 for yes and the tag
159 * does NOT have a jwcid.
160 */
161 protected static int insideTag(String template, int start)
162 {
163 return insideTag(template, start, template.length());
164 }
165
166 /***
167 *
168 * @return 0 for no, 1 for yes and the tag has a jwcid, 2 for yes and the tag
169 * does NOT have a jwcid.
170 */
171 protected static int insideTag(String template)
172 {
173 return insideTag(template, 0, template.length());
174 }
175
176 protected static boolean needsProcessing(String value)
177 {
178 return needsProcessing(value, "$");
179 }
180
181 protected static boolean needsProcessing(String value, String prefix)
182 {
183 return value.indexOf(prefix + "{")>=0;
184 }
185
186 protected static String applyBinding(String value)
187 {
188 int pos = value.indexOf(":");
189 if (pos<0)
190 {
191 return "ognl:" + value;
192 }
193 else
194 {
195 for (int i = 0; i < pos; i++)
196 {
197 char c = value.charAt(i);
198 if ((c>='a' && c<='z') || (c>='A' && c<='Z'))
199 continue;
200 return "ognl:" + value;
201 }
202 return value;
203 }
204 }
205 }