XSLURIResolver.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* XSLURIResolver.java --
  2. Copyright (C) 2004 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.File;
  33. import java.io.InputStream;
  34. import java.io.IOException;
  35. import java.io.Reader;
  36. import java.net.MalformedURLException;
  37. import java.net.URL;
  38. import java.net.URLConnection;
  39. import java.util.HashMap;
  40. import java.util.Map;
  41. import javax.xml.parsers.DocumentBuilder;
  42. import javax.xml.parsers.DocumentBuilderFactory;
  43. import javax.xml.transform.ErrorListener;
  44. import javax.xml.transform.Source;
  45. import javax.xml.transform.TransformerException;
  46. import javax.xml.transform.URIResolver;
  47. import javax.xml.transform.dom.DOMSource;
  48. import javax.xml.transform.sax.SAXSource;
  49. import javax.xml.transform.stream.StreamSource;
  50. import org.w3c.dom.Document;
  51. import org.w3c.dom.Node;
  52. import org.xml.sax.InputSource;
  53. import org.xml.sax.SAXException;
  54. import org.xml.sax.XMLReader;
  55. import gnu.xml.dom.DomDocument;
  56. import gnu.xml.dom.ls.SAXEventSink;
  57. import gnu.xml.dom.ls.ReaderInputStream;
  58. /**
  59. * URI resolver for XSLT.
  60. * This resolver parses external entities into DOMSources. It
  61. * maintains a cache of URIs to DOMSources to avoid expensive re-parsing.
  62. *
  63. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  64. */
  65. class XSLURIResolver
  66. implements URIResolver
  67. {
  68. final Map<String,Long> lastModifiedCache = new HashMap<String,Long>();
  69. final Map<String,Node> nodeCache = new HashMap<String,Node>();
  70. DocumentBuilder builder;
  71. URIResolver userResolver;
  72. ErrorListener userListener;
  73. void setUserResolver(URIResolver userResolver)
  74. {
  75. this.userResolver = userResolver;
  76. }
  77. void setUserListener(ErrorListener userListener)
  78. {
  79. this.userListener = userListener;
  80. }
  81. /**
  82. * Clear the cache.
  83. */
  84. void flush()
  85. {
  86. lastModifiedCache.clear();
  87. nodeCache.clear();
  88. }
  89. public Source resolve(String href, String base)
  90. throws TransformerException
  91. {
  92. Source source = null;
  93. if (userResolver != null)
  94. {
  95. source = userResolver.resolve(base, href);
  96. }
  97. return resolveDOM(source, href, base);
  98. }
  99. DOMSource resolveDOM(Source source, String base, String href)
  100. throws TransformerException
  101. {
  102. if (source != null && source instanceof DOMSource)
  103. {
  104. return (DOMSource) source;
  105. }
  106. String systemId = (source == null) ? null : source.getSystemId();
  107. long lastModified = 0L, lastLastModified = 0L;
  108. try
  109. {
  110. Node node = null;
  111. InputStream in = null;
  112. if (source != null && source instanceof StreamSource)
  113. {
  114. StreamSource ss = (StreamSource) source;
  115. in = ss.getInputStream();
  116. if (in == null)
  117. {
  118. Reader reader = ss.getReader();
  119. if (reader != null)
  120. {
  121. in = new ReaderInputStream(reader);
  122. }
  123. }
  124. }
  125. else if (source != null && source instanceof SAXSource)
  126. {
  127. SAXSource ss = (SAXSource) source;
  128. InputSource input = ss.getInputSource();
  129. if (input != null)
  130. {
  131. if (systemId == null)
  132. systemId = input.getSystemId();
  133. XMLReader reader = ss.getXMLReader();
  134. if (reader != null)
  135. return parse(input, reader);
  136. }
  137. }
  138. if (in == null)
  139. {
  140. URL url = resolveURL(systemId, base, href);
  141. if (url != null)
  142. {
  143. systemId = url.toString();
  144. node = nodeCache.get(systemId);
  145. // Is the resource up to date?
  146. URLConnection conn = url.openConnection();
  147. Long llm = lastModifiedCache.get(systemId);
  148. if (llm != null)
  149. {
  150. lastLastModified = llm.longValue();
  151. conn.setIfModifiedSince(lastLastModified);
  152. }
  153. conn.connect();
  154. lastModified = conn.getLastModified();
  155. if (node != null &&
  156. lastModified > 0L &&
  157. lastModified <= lastLastModified)
  158. {
  159. // Resource unchanged
  160. return new DOMSource(node, systemId);
  161. }
  162. else
  163. {
  164. // Resource new or modified
  165. in = conn.getInputStream();
  166. nodeCache.put(systemId, node);
  167. lastModifiedCache.put(systemId, new Long(lastModified));
  168. }
  169. }
  170. else
  171. {
  172. throw new TransformerException("can't resolve URL: " +
  173. systemId);
  174. }
  175. }
  176. InputSource input = new InputSource(in);
  177. input.setSystemId(systemId);
  178. DocumentBuilder builder = getDocumentBuilder();
  179. node = builder.parse(input);
  180. return new DOMSource(node, systemId);
  181. }
  182. catch (IOException e)
  183. {
  184. throw new TransformerException(e);
  185. }
  186. catch (SAXException e)
  187. {
  188. throw new TransformerException(e);
  189. }
  190. }
  191. URL resolveURL(String systemId, String base, String href)
  192. throws IOException
  193. {
  194. URL url = null;
  195. try
  196. {
  197. if (systemId != null)
  198. {
  199. try
  200. {
  201. url = new URL(systemId);
  202. }
  203. catch (MalformedURLException e)
  204. {
  205. // Try building from base + href
  206. }
  207. }
  208. if (url == null)
  209. {
  210. if (base != null)
  211. {
  212. URL baseURL = new URL(base);
  213. url = new URL(baseURL, href);
  214. }
  215. else if (href != null)
  216. {
  217. url = new URL(href);
  218. }
  219. else
  220. {
  221. // See below
  222. throw new MalformedURLException(systemId);
  223. }
  224. }
  225. return url;
  226. }
  227. catch (MalformedURLException e)
  228. {
  229. // Fall back to local filesystem
  230. File file = null;
  231. if (href == null)
  232. {
  233. href = systemId;
  234. }
  235. if (base != null)
  236. {
  237. int lsi = base.lastIndexOf(File.separatorChar);
  238. if (lsi != -1 && lsi < base.length() - 1)
  239. {
  240. base = base.substring(0, lsi);
  241. }
  242. File baseFile = new File(base);
  243. file = new File(baseFile, href);
  244. }
  245. else if (href != null)
  246. {
  247. file = new File(href);
  248. }
  249. return (file == null) ? null : file.toURL();
  250. }
  251. }
  252. DocumentBuilder getDocumentBuilder()
  253. throws TransformerException
  254. {
  255. try
  256. {
  257. if (builder == null)
  258. {
  259. DocumentBuilderFactory factory =
  260. DocumentBuilderFactory.newInstance();
  261. factory.setNamespaceAware(true);
  262. factory.setExpandEntityReferences(true);
  263. builder = factory.newDocumentBuilder();
  264. }
  265. if (userResolver != null)
  266. {
  267. builder.setEntityResolver(new URIResolverEntityResolver(userResolver));
  268. }
  269. if (userListener != null)
  270. {
  271. builder.setErrorHandler(new ErrorListenerErrorHandler(userListener));
  272. }
  273. return builder;
  274. }
  275. catch (Exception e)
  276. {
  277. throw new TransformerException(e);
  278. }
  279. }
  280. DOMSource parse(InputSource source, XMLReader reader)
  281. throws SAXException, IOException
  282. {
  283. SAXEventSink eventSink = new SAXEventSink();
  284. eventSink.setReader(reader);
  285. eventSink.setNamespaceAware(true);
  286. reader.setContentHandler(eventSink);
  287. reader.setDTDHandler(eventSink);
  288. reader.setProperty("http://xml.org/sax/properties/lexical-handler",
  289. eventSink);
  290. reader.setProperty("http://xml.org/sax/properties/declaration-handler",
  291. eventSink);
  292. // XXX entityResolver
  293. // XXX errorHandler
  294. reader.parse(source);
  295. Document doc = eventSink.getDocument();
  296. String systemId = source.getSystemId();
  297. if (systemId != null && doc instanceof DomDocument)
  298. ((DomDocument) doc).setDocumentURI(systemId);
  299. return new DOMSource(doc, systemId);
  300. }
  301. }