KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > parsers > XIncludeParserConfiguration


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
59 import com.sun.org.apache.xerces.internal.impl.Constants;
60 import com.sun.org.apache.xerces.internal.util.SymbolTable;
61 import com.sun.org.apache.xerces.internal.xinclude.XIncludeHandler;
62 import com.sun.org.apache.xerces.internal.xinclude.XIncludeNamespaceSupport;
63 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler;
64 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
65 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
66 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
67 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource;
68
69 /**
70  * This parser configuration includes an <code>XIncludeHandler</code> in the pipeline
71  * before the schema validator, or as the last component in the pipeline if there is
72  * no schema validator. Using this pipeline will enable processing according to the
73  * XML Inclusions specification, to the conformance level described in
74  * <code>XIncludeHandler</code>.
75  *
76  * @author Peter McCracken, IBM
77  * @author Arun Yadav, Sun Microsystem
78  * @see com.sun.org.apache.xerces.internal.xinclude.XIncludeHandler
79  */

80 public class XIncludeParserConfiguration extends XML11Configuration {
81
82     private XIncludeHandler fXIncludeHandler;
83
84     /** Feature identifier: allow notation and unparsed entity events to be sent out of order. */
85     protected static final String JavaDoc ALLOW_UE_AND_NOTATION_EVENTS =
86         Constants.SAX_FEATURE_PREFIX + Constants.ALLOW_DTD_EVENTS_AFTER_ENDDTD_FEATURE;
87
88      /** Feature identifier: XInclude Aware */
89     protected static final String JavaDoc XINCLUDE_AWARE =
90         Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_AWARE ;
91
92
93     /** Property identifier: error reporter. */
94     protected static final String JavaDoc XINCLUDE_HANDLER =
95         Constants.XERCES_PROPERTY_PREFIX + Constants.XINCLUDE_HANDLER_PROPERTY;
96
97     /** Property identifier: error reporter. */
98     protected static final String JavaDoc NAMESPACE_CONTEXT =
99         Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_CONTEXT_PROPERTY;
100
101
102     private boolean enableXInclude = false;
103
104     /** Default constructor. */
105     public XIncludeParserConfiguration() {
106         this(null, null, null);
107     } // <init>()
108

109     /**
110      * Constructs a parser configuration using the specified symbol table.
111      *
112      * @param symbolTable The symbol table to use.
113      */

114     public XIncludeParserConfiguration(SymbolTable symbolTable) {
115         this(symbolTable, null, null);
116     } // <init>(SymbolTable)
117

118     /**
119      * Constructs a parser configuration using the specified symbol table and
120      * grammar pool.
121      * <p>
122      *
123      * @param symbolTable The symbol table to use.
124      * @param grammarPool The grammar pool to use.
125      */

126     public XIncludeParserConfiguration(
127         SymbolTable symbolTable,
128         XMLGrammarPool grammarPool) {
129         this(symbolTable, grammarPool, null);
130     } // <init>(SymbolTable,XMLGrammarPool)
131

132     /**
133      * Constructs a parser configuration using the specified symbol table,
134      * grammar pool, and parent settings.
135      * <p>
136      *
137      * @param symbolTable The symbol table to use.
138      * @param grammarPool The grammar pool to use.
139      * @param parentSettings The parent settings.
140      */

141     public XIncludeParserConfiguration(
142         SymbolTable symbolTable,
143         XMLGrammarPool grammarPool,
144         XMLComponentManager parentSettings) {
145         super(symbolTable, grammarPool, parentSettings);
146        
147
148          //add default recognized features
149
final String JavaDoc[] recognizedFeatures = {
150              ALLOW_UE_AND_NOTATION_EVENTS,
151              XINCLUDE_AWARE
152          };
153          addRecognizedFeatures(recognizedFeatures);
154         // add default recognized properties
155
fXIncludeHandler = new XIncludeHandler();
156         addComponent(fXIncludeHandler);
157         final String JavaDoc[] recognizedProperties = {
158             XINCLUDE_HANDLER,
159             NAMESPACE_CONTEXT
160         };
161
162         addRecognizedProperties(recognizedProperties);
163         setProperty(XINCLUDE_HANDLER, fXIncludeHandler);
164         setProperty(NAMESPACE_CONTEXT, new XIncludeNamespaceSupport());
165         setFeature(ALLOW_UE_AND_NOTATION_EVENTS, true);
166         setFeature(XINCLUDE_AWARE, false);
167     } // <init>(SymbolTable,XMLGrammarPool)
168

169     
170     /** Configures the pipeline. */
171     protected void configurePipeline() {
172         super.configurePipeline();
173         if(enableXInclude){
174         //configure DTD pipeline
175
fDTDScanner.setDTDHandler(fDTDProcessor);
176         fDTDProcessor.setDTDSource(fDTDScanner);
177         fDTDProcessor.setDTDHandler(fXIncludeHandler);
178         fXIncludeHandler.setDTDSource(fDTDProcessor);
179         fXIncludeHandler.setDTDHandler(fDTDHandler);
180         if (fDTDHandler != null) {
181             fDTDHandler.setDTDSource(fXIncludeHandler);
182         }
183
184         // configure XML document pipeline: insert after DTDValidator and
185
// before XML Schema validator
186
XMLDocumentSource prev = null;
187         if (fFeatures.get(XMLSCHEMA_VALIDATION) == Boolean.TRUE) {
188             // we don't have to worry about fSchemaValidator being null, since
189
// super.configurePipeline() instantiated it if the feature was set
190
prev = fSchemaValidator.getDocumentSource();
191         }
192         // Otherwise, insert after the last component in the pipeline
193
else {
194             prev = fLastComponent;
195             fLastComponent = fXIncludeHandler;
196         }
197
198         if (prev != null) {
199             XMLDocumentHandler next = prev.getDocumentHandler();
200             if (next != null) {
201                 fXIncludeHandler.setDocumentHandler(next);
202                 next.setDocumentSource(fXIncludeHandler);
203             }
204             prev.setDocumentHandler(fXIncludeHandler);
205             fXIncludeHandler.setDocumentSource(prev);
206         }
207         else {
208             setDocumentHandler(fXIncludeHandler);
209         }
210         }
211     } // configurePipeline()
212

213     protected void configureXML11Pipeline() {
214         super.configureXML11Pipeline();
215         if(enableXInclude){
216         addXML11Component(fXIncludeHandler);
217         // configure XML 1.1. DTD pipeline
218
fXML11DTDScanner.setDTDHandler(fXML11DTDProcessor);
219         fXML11DTDProcessor.setDTDSource(fXML11DTDScanner);
220         fXML11DTDProcessor.setDTDHandler(fXIncludeHandler);
221         fXIncludeHandler.setDTDSource(fXML11DTDProcessor);
222         fXIncludeHandler.setDTDHandler(fDTDHandler);
223         if (fDTDHandler != null) {
224             fDTDHandler.setDTDSource(fXIncludeHandler);
225         }
226         
227         // configure XML document pipeline: insert after DTDValidator and
228
// before XML Schema validator
229
XMLDocumentSource prev = null;
230         if (fFeatures.get(XMLSCHEMA_VALIDATION) == Boolean.TRUE) {
231             // we don't have to worry about fSchemaValidator being null, since
232
// super.configurePipeline() instantiated it if the feature was set
233
prev = fSchemaValidator.getDocumentSource();
234         }
235         // Otherwise, insert after the last component in the pipeline
236
else {
237             prev = fLastComponent;
238             fLastComponent = fXIncludeHandler;
239         }
240
241         XMLDocumentHandler next = prev.getDocumentHandler();
242         prev.setDocumentHandler(fXIncludeHandler);
243         fXIncludeHandler.setDocumentSource(prev);
244         if (next != null) {
245             fXIncludeHandler.setDocumentHandler(next);
246             next.setDocumentSource(fXIncludeHandler);
247         }
248         }
249     } // configureXML11Pipeline()
250

251     public void setProperty(String JavaDoc propertyId, Object JavaDoc value)
252         throws XMLConfigurationException {
253
254         if (propertyId.equals(XINCLUDE_HANDLER)) {
255         }
256
257         super.setProperty(propertyId, value);
258     } // setProperty(String,Object)
259

260     public void setFeature(String JavaDoc featureId, boolean state)
261     throws XMLConfigurationException {
262         if(featureId.equals(Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_AWARE)){
263             enableXInclude = state;
264         }
265         super.setFeature(featureId,state);
266     }
267 }
268
Popular Tags