Re: enhancement request for Ant event builder task
Thorsten Möller <[email protected]>
| Newsgroups | gmane.comp.java.enhydra.barracuda.general |
|---|---|
| Message-ID | <001001c37c36$7fba2650$2a2e4c8d@Thoro> |
Jacob Kjome <[email protected]> wrote: > I'd be very open to a change like this. However, I don't see it as a > priority. If you are willing to modify the code (from the current > CVS) to add this capability (as an optional parameter), I'll surely > review it and check it in if it passes muster. Yep, the code is changed and I generated a patch (rdiff) against the head of CVS. I included the patch as an attachment to this E-Mail. It is now possible to have a task like this: <events sourceout="myOutDir" descriptor="myOptionsDir/events.xml" template="myOptionsDir/myEventTemplate.template"/> The algorithm to choose the template is as follows: 1. Choose the template attribute inside the Ant task. If the attribute is absent: 2. Choose the template attribute inside the <build-events> tag. If the attribute is absent: 3. Fallback and choose "EventBuilder.template". Thorsten
EventBuilder.patch.txt
(text/plain, 6.3 KB)
Index: EventBuilder.java =================================================================== RCS file: /var/cvs/Barracuda/src/org/enhydra/barracuda/taskdefs/EventBuilder.java,v retrieving revision 1.13 diff -u -r1.13 EventBuilder.java --- EventBuilder.java 11 Sep 2003 14:51:17 -0000 1.13 +++ EventBuilder.java 16 Sep 2003 09:03:22 -0000 @@ -23,14 +23,32 @@ */ package org.enhydra.barracuda.taskdefs; -import java.util.*; -import java.io.*; -import org.apache.tools.ant.*; -import org.apache.tools.ant.taskdefs.*; -import org.apache.tools.ant.types.*; - -import org.xml.sax.*; -import org.xml.sax.helpers.*; +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.Task; +import org.apache.tools.ant.taskdefs.Replace; +import org.apache.tools.ant.util.FileUtils; +import org.xml.sax.Attributes; +import org.xml.sax.EntityResolver; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.DefaultHandler; +import org.xml.sax.helpers.XMLReaderFactory; /** * <p>This Ant taskdef reads in an xml file that conforms to @@ -43,12 +61,16 @@ * * <p>This taskdef takes two required parameters: <strong>sourceout</strong> (which * should point to a location to write generated .java event source files) and - * <strong>descriptor<strong> (which should refer to the location of a valid xml file - * describing the event hierarchy).</p> + * <strong>descriptor</strong> (which should refer to the location of a valid xml file + * describing the event hierarchy).<br>Second, there is a third optional parameter: + * <strong>template</strong> which should refer to the localtion of the template file + * for generating the Java classes. Specifying this parameter will overwrite the + * template attribute inside the "build-events" tag in the xml file.</p> * * <p>Look at the Barracuda build.xml file for further usage examples.</p> * * @author Christian Cryder <a href="mailto:[email protected]">[email protected]</a> + * @author Thorsten Möller - ThorstenMoeller(at)web.de */ public class EventBuilder extends Task { @@ -58,6 +80,7 @@ //private vars protected String parserClass = null; protected File xmlFile = null; //the xml file + protected File templateFile = null; //the Java class template protected String sourceOutDir = null; //the directory where source is generated /** @@ -73,6 +96,13 @@ public void setSourceout(String sourceOutDir) { this.sourceOutDir = sourceOutDir; } + + /** + * Sets the Java class template. + */ + public void setTemplate(File templateFile) { + this.templateFile = templateFile; + } /** * Parse the specified event.xml file, generate event classes from @@ -299,7 +329,7 @@ } public void endDocument() { - if (fileCnt>0) log("Created "+fileCnt+" event files from "+xmlFile); + if (fileCnt>0) log("Created "+fileCnt+" event files from: "+xmlFile+((templateFile!=null)? ", using template: "+templateFile : "")); } private String getLocationString(SAXParseException ex) { @@ -319,38 +349,40 @@ protected void loadTemplate() { if (templateBytes==null) { - InputStream is = null; - templateBytes = new byte[10240]; + + BufferedReader br = null; try { - //if the specified a different template, load it from there - //otherwise use the default template from the classpath - if (cs.template!=null) is = new FileInputStream(cs.template); - else is = this.getClass().getResourceAsStream("EventBuilder.template"); - - //read in the file and store it in a byte array - BufferedInputStream in = new BufferedInputStream(is); - int offset = 0; - int len = 1024; - byte[] inbytes = new byte[len]; - while (true) { - int cnt = in.read(inbytes, 0, len); - if (cnt==-1) break; - if (offset+cnt>=templateBytes.length) { - byte[] newbytes = new byte[(int) (templateBytes.length+(len*5))]; - System.arraycopy(templateBytes, 0, newbytes, 0, templateBytes.length); - templateBytes = newbytes; - } - System.arraycopy(inbytes, 0, templateBytes, offset, cnt); - offset+=cnt; - } - - //when we're done trim off the excess - byte[] newbytes = new byte[offset]; - System.arraycopy(templateBytes, 0, newbytes, 0, offset); - templateBytes = newbytes; + + if (templateFile != null && templateFile.exists()) + { + br = new BufferedReader(new FileReader(templateFile)); + } + else if (cs.template != null) + { + br = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(cs.template))); + } + else + { + br = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("EventBuilder.template"))); + } + + templateBytes = FileUtils.readFully(br).getBytes(); } catch (IOException e) { throw new BuildException("Error reading template", e); + } finally + { + try + { + if (br != null) + { + br.close(); + } + } + catch (IOException e) + { + throw new BuildException("Error closing template", e); + } } } }