1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xalan.processor;
  58. import org.apache.xml.utils.TreeWalker;
  59. import org.apache.xalan.templates.Stylesheet;
  60. import org.apache.xalan.res.XSLMessages;
  61. import org.apache.xalan.res.XSLTErrorResources;
  62. import javax.xml.transform.TransformerException;
  63. import org.xml.sax.Attributes;
  64. import org.xml.sax.XMLReader;
  65. import org.xml.sax.InputSource;
  66. import org.xml.sax.EntityResolver;
  67. import java.net.URL;
  68. import java.io.IOException;
  69. import org.xml.sax.helpers.XMLReaderFactory;
  70. import javax.xml.transform.URIResolver;
  71. import javax.xml.transform.stream.StreamSource;
  72. import javax.xml.transform.dom.DOMSource;
  73. import javax.xml.transform.sax.SAXSource;
  74. import javax.xml.transform.stream.StreamSource;
  75. import javax.xml.transform.Source;
  76. import javax.xml.transform.TransformerException;
  77. import org.w3c.dom.Node;
  78. import org.apache.xml.utils.SystemIDResolver;
  79. /**
  80. * TransformerFactory class for xsl:include markup.
  81. * @see <a href="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a>
  82. * @see <a href="http://www.w3.org/TR/xslt#include">include in XSLT Specification</a>
  83. */
  84. class ProcessorInclude extends XSLTElementProcessor
  85. {
  86. /**
  87. * The base URL of the XSL document.
  88. * @serial
  89. */
  90. private String m_href = null;
  91. /**
  92. * Get the base identifier with which this stylesheet is associated.
  93. *
  94. * @return non-null reference to the href attribute string, or
  95. * null if setHref has not been called.
  96. */
  97. public String getHref()
  98. {
  99. return m_href;
  100. }
  101. /**
  102. * Get the base identifier with which this stylesheet is associated.
  103. *
  104. * @param baseIdent Should be a non-null reference to a valid URL string.
  105. */
  106. public void setHref(String baseIdent)
  107. {
  108. // Validate?
  109. m_href = baseIdent;
  110. }
  111. /**
  112. * Get the stylesheet type associated with an included stylesheet
  113. *
  114. * @return the type of the stylesheet
  115. */
  116. protected int getStylesheetType()
  117. {
  118. return StylesheetHandler.STYPE_INCLUDE;
  119. }
  120. /**
  121. * Get the error number associated with this type of stylesheet including itself
  122. *
  123. * @return the appropriate error number
  124. */
  125. protected String getStylesheetInclErr()
  126. {
  127. return XSLTErrorResources.ER_STYLESHEET_INCLUDES_ITSELF;
  128. }
  129. /**
  130. * Receive notification of the start of an xsl:include element.
  131. *
  132. * @param handler The calling StylesheetHandler/TemplatesBuilder.
  133. * @param uri The Namespace URI, or the empty string if the
  134. * element has no Namespace URI or if Namespace
  135. * processing is not being performed.
  136. * @param localName The local name (without prefix), or the
  137. * empty string if Namespace processing is not being
  138. * performed.
  139. * @param rawName The raw XML 1.0 name (with prefix), or the
  140. * empty string if raw names are not available.
  141. * @param attributes The attributes attached to the element. If
  142. * there are no attributes, it shall be an empty
  143. * Attributes object.
  144. *
  145. * @throws org.xml.sax.SAXException Any SAX exception, possibly
  146. * wrapping another exception.
  147. */
  148. public void startElement(
  149. StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes)
  150. throws org.xml.sax.SAXException
  151. {
  152. setPropertiesFromAttributes(handler, rawName, attributes, this);
  153. try
  154. {
  155. String hrefUrl = SystemIDResolver.getAbsoluteURI(getHref(),
  156. handler.getBaseIdentifier());
  157. if (handler.importStackContains(hrefUrl))
  158. {
  159. throw new org.xml.sax.SAXException(
  160. XSLMessages.createMessage(
  161. getStylesheetInclErr(), new Object[]{ hrefUrl })); //"(StylesheetHandler) "+hrefUrl+" is directly or indirectly importing itself!");
  162. }
  163. handler.pushImportURL(hrefUrl);
  164. int savedStylesheetType = handler.getStylesheetType();
  165. handler.setStylesheetType(this.getStylesheetType());
  166. handler.pushNewNamespaceSupport();
  167. try
  168. {
  169. parse(handler, uri, localName, rawName, attributes);
  170. }
  171. finally
  172. {
  173. handler.setStylesheetType(savedStylesheetType);
  174. handler.popImportURL();
  175. handler.popNamespaceSupport();
  176. }
  177. }
  178. catch(TransformerException te)
  179. {
  180. handler.error(te.getMessage(), te);
  181. }
  182. }
  183. /**
  184. * Set off a new parse for an included or imported stylesheet. This will
  185. * set the {@link StylesheetHandler} to a new state, and recurse in with
  186. * a new set of parse events. Once this function returns, the state of
  187. * the StylesheetHandler should be restored.
  188. *
  189. * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
  190. * @param uri The Namespace URI, which should be the XSLT namespace.
  191. * @param localName The local name (without prefix), which should be "include" or "import".
  192. * @param rawName The qualified name (with prefix).
  193. * @param attributes The list of attributes on the xsl:include or xsl:import element.
  194. *
  195. * @throws org.xml.sax.SAXException Any SAX exception, possibly
  196. * wrapping another exception.
  197. */
  198. protected void parse(
  199. StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes)
  200. throws org.xml.sax.SAXException
  201. {
  202. TransformerFactoryImpl processor = handler.getStylesheetProcessor();
  203. URIResolver uriresolver = processor.getURIResolver();
  204. try
  205. {
  206. Source source = null;
  207. if (null != uriresolver)
  208. {
  209. source = uriresolver.resolve(getHref(),
  210. handler.getBaseIdentifier());
  211. if (null != source && source instanceof DOMSource)
  212. {
  213. Node node = ((DOMSource)source).getNode();
  214. String systemId = source.getSystemId();
  215. if (systemId == null)
  216. {
  217. systemId = SystemIDResolver.getAbsoluteURI(getHref(),
  218. handler.getBaseIdentifier());
  219. }
  220. TreeWalker walker = new TreeWalker(handler, new org.apache.xpath.DOM2Helper(), systemId);
  221. try
  222. {
  223. walker.traverse(node);
  224. }
  225. catch(org.xml.sax.SAXException se)
  226. {
  227. throw new TransformerException(se);
  228. }
  229. return;
  230. }
  231. }
  232. if(null == source)
  233. {
  234. String absURL = SystemIDResolver.getAbsoluteURI(getHref(),
  235. handler.getBaseIdentifier());
  236. source = new StreamSource(absURL);
  237. }
  238. XMLReader reader = null;
  239. if(source instanceof SAXSource)
  240. {
  241. SAXSource saxSource = (SAXSource)source;
  242. reader = saxSource.getXMLReader(); // may be null
  243. }
  244. boolean isUserReader = (reader != null);
  245. InputSource inputSource = SAXSource.sourceToInputSource(source);
  246. if (null == reader)
  247. {
  248. // Use JAXP1.1 ( if possible )
  249. try {
  250. javax.xml.parsers.SAXParserFactory factory=
  251. javax.xml.parsers.SAXParserFactory.newInstance();
  252. factory.setNamespaceAware( true );
  253. javax.xml.parsers.SAXParser jaxpParser=
  254. factory.newSAXParser();
  255. reader=jaxpParser.getXMLReader();
  256. } catch( javax.xml.parsers.ParserConfigurationException ex ) {
  257. throw new org.xml.sax.SAXException( ex );
  258. } catch( javax.xml.parsers.FactoryConfigurationError ex1 ) {
  259. throw new org.xml.sax.SAXException( ex1.toString() );
  260. }
  261. catch( NoSuchMethodError ex2 )
  262. {
  263. }
  264. catch (AbstractMethodError ame){}
  265. }
  266. if (null == reader)
  267. reader = XMLReaderFactory.createXMLReader();
  268. if (null != reader)
  269. {
  270. reader.setContentHandler(handler);
  271. try
  272. {
  273. if(!isUserReader)
  274. reader.setFeature("http://apache.org/xml/features/validation/dynamic",
  275. true);
  276. }
  277. catch(org.xml.sax.SAXException se) {}
  278. handler.pushBaseIndentifier(inputSource.getSystemId());
  279. try
  280. {
  281. reader.parse(inputSource);
  282. }
  283. finally
  284. {
  285. handler.popBaseIndentifier();
  286. }
  287. }
  288. }
  289. catch (IOException ioe)
  290. {
  291. handler.error(XSLTErrorResources.ER_IOEXCEPTION,
  292. new Object[]{ getHref() }, ioe);
  293. }
  294. catch(TransformerException te)
  295. {
  296. handler.error(te.getMessage(), te);
  297. }
  298. }
  299. }