1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: SourceTreeManager.java,v 1.34 2004/02/17 04:30:02 minchau Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal;
  20. import java.io.IOException;
  21. import java.util.Vector;
  22. import javax.xml.transform.Source;
  23. import javax.xml.transform.SourceLocator;
  24. import javax.xml.transform.TransformerException;
  25. import javax.xml.transform.URIResolver;
  26. import javax.xml.transform.sax.SAXSource;
  27. import javax.xml.transform.stream.StreamSource;
  28. import com.sun.org.apache.xml.internal.dtm.DTM;
  29. import com.sun.org.apache.xml.internal.utils.SystemIDResolver;
  30. import org.xml.sax.XMLReader;
  31. import org.xml.sax.helpers.XMLReaderFactory;
  32. /**
  33. * This class bottlenecks all management of source trees. The methods
  34. * in this class should allow easy garbage collection of source
  35. * trees (not yet!), and should centralize parsing for those source trees.
  36. */
  37. public class SourceTreeManager
  38. {
  39. /** Vector of SourceTree objects that this manager manages. */
  40. private Vector m_sourceTree = new Vector();
  41. /**
  42. * Reset the list of SourceTree objects that this manager manages.
  43. *
  44. */
  45. public void reset()
  46. {
  47. m_sourceTree = new Vector();
  48. }
  49. /** The TrAX URI resolver used to obtain source trees. */
  50. URIResolver m_uriResolver;
  51. /**
  52. * Set an object that will be used to resolve URIs used in
  53. * document(), etc.
  54. * @param resolver An object that implements the URIResolver interface,
  55. * or null.
  56. */
  57. public void setURIResolver(URIResolver resolver)
  58. {
  59. m_uriResolver = resolver;
  60. }
  61. /**
  62. * Get the object that will be used to resolve URIs used in
  63. * document(), etc.
  64. * @return An object that implements the URIResolver interface,
  65. * or null.
  66. */
  67. public URIResolver getURIResolver()
  68. {
  69. return m_uriResolver;
  70. }
  71. /**
  72. * Given a document, find the URL associated with that document.
  73. * @param owner Document that was previously processed by this liaison.
  74. *
  75. * @return The base URI of the owner argument.
  76. */
  77. public String findURIFromDoc(int owner)
  78. {
  79. int n = m_sourceTree.size();
  80. for (int i = 0; i < n; i++)
  81. {
  82. SourceTree sTree = (SourceTree) m_sourceTree.elementAt(i);
  83. if (owner == sTree.m_root)
  84. return sTree.m_url;
  85. }
  86. return null;
  87. }
  88. /**
  89. * This will be called by the processor when it encounters
  90. * an xsl:include, xsl:import, or document() function.
  91. *
  92. * @param base The base URI that should be used.
  93. * @param urlString Value from an xsl:import or xsl:include's href attribute,
  94. * or a URI specified in the document() function.
  95. *
  96. * @return a Source that can be used to process the resource.
  97. *
  98. * @throws IOException
  99. * @throws TransformerException
  100. */
  101. public Source resolveURI(
  102. String base, String urlString, SourceLocator locator)
  103. throws TransformerException, IOException
  104. {
  105. Source source = null;
  106. if (null != m_uriResolver)
  107. {
  108. source = m_uriResolver.resolve(urlString, base);
  109. }
  110. if (null == source)
  111. {
  112. String uri = SystemIDResolver.getAbsoluteURI(urlString, base);
  113. source = new StreamSource(uri);
  114. }
  115. return source;
  116. }
  117. /** JJK: Support <?xalan:doc_cache_off?> kluge in ElemForEach.
  118. * TODO: This function is highly dangerous. Cache management must be improved.
  119. *
  120. * @param n The node to remove.
  121. */
  122. public void removeDocumentFromCache(int n)
  123. {
  124. if(DTM.NULL ==n)
  125. return;
  126. for(int i=m_sourceTree.size()-1;i>=0;--i)
  127. {
  128. SourceTree st=(SourceTree)m_sourceTree.elementAt(i);
  129. if(st!=null && st.m_root==n)
  130. {
  131. m_sourceTree.removeElementAt(i);
  132. return;
  133. }
  134. }
  135. }
  136. /**
  137. * Put the source tree root node in the document cache.
  138. * TODO: This function needs to be a LOT more sophisticated.
  139. *
  140. * @param n The node to cache.
  141. * @param source The Source object to cache.
  142. */
  143. public void putDocumentInCache(int n, Source source)
  144. {
  145. int cachedNode = getNode(source);
  146. if (DTM.NULL != cachedNode)
  147. {
  148. if (!(cachedNode == n))
  149. throw new RuntimeException(
  150. "Programmer's Error! "
  151. + "putDocumentInCache found reparse of doc: "
  152. + source.getSystemId());
  153. return;
  154. }
  155. if (null != source.getSystemId())
  156. {
  157. m_sourceTree.addElement(new SourceTree(n, source.getSystemId()));
  158. }
  159. }
  160. /**
  161. * Given a Source object, find the node associated with it.
  162. *
  163. * @param source The Source object to act as the key.
  164. *
  165. * @return The node that is associated with the Source, or null if not found.
  166. */
  167. public int getNode(Source source)
  168. {
  169. // if (source instanceof DOMSource)
  170. // return ((DOMSource) source).getNode();
  171. // TODO: Not sure if the BaseID is really the same thing as the ID.
  172. String url = source.getSystemId();
  173. if (null == url)
  174. return DTM.NULL;
  175. int n = m_sourceTree.size();
  176. // System.out.println("getNode: "+n);
  177. for (int i = 0; i < n; i++)
  178. {
  179. SourceTree sTree = (SourceTree) m_sourceTree.elementAt(i);
  180. // System.out.println("getNode - url: "+url);
  181. // System.out.println("getNode - sTree.m_url: "+sTree.m_url);
  182. if (url.equals(sTree.m_url))
  183. return sTree.m_root;
  184. }
  185. // System.out.println("getNode - returning: "+node);
  186. return DTM.NULL;
  187. }
  188. /**
  189. * Get the source tree from the a base URL and a URL string.
  190. *
  191. * @param base The base URI to use if the urlString is relative.
  192. * @param urlString An absolute or relative URL string.
  193. * @param locator The location of the caller, for diagnostic purposes.
  194. *
  195. * @return should be a non-null reference to the node identified by the
  196. * base and urlString.
  197. *
  198. * @throws TransformerException If the URL can not resolve to a node.
  199. */
  200. public int getSourceTree(
  201. String base, String urlString, SourceLocator locator, XPathContext xctxt)
  202. throws TransformerException
  203. {
  204. // System.out.println("getSourceTree");
  205. try
  206. {
  207. Source source = this.resolveURI(base, urlString, locator);
  208. // System.out.println("getSourceTree - base: "+base+", urlString: "+urlString+", source: "+source.getSystemId());
  209. return getSourceTree(source, locator, xctxt);
  210. }
  211. catch (IOException ioe)
  212. {
  213. throw new TransformerException(ioe.getMessage(), locator, ioe);
  214. }
  215. /* catch (TransformerException te)
  216. {
  217. throw new TransformerException(te.getMessage(), locator, te);
  218. }*/
  219. }
  220. /**
  221. * Get the source tree from the input source.
  222. *
  223. * @param source The Source object that should identify the desired node.
  224. * @param locator The location of the caller, for diagnostic purposes.
  225. *
  226. * @return non-null reference to a node.
  227. *
  228. * @throws TransformerException if the Source argument can't be resolved to
  229. * a node.
  230. */
  231. public int getSourceTree(Source source, SourceLocator locator, XPathContext xctxt)
  232. throws TransformerException
  233. {
  234. int n = getNode(source);
  235. if (DTM.NULL != n)
  236. return n;
  237. n = parseToNode(source, locator, xctxt);
  238. if (DTM.NULL != n)
  239. putDocumentInCache(n, source);
  240. return n;
  241. }
  242. /**
  243. * Try to create a DOM source tree from the input source.
  244. *
  245. * @param source The Source object that identifies the source node.
  246. * @param locator The location of the caller, for diagnostic purposes.
  247. *
  248. * @return non-null reference to node identified by the source argument.
  249. *
  250. * @throws TransformerException if the source argument can not be resolved
  251. * to a source node.
  252. */
  253. public int parseToNode(Source source, SourceLocator locator, XPathContext xctxt)
  254. throws TransformerException
  255. {
  256. try
  257. {
  258. Object xowner = xctxt.getOwnerObject();
  259. DTM dtm;
  260. if(null != xowner && xowner instanceof com.sun.org.apache.xml.internal.dtm.DTMWSFilter)
  261. {
  262. dtm = xctxt.getDTM(source, false,
  263. (com.sun.org.apache.xml.internal.dtm.DTMWSFilter)xowner, false, true);
  264. }
  265. else
  266. {
  267. dtm = xctxt.getDTM(source, false, null, false, true);
  268. }
  269. return dtm.getDocument();
  270. }
  271. catch (Exception e)
  272. {
  273. //e.printStackTrace();
  274. throw new TransformerException(e.getMessage(), locator, e);
  275. }
  276. }
  277. /**
  278. * This method returns the SAX2 parser to use with the InputSource
  279. * obtained from this URI.
  280. * It may return null if any SAX2-conformant XML parser can be used,
  281. * or if getInputSource() will also return null. The parser must
  282. * be free for use (i.e.
  283. * not currently in use for another parse().
  284. *
  285. * @param inputSource The value returned from the URIResolver.
  286. * @returns a SAX2 XMLReader to use to resolve the inputSource argument.
  287. * @param locator The location of the original caller, for diagnostic purposes.
  288. *
  289. * @return non-null XMLReader reference ready to parse.
  290. *
  291. * @throws TransformerException if the reader can not be created.
  292. */
  293. public static XMLReader getXMLReader(Source inputSource, SourceLocator locator)
  294. throws TransformerException
  295. {
  296. try
  297. {
  298. XMLReader reader = (inputSource instanceof SAXSource)
  299. ? ((SAXSource) inputSource).getXMLReader() : null;
  300. if (null == reader)
  301. {
  302. try {
  303. javax.xml.parsers.SAXParserFactory factory=
  304. javax.xml.parsers.SAXParserFactory.newInstance();
  305. factory.setNamespaceAware( true );
  306. javax.xml.parsers.SAXParser jaxpParser=
  307. factory.newSAXParser();
  308. reader=jaxpParser.getXMLReader();
  309. } catch( javax.xml.parsers.ParserConfigurationException ex ) {
  310. throw new org.xml.sax.SAXException( ex );
  311. } catch( javax.xml.parsers.FactoryConfigurationError ex1 ) {
  312. throw new org.xml.sax.SAXException( ex1.toString() );
  313. } catch( NoSuchMethodError ex2 ) {
  314. }
  315. catch (AbstractMethodError ame){}
  316. if(null == reader)
  317. reader = XMLReaderFactory.createXMLReader();
  318. }
  319. try
  320. {
  321. reader.setFeature("http://xml.org/sax/features/namespace-prefixes",
  322. true);
  323. }
  324. catch (org.xml.sax.SAXException se)
  325. {
  326. // What can we do?
  327. // TODO: User diagnostics.
  328. }
  329. return reader;
  330. }
  331. catch (org.xml.sax.SAXException se)
  332. {
  333. throw new TransformerException(se.getMessage(), locator, se);
  334. }
  335. }
  336. }