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 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.tools.ant.taskdefs;
  55. import org.apache.tools.ant.Task;
  56. import java.net.*;
  57. import java.io.*;
  58. import org.apache.xalan.xslt.*;
  59. /**
  60. * Task to call the XSLT processor Xalan (part of xml.apache.org), which converts xml files
  61. * from a source to an output using a stylesheet file
  62. *
  63. * <p>
  64. * This task can take the following arguments:
  65. * <ul>
  66. * <li>infile
  67. * <li>xsltfile
  68. * <li>outfile
  69. * <li>smart
  70. * </ul>
  71. * <p>
  72. * Of these arguments, <b>infile, outfile</b> and <b>xsltfile</b> are required.
  73. * smart defaults to 'no'. The other allowed value is 'yes'. If smart is set to 'yes'
  74. * xalan is only called if either the outfile is older than the infile or the stylesheet
  75. * or the outfile doesn't exist.
  76. * <p>
  77. * @author Fotis Jannidis <a href="mailto:fotis@jannidis.de">fotis@jannidis.de</a>
  78. */
  79. public class Xslt extends Task {
  80. private String infile, outfile, xsltfile;
  81. private String smart = "no"; //defaults to do conversion everytime task is called
  82. /**
  83. * Calls Xalan and does the transformation
  84. *
  85. */
  86. private void transform(String xmlSourceURL, String xslURL, String outputURL)
  87. throws java.io.IOException,
  88. java.net.MalformedURLException,
  89. org.xml.sax.SAXException {
  90. // Use XSLTProcessor to instantiate an XSLTProcessor.
  91. org.apache.xalan.xslt.XSLTProcessor processor =
  92. org.apache.xalan.xslt.XSLTProcessorFactory.getProcessor();
  93. // Create the 3 objects the XSLTProcessor needs to perform the transformation.
  94. org.apache.xalan.xslt.XSLTInputSource xmlSource =
  95. new org.apache.xalan.xslt.XSLTInputSource (infile);
  96. org.apache.xalan.xslt.XSLTInputSource xslSheet =
  97. new org.apache.xalan.xslt.XSLTInputSource (xsltfile);
  98. org.apache.xalan.xslt.XSLTResultTarget xmlResult =
  99. new org.apache.xalan.xslt.XSLTResultTarget (outfile);
  100. // Perform the transformation.
  101. System.out.println("============================");
  102. System.out.println("xslt \nin: " + infile + "\nstyle: " + xsltfile + "\nout: " + outfile);
  103. System.out.println("============================");
  104. processor.process(xmlSource, xslSheet, xmlResult);
  105. }
  106. /**
  107. * Sets the input file
  108. *
  109. */
  110. public void setInfile (String infile) {
  111. this.infile = infile;
  112. }
  113. /**
  114. * Sets the stylesheet file
  115. *
  116. */
  117. public void setXsltfile (String xsltfile) {
  118. this.xsltfile = xsltfile;
  119. }
  120. /**
  121. * Sets the output file
  122. *
  123. */
  124. public void setOutfile (String outfile) {
  125. this.outfile = outfile;
  126. }
  127. /**
  128. * Sets the value for smart
  129. *
  130. * @param option valid values:
  131. * <ul>
  132. * <li>yes: check whether output file is older than input or stylesheet
  133. * <li>no: (default) do conversion everytime task is called
  134. * </ul>
  135. */
  136. public void setSmart (String smart) {
  137. this.smart = smart;
  138. }
  139. /**
  140. * Catches the errors transform() can throw and
  141. * returns meaningfull error messages
  142. */
  143. private void startTransform () {
  144. try {
  145. transform (infile,xsltfile,outfile);
  146. } catch (org.xml.sax.SAXException saxerror) {
  147. System.out.println("SAX Error: " + saxerror);
  148. } catch (MalformedURLException urlerror) {
  149. System.out.println("URL Error: " + urlerror);
  150. } catch (IOException ioerror) {
  151. System.out.println("IO Error: " + ioerror);
  152. }
  153. }
  154. /**
  155. * Main method, which is called by ant.
  156. * Checks for the value of smart and calls startTransform accordingly
  157. */
  158. public void execute () throws org.apache.tools.ant.BuildException {
  159. if (smart.equals("no")) {
  160. startTransform();
  161. } else if (smart.equals("yes")) {
  162. File outfileF = new File (outfile);
  163. //checks for existence of output file
  164. if (outfileF.exists()) {
  165. //checks whether output file is older than input file or xslt stylesheet file
  166. if ((outfileF.lastModified() < new File(infile).lastModified()) | (outfileF.lastModified() < new
  167. File(xsltfile).lastModified())) {
  168. startTransform();
  169. }
  170. } else {
  171. startTransform();
  172. }
  173. //returns error message, if smart has another value as 'yes' or 'no'
  174. } else {
  175. System.err.println("Error: Allowed values for the attribute smart are 'yes' or 'no'");
  176. }
  177. }
  178. //quick access for debugging
  179. //usage XSLT infile xsltfile outfile (smart is 'yes')
  180. /*
  181. public static void main (String args[]) {
  182. Xslt xslt = new Xslt();
  183. xslt.setInfile(args[0]);
  184. xslt.setXsltfile(args[1]);
  185. xslt.setOutfile(args[2]);
  186. xslt.setSmart("yes");
  187. xslt.execute();
  188. }
  189. */
  190. }