From 9a00e131fd3f2f97ba9cc80f4b65cb6da6b368b4 Mon Sep 17 00:00:00 2001 From: Vincent Hennebert Date: Fri, 9 Apr 2010 16:48:27 +0000 Subject: [PATCH] Generate event models in the same directory as the corresponding EventProducer interfaces and name it event-model.xml by default The eventResourceGenerator task now takes a destination directory parameter instead of modelfile git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@932502 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 34 ++++----- .../fop/tools/EventProducerCollector.java | 21 ++--- .../fop/tools/EventProducerCollectorTask.java | 76 ++++++++++++------- 3 files changed, 72 insertions(+), 59 deletions(-) diff --git a/build.xml b/build.xml index f85bceecd..026a68b82 100644 --- a/build.xml +++ b/build.xml @@ -360,57 +360,57 @@ list of possible build targets. - - - - - - - @@ -418,14 +418,14 @@ list of possible build targets. - @@ -433,7 +433,7 @@ list of possible build targets. @@ -441,7 +441,7 @@ list of possible build targets. @@ -449,14 +449,14 @@ list of possible build targets. - @@ -464,7 +464,7 @@ list of possible build targets. @@ -838,7 +838,7 @@ list of possible build targets. + destdir="${build.dir}/test-gensrc"> diff --git a/src/codegen/java/org/apache/fop/tools/EventProducerCollector.java b/src/codegen/java/org/apache/fop/tools/EventProducerCollector.java index 5cefc1d4a..4721f41df 100644 --- a/src/codegen/java/org/apache/fop/tools/EventProducerCollector.java +++ b/src/codegen/java/org/apache/fop/tools/EventProducerCollector.java @@ -21,7 +21,9 @@ package org.apache.fop.tools; import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.Map; import org.apache.fop.events.EventProducer; @@ -61,7 +63,7 @@ class EventProducerCollector { } private DocletTagFactory tagFactory; - private EventModel model = new EventModel(); + private List models = new ArrayList(); /** * Creates a new EventProducerCollector. @@ -128,7 +130,9 @@ class EventProducerCollector { EventMethodModel methodMeta = createMethodModel(method); prodMeta.addMethod(methodMeta); } - this.model.addProducer(prodMeta); + EventModel model = new EventModel(); + model.addProducer(prodMeta); + models.add(model); } private EventMethodModel createMethodModel(JavaMethod method) @@ -193,17 +197,8 @@ class EventProducerCollector { * Returns the event model that has been accumulated. * @return the event model. */ - public EventModel getModel() { - return this.model; - } - - /** - * Saves the accumulated event model to an XML file. - * @param modelFile the target model file - * @throws IOException if an I/O error occurs - */ - public void saveModelToXML(File modelFile) throws IOException { - getModel().saveToXML(modelFile); + public List getModels() { + return this.models; } } diff --git a/src/codegen/java/org/apache/fop/tools/EventProducerCollectorTask.java b/src/codegen/java/org/apache/fop/tools/EventProducerCollectorTask.java index b089a361e..a79dfc975 100644 --- a/src/codegen/java/org/apache/fop/tools/EventProducerCollectorTask.java +++ b/src/codegen/java/org/apache/fop/tools/EventProducerCollectorTask.java @@ -47,6 +47,9 @@ import org.apache.tools.ant.Task; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.selectors.FilenameSelector; +import org.apache.fop.events.model.EventModel; +import org.apache.fop.events.model.EventProducerModel; + /** * Ant task which inspects a file set for Java interfaces which extend the * {@link org.apache.fop.events.EventProducer} interface. For all such interfaces an event model @@ -56,7 +59,7 @@ import org.apache.tools.ant.types.selectors.FilenameSelector; public class EventProducerCollectorTask extends Task { private List filesets = new java.util.ArrayList(); - private File modelFile; + private File destDir; private File translationFile; /** {@inheritDoc} */ @@ -64,19 +67,23 @@ public class EventProducerCollectorTask extends Task { try { EventProducerCollector collector = new EventProducerCollector(); long lastModified = processFileSets(collector); - File parentDir = getModelFile().getParentFile(); - if (!parentDir.exists() && !parentDir.mkdirs()) { - throw new BuildException( - "Could not create target directory for event model file: " + parentDir); - } - if (!getModelFile().exists() || lastModified > getModelFile().lastModified()) { - collector.saveModelToXML(getModelFile()); - log("Event model written to " + getModelFile()); - } - if (getTranslationFile() != null) { - if (!getTranslationFile().exists() - || lastModified > getTranslationFile().lastModified()) { - updateTranslationFile(); + for (Iterator iter = collector.getModels().iterator(); iter.hasNext();) { + EventModel model = (EventModel) iter.next(); + File parentDir = getParentDir(model); + if (!parentDir.exists() && !parentDir.mkdirs()) { + throw new BuildException( + "Could not create target directory for event model file: " + parentDir); + } + File modelFile = new File(parentDir, "event-model.xml"); + if (!modelFile.exists() || lastModified > modelFile.lastModified()) { + model.saveToXML(modelFile); + log("Event model written to " + modelFile); + } + if (getTranslationFile() != null) { + if (!getTranslationFile().exists() + || lastModified > getTranslationFile().lastModified()) { + updateTranslationFile(modelFile); + } } } } catch (ClassNotFoundException e) { @@ -91,18 +98,34 @@ public class EventProducerCollectorTask extends Task { private static final String MODEL2TRANSLATION = "model2translation.xsl"; private static final String MERGETRANSLATION = "merge-translation.xsl"; + private File getParentDir(EventModel model) { + Iterator iter = model.getProducers(); + assert iter.hasNext(); + EventProducerModel producer = (EventProducerModel) iter.next(); + assert !iter.hasNext(); + String interfaceName = producer.getInterfaceName(); + int startLocalName = interfaceName.lastIndexOf("."); + if (startLocalName < 0) { + return destDir; + } else { + String dirname = interfaceName.substring(0, startLocalName); + dirname = dirname.replace('.', File.separatorChar); + return new File(destDir, dirname); + } + } + /** * Updates the translation file with new entries for newly found event producer methods. * @throws IOException if an I/O error occurs */ - protected void updateTranslationFile() throws IOException { + protected void updateTranslationFile(File modelFile) throws IOException { try { boolean resultExists = getTranslationFile().exists(); SAXTransformerFactory tFactory = (SAXTransformerFactory)SAXTransformerFactory.newInstance(); //Generate fresh generated translation file as template - Source src = new StreamSource(getModelFile().toURI().toURL().toExternalForm()); + Source src = new StreamSource(modelFile.toURI().toURL().toExternalForm()); StreamSource xslt1 = new StreamSource( getClass().getResourceAsStream(MODEL2TRANSLATION)); if (xslt1.getInputStream() == null) { @@ -204,19 +227,15 @@ public class EventProducerCollectorTask extends Task { } /** - * Sets the model file to be written. - * @param f the model file + * Sets the destination directory for the event models. + * + * @param destDir the destination directory */ - public void setModelFile(File f) { - this.modelFile = f; - } - - /** - * Returns the model file to be written. - * @return the model file - */ - public File getModelFile() { - return this.modelFile; + public void setDestDir(File destDir) { + if (!destDir.isDirectory()) { + throw new IllegalArgumentException("destDir must be a directory"); + } + this.destDir = destDir; } /** @@ -257,7 +276,6 @@ public class EventProducerCollectorTask extends Task { File targetDir = new File("build/codegen1"); targetDir.mkdirs(); - generator.setModelFile(new File("D:/out.xml")); generator.setTranslationFile(new File("D:/out1.xml")); generator.execute(); } catch (Exception e) { -- 2.39.5