TransformerFactoryImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* TransformerFactoryImpl.java --
  2. Copyright (C) 2004,2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.transform;
  32. import java.io.FileInputStream;
  33. import java.io.FileOutputStream;
  34. import java.io.InputStream;
  35. import java.io.IOException;
  36. import java.io.OutputStream;
  37. import java.net.URL;
  38. import java.util.Iterator;
  39. import java.util.LinkedHashMap;
  40. import java.util.LinkedList;
  41. import java.util.Map;
  42. import java.util.Properties;
  43. import javax.xml.transform.ErrorListener;
  44. import javax.xml.transform.Source;
  45. import javax.xml.transform.Templates;
  46. import javax.xml.transform.Transformer;
  47. import javax.xml.transform.TransformerConfigurationException;
  48. import javax.xml.transform.TransformerException;
  49. import javax.xml.transform.TransformerFactory;
  50. import javax.xml.transform.URIResolver;
  51. import javax.xml.transform.dom.DOMResult;
  52. import javax.xml.transform.dom.DOMSource;
  53. import javax.xml.transform.sax.SAXResult;
  54. import javax.xml.transform.sax.SAXSource;
  55. import javax.xml.transform.sax.SAXTransformerFactory;
  56. import javax.xml.transform.sax.TemplatesHandler;
  57. import javax.xml.transform.sax.TransformerHandler;
  58. import javax.xml.transform.stream.StreamResult;
  59. import javax.xml.transform.stream.StreamSource;
  60. import javax.xml.xpath.XPathFactory;
  61. import org.w3c.dom.Document;
  62. import org.w3c.dom.Node;
  63. import org.xml.sax.XMLFilter;
  64. import gnu.xml.dom.DomDocument;
  65. /**
  66. * GNU transformer factory implementation.
  67. *
  68. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  69. */
  70. public class TransformerFactoryImpl
  71. extends SAXTransformerFactory
  72. {
  73. final XPathFactory xpathFactory;
  74. final XSLURIResolver resolver;
  75. ErrorListener userListener;
  76. URIResolver userResolver;
  77. public TransformerFactoryImpl()
  78. {
  79. xpathFactory = new gnu.xml.xpath.XPathFactoryImpl();
  80. resolver = new XSLURIResolver();
  81. }
  82. public Transformer newTransformer(Source source)
  83. throws TransformerConfigurationException
  84. {
  85. Stylesheet stylesheet = newStylesheet(source, 0, null);
  86. Properties outputProperties =
  87. new TransformerOutputProperties(stylesheet);
  88. TransformerImpl transformer =
  89. new TransformerImpl(this, stylesheet, outputProperties);
  90. stylesheet.transformer = transformer;
  91. return transformer;
  92. }
  93. public Transformer newTransformer()
  94. throws TransformerConfigurationException
  95. {
  96. return new TransformerImpl(this, null, new Properties());
  97. }
  98. public Templates newTemplates(Source source)
  99. throws TransformerConfigurationException
  100. {
  101. Stylesheet stylesheet = newStylesheet(source, 0, null);
  102. return new TemplatesImpl(this, stylesheet);
  103. }
  104. Stylesheet newStylesheet(Source source, int precedence, Stylesheet parent)
  105. throws TransformerConfigurationException
  106. {
  107. Document doc = null;
  108. String systemId = null;
  109. if (source != null)
  110. {
  111. try
  112. {
  113. DOMSource ds;
  114. synchronized (resolver)
  115. {
  116. resolver.setUserResolver(userResolver);
  117. resolver.setUserListener(userListener);
  118. ds = resolver.resolveDOM(source, null, null);
  119. }
  120. Node node = ds.getNode();
  121. if (node == null)
  122. {
  123. throw new TransformerConfigurationException("no source document");
  124. }
  125. doc = (node instanceof Document) ? (Document) node :
  126. node.getOwnerDocument();
  127. systemId = ds.getSystemId();
  128. }
  129. catch (TransformerException e)
  130. {
  131. throw new TransformerConfigurationException(e);
  132. }
  133. }
  134. return new Stylesheet(this, parent, doc, systemId, precedence);
  135. }
  136. public Source getAssociatedStylesheet(Source source,
  137. String media,
  138. String title,
  139. String charset)
  140. throws TransformerConfigurationException
  141. {
  142. try
  143. {
  144. DOMSource ds;
  145. synchronized (resolver)
  146. {
  147. resolver.setUserResolver(userResolver);
  148. resolver.setUserListener(userListener);
  149. ds = resolver.resolveDOM(source, null, null);
  150. }
  151. Node node = ds.getNode();
  152. if (node == null)
  153. {
  154. throw new TransformerConfigurationException("no source document");
  155. }
  156. Document doc = (node instanceof Document) ? (Document) node :
  157. node.getOwnerDocument();
  158. LinkedList matches = new LinkedList();
  159. for (node = doc.getFirstChild();
  160. node != null;
  161. node = node.getNextSibling())
  162. {
  163. if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE &&
  164. "xml-stylesheet".equals(node.getNodeName()))
  165. {
  166. Map params = parseParameters(node.getNodeValue());
  167. if (media != null && !media.equals(params.get("media")))
  168. {
  169. continue;
  170. }
  171. if (title != null && !title.equals(params.get("title")))
  172. {
  173. continue;
  174. }
  175. if (charset != null && !charset.equals(params.get("charset")))
  176. {
  177. continue;
  178. }
  179. String href = (String) params.get("href");
  180. URL url = resolver.resolveURL(null, node.getBaseURI(), href);
  181. matches.add(url);
  182. }
  183. }
  184. switch (matches.size())
  185. {
  186. case 0:
  187. return null;
  188. case 1:
  189. return new StreamSource(((URL) matches.getFirst()).toString());
  190. default:
  191. // Create a source representing a stylesheet with a list of
  192. // imports
  193. DomDocument ssDoc = new DomDocument();
  194. ssDoc.setBuilding(true);
  195. // Create document element
  196. Node root =
  197. ssDoc.createElementNS(Stylesheet.XSL_NS, "stylesheet");
  198. Node version =
  199. ssDoc.createAttributeNS(null, "version");
  200. version.setNodeValue("1.0");
  201. root.getAttributes().setNamedItemNS(version);
  202. ssDoc.appendChild(root);
  203. // Create xsl:import for each URL
  204. for (Iterator i = matches.iterator(); i.hasNext(); )
  205. {
  206. URL url = (URL) i.next();
  207. Node imp =
  208. ssDoc.createElementNS(Stylesheet.XSL_NS, "import");
  209. Node href =
  210. ssDoc.createAttributeNS(null, "href");
  211. href.setNodeValue(url.toString());
  212. imp.getAttributes().setNamedItemNS(href);
  213. root.appendChild(imp);
  214. }
  215. ssDoc.setBuilding(false);
  216. return new DOMSource(ssDoc);
  217. }
  218. }
  219. catch (IOException e)
  220. {
  221. throw new TransformerConfigurationException(e);
  222. }
  223. catch (TransformerException e)
  224. {
  225. throw new TransformerConfigurationException(e);
  226. }
  227. }
  228. Map parseParameters(String data)
  229. {
  230. Map ret = new LinkedHashMap();
  231. int len = data.length();
  232. String key = null;
  233. int start = 0;
  234. char quoteChar = '\u0000';
  235. for (int i = 0; i < len; i++)
  236. {
  237. char c = data.charAt(i);
  238. if (quoteChar == '\u0000' && c == ' ')
  239. {
  240. if (key == null && start < i)
  241. {
  242. key = data.substring(start, i);
  243. }
  244. else
  245. {
  246. String val = unquote(data.substring(start, i).trim());
  247. ret.put(key, val);
  248. key = null;
  249. }
  250. start = i + 1;
  251. }
  252. else if (c == '"')
  253. {
  254. quoteChar = (quoteChar == c) ? '\u0000' : c;
  255. }
  256. else if (c == '\'')
  257. {
  258. quoteChar = (quoteChar == c) ? '\u0000' : c;
  259. }
  260. }
  261. if (start < len && key != null)
  262. {
  263. String val = unquote(data.substring(start, len).trim());
  264. ret.put(key, val);
  265. }
  266. return ret;
  267. }
  268. String unquote(String text)
  269. {
  270. int end = text.length() - 1;
  271. if (text.charAt(0) == '\'' && text.charAt(end) == '\'')
  272. {
  273. return text.substring(1, end);
  274. }
  275. if (text.charAt(0) == '"' && text.charAt(end) == '"')
  276. {
  277. return text.substring(1, end);
  278. }
  279. return text;
  280. }
  281. public void setURIResolver(URIResolver resolver)
  282. {
  283. userResolver = resolver;
  284. }
  285. public URIResolver getURIResolver()
  286. {
  287. return userResolver;
  288. }
  289. public void setFeature(String name, boolean value)
  290. throws TransformerConfigurationException
  291. {
  292. throw new TransformerConfigurationException("not supported");
  293. }
  294. public boolean getFeature(String name)
  295. {
  296. if (SAXSource.FEATURE.equals(name) ||
  297. SAXResult.FEATURE.equals(name) ||
  298. StreamSource.FEATURE.equals(name) ||
  299. StreamResult.FEATURE.equals(name) ||
  300. DOMSource.FEATURE.equals(name) ||
  301. DOMResult.FEATURE.equals(name) ||
  302. SAXTransformerFactory.FEATURE.equals(name))
  303. {
  304. return true;
  305. }
  306. return false;
  307. }
  308. public void setAttribute(String name, Object value)
  309. throws IllegalArgumentException
  310. {
  311. throw new IllegalArgumentException("not supported");
  312. }
  313. public Object getAttribute(String name)
  314. throws IllegalArgumentException
  315. {
  316. throw new IllegalArgumentException("not supported");
  317. }
  318. public void setErrorListener(ErrorListener listener)
  319. throws IllegalArgumentException
  320. {
  321. userListener = listener;
  322. }
  323. public ErrorListener getErrorListener()
  324. {
  325. return userListener;
  326. }
  327. // -- SAXTransformerFactory --
  328. public TemplatesHandler newTemplatesHandler()
  329. throws TransformerConfigurationException
  330. {
  331. return new SAXTemplatesHandler(this);
  332. }
  333. public TransformerHandler newTransformerHandler()
  334. throws TransformerConfigurationException
  335. {
  336. Transformer transformer = newTransformer();
  337. return new SAXTransformerHandler(this, transformer);
  338. }
  339. public TransformerHandler newTransformerHandler(Source source)
  340. throws TransformerConfigurationException
  341. {
  342. Transformer transformer = newTransformer(source);
  343. return new SAXTransformerHandler(this, transformer);
  344. }
  345. public TransformerHandler newTransformerHandler(Templates templates)
  346. throws TransformerConfigurationException
  347. {
  348. Transformer transformer = templates.newTransformer();
  349. return new SAXTransformerHandler(this, transformer);
  350. }
  351. public XMLFilter newXMLFilter(Source source)
  352. throws TransformerConfigurationException
  353. {
  354. throw new UnsupportedOperationException();
  355. }
  356. public XMLFilter newXMLFilter(Templates templates)
  357. throws TransformerConfigurationException
  358. {
  359. throw new UnsupportedOperationException();
  360. }
  361. // -- SAXTransformerFactory end --
  362. /**
  363. * Syntax: TransformerFactoryImpl [<stylesheet> [<input> [<output>]]]
  364. */
  365. public static void main(String[] args)
  366. throws Exception
  367. {
  368. InputStream stylesheet = null, in = null;
  369. OutputStream out = null;
  370. try
  371. {
  372. if (args.length > 0)
  373. {
  374. stylesheet = new FileInputStream(args[0]);
  375. if (args.length > 1)
  376. {
  377. in = new FileInputStream(args[1]);
  378. if (args.length > 2)
  379. out = new FileOutputStream(args[2]);
  380. }
  381. }
  382. if (in == null)
  383. in = System.in;
  384. if (out == null)
  385. out = System.out;
  386. TransformerFactory f = new TransformerFactoryImpl();
  387. Transformer t = (stylesheet != null) ?
  388. f.newTransformer(new StreamSource(stylesheet)) :
  389. f.newTransformer();
  390. t.transform(new StreamSource(in), new StreamResult(out));
  391. }
  392. finally
  393. {
  394. if (stylesheet != null)
  395. stylesheet.close();
  396. if (in != null && in instanceof FileInputStream)
  397. in.close();
  398. if (out != null && out instanceof FileOutputStream)
  399. out.close();
  400. }
  401. }
  402. }