You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EventProducerCollectorTask.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.tools;
  19. import java.io.File;
  20. import java.io.FileNotFoundException;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import javax.xml.transform.Result;
  26. import javax.xml.transform.Source;
  27. import javax.xml.transform.Transformer;
  28. import javax.xml.transform.TransformerException;
  29. import javax.xml.transform.URIResolver;
  30. import javax.xml.transform.dom.DOMResult;
  31. import javax.xml.transform.dom.DOMSource;
  32. import javax.xml.transform.sax.SAXTransformerFactory;
  33. import javax.xml.transform.stream.StreamResult;
  34. import javax.xml.transform.stream.StreamSource;
  35. import org.w3c.dom.Node;
  36. import org.apache.commons.io.IOUtils;
  37. import org.apache.tools.ant.BuildException;
  38. import org.apache.tools.ant.DirectoryScanner;
  39. import org.apache.tools.ant.Project;
  40. import org.apache.tools.ant.Task;
  41. import org.apache.tools.ant.types.FileSet;
  42. import org.apache.tools.ant.types.selectors.FilenameSelector;
  43. import org.apache.fop.events.model.EventModel;
  44. import org.apache.fop.events.model.EventProducerModel;
  45. /**
  46. * Ant task which inspects a file set for Java interfaces which extend the
  47. * {@link org.apache.fop.events.EventProducer} interface. For all such interfaces an event model
  48. * file and a translation file for the human-readable messages generated by the events is
  49. * created and/or updated.
  50. */
  51. public class EventProducerCollectorTask extends Task {
  52. private List filesets = new java.util.ArrayList();
  53. private File destDir;
  54. private File translationFile;
  55. /** {@inheritDoc} */
  56. public void execute() throws BuildException {
  57. try {
  58. EventProducerCollector collector = new EventProducerCollector();
  59. long lastModified = processFileSets(collector);
  60. for (Iterator iter = collector.getModels().iterator(); iter.hasNext();) {
  61. EventModel model = (EventModel) iter.next();
  62. File parentDir = getParentDir(model);
  63. if (!parentDir.exists() && !parentDir.mkdirs()) {
  64. throw new BuildException(
  65. "Could not create target directory for event model file: " + parentDir);
  66. }
  67. File modelFile = new File(parentDir, "event-model.xml");
  68. if (!modelFile.exists() || lastModified > modelFile.lastModified()) {
  69. model.saveToXML(modelFile);
  70. log("Event model written to " + modelFile);
  71. }
  72. if (getTranslationFile() != null) {
  73. // TODO Remove translation file creation facility?
  74. if (!getTranslationFile().exists()
  75. || lastModified > getTranslationFile().lastModified()) {
  76. updateTranslationFile(modelFile);
  77. }
  78. }
  79. }
  80. } catch (ClassNotFoundException e) {
  81. throw new BuildException(e);
  82. } catch (EventConventionException ece) {
  83. throw new BuildException(ece);
  84. } catch (IOException ioe) {
  85. throw new BuildException(ioe);
  86. }
  87. }
  88. private static final String MODEL2TRANSLATION = "model2translation.xsl";
  89. private static final String MERGETRANSLATION = "merge-translation.xsl";
  90. private File getParentDir(EventModel model) {
  91. Iterator iter = model.getProducers();
  92. assert iter.hasNext();
  93. EventProducerModel producer = (EventProducerModel) iter.next();
  94. assert !iter.hasNext();
  95. String interfaceName = producer.getInterfaceName();
  96. int startLocalName = interfaceName.lastIndexOf(".");
  97. if (startLocalName < 0) {
  98. return destDir;
  99. } else {
  100. String dirname = interfaceName.substring(0, startLocalName);
  101. dirname = dirname.replace('.', File.separatorChar);
  102. return new File(destDir, dirname);
  103. }
  104. }
  105. /**
  106. * Updates the translation file with new entries for newly found event producer methods.
  107. * @param modelFile the model file to use
  108. * @throws IOException if an I/O error occurs
  109. */
  110. protected void updateTranslationFile(File modelFile) throws IOException {
  111. try {
  112. boolean resultExists = getTranslationFile().exists();
  113. SAXTransformerFactory tFactory
  114. = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  115. //Generate fresh generated translation file as template
  116. Source src = new StreamSource(modelFile.toURI().toURL().toExternalForm());
  117. StreamSource xslt1 = new StreamSource(
  118. getClass().getResourceAsStream(MODEL2TRANSLATION));
  119. if (xslt1.getInputStream() == null) {
  120. throw new FileNotFoundException(MODEL2TRANSLATION + " not found");
  121. }
  122. DOMResult domres = new DOMResult();
  123. Transformer transformer = tFactory.newTransformer(xslt1);
  124. transformer.transform(src, domres);
  125. final Node generated = domres.getNode();
  126. Node sourceDocument;
  127. if (resultExists) {
  128. //Load existing translation file into memory (because we overwrite it later)
  129. src = new StreamSource(getTranslationFile().toURI().toURL().toExternalForm());
  130. domres = new DOMResult();
  131. transformer = tFactory.newTransformer();
  132. transformer.transform(src, domres);
  133. sourceDocument = domres.getNode();
  134. } else {
  135. //Simply use generated as source document
  136. sourceDocument = generated;
  137. }
  138. //Generate translation file (with potentially new translations)
  139. src = new DOMSource(sourceDocument);
  140. //The following triggers a bug in older Xalan versions
  141. //Result res = new StreamResult(getTranslationFile());
  142. OutputStream out = new java.io.FileOutputStream(getTranslationFile());
  143. out = new java.io.BufferedOutputStream(out);
  144. Result res = new StreamResult(out);
  145. try {
  146. StreamSource xslt2 = new StreamSource(
  147. getClass().getResourceAsStream(MERGETRANSLATION));
  148. if (xslt2.getInputStream() == null) {
  149. throw new FileNotFoundException(MERGETRANSLATION + " not found");
  150. }
  151. transformer = tFactory.newTransformer(xslt2);
  152. transformer.setURIResolver(new URIResolver() {
  153. public Source resolve(String href, String base) throws TransformerException {
  154. if ("my:dom".equals(href)) {
  155. return new DOMSource(generated);
  156. }
  157. return null;
  158. }
  159. });
  160. if (resultExists) {
  161. transformer.setParameter("generated-url", "my:dom");
  162. }
  163. transformer.transform(src, res);
  164. if (resultExists) {
  165. log("Translation file updated: " + getTranslationFile());
  166. } else {
  167. log("Translation file generated: " + getTranslationFile());
  168. }
  169. } finally {
  170. IOUtils.closeQuietly(out);
  171. }
  172. } catch (TransformerException te) {
  173. throw new IOException(te.getMessage());
  174. }
  175. }
  176. /**
  177. * Processes the file sets defined for the task.
  178. * @param collector the collector to use for collecting the event producers
  179. * @return the time of the latest modification of any of the files inspected
  180. * @throws IOException if an I/O error occurs
  181. * @throws EventConventionException if the EventProducer conventions are violated
  182. * @throws ClassNotFoundException if a required class cannot be found
  183. */
  184. protected long processFileSets(EventProducerCollector collector)
  185. throws IOException, EventConventionException, ClassNotFoundException {
  186. long lastModified = 0;
  187. Iterator iter = filesets.iterator();
  188. while (iter.hasNext()) {
  189. FileSet fs = (FileSet)iter.next();
  190. DirectoryScanner ds = fs.getDirectoryScanner(getProject());
  191. String[] srcFiles = ds.getIncludedFiles();
  192. File directory = fs.getDir(getProject());
  193. for (int i = 0, c = srcFiles.length; i < c; i++) {
  194. String filename = srcFiles[i];
  195. File src = new File(directory, filename);
  196. boolean eventProducerFound = collector.scanFile(src);
  197. if (eventProducerFound) {
  198. lastModified = Math.max(lastModified, src.lastModified());
  199. }
  200. }
  201. }
  202. return lastModified;
  203. }
  204. /**
  205. * Adds a file set.
  206. * @param set the file set
  207. */
  208. public void addFileset(FileSet set) {
  209. filesets.add(set);
  210. }
  211. /**
  212. * Sets the destination directory for the event models.
  213. *
  214. * @param destDir the destination directory
  215. */
  216. public void setDestDir(File destDir) {
  217. if (!destDir.isDirectory()) {
  218. throw new IllegalArgumentException("destDir must be a directory");
  219. }
  220. this.destDir = destDir;
  221. }
  222. /**
  223. * Sets the translation file for the event producer methods.
  224. * @param f the translation file
  225. */
  226. public void setTranslationFile(File f) {
  227. this.translationFile = f;
  228. }
  229. /**
  230. * Returns the translation file for the event producer methods.
  231. * @return the translation file
  232. */
  233. public File getTranslationFile() {
  234. return this.translationFile;
  235. }
  236. /**
  237. * Command-line interface for testing purposes.
  238. * @param args the command-line arguments
  239. */
  240. public static void main(String[] args) {
  241. try {
  242. Project project = new Project();
  243. EventProducerCollectorTask generator = new EventProducerCollectorTask();
  244. generator.setProject(project);
  245. project.setName("Test");
  246. FileSet fileset = new FileSet();
  247. fileset.setDir(new File("test/java"));
  248. FilenameSelector selector = new FilenameSelector();
  249. selector.setName("**/*.java");
  250. fileset.add(selector);
  251. generator.addFileset(fileset);
  252. File targetDir = new File("build/codegen1");
  253. targetDir.mkdirs();
  254. generator.setTranslationFile(new File("D:/out1.xml"));
  255. generator.execute();
  256. } catch (Exception e) {
  257. e.printStackTrace();
  258. }
  259. }
  260. }