We don't really need Velocity here, we just want some simple key/value

substitutions. Ditch our dependency on the sometimes fragile and frequently
annoying template engine and do things the old fashioned way.
This commit is contained in:
Michael Bayne
2009-07-11 21:35:19 +00:00
parent c1166e0f4a
commit 1daac11a36
5 changed files with 32 additions and 39 deletions
+1 -2
View File
@@ -3,11 +3,10 @@
<project name="library-dependencies">
<fileset dir="${libs.dir}" id="depot.libs">
<include name="ant.jar"/>
<include name="commons-collections.jar"/>
<include name="commons-io.jar"/>
<include name="ehcache.jar"/>
<include name="google-collect.jar"/>
<include name="junit4.jar"/>
<include name="samskivert.jar"/>
<include name="velocity-1.5-dev.jar"/>
</fileset>
</project>
@@ -33,13 +33,17 @@ import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.io.IOUtils;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Task;
@@ -47,9 +51,6 @@ import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Reference;
import org.apache.tools.ant.util.ClasspathUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.annotation.Id;
import com.samskivert.depot.annotation.Transient;
@@ -57,7 +58,6 @@ import com.samskivert.depot.impl.DepotUtil;
import com.samskivert.util.ClassUtil;
import com.samskivert.util.GenUtil;
import com.samskivert.util.StringUtil;
import com.samskivert.velocity.VelocityUtil;
/**
* An ant task that updates the column constants for a persistent record.
@@ -89,12 +89,6 @@ public class GenRecordTask extends Task
throw new BuildException(errmsg);
}
try {
_velocity = VelocityUtil.createEngine();
} catch (Exception e) {
throw new BuildException("Failure initializing Velocity", e);
}
// resolve the PersistentRecord class using our classloader
try {
_prclass = _cloader.loadClass(PersistentRecord.class.getName());
@@ -257,22 +251,22 @@ public class GenRecordTask extends Task
StringBuilder fsection = new StringBuilder();
// add our prototype declaration
VelocityContext ctx = new VelocityContext();
ctx.put("record", rname);
fsection.append(mergeTemplate(PROTO_TMPL, ctx));
Map<String, String> subs = Maps.newHashMap();
subs.put("record", rname);
fsection.append(mergeTemplate(PROTO_TMPL, subs));
// add our ColumnExp constants
for (int ii = 0; ii < flist.size(); ii++) {
Field f = flist.get(ii);
String fname = f.getName();
// create our velocity context
VelocityContext fctx = (VelocityContext)ctx.clone();
fctx.put("field", fname);
fctx.put("capfield", StringUtil.unStudlyName(fname).toUpperCase());
// create our substitution mappings
Map<String, String> fsubs = Maps.newHashMap(subs);
fsubs.put("field", fname);
fsubs.put("capfield", StringUtil.unStudlyName(fname).toUpperCase());
// now generate our bits
fsection.append(mergeTemplate(COL_TMPL, fctx));
fsection.append(mergeTemplate(COL_TMPL, fsubs));
}
// generate our methods section
@@ -295,12 +289,12 @@ public class GenRecordTask extends Task
fieldNameList.append(StringUtil.unStudlyName(name));
}
ctx.put("argList", argList.toString());
ctx.put("argNameList", argNameList.toString());
ctx.put("fieldNameList", fieldNameList.toString());
subs.put("argList", argList.toString());
subs.put("argNameList", argNameList.toString());
subs.put("fieldNameList", fieldNameList.toString());
// generate our bits and append them as appropriate to the string buffers
msection.append(mergeTemplate(KEY_TMPL, ctx));
msection.append(mergeTemplate(KEY_TMPL, subs));
}
// now bolt everything back together into a class declaration
@@ -388,12 +382,15 @@ public class GenRecordTask extends Task
}
/** Helper function for generating our boilerplate code. */
protected String mergeTemplate (String tmpl, VelocityContext ctx)
protected String mergeTemplate (String tmpl, Map<String, String> subs)
{
try {
StringWriter writer = new StringWriter();
_velocity.mergeTemplate(tmpl, "UTF-8", ctx, writer);
return writer.toString();
String text = IOUtils.toString(
getClass().getClassLoader().getResourceAsStream(tmpl), "UTF-8");
for (Map.Entry<String, String> entry : subs.entrySet()) {
text = text.replaceAll("@"+entry.getKey()+"@", entry.getValue());
}
return text;
} catch (Exception e) {
throw new BuildException("Failed processing template [tmpl=" + tmpl + "]", e);
}
@@ -453,9 +450,6 @@ public class GenRecordTask extends Task
/** Used to do our own classpath business. */
protected ClassLoader _cloader;
/** Used to generate source files from templates. */
protected VelocityEngine _velocity;
/** {@link PersistentRecord} resolved with the proper classloader so that we can compare it to
* loaded derived classes. */
protected Class<?> _prclass;
@@ -1 +1 @@
public static final ColumnExp ${capfield} = colexp(_R, "$field");
public static final ColumnExp @capfield@ = colexp(_R, "@field@");
@@ -1,11 +1,11 @@
/**
* Create and return a primary {@link Key} to identify a {@link $record}
* Create and return a primary {@link Key} to identify a {@link @record@}
* with the supplied key values.
*/
public static Key<${record}> getKey (${argList})
public static Key<@record@> getKey (@argList@)
{
return new Key<${record}>(
${record}.class,
new ColumnExp[] { $fieldNameList },
new Comparable[] { $argNameList });
return new Key<@record@>(
@record@.class,
new ColumnExp[] { @fieldNameList@ },
new Comparable[] { @argNameList@ });
}
@@ -1 +1 @@
public static final Class<${record}> _R = ${record}.class;
public static final Class<@record@> _R = @record@.class;