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.xpointer.XPointerHandler;
022: import org.apache.xerces.xinclude.XIncludeHandler;
023: import org.apache.xerces.xinclude.XIncludeNamespaceSupport;
024: import org.apache.xerces.xni.XMLDocumentHandler;
025: import org.apache.xerces.xni.grammars.XMLGrammarPool;
026: import org.apache.xerces.xni.parser.XMLComponentManager;
027: import org.apache.xerces.xni.parser.XMLConfigurationException;
028: import org.apache.xerces.xni.parser.XMLDocumentSource;
029:
030: /**
031: * This parser configuration includes an <code>XPointerHandler</code> in the pipeline
032: * before the schema validator, or as the last component in the pipeline if there is
033: * no schema validator. Using this pipeline will enable processing according to the
034: * XML Inclusions specification with XPointers, to the conformance level described in
035: * <code>XPointerHandler.</code>.
036: *
037: * @see org.apache.xerces.xpointer.XPointerHandler
038: */
039: public class XPointerParserConfiguration extends XML11Configuration {
040:
041: private XPointerHandler fXPointerHandler;
042:
043: private XIncludeHandler fXIncludeHandler;
044:
045: /** Feature identifier: allow notation and unparsed entity events to be sent out of order. */
046: protected static final String ALLOW_UE_AND_NOTATION_EVENTS = Constants.SAX_FEATURE_PREFIX
047: + Constants.ALLOW_DTD_EVENTS_AFTER_ENDDTD_FEATURE;
048:
049: /** Feature identifier: fixup base URIs. */
050: protected static final String XINCLUDE_FIXUP_BASE_URIS = Constants.XERCES_FEATURE_PREFIX
051: + Constants.XINCLUDE_FIXUP_BASE_URIS_FEATURE;
052:
053: /** Feature identifier: fixup language. */
054: protected static final String XINCLUDE_FIXUP_LANGUAGE = Constants.XERCES_FEATURE_PREFIX
055: + Constants.XINCLUDE_FIXUP_LANGUAGE_FEATURE;
056:
057: /** Property identifier: error reporter. */
058: protected static final String XPOINTER_HANDLER = Constants.XERCES_PROPERTY_PREFIX
059: + Constants.XPOINTER_HANDLER_PROPERTY;
060:
061: /** Property identifier: error reporter. */
062: protected static final String XINCLUDE_HANDLER = Constants.XERCES_PROPERTY_PREFIX
063: + Constants.XINCLUDE_HANDLER_PROPERTY;
064:
065: /** Property identifier: error reporter. */
066: protected static final String NAMESPACE_CONTEXT = Constants.XERCES_PROPERTY_PREFIX
067: + Constants.NAMESPACE_CONTEXT_PROPERTY;
068:
069: /** Default constructor. */
070: public XPointerParserConfiguration() {
071: this (null, null, null);
072: } // <init>()
073:
074: /**
075: * Constructs a parser configuration using the specified symbol table.
076: *
077: * @param symbolTable The symbol table to use.
078: */
079: public XPointerParserConfiguration(SymbolTable symbolTable) {
080: this (symbolTable, null, null);
081: } // <init>(SymbolTable)
082:
083: /**
084: * Constructs a parser configuration using the specified symbol table and
085: * grammar pool.
086: * <p>
087: *
088: * @param symbolTable The symbol table to use.
089: * @param grammarPool The grammar pool to use.
090: */
091: public XPointerParserConfiguration(SymbolTable symbolTable,
092: XMLGrammarPool grammarPool) {
093: this (symbolTable, grammarPool, null);
094: } // <init>(SymbolTable,XMLGrammarPool)
095:
096: /**
097: * Constructs a parser configuration using the specified symbol table,
098: * grammar pool, and parent settings.
099: * <p>
100: *
101: * @param symbolTable The symbol table to use.
102: * @param grammarPool The grammar pool to use.
103: * @param parentSettings The parent settings.
104: */
105: public XPointerParserConfiguration(SymbolTable symbolTable,
106: XMLGrammarPool grammarPool,
107: XMLComponentManager parentSettings) {
108: super (symbolTable, grammarPool, parentSettings);
109:
110: fXIncludeHandler = new XIncludeHandler();
111: addCommonComponent(fXIncludeHandler);
112:
113: fXPointerHandler = new XPointerHandler();
114: addCommonComponent(fXPointerHandler);
115:
116: final String[] recognizedFeatures = {
117: ALLOW_UE_AND_NOTATION_EVENTS, XINCLUDE_FIXUP_BASE_URIS,
118: XINCLUDE_FIXUP_LANGUAGE };
119: addRecognizedFeatures(recognizedFeatures);
120:
121: // add default recognized properties
122: final String[] recognizedProperties = { XINCLUDE_HANDLER,
123: XPOINTER_HANDLER, NAMESPACE_CONTEXT };
124: addRecognizedProperties(recognizedProperties);
125:
126: setFeature(ALLOW_UE_AND_NOTATION_EVENTS, true);
127: setFeature(XINCLUDE_FIXUP_BASE_URIS, true);
128: setFeature(XINCLUDE_FIXUP_LANGUAGE, true);
129:
130: setProperty(XINCLUDE_HANDLER, fXIncludeHandler);
131: setProperty(XPOINTER_HANDLER, fXPointerHandler);
132: setProperty(NAMESPACE_CONTEXT, new XIncludeNamespaceSupport());
133:
134: } // <init>(SymbolTable,XMLGrammarPool)}
135:
136: /** Configures the pipeline. */
137: protected void configurePipeline() {
138: super .configurePipeline();
139:
140: //configure DTD pipeline
141: fDTDScanner.setDTDHandler(fDTDProcessor);
142: fDTDProcessor.setDTDSource(fDTDScanner);
143:
144: fDTDProcessor.setDTDHandler(fXIncludeHandler);
145: fXIncludeHandler.setDTDSource(fDTDProcessor);
146: fXIncludeHandler.setDTDHandler(fXPointerHandler);
147: fXPointerHandler.setDTDSource(fXIncludeHandler);
148: fXPointerHandler.setDTDHandler(fDTDHandler);
149: if (fDTDHandler != null) {
150: fDTDHandler.setDTDSource(fXPointerHandler);
151: }
152:
153: // configure XML document pipeline: insert after DTDValidator and
154: // before XML Schema validator
155: XMLDocumentSource prev = null;
156: if (fFeatures.get(XMLSCHEMA_VALIDATION) == Boolean.TRUE) {
157: // we don't have to worry about fSchemaValidator being null, since
158: // super.configurePipeline() instantiated it if the feature was set
159: prev = fSchemaValidator.getDocumentSource();
160: }
161: // Otherwise, insert after the last component in the pipeline
162: else {
163: prev = fLastComponent;
164: fLastComponent = fXPointerHandler;
165: }
166:
167: XMLDocumentHandler next = prev.getDocumentHandler();
168: prev.setDocumentHandler(fXIncludeHandler);
169: fXIncludeHandler.setDocumentSource(prev);
170:
171: if (next != null) {
172: fXIncludeHandler.setDocumentHandler(next);
173: next.setDocumentSource(fXIncludeHandler);
174: }
175:
176: fXIncludeHandler.setDocumentHandler(fXPointerHandler);
177: fXPointerHandler.setDocumentSource(fXIncludeHandler);
178: } // configurePipeline()
179:
180: protected void configureXML11Pipeline() {
181: super .configureXML11Pipeline();
182:
183: // configure XML 1.1. DTD pipeline
184: fXML11DTDScanner.setDTDHandler(fXML11DTDProcessor);
185: fXML11DTDProcessor.setDTDSource(fXML11DTDScanner);
186:
187: fDTDProcessor.setDTDHandler(fXIncludeHandler);
188: fXIncludeHandler.setDTDSource(fXML11DTDProcessor);
189: fXIncludeHandler.setDTDHandler(fXPointerHandler);
190: fXPointerHandler.setDTDSource(fXIncludeHandler);
191: fXPointerHandler.setDTDHandler(fDTDHandler);
192: if (fDTDHandler != null) {
193: fDTDHandler.setDTDSource(fXPointerHandler);
194: }
195:
196: // configure XML document pipeline: insert after DTDValidator and
197: // before XML Schema validator
198: XMLDocumentSource prev = null;
199: if (fFeatures.get(XMLSCHEMA_VALIDATION) == Boolean.TRUE) {
200: // we don't have to worry about fSchemaValidator being null, since
201: // super.configurePipeline() instantiated it if the feature was set
202: prev = fSchemaValidator.getDocumentSource();
203: }
204: // Otherwise, insert after the last component in the pipeline
205: else {
206: prev = fLastComponent;
207: fLastComponent = fXPointerHandler;
208: }
209:
210: XMLDocumentHandler next = prev.getDocumentHandler();
211: prev.setDocumentHandler(fXIncludeHandler);
212: fXIncludeHandler.setDocumentSource(prev);
213:
214: if (next != null) {
215: fXIncludeHandler.setDocumentHandler(next);
216: next.setDocumentSource(fXIncludeHandler);
217: }
218:
219: fXIncludeHandler.setDocumentHandler(fXPointerHandler);
220: fXPointerHandler.setDocumentSource(fXIncludeHandler);
221:
222: } // configureXML11Pipeline()
223:
224: public void setProperty(String propertyId, Object value)
225: throws XMLConfigurationException {
226:
227: //if (propertyId.equals(XINCLUDE_HANDLER)) {
228: //}
229:
230: super .setProperty(propertyId, value);
231: } // setProperty(String,Object)
232: }
|