Desktop tool for browsing account info from EVE-Online
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.

XSLTransformer.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*--
  2. $Id: XSLTransformer.java,v 1.5 2007/11/14 04:36:54 jhunter Exp $
  3. Copyright (C) 2001-2007 Jason Hunter & Brett McLaughlin.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions, and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions, and the disclaimer that follows
  12. these conditions in the documentation and/or other materials
  13. provided with the distribution.
  14. 3. The name "JDOM" must not be used to endorse or promote products
  15. derived from this software without prior written permission. For
  16. written permission, please contact <request_AT_jdom_DOT_org>.
  17. 4. Products derived from this software may not be called "JDOM", nor
  18. may "JDOM" appear in their name, without prior written permission
  19. from the JDOM Project Management <request_AT_jdom_DOT_org>.
  20. In addition, we request (but do not require) that you include in the
  21. end-user documentation provided with the redistribution and/or in the
  22. software itself an acknowledgement equivalent to the following:
  23. "This product includes software developed by the
  24. JDOM Project (http://www.jdom.org/)."
  25. Alternatively, the acknowledgment may be graphical using the logos
  26. available at http://www.jdom.org/images/logos.
  27. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  28. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  34. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. SUCH DAMAGE.
  39. This software consists of voluntary contributions made by many
  40. individuals on behalf of the JDOM Project and was originally
  41. created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
  42. Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
  43. on the JDOM Project, please see <http://www.jdom.org/>.
  44. */
  45. package org.jdom.transform;
  46. import java.util.*;
  47. import java.io.*;
  48. import javax.xml.transform.*;
  49. import javax.xml.transform.stream.StreamSource;
  50. import org.jdom.*;
  51. import org.xml.sax.EntityResolver;
  52. /**
  53. * A convenience class to handle simple transformations. The JAXP TrAX classes
  54. * have more bells and whistles and can be used with {@link JDOMSource} and
  55. * {@link JDOMResult} for advanced uses. This class handles the common case and
  56. * presents a simple interface. XSLTransformer is thread safe and may be
  57. * used from multiple threads.
  58. *
  59. * <pre><code>
  60. * XSLTransformer transformer = new XSLTransformer("file.xsl");
  61. *
  62. * Document x2 = transformer.transform(x); // x is a Document
  63. * Document y2 = transformer.transform(y); // y is a Document
  64. * </code></pre>
  65. *
  66. * JDOM relies on TrAX to perform the transformation.
  67. * The <code>javax.xml.transform.TransformerFactory</code> Java system property
  68. * determines which XSLT engine TrAX uses. Its value should be
  69. * the fully qualified name of the implementation of the abstract
  70. * <code>javax.xml.transform.TransformerFactory</code> class.
  71. * Values of this property for popular XSLT processors include:
  72. * </p>
  73. * <ul><li>Saxon 6.x: <code>com.icl.saxon.TransformerFactoryImpl</code></li>
  74. * <li>Saxon 7.x: <code>net.sf.saxon.TransformerFactoryImpl</code></li>
  75. * <li>Xalan: <code>org.apache.xalan.processor.TransformerFactoryImpl</code></li>
  76. * <li>jd.xslt: <code>jd.xml.xslt.trax.TransformerFactoryImpl</code></li>
  77. * <li>Oracle: <code>oracle.xml.jaxp.JXSAXTransformerFactory</code></li>
  78. * </ul>
  79. * <p>
  80. * This property can be set in all the usual ways a Java system property
  81. * can be set. TrAX picks from them in this order:</p>
  82. * <ol>
  83. * <li> Invoking <code>System.setProperty( "javax.xml.transform.TransformerFactory",
  84. * "<i><code>classname</code></i>")</code></li>
  85. * <li>The value specified at the command line using the
  86. * <tt>-Djavax.xml.transform.TransformerFactory=<i><code>classname</code></i></tt>
  87. * option to the <b>java</b> interpreter</li>
  88. * <li>The class named in the <code>lib/jaxp.properties</code> properties file
  89. * in the JRE directory, in a line like this one:
  90. * <pre>javax.xml.parsers.DocumentBuilderFactory=<i><code>classname</code></i></pre></li>
  91. * <li>The class named in the
  92. * <code>META-INF/services/javax.xml.transform.TransformerFactory</code> file
  93. * in the JAR archives available to the runtime</li>
  94. * <li>Finally, if all of the above options fail,
  95. * a default implementation is chosen. In Sun's JDK 1.4, this is
  96. * Xalan 2.2d10. </li>
  97. * </ol>
  98. * @version $Revision: 1.5 $, $Date: 2007/11/14 04:36:54 $
  99. * @author Jason Hunter
  100. * @author Elliotte Rusty Harold
  101. */
  102. public class XSLTransformer {
  103. private static final String CVS_ID =
  104. "@(#) $RCSfile: XSLTransformer.java,v $ $Revision: 1.5 $ $Date: 2007/11/14 04:36:54 $ $Name: jdom_1_1 $";
  105. private Templates templates;
  106. /**
  107. * The custom JDOM factory to use when building the transformation
  108. * result or <code>null</code> to use the default JDOM classes.
  109. */
  110. private JDOMFactory factory = null;
  111. // Internal constructor to support the other constructors
  112. private XSLTransformer(Source stylesheet) throws XSLTransformException {
  113. try {
  114. templates = TransformerFactory.newInstance()
  115. .newTemplates(stylesheet);
  116. }
  117. catch (TransformerException e) {
  118. throw new XSLTransformException("Could not construct XSLTransformer", e);
  119. }
  120. }
  121. /**
  122. * Creates a transformer for a given stylesheet system id.
  123. *
  124. * @param stylesheetSystemId source stylesheet as a Source object
  125. * @throws XSLTransformException if there's a problem in the TrAX back-end
  126. */
  127. public XSLTransformer(String stylesheetSystemId) throws XSLTransformException {
  128. this(new StreamSource(stylesheetSystemId));
  129. }
  130. /**
  131. * <p>
  132. * This will create a new <code>XSLTransformer</code> by
  133. * reading the stylesheet from the specified
  134. * <code>InputStream</code>.
  135. * </p>
  136. *
  137. * @param stylesheet <code>InputStream</code> from which the stylesheet is read.
  138. * @throws XSLTransformException when an IOException, format error, or
  139. * something else prevents the stylesheet from being compiled
  140. */
  141. public XSLTransformer(InputStream stylesheet) throws XSLTransformException {
  142. this(new StreamSource(stylesheet));
  143. }
  144. /**
  145. * <p>
  146. * This will create a new <code>XSLTransformer</code> by
  147. * reading the stylesheet from the specified
  148. * <code>Reader</code>.
  149. * </p>
  150. *
  151. * @param stylesheet <code>Reader</code> from which the stylesheet is read.
  152. * @throws XSLTransformException when an IOException, format error, or
  153. * something else prevents the stylesheet from being compiled
  154. */
  155. public XSLTransformer(Reader stylesheet) throws XSLTransformException {
  156. this(new StreamSource(stylesheet));
  157. }
  158. /**
  159. * <p>
  160. * This will create a new <code>XSLTransformer</code> by
  161. * reading the stylesheet from the specified
  162. * <code>File</code>.
  163. * </p>
  164. *
  165. * @param stylesheet <code>File</code> from which the stylesheet is read.
  166. * @throws XSLTransformException when an IOException, format error, or
  167. * something else prevents the stylesheet from being compiled
  168. */
  169. public XSLTransformer(File stylesheet) throws XSLTransformException {
  170. this(new StreamSource(stylesheet));
  171. }
  172. /**
  173. * <p>
  174. * This will create a new <code>XSLTransformer</code> by
  175. * reading the stylesheet from the specified
  176. * <code>Document</code>.
  177. * </p>
  178. *
  179. * @param stylesheet <code>Document</code> containing the stylesheet.
  180. * @throws XSLTransformException when the supplied <code>Document</code>
  181. * is not syntactically correct XSLT
  182. */
  183. public XSLTransformer(Document stylesheet) throws XSLTransformException {
  184. this(new JDOMSource(stylesheet));
  185. }
  186. /**
  187. * Transforms the given input nodes to a list of output nodes.
  188. *
  189. * @param inputNodes input nodes
  190. * @return transformed output nodes
  191. * @throws XSLTransformException if there's a problem in the transformation
  192. */
  193. public List transform(List inputNodes) throws XSLTransformException {
  194. JDOMSource source = new JDOMSource(inputNodes);
  195. JDOMResult result = new JDOMResult();
  196. result.setFactory(factory); // null ok
  197. try {
  198. templates.newTransformer().transform(source, result);
  199. return result.getResult();
  200. }
  201. catch (TransformerException e) {
  202. throw new XSLTransformException("Could not perform transformation", e);
  203. }
  204. }
  205. /**
  206. * Transforms the given document to an output document.
  207. *
  208. * @param inputDoc input document
  209. * @return transformed output document
  210. * @throws XSLTransformException if there's a problem in the transformation
  211. */
  212. public Document transform(Document inputDoc) throws XSLTransformException {
  213. return transform(inputDoc, null);
  214. }
  215. /**
  216. * Transforms the given document to an output document.
  217. *
  218. * @param inputDoc input document
  219. * @param resolver entity resolver for the input document
  220. * @return transformed output document
  221. * @throws XSLTransformException if there's a problem in the transformation
  222. */
  223. public Document transform(Document inputDoc, EntityResolver resolver) throws XSLTransformException {
  224. JDOMSource source = new JDOMSource(inputDoc, resolver);
  225. JDOMResult result = new JDOMResult();
  226. result.setFactory(factory); // null ok
  227. try {
  228. templates.newTransformer().transform(source, result);
  229. return result.getDocument();
  230. }
  231. catch (TransformerException e) {
  232. throw new XSLTransformException("Could not perform transformation", e);
  233. }
  234. }
  235. /**
  236. * Sets a custom JDOMFactory to use when building the
  237. * transformation result. Use a custom factory to build the tree
  238. * with your own subclasses of the JDOM classes.
  239. *
  240. * @param factory the custom <code>JDOMFactory</code> to use or
  241. * <code>null</code> to use the default JDOM
  242. * classes.
  243. *
  244. * @see #getFactory
  245. */
  246. public void setFactory(JDOMFactory factory) {
  247. this.factory = factory;
  248. }
  249. /**
  250. * Returns the custom JDOMFactory used to build the transformation
  251. * result.
  252. *
  253. * @return the custom <code>JDOMFactory</code> used to build the
  254. * transformation result or <code>null</code> if the
  255. * default JDOM classes are being used.
  256. *
  257. * @see #setFactory
  258. */
  259. public JDOMFactory getFactory() {
  260. return this.factory;
  261. }
  262. }