dlr 01/05/07 20:38:54
Modified: src/java/org/apache/velocity/convert WebMacro.java
Log:
Conversion enhancement and class cleanup:
* Turned comment for res into a JavaDoc comment. Also added a TODO
item which I haven't implemented yet.
* VL is now known as VTL.
* Added conversion regex for explicitly terminated WM statements.
* Changed convert() signature to take a single target argument instead
of a String[] (was only using the first element). Changed callers
appropriately. Moved program arguments length check into main().
* Changed usage() from a public instance method to a private static
final class method, since it's now called only from main().
* Changed call to ORO regex subsitution method to use the global
option (thanks Geir!).
Revision Changes Path
1.11 +21 -15
jakarta-velocity/src/java/org/apache/velocity/convert/WebMacro.java
Index: WebMacro.java
===================================================================
RCS file:
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/convert/WebMacro.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- WebMacro.java 2001/05/08 01:27:21 1.10
+++ WebMacro.java 2001/05/08 03:38:53 1.11
@@ -70,7 +70,7 @@
* this class.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
- * @version $Id: WebMacro.java,v 1.10 2001/05/08 01:27:21 dlr Exp $
+ * @version $Id: WebMacro.java,v 1.11 2001/05/08 03:38:53 dlr Exp $
*/
public class WebMacro
{
@@ -86,10 +86,12 @@
protected final static String VM_EXT = ".vm";
protected final static String WM_EXT = ".wm";
- /*
+ /**
* The regexes to use for substition. The regexes come
* in pairs. The first is the string to match, the
* second is the substitution to make.
+ * <p>
+ * TODO: Handle case where $ is escaped, such as <code>\$foo</code>
*/
protected String[] res =
{
@@ -113,7 +115,7 @@
// possibility of javascript.
"\n}", // assumes that javascript is indented, WMs not!!!
"\n#end",
-
+
// Convert WM style #set to Velocity directive style.
"#set\\s+(\\$[^\\s=]+)\\s*=\\s*(.*\\S)[ \\t]*",
"#set( $1 = $2 )",
@@ -128,7 +130,7 @@
"#include\\s+([^\\s#]+)[ \\t]?",
"#include( $1 )",
- // Convert WM formal reference to VL syntax.
+ // Convert WM formal reference to VTL syntax.
"\\$\\(([^\\)]+)\\)",
"${$1}",
"\\${([^}\\(]+)\\(([^}]+)}\\)", // fix encapsulated brakets: {(})
@@ -139,7 +141,11 @@
"$l_",
"\\${(_[^}]+)}", // within a formal reference
"${l$1}",
-
+
+ // Convert explicitly terminated WM statements to VTL syntax.
+ "\\$([^; \\t]+);",
+ "${$1}",
+
// Change extensions when seen.
"\\.wm",
".vm"
@@ -149,12 +155,9 @@
* Iterate through the set of find/replace regexes
* that will convert a given WM template to a VM template
*/
- public void convert(String args[])
+ public void convert(String target)
{
- if (args.length < 1)
- usage();
-
- File file = new File(args[0]);
+ File file = new File(target);
if (!file.exists())
{
@@ -167,7 +170,7 @@
{
String basedir = file.getAbsolutePath();
String newBasedir = basedir + VM_EXT;
-
+
DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir(basedir);
ds.addDefaultExcludes();
@@ -179,7 +182,7 @@
}
else
{
- writeTemplate(args[0], "", "");
+ writeTemplate(file.getAbsolutePath(), "", "");
}
}
@@ -261,7 +264,7 @@
/**
* How to use this little puppy :-)
*/
- public void usage()
+ private static final void usage()
{
System.err.println("Usage: convert-wm <template.wm | directory>");
System.exit(1);
@@ -284,7 +287,7 @@
while (perl.match("/" + res[i] + "/", orignalTemplate))
{
orignalTemplate = perl.substitute(
- "s/" + res[i] + "/" + res[i+1] + "/", orignalTemplate);
+ "s/" + res[i] + "/" + res[i+1] + "/g", orignalTemplate);
}
}
@@ -296,7 +299,10 @@
*/
public static void main(String[] args)
{
+ if (args.length < 1)
+ usage();
+
WebMacro converter = new WebMacro();
- converter.convert(args);
+ converter.convert(args[0]);
}
}