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.

Xslt.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.fop.tools.anttasks;
  55. import org.apache.tools.ant.Task;
  56. import java.net.*;
  57. import java.io.*;
  58. import java.util.*;
  59. import org.w3c.dom.*;
  60. import org.xml.sax.SAXException;
  61. /**
  62. * Task to call the XSLT processor Xalan (part of xml.apache.org), which converts xml files
  63. * from a source to an output using a stylesheet file
  64. *
  65. * <p>
  66. * This task can take the following arguments:
  67. * <ul>
  68. * <li>infile
  69. * <li>xsltfile
  70. * <li>outfile
  71. * <li>mergefile
  72. * <li>smart
  73. * <li>dependent
  74. * </ul>
  75. * <p>
  76. * Of these arguments, <b>infile, outfile</b> and <b>xsltfile</b> are required.
  77. * <p>smart defaults to 'no'. The other allowed value is 'yes'. If smart is set to 'yes'
  78. * <P>
  79. * xalan is only called if either the outfile is older than the infile or the stylesheet
  80. * or the outfile doesn't exist.
  81. * <P>
  82. * <p>dependent defaults to 'none'. Other possible values: a comma delimited list of file names
  83. * which date is checked against the output file. This way you can name files which, if
  84. * they have been modified, initiate a restart of the xslt process, like external entities etc.
  85. * <p>
  86. * The mergefile parameter causes this task to merge the contents of the specified file into the infile at the end. This is used for the font character mapping generation because the keys() xslt function doesn't work on an external document.
  87. *
  88. * @author Fotis Jannidis <a href="mailto:fotis@jannidis.de">fotis@jannidis.de</a>
  89. * @author Kelly A. Campbell <a href="mailto:camk@camk.net">camk@camk.net</a>
  90. */
  91. public class Xslt extends Task {
  92. private String infile, outfile, xsltfile, mergefile;
  93. private String smart = "no"; //defaults to do conversion everytime task is called
  94. private String dependent = "none"; //defaults to no dependencies
  95. private boolean startXslt = false;
  96. /** When true, we use the trax api's from xalan2, otherwise
  97. * just the xalan1 native interfaces
  98. */
  99. private boolean useTrax = false;
  100. /**
  101. * Sets the input file
  102. *
  103. */
  104. public void setInfile (String infile) {
  105. this.infile = infile;
  106. }
  107. public void setMergefile (String mergefile) {
  108. this.mergefile = mergefile;
  109. }
  110. /**
  111. * Sets the stylesheet file
  112. *
  113. */
  114. public void setXsltfile (String xsltfile) {
  115. this.xsltfile = xsltfile;
  116. }
  117. /**
  118. * Sets the output file
  119. *
  120. */
  121. public void setOutfile (String outfile) {
  122. this.outfile = outfile;
  123. }
  124. /**
  125. * Sets the value for smart
  126. *
  127. * @param option valid values:
  128. * <ul>
  129. * <li>yes: check whether output file is older than input or stylesheet
  130. * <li>no: (default) do conversion everytime task is called
  131. * </ul>
  132. */
  133. public void setSmart (String smart) {
  134. this.smart = smart;
  135. }
  136. /**
  137. * Sets the value for dependent
  138. *
  139. * @param option valid values:
  140. * <ul>
  141. * <li>none: (default)
  142. * <li>comma delimited list of files whose existence and date is checked
  143. * against the output file
  144. * </ul>
  145. */
  146. public void setDependent (String dependent) {
  147. this.dependent = dependent;
  148. }
  149. /**
  150. * Builds a document from the given file, merging the mergefile onto the end of the root node
  151. */
  152. private org.w3c.dom.Document buildDocument(String xmlFile)
  153. throws IOException, SAXException {
  154. try {
  155. javax.xml.parsers.DocumentBuilder docBuilder =
  156. javax.xml.parsers.DocumentBuilderFactory.newInstance().
  157. newDocumentBuilder();
  158. Document doc = docBuilder.parse(new FileInputStream(xmlFile));
  159. if (mergefile != null && !mergefile.equals("")) {
  160. File mergefileF = new File(mergefile);
  161. Document mergedoc =
  162. docBuilder.parse(new FileInputStream(mergefileF));
  163. Node mergenode =
  164. doc.importNode(mergedoc.getDocumentElement(), true);
  165. doc.getDocumentElement().appendChild(mergenode);
  166. }
  167. return doc;
  168. } catch (javax.xml.parsers.ParserConfigurationException e) {
  169. System.out.println("Task xslt - SAX ERROR:\n " +
  170. e.getMessage());
  171. }
  172. return null;
  173. }
  174. /**
  175. * Calls Xalan and does the transformation
  176. *
  177. */
  178. private void transform() {
  179. try {
  180. org.w3c.dom.Document source = buildDocument(infile);
  181. // Perform the transformation.
  182. System.out.println("============================");
  183. System.out.println("xslt \nin: " + infile + "\nstyle: " +
  184. xsltfile + "\nout: " + outfile);
  185. System.out.println("============================");
  186. org.apache.fop.tools.xslt.XSLTransform.transform(source,xsltfile,outfile);
  187. } catch (org.xml.sax.SAXException saxerror) {
  188. System.out.println("Task xslt - SAX ERROR:\n " + saxerror);
  189. }
  190. catch (MalformedURLException urlerror) {
  191. System.out.println("Task xslt - URL ERROR:\n " + urlerror);
  192. }
  193. catch (IOException ioerror) {
  194. System.out.println("Task xslt - IO ERROR:\n " + ioerror);
  195. }
  196. catch (Exception ex) {
  197. ex.printStackTrace();
  198. }
  199. } //end transform
  200. /**
  201. * Checks for existence of output file and compares
  202. * dates with input and stylesheet file
  203. */
  204. private boolean smartCheck (File outfileF,
  205. long outfileLastModified, File infileF, File xsltfileF) {
  206. if (outfileF.exists()) {
  207. //checks whether output file is older than input file or xslt stylesheet file
  208. if ((outfileLastModified < infileF.lastModified()) |
  209. (outfileLastModified < xsltfileF.lastModified())) {
  210. return true;
  211. }
  212. } else {
  213. //if output file does not exist, start xslt process
  214. return true;
  215. }
  216. return false;
  217. } //end smartCheck
  218. /**
  219. * Checks for existence and date of dependent files
  220. * This could be folded together with smartCheck by using
  221. * a general routine but it wouldn't be as fast as now
  222. */
  223. private boolean dependenciesCheck(File outfileF,
  224. long outfileLastModified) {
  225. String dependentFileName;
  226. File dependentFile;
  227. StringTokenizer tokens = new StringTokenizer(dependent, ",");
  228. while (tokens.hasMoreTokens()) {
  229. dependentFileName = (String) tokens.nextToken();
  230. dependentFile = new File (dependentFileName);
  231. //check: does dependent file exist
  232. if (dependentFile.exists()) {
  233. //check dates
  234. if ((outfileLastModified < dependentFile.lastModified())) {
  235. return true;
  236. }
  237. } else {
  238. System.err.println(
  239. "Task xslt - ERROR in attribute 'dependent':\n file " +
  240. dependentFileName + " does not exist.");
  241. }
  242. }
  243. return false;
  244. } //end dependenciesCheck
  245. /**
  246. * Main method, which is called by ant.
  247. * Checks for the value of smart and calls startTransform accordingly
  248. */
  249. public void execute () throws org.apache.tools.ant.BuildException {
  250. File outfileF = new File (outfile);
  251. File infileF = new File(infile);
  252. File xsltfileF = new File (xsltfile);
  253. long outfileLastModified = outfileF.lastModified();
  254. boolean startFileExist = true;
  255. //checks whether input and stylesheet exist.
  256. //this could be left to the parser, but this solution does make problems if smart is set to yes
  257. if (!infileF.exists()) {
  258. System.err.println(
  259. "Task xslt - ERROR:\n Input file " + infile +
  260. " does not exist!");
  261. startFileExist = false;
  262. } else if (!xsltfileF.exists()) {
  263. System.err.println(
  264. "Task xslt - ERROR:\n Stylesheet file " +
  265. xsltfile + " does not exist!");
  266. startFileExist = false;
  267. }
  268. //checks attribute 'smart'
  269. if (smart.equals("no")) {
  270. startXslt = true;
  271. //if attribute smart = 'yes'
  272. } else if (smart.equals("yes")) {
  273. startXslt = smartCheck (outfileF, outfileLastModified,
  274. infileF, xsltfileF);
  275. //checks dependent files against output file, makes only sense if smartCheck returns false
  276. if (!dependent.equals("none") & (startXslt == false)) {
  277. startXslt =
  278. dependenciesCheck(outfileF, outfileLastModified);
  279. }
  280. //returns error message, if smart has another value as 'yes' or 'no'
  281. } else {
  282. System.err.println("Task xslt - ERROR: Allowed values for the attribute smart are 'yes' or 'no'");
  283. }
  284. if (startFileExist & startXslt) {
  285. transform();
  286. }
  287. } //end execute
  288. //quick access for debugging
  289. //usage XSLT infile xsltfile outfile (smart is 'yes')
  290. /*
  291. public static void main (String args[]) {
  292. Xslt xslt = new Xslt();
  293. xslt.setInfile(args[0]);
  294. xslt.setXsltfile(args[1]);
  295. xslt.setOutfile(args[2]);
  296. xslt.setSmart("yes");
  297. xslt.setDependent("test1,test2");
  298. xslt.execute();
  299. } */
  300. }