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