1 package org.apache.velocity.convert;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileWriter;
24 import java.io.IOException;
25
26 import org.apache.oro.text.perl.Perl5Util;
27 import org.apache.velocity.util.StringUtils;
28 import org.apache.tools.ant.DirectoryScanner;
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class WebMacro
43 {
44
45
46
47 protected static final String VM_EXT = ".vm";
48
49
50
51
52 protected static final String WM_EXT = ".wm";
53
54
55
56
57
58
59 protected static String[] perLineREs =
60 {
61
62 "#if\\s*[(]\\s*(.*\\S)\\s*[)]\\s*(#begin|{)[ \\t]?",
63 "#if( $1 )",
64
65
66 "[ \\t]?(#end|})[ \\t]*\n(\\s*)#else\\s*(#begin|{)[ \\t]?(\\w)",
67 "$2#else#**#$4",
68 "[ \\t]?(#end|})[ \\t]*\n(\\s*)#else\\s*(#begin|{)[ \\t]?",
69 "$2#else",
70 "(#end|})(\\s*#else)\\s*(#begin|{)[ \\t]?",
71 "$1\n$2",
72
73
74 "#foreach\\s+(\\$\\w+)\\s+in\\s+(\\$[^\\s#]+)\\s*(#begin|{)[ \\t]?",
75 "#foreach( $1 in $2 )",
76
77
78 "#set\\s+(\\$[^\\s=]+)\\s*=\\s*([\\S \\t]+)",
79 "#set( $1 = $2 )",
80 "(##[# \\t\\w]*)\\)",
81 ")$1",
82
83
84 "#parse\\s+([^\\s#]+)[ \\t]?",
85 "#parse( $1 )",
86
87
88 "#include\\s+([^\\s#]+)[ \\t]?",
89 "#include( $1 )",
90
91
92 "\\$\\(([^\\)]+)\\)",
93 "${$1}",
94 "\\${([^}\\(]+)\\(([^}]+)}\\)",
95 "${$1($2)}",
96
97
98 "\\$_",
99 "$l_",
100 "\\${(_[^}]+)}",
101 "${l$1}",
102
103
104 "(#set\\s*\\([^;]+);(\\s*\\))",
105 "$1$2",
106
107
108 "(^|[^\\\\])\\$(\\w[^=\n;'\"]*);",
109 "$1${$2}",
110
111
112 "\\.wm",
113 ".vm"
114 };
115
116
117
118
119
120
121 public void convert(String target)
122 {
123 File file = new File(target);
124
125 if (!file.exists())
126 {
127 throw new RuntimeException("The specified template or directory does not exist");
128 }
129
130 if (file.isDirectory())
131 {
132 String basedir = file.getAbsolutePath();
133 String newBasedir = basedir + VM_EXT;
134
135 DirectoryScanner ds = new DirectoryScanner();
136 ds.setBasedir(basedir);
137 ds.addDefaultExcludes();
138 ds.scan();
139 String[] files = ds.getIncludedFiles();
140
141 for (int i = 0; i < files.length; i++)
142 {
143 writeTemplate(files[i], basedir, newBasedir);
144 }
145 }
146 else
147 {
148 writeTemplate(file.getAbsolutePath(), "", "");
149 }
150 }
151
152
153
154
155
156 private boolean writeTemplate(String file, String basedir,
157 String newBasedir)
158 {
159 if (file.indexOf(WM_EXT) < 0)
160 {
161 return false;
162 }
163
164 System.out.println("Converting " + file + "...");
165
166 String template = file;
167 String newTemplate = convertName(file);
168
169 if (basedir.length() > 0)
170 {
171 String templateDir = newBasedir + extractPath(file);
172 File outputDirectory = new File(templateDir);
173
174 template = basedir + File.separator + file;
175
176
177 if (! outputDirectory.exists())
178 {
179 outputDirectory.mkdirs();
180 }
181
182 newTemplate = newBasedir + File.separator + convertName(file);
183 }
184
185 String convertedTemplate = convertTemplate(template);
186
187 FileWriter fw = null;
188 try
189 {
190 fw = new FileWriter(newTemplate);
191 fw.write(convertedTemplate);
192 }
193 catch (Exception e)
194 {
195 e.printStackTrace();
196 }
197 finally
198 {
199 if (fw != null)
200 {
201 try
202 {
203 fw.close();
204 }
205 catch (IOException io)
206 {
207
208 }
209 }
210 }
211
212 return true;
213 }
214
215
216
217
218
219 private final String extractPath(String file)
220 {
221 int lastSepPos = file.lastIndexOf(File.separator);
222 return (lastSepPos == -1 ? "" :
223 File.separator + file.substring(0, lastSepPos));
224 }
225
226
227
228
229 private String convertName(String name)
230 {
231 return (name.indexOf(WM_EXT) < 0)
232 ? name
233 : name.substring(0, name.indexOf(WM_EXT)) + VM_EXT;
234 }
235
236
237
238
239 private static final void usage()
240 {
241 System.err.println("Usage: convert-wm <template.wm | directory>");
242 }
243
244
245
246
247
248
249 public String convertTemplate(String template)
250 {
251 String contents = StringUtils.fileContentsToString(template);
252
253
254
255 if (!contents.endsWith("\n"))
256 {
257 contents += "\n";
258 }
259
260
261 Perl5Util perl = new Perl5Util();
262 for (int i = 0; i < perLineREs.length; i += 2)
263 {
264 contents = perl.substitute(makeSubstRE(i), contents);
265 }
266
267
268 if (perl.match("m/javascript/i", contents))
269 {
270
271 contents = perl.substitute("s/\n}/\n#end/g", contents);
272 }
273 else
274 {
275 contents = perl.substitute("s/(\n\\s*)}/$1#end/g", contents);
276 contents = perl.substitute("s/#end\\s*\n\\s*#else/#else/g",
277 contents);
278 }
279
280 return contents;
281 }
282
283
284
285
286 private final String makeSubstRE(int i)
287 {
288 return ("s/" + perLineREs[i] + '/' + perLineREs[i + 1] + "/g");
289 }
290
291
292
293
294
295 public static void main(String[] args)
296 {
297 if (args.length > 0)
298 {
299 for (int x=0; x < args.length; x++)
300 {
301 WebMacro converter = new WebMacro();
302 converter.convert(args[x]);
303 }
304 }
305 else
306 {
307 usage();
308 }
309 }
310 }