001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.xerces.parsers;
018:
019: import org.apache.xerces.impl.Constants;
020: import org.apache.xerces.util.SymbolTable;
021: import org.apache.xerces.xinclude.XIncludeHandler;
022: import org.apache.xerces.xinclude.XIncludeNamespaceSupport;
023: import org.apache.xerces.xni.XMLDocumentHandler;
024: import org.apache.xerces.xni.grammars.XMLGrammarPool;
025: import org.apache.xerces.xni.parser.XMLComponentManager;
026: import org.apache.xerces.xni.parser.XMLConfigurationException;
027: import org.apache.xerces.xni.parser.XMLDocumentSource;
028:
029: /**
030: * This parser configuration includes an <code>XIncludeHandler</code> in the pipeline
031: * before the schema validator, or as the last component in the pipeline if there is
032: * no schema validator. Using this pipeline will enable processing according to the
033: * XML Inclusions specification, to the conformance level described in
034: * <code>XIncludeHandler</code>.
035: *
036: * @author Peter McCracken, IBM
037: * @see org.apache.xerces.xinclude.XIncludeHandler
038: */
039: public class XIncludeParserConfiguration extends XML11Configuration {
040:
041: private XIncludeHandler fXIncludeHandler;
042:
043: /** Feature identifier: allow notation and unparsed entity events to be sent out of order. */
044: protected static final String ALLOW_UE_AND_NOTATION_EVENTS = Constants.SAX_FEATURE_PREFIX
045: + Constants.ALLOW_DTD_EVENTS_AFTER_ENDDTD_FEATURE;
046:
047: /** Feature identifier: fixup base URIs. */
048: protected static final String XINCLUDE_FIXUP_BASE_URIS = Constants.XERCES_FEATURE_PREFIX
049: + Constants.XINCLUDE_FIXUP_BASE_URIS_FEATURE;
050:
051: /** Feature identifier: fixup language. */
052: protected static final String XINCLUDE_FIXUP_LANGUAGE = Constants.XERCES_FEATURE_PREFIX
053: + Constants.XINCLUDE_FIXUP_LANGUAGE_FEATURE;
054:
055: /** Property identifier: error reporter. */
056: protected static final String XINCLUDE_HANDLER = Constants.XERCES_PROPERTY_PREFIX
057: + Constants.XINCLUDE_HANDLER_PROPERTY;
058:
059: /** Property identifier: error reporter. */
060: protected static final String NAMESPACE_CONTEXT = Constants.XERCES_PROPERTY_PREFIX
061: + Constants.NAMESPACE_CONTEXT_PROPERTY;
062:
063: /** Default constructor. */
064: public XIncludeParserConfiguration() {
065: this (null, null, null);
066: } // <init>()
067:
068: /**
069: * Constructs a parser configuration using the specified symbol table.
070: *
071: * @param symbolTable The symbol table to use.
072: */
073: public XIncludeParserConfiguration(SymbolTable symbolTable) {
074: this (symbolTable, null, null);
075: } // <init>(SymbolTable)
076:
077: /**
078: * Constructs a parser configuration using the specified symbol table and
079: * grammar pool.
080: * <p>
081: *
082: * @param symbolTable The symbol table to use.
083: * @param grammarPool The grammar pool to use.
084: */
085: public XIncludeParserConfiguration(SymbolTable symbolTable,
086: XMLGrammarPool grammarPool) {
087: this (symbolTable, grammarPool, null);
088: } // <init>(SymbolTable,XMLGrammarPool)
089:
090: /**
091: * Constructs a parser configuration using the specified symbol table,
092: * grammar pool, and parent settings.
093: * <p>
094: *
095: * @param symbolTable The symbol table to use.
096: * @param grammarPool The grammar pool to use.
097: * @param parentSettings The parent settings.
098: */
099: public XIncludeParserConfiguration(SymbolTable symbolTable,
100: XMLGrammarPool grammarPool,
101: XMLComponentManager parentSettings) {
102: super (symbolTable, grammarPool, parentSettings);
103:
104: fXIncludeHandler = new XIncludeHandler();
105: addCommonComponent(fXIncludeHandler);
106:
107: final String[] recognizedFeatures = {
108: ALLOW_UE_AND_NOTATION_EVENTS, XINCLUDE_FIXUP_BASE_URIS,
109: XINCLUDE_FIXUP_LANGUAGE };
110: addRecognizedFeatures(recognizedFeatures);
111:
112: // add default recognized properties
113: final String[] recognizedProperties = { XINCLUDE_HANDLER,
114: NAMESPACE_CONTEXT };
115: addRecognizedProperties(recognizedProperties);
116:
117: setFeature(ALLOW_UE_AND_NOTATION_EVENTS, true);
118: setFeature(XINCLUDE_FIXUP_BASE_URIS, true);
119: setFeature(XINCLUDE_FIXUP_LANGUAGE, true);
120:
121: setProperty(XINCLUDE_HANDLER, fXIncludeHandler);
122: setProperty(NAMESPACE_CONTEXT, new XIncludeNamespaceSupport());
123: } // <init>(SymbolTable,XMLGrammarPool)}
124:
125: /** Configures the pipeline. */
126: protected void configurePipeline() {
127: super .configurePipeline();
128:
129: //configure DTD pipeline
130: fDTDScanner.setDTDHandler(fDTDProcessor);
131: fDTDProcessor.setDTDSource(fDTDScanner);
132: fDTDProcessor.setDTDHandler(fXIncludeHandler);
133: fXIncludeHandler.setDTDSource(fDTDProcessor);
134: fXIncludeHandler.setDTDHandler(fDTDHandler);
135: if (fDTDHandler != null) {
136: fDTDHandler.setDTDSource(fXIncludeHandler);
137: }
138:
139: // configure XML document pipeline: insert after DTDValidator and
140: // before XML Schema validator
141: XMLDocumentSource prev = null;
142: if (fFeatures.get(XMLSCHEMA_VALIDATION) == Boolean.TRUE) {
143: // we don't have to worry about fSchemaValidator being null, since
144: // super.configurePipeline() instantiated it if the feature was set
145: prev = fSchemaValidator.getDocumentSource();
146: }
147: // Otherwise, insert after the last component in the pipeline
148: else {
149: prev = fLastComponent;
150: fLastComponent = fXIncludeHandler;
151: }
152:
153: XMLDocumentHandler next = prev.getDocumentHandler();
154: prev.setDocumentHandler(fXIncludeHandler);
155: fXIncludeHandler.setDocumentSource(prev);
156: if (next != null) {
157: fXIncludeHandler.setDocumentHandler(next);
158: next.setDocumentSource(fXIncludeHandler);
159: }
160:
161: } // configurePipeline()
162:
163: protected void configureXML11Pipeline() {
164: super .configureXML11Pipeline();
165:
166: // configure XML 1.1. DTD pipeline
167: fXML11DTDScanner.setDTDHandler(fXML11DTDProcessor);
168: fXML11DTDProcessor.setDTDSource(fXML11DTDScanner);
169: fXML11DTDProcessor.setDTDHandler(fXIncludeHandler);
170: fXIncludeHandler.setDTDSource(fXML11DTDProcessor);
171: fXIncludeHandler.setDTDHandler(fDTDHandler);
172: if (fDTDHandler != null) {
173: fDTDHandler.setDTDSource(fXIncludeHandler);
174: }
175:
176: // configure XML document pipeline: insert after DTDValidator and
177: // before XML Schema validator
178: XMLDocumentSource prev = null;
179: if (fFeatures.get(XMLSCHEMA_VALIDATION) == Boolean.TRUE) {
180: // we don't have to worry about fSchemaValidator being null, since
181: // super.configurePipeline() instantiated it if the feature was set
182: prev = fSchemaValidator.getDocumentSource();
183: }
184: // Otherwise, insert after the last component in the pipeline
185: else {
186: prev = fLastComponent;
187: fLastComponent = fXIncludeHandler;
188: }
189:
190: XMLDocumentHandler next = prev.getDocumentHandler();
191: prev.setDocumentHandler(fXIncludeHandler);
192: fXIncludeHandler.setDocumentSource(prev);
193: if (next != null) {
194: fXIncludeHandler.setDocumentHandler(next);
195: next.setDocumentSource(fXIncludeHandler);
196: }
197:
198: } // configureXML11Pipeline()
199:
200: public void setProperty(String propertyId, Object value)
201: throws XMLConfigurationException {
202:
203: if (propertyId.equals(XINCLUDE_HANDLER)) {
204: }
205:
206: super .setProperty(propertyId, value);
207: } // setProperty(String,Object)
208: }
|