1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001-2003 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 "Xerces" 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) 2003, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.parsers;
  58. import com.sun.org.apache.xerces.internal.impl.Constants;
  59. import com.sun.org.apache.xerces.internal.util.SymbolTable;
  60. import com.sun.org.apache.xerces.internal.xinclude.XIncludeHandler;
  61. import com.sun.org.apache.xerces.internal.xinclude.XIncludeNamespaceSupport;
  62. import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler;
  63. import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
  64. import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
  65. import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
  66. import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource;
  67. /**
  68. * This parser configuration includes an <code>XIncludeHandler</code> in the pipeline
  69. * before the schema validator, or as the last component in the pipeline if there is
  70. * no schema validator. Using this pipeline will enable processing according to the
  71. * XML Inclusions specification, to the conformance level described in
  72. * <code>XIncludeHandler</code>.
  73. *
  74. * @author Peter McCracken, IBM
  75. * @author Arun Yadav, Sun Microsystem
  76. * @see com.sun.org.apache.xerces.internal.xinclude.XIncludeHandler
  77. */
  78. public class XIncludeParserConfiguration extends XML11Configuration {
  79. private XIncludeHandler fXIncludeHandler;
  80. /** Feature identifier: allow notation and unparsed entity events to be sent out of order. */
  81. protected static final String ALLOW_UE_AND_NOTATION_EVENTS =
  82. Constants.SAX_FEATURE_PREFIX + Constants.ALLOW_DTD_EVENTS_AFTER_ENDDTD_FEATURE;
  83. /** Feature identifier: XInclude Aware */
  84. protected static final String XINCLUDE_AWARE =
  85. Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_AWARE ;
  86. /** Property identifier: error reporter. */
  87. protected static final String XINCLUDE_HANDLER =
  88. Constants.XERCES_PROPERTY_PREFIX + Constants.XINCLUDE_HANDLER_PROPERTY;
  89. /** Property identifier: error reporter. */
  90. protected static final String NAMESPACE_CONTEXT =
  91. Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_CONTEXT_PROPERTY;
  92. private boolean enableXInclude = false;
  93. /** Default constructor. */
  94. public XIncludeParserConfiguration() {
  95. this(null, null, null);
  96. } // <init>()
  97. /**
  98. * Constructs a parser configuration using the specified symbol table.
  99. *
  100. * @param symbolTable The symbol table to use.
  101. */
  102. public XIncludeParserConfiguration(SymbolTable symbolTable) {
  103. this(symbolTable, null, null);
  104. } // <init>(SymbolTable)
  105. /**
  106. * Constructs a parser configuration using the specified symbol table and
  107. * grammar pool.
  108. * <p>
  109. *
  110. * @param symbolTable The symbol table to use.
  111. * @param grammarPool The grammar pool to use.
  112. */
  113. public XIncludeParserConfiguration(
  114. SymbolTable symbolTable,
  115. XMLGrammarPool grammarPool) {
  116. this(symbolTable, grammarPool, null);
  117. } // <init>(SymbolTable,XMLGrammarPool)
  118. /**
  119. * Constructs a parser configuration using the specified symbol table,
  120. * grammar pool, and parent settings.
  121. * <p>
  122. *
  123. * @param symbolTable The symbol table to use.
  124. * @param grammarPool The grammar pool to use.
  125. * @param parentSettings The parent settings.
  126. */
  127. public XIncludeParserConfiguration(
  128. SymbolTable symbolTable,
  129. XMLGrammarPool grammarPool,
  130. XMLComponentManager parentSettings) {
  131. super(symbolTable, grammarPool, parentSettings);
  132. //add default recognized features
  133. final String[] recognizedFeatures = {
  134. ALLOW_UE_AND_NOTATION_EVENTS,
  135. XINCLUDE_AWARE
  136. };
  137. addRecognizedFeatures(recognizedFeatures);
  138. // add default recognized properties
  139. fXIncludeHandler = new XIncludeHandler();
  140. addComponent(fXIncludeHandler);
  141. final String[] recognizedProperties = {
  142. XINCLUDE_HANDLER,
  143. NAMESPACE_CONTEXT
  144. };
  145. addRecognizedProperties(recognizedProperties);
  146. setProperty(XINCLUDE_HANDLER, fXIncludeHandler);
  147. setProperty(NAMESPACE_CONTEXT, new XIncludeNamespaceSupport());
  148. setFeature(ALLOW_UE_AND_NOTATION_EVENTS, true);
  149. setFeature(XINCLUDE_AWARE, false);
  150. } // <init>(SymbolTable,XMLGrammarPool)
  151. /** Configures the pipeline. */
  152. protected void configurePipeline() {
  153. super.configurePipeline();
  154. if(enableXInclude){
  155. //configure DTD pipeline
  156. fDTDScanner.setDTDHandler(fDTDProcessor);
  157. fDTDProcessor.setDTDSource(fDTDScanner);
  158. fDTDProcessor.setDTDHandler(fXIncludeHandler);
  159. fXIncludeHandler.setDTDSource(fDTDProcessor);
  160. fXIncludeHandler.setDTDHandler(fDTDHandler);
  161. if (fDTDHandler != null) {
  162. fDTDHandler.setDTDSource(fXIncludeHandler);
  163. }
  164. // configure XML document pipeline: insert after DTDValidator and
  165. // before XML Schema validator
  166. XMLDocumentSource prev = null;
  167. if (fFeatures.get(XMLSCHEMA_VALIDATION) == Boolean.TRUE) {
  168. // we don't have to worry about fSchemaValidator being null, since
  169. // super.configurePipeline() instantiated it if the feature was set
  170. prev = fSchemaValidator.getDocumentSource();
  171. }
  172. // Otherwise, insert after the last component in the pipeline
  173. else {
  174. prev = fLastComponent;
  175. fLastComponent = fXIncludeHandler;
  176. }
  177. if (prev != null) {
  178. XMLDocumentHandler next = prev.getDocumentHandler();
  179. if (next != null) {
  180. fXIncludeHandler.setDocumentHandler(next);
  181. next.setDocumentSource(fXIncludeHandler);
  182. }
  183. prev.setDocumentHandler(fXIncludeHandler);
  184. fXIncludeHandler.setDocumentSource(prev);
  185. }
  186. else {
  187. setDocumentHandler(fXIncludeHandler);
  188. }
  189. }
  190. } // configurePipeline()
  191. protected void configureXML11Pipeline() {
  192. super.configureXML11Pipeline();
  193. if(enableXInclude){
  194. addXML11Component(fXIncludeHandler);
  195. // configure XML 1.1. DTD pipeline
  196. fXML11DTDScanner.setDTDHandler(fXML11DTDProcessor);
  197. fXML11DTDProcessor.setDTDSource(fXML11DTDScanner);
  198. fXML11DTDProcessor.setDTDHandler(fXIncludeHandler);
  199. fXIncludeHandler.setDTDSource(fXML11DTDProcessor);
  200. fXIncludeHandler.setDTDHandler(fDTDHandler);
  201. if (fDTDHandler != null) {
  202. fDTDHandler.setDTDSource(fXIncludeHandler);
  203. }
  204. // configure XML document pipeline: insert after DTDValidator and
  205. // before XML Schema validator
  206. XMLDocumentSource prev = null;
  207. if (fFeatures.get(XMLSCHEMA_VALIDATION) == Boolean.TRUE) {
  208. // we don't have to worry about fSchemaValidator being null, since
  209. // super.configurePipeline() instantiated it if the feature was set
  210. prev = fSchemaValidator.getDocumentSource();
  211. }
  212. // Otherwise, insert after the last component in the pipeline
  213. else {
  214. prev = fLastComponent;
  215. fLastComponent = fXIncludeHandler;
  216. }
  217. XMLDocumentHandler next = prev.getDocumentHandler();
  218. prev.setDocumentHandler(fXIncludeHandler);
  219. fXIncludeHandler.setDocumentSource(prev);
  220. if (next != null) {
  221. fXIncludeHandler.setDocumentHandler(next);
  222. next.setDocumentSource(fXIncludeHandler);
  223. }
  224. }
  225. } // configureXML11Pipeline()
  226. public void setProperty(String propertyId, Object value)
  227. throws XMLConfigurationException {
  228. if (propertyId.equals(XINCLUDE_HANDLER)) {
  229. }
  230. super.setProperty(propertyId, value);
  231. } // setProperty(String,Object)
  232. public void setFeature(String featureId, boolean state)
  233. throws XMLConfigurationException {
  234. if(featureId.equals(Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_AWARE)){
  235. enableXInclude = state;
  236. }
  237. super.setFeature(featureId,state);
  238. }
  239. }