KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > parsers > XIncludeAwareParserConfiguration


1 /*
2  * Copyright 2005 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 package org.apache.xerces.parsers;
18
19 import org.apache.xerces.impl.Constants;
20 import org.apache.xerces.util.NamespaceSupport;
21 import org.apache.xerces.util.SymbolTable;
22 import org.apache.xerces.xinclude.XIncludeHandler;
23 import org.apache.xerces.xinclude.XIncludeNamespaceSupport;
24 import org.apache.xerces.xni.NamespaceContext;
25 import org.apache.xerces.xni.XMLDocumentHandler;
26 import org.apache.xerces.xni.grammars.XMLGrammarPool;
27 import org.apache.xerces.xni.parser.XMLComponentManager;
28 import org.apache.xerces.xni.parser.XMLConfigurationException;
29 import org.apache.xerces.xni.parser.XMLDocumentSource;
30
31 /**
32  * This class is the configuration used to parse XML 1.0 and XML 1.1 documents
33  * and provides support for XInclude. This is the default Xerces configuration.
34  *
35  * @author Michael Glavassevich, IBM
36  *
37  * @version $Id: XIncludeAwareParserConfiguration.java,v 1.2 2005/05/09 02:08:10 mrglavas Exp $
38  */

39 public class XIncludeAwareParserConfiguration extends XML11Configuration {
40     
41     /** Feature identifier: allow notation and unparsed entity events to be sent out of order. */
42     protected static final String JavaDoc ALLOW_UE_AND_NOTATION_EVENTS =
43         Constants.SAX_FEATURE_PREFIX + Constants.ALLOW_DTD_EVENTS_AFTER_ENDDTD_FEATURE;
44     
45     /** Feature identifier: fixup base URIs. */
46     protected static final String JavaDoc XINCLUDE_FIXUP_BASE_URIS =
47         Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_FIXUP_BASE_URIS_FEATURE;
48     
49     /** Feature identifier: fixup language. */
50     protected static final String JavaDoc XINCLUDE_FIXUP_LANGUAGE =
51         Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_FIXUP_LANGUAGE_FEATURE;
52     
53     /** Feature identifier: XInclude processing */
54     protected static final String JavaDoc XINCLUDE_FEATURE =
55         Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_FEATURE;
56     
57     /** Property identifier: error reporter. */
58     protected static final String JavaDoc XINCLUDE_HANDLER =
59         Constants.XERCES_PROPERTY_PREFIX + Constants.XINCLUDE_HANDLER_PROPERTY;
60     
61     /** Property identifier: error reporter. */
62     protected static final String JavaDoc NAMESPACE_CONTEXT =
63         Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_CONTEXT_PROPERTY;
64     
65     //
66
// Components
67
//
68

69     /** XInclude handler. */
70     protected XIncludeHandler fXIncludeHandler;
71     
72     /** Non-XInclude NamespaceContext. */
73     protected NamespaceSupport fNonXIncludeNSContext;
74     
75     /** XInclude NamespaceContext. */
76     protected XIncludeNamespaceSupport fXIncludeNSContext;
77     
78     /** Current NamespaceContext. */
79     protected NamespaceContext fCurrentNSContext;
80     
81     /** Flag indicating whether XInclude processsing is enabled. */
82     protected boolean fXIncludeEnabled = false;
83     
84     /** Default constructor. */
85     public XIncludeAwareParserConfiguration() {
86         this(null, null, null);
87     } // <init>()
88

89     /**
90      * Constructs a parser configuration using the specified symbol table.
91      *
92      * @param symbolTable The symbol table to use.
93      */

94     public XIncludeAwareParserConfiguration(SymbolTable symbolTable) {
95         this(symbolTable, null, null);
96     } // <init>(SymbolTable)
97

98     /**
99      * Constructs a parser configuration using the specified symbol table and
100      * grammar pool.
101      * <p>
102      *
103      * @param symbolTable The symbol table to use.
104      * @param grammarPool The grammar pool to use.
105      */

106     public XIncludeAwareParserConfiguration(
107             SymbolTable symbolTable,
108             XMLGrammarPool grammarPool) {
109         this(symbolTable, grammarPool, null);
110     } // <init>(SymbolTable,XMLGrammarPool)
111

112     /**
113      * Constructs a parser configuration using the specified symbol table,
114      * grammar pool, and parent settings.
115      * <p>
116      *
117      * @param symbolTable The symbol table to use.
118      * @param grammarPool The grammar pool to use.
119      * @param parentSettings The parent settings.
120      */

121     public XIncludeAwareParserConfiguration(
122             SymbolTable symbolTable,
123             XMLGrammarPool grammarPool,
124             XMLComponentManager parentSettings) {
125         super(symbolTable, grammarPool, parentSettings);
126         
127         final String JavaDoc[] recognizedFeatures = {
128                 ALLOW_UE_AND_NOTATION_EVENTS,
129                 XINCLUDE_FIXUP_BASE_URIS,
130                 XINCLUDE_FIXUP_LANGUAGE
131         };
132         addRecognizedFeatures(recognizedFeatures);
133         
134         // add default recognized properties
135
final String JavaDoc[] recognizedProperties =
136         { XINCLUDE_HANDLER, NAMESPACE_CONTEXT };
137         addRecognizedProperties(recognizedProperties);
138         
139         setFeature(ALLOW_UE_AND_NOTATION_EVENTS, true);
140         setFeature(XINCLUDE_FIXUP_BASE_URIS, true);
141         setFeature(XINCLUDE_FIXUP_LANGUAGE, true);
142         
143         fNonXIncludeNSContext = new NamespaceSupport();
144         fCurrentNSContext = fNonXIncludeNSContext;
145         setProperty(NAMESPACE_CONTEXT, fNonXIncludeNSContext);
146     }
147     
148     
149     /** Configures the pipeline. */
150     protected void configurePipeline() {
151         super.configurePipeline();
152         if (fXIncludeEnabled) {
153             // If the XInclude handler was not in the pipeline insert it.
154
if (fXIncludeHandler == null) {
155                 fXIncludeHandler = new XIncludeHandler();
156                 // add XInclude component
157
setProperty(XINCLUDE_HANDLER, fXIncludeHandler);
158                 addCommonComponent(fXIncludeHandler);
159                 fXIncludeHandler.reset(this);
160             }
161             // Setup NamespaceContext
162
if (fCurrentNSContext != fXIncludeNSContext) {
163                 if (fXIncludeNSContext == null) {
164                     fXIncludeNSContext = new XIncludeNamespaceSupport();
165                 }
166                 fCurrentNSContext = fXIncludeNSContext;
167                 setProperty(NAMESPACE_CONTEXT, fXIncludeNSContext);
168             }
169             //configure DTD pipeline
170
fDTDScanner.setDTDHandler(fDTDProcessor);
171             fDTDProcessor.setDTDSource(fDTDScanner);
172             fDTDProcessor.setDTDHandler(fXIncludeHandler);
173             fXIncludeHandler.setDTDSource(fDTDProcessor);
174             fXIncludeHandler.setDTDHandler(fDTDHandler);
175             if (fDTDHandler != null) {
176                 fDTDHandler.setDTDSource(fXIncludeHandler);
177             }
178             
179             // configure XML document pipeline: insert after DTDValidator and
180
// before XML Schema validator
181
XMLDocumentSource prev = null;
182             if (fFeatures.get(XMLSCHEMA_VALIDATION) == Boolean.TRUE) {
183                 // we don't have to worry about fSchemaValidator being null, since
184
// super.configurePipeline() instantiated it if the feature was set
185
prev = fSchemaValidator.getDocumentSource();
186             }
187             // Otherwise, insert after the last component in the pipeline
188
else {
189                 prev = fLastComponent;
190                 fLastComponent = fXIncludeHandler;
191             }
192             
193             XMLDocumentHandler next = prev.getDocumentHandler();
194             prev.setDocumentHandler(fXIncludeHandler);
195             fXIncludeHandler.setDocumentSource(prev);
196             if (next != null) {
197                 fXIncludeHandler.setDocumentHandler(next);
198                 next.setDocumentSource(fXIncludeHandler);
199             }
200         }
201         else {
202             // Setup NamespaceContext
203
if (fCurrentNSContext != fNonXIncludeNSContext) {
204                 fCurrentNSContext = fNonXIncludeNSContext;
205                 setProperty(NAMESPACE_CONTEXT, fNonXIncludeNSContext);
206             }
207         }
208     } // configurePipeline()
209

210     protected void configureXML11Pipeline() {
211         super.configureXML11Pipeline();
212         if (fXIncludeEnabled) {
213             // If the XInclude handler was not in the pipeline insert it.
214
if (fXIncludeHandler == null) {
215                 fXIncludeHandler = new XIncludeHandler();
216                 // add XInclude component
217
setProperty(XINCLUDE_HANDLER, fXIncludeHandler);
218                 addCommonComponent(fXIncludeHandler);
219                 fXIncludeHandler.reset(this);
220             }
221             // Setup NamespaceContext
222
if (fCurrentNSContext != fXIncludeNSContext) {
223                 if (fXIncludeNSContext == null) {
224                     fXIncludeNSContext = new XIncludeNamespaceSupport();
225                 }
226                 fCurrentNSContext = fXIncludeNSContext;
227                 setProperty(NAMESPACE_CONTEXT, fXIncludeNSContext);
228             }
229             // configure XML 1.1. DTD pipeline
230
fXML11DTDScanner.setDTDHandler(fXML11DTDProcessor);
231             fXML11DTDProcessor.setDTDSource(fXML11DTDScanner);
232             fXML11DTDProcessor.setDTDHandler(fXIncludeHandler);
233             fXIncludeHandler.setDTDSource(fXML11DTDProcessor);
234             fXIncludeHandler.setDTDHandler(fDTDHandler);
235             if (fDTDHandler != null) {
236                 fDTDHandler.setDTDSource(fXIncludeHandler);
237             }
238             
239             // configure XML document pipeline: insert after DTDValidator and
240
// before XML Schema validator
241
XMLDocumentSource prev = null;
242             if (fFeatures.get(XMLSCHEMA_VALIDATION) == Boolean.TRUE) {
243                 // we don't have to worry about fSchemaValidator being null, since
244
// super.configurePipeline() instantiated it if the feature was set
245
prev = fSchemaValidator.getDocumentSource();
246             }
247             // Otherwise, insert after the last component in the pipeline
248
else {
249                 prev = fLastComponent;
250                 fLastComponent = fXIncludeHandler;
251             }
252             
253             XMLDocumentHandler next = prev.getDocumentHandler();
254             prev.setDocumentHandler(fXIncludeHandler);
255             fXIncludeHandler.setDocumentSource(prev);
256             if (next != null) {
257                 fXIncludeHandler.setDocumentHandler(next);
258                 next.setDocumentSource(fXIncludeHandler);
259             }
260         }
261         else {
262             // Setup NamespaceContext
263
if (fCurrentNSContext != fNonXIncludeNSContext) {
264                 fCurrentNSContext = fNonXIncludeNSContext;
265                 setProperty(NAMESPACE_CONTEXT, fNonXIncludeNSContext);
266             }
267         }
268     } // configureXML11Pipeline()
269

270     public boolean getFeature(String JavaDoc featureId)
271         throws XMLConfigurationException {
272         if (featureId.equals(PARSER_SETTINGS)) {
273             return fConfigUpdated;
274         }
275         else if (featureId.equals(XINCLUDE_FEATURE)) {
276             return fXIncludeEnabled;
277         }
278         return super.getFeature0(featureId);
279         
280     } // getFeature(String):boolean
281

282     public void setFeature(String JavaDoc featureId, boolean state)
283         throws XMLConfigurationException {
284         if (featureId.equals(XINCLUDE_FEATURE)) {
285             fXIncludeEnabled = state;
286             fConfigUpdated = true;
287             return;
288         }
289         super.setFeature(featureId,state);
290     }
291     
292 }
293
Popular Tags