KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > dom > CoreDOMImplementationImpl


1 /*
2  * Copyright 1999-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 package org.apache.xerces.dom;
17
18 import org.apache.xerces.impl.RevalidationHandler;
19 import org.apache.xerces.parsers.DOMParserImpl;
20 import org.apache.xerces.util.XMLChar;
21 import org.apache.xerces.xni.grammars.XMLGrammarDescription;
22 import org.apache.xml.serialize.DOMSerializerImpl;
23 import org.w3c.dom.DOMException JavaDoc;
24 import org.w3c.dom.DOMImplementation JavaDoc;
25 import org.w3c.dom.Document JavaDoc;
26 import org.w3c.dom.DocumentType JavaDoc;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.ls.LSParser JavaDoc;
29 import org.w3c.dom.ls.DOMImplementationLS JavaDoc;
30 import org.w3c.dom.ls.LSInput JavaDoc;
31 import org.w3c.dom.ls.LSOutput JavaDoc;
32 import org.w3c.dom.ls.LSSerializer JavaDoc;
33 /**
34  * The DOMImplementation class is description of a particular
35  * implementation of the Document Object Model. As such its data is
36  * static, shared by all instances of this implementation.
37  * <P>
38  * The DOM API requires that it be a real object rather than static
39  * methods. However, there's nothing that says it can't be a singleton,
40  * so that's how I've implemented it.
41  * <P>
42  * This particular class, along with CoreDocumentImpl, supports the DOM
43  * Core and Load/Save (Experimental). Optional modules are supported by
44  * the more complete DOMImplementation class along with DocumentImpl.
45  *
46  * @xerces.internal
47  *
48  * @version $Id: CoreDOMImplementationImpl.java,v 1.35 2005/05/10 15:36:42 ankitp Exp $
49  * @since PR-DOM-Level-1-19980818.
50  */

51 public class CoreDOMImplementationImpl
52     implements DOMImplementation JavaDoc, DOMImplementationLS JavaDoc {
53     //
54
// Data
55
//
56

57     // validators pool
58
private static final int SIZE = 2;
59     private RevalidationHandler validators[] = new RevalidationHandler[SIZE];
60     
61     private RevalidationHandler dtdValidators[] = new RevalidationHandler[SIZE];
62     private int freeValidatorIndex = -1;
63     private int freeDTDValidatorIndex = -1;
64     private int currentSize = SIZE;
65
66     // Document and doctype counter. Used to assign order to documents and
67
// doctypes without owners, on an demand basis. Used for
68
// compareDocumentPosition
69
private int docAndDoctypeCounter = 0;
70     
71     // static
72
/** Dom implementation singleton. */
73     static CoreDOMImplementationImpl singleton =
74         new CoreDOMImplementationImpl();
75     //
76
// Public methods
77
//
78
/** NON-DOM: Obtain and return the single shared object */
79     public static DOMImplementation JavaDoc getDOMImplementation() {
80         return singleton;
81     }
82     //
83
// DOMImplementation methods
84
//
85
/**
86      * Test if the DOM implementation supports a specific "feature" --
87      * currently meaning language and level thereof.
88      *
89      * @param feature The package name of the feature to test.
90      * In Level 1, supported values are "HTML" and "XML" (case-insensitive).
91      * At this writing, org.apache.xerces.dom supports only XML.
92      *
93      * @param version The version number of the feature being tested.
94      * This is interpreted as "Version of the DOM API supported for the
95      * specified Feature", and in Level 1 should be "1.0"
96      *
97      * @return true iff this implementation is compatable with the specified
98      * feature and version.
99      */

100     public boolean hasFeature(String JavaDoc feature, String JavaDoc version) {
101         
102         boolean anyVersion = version == null || version.length() == 0;
103         
104         // check if Xalan implementation is around and if yes report true for supporting
105
// XPath API
106
// if a plus sign "+" is prepended to any feature name, implementations
107
// are considered in which the specified feature may not be directly
108
// castable DOMImplementation.getFeature(feature, version). Without a
109
// plus, only features whose interfaces are directly castable are considered.
110
if ((feature.equalsIgnoreCase("+XPath"))
111             && (anyVersion || version.equals("3.0"))) {
112             try {
113                 Class JavaDoc xpathClass = ObjectFactory.findProviderClass(
114                     "org.apache.xpath.domapi.XPathEvaluatorImpl",
115                     ObjectFactory.findClassLoader(), true);
116                 
117                 // Check if the DOM XPath implementation implements
118
// the interface org.w3c.dom.XPathEvaluator
119
Class JavaDoc interfaces[] = xpathClass.getInterfaces();
120                 for (int i = 0; i < interfaces.length; i++) {
121                     if (interfaces[i].getName().equals(
122                         "org.w3c.dom.xpath.XPathEvaluator")) {
123                         return true;
124                     }
125                 }
126             } catch (Exception JavaDoc e) {
127                 return false;
128             }
129             return true;
130         }
131         if (feature.startsWith("+")) {
132             feature = feature.substring(1);
133         }
134         return (
135             feature.equalsIgnoreCase("Core")
136                 && (anyVersion
137                     || version.equals("1.0")
138                     || version.equals("2.0")
139                     || version.equals("3.0")))
140                     || (feature.equalsIgnoreCase("XML")
141                 && (anyVersion
142                     || version.equals("1.0")
143                     || version.equals("2.0")
144                     || version.equals("3.0")))
145                     || (feature.equalsIgnoreCase("LS")
146                 && (anyVersion || version.equals("3.0")));
147     } // hasFeature(String,String):boolean
148

149
150     /**
151      * Introduced in DOM Level 2. <p>
152      *
153      * Creates an empty DocumentType node.
154      *
155      * @param qualifiedName The qualified name of the document type to be created.
156      * @param publicID The document type public identifier.
157      * @param systemID The document type system identifier.
158      * @since WD-DOM-Level-2-19990923
159      */

160     public DocumentType JavaDoc createDocumentType( String JavaDoc qualifiedName,
161                                     String JavaDoc publicID, String JavaDoc systemID) {
162         // REVISIT: this might allow creation of invalid name for DOCTYPE
163
// xmlns prefix.
164
// also there is no way for a user to turn off error checking.
165
checkQName(qualifiedName);
166         return new DocumentTypeImpl(null, qualifiedName, publicID, systemID);
167     }
168
169     final void checkQName(String JavaDoc qname){
170         int index = qname.indexOf(':');
171         int lastIndex = qname.lastIndexOf(':');
172         int length = qname.length();
173
174         // it is an error for NCName to have more than one ':'
175
// check if it is valid QName [Namespace in XML production 6]
176
if (index == 0 || index == length - 1 || lastIndex != index) {
177             String JavaDoc msg =
178                 DOMMessageFormatter.formatMessage(
179                     DOMMessageFormatter.DOM_DOMAIN,
180                     "NAMESPACE_ERR",
181                     null);
182             throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR, msg);
183         }
184         int start = 0;
185         // Namespace in XML production [6]
186
if (index > 0) {
187             // check that prefix is NCName
188
if (!XMLChar.isNCNameStart(qname.charAt(start))) {
189                 String JavaDoc msg =
190                     DOMMessageFormatter.formatMessage(
191                         DOMMessageFormatter.DOM_DOMAIN,
192                         "INVALID_CHARACTER_ERR",
193                         null);
194                 throw new DOMException JavaDoc(DOMException.INVALID_CHARACTER_ERR, msg);
195             }
196             for (int i = 1; i < index; i++) {
197                 if (!XMLChar.isNCName(qname.charAt(i))) {
198                     String JavaDoc msg =
199                         DOMMessageFormatter.formatMessage(
200                             DOMMessageFormatter.DOM_DOMAIN,
201                             "INVALID_CHARACTER_ERR",
202                             null);
203                     throw new DOMException JavaDoc(
204                         DOMException.INVALID_CHARACTER_ERR,
205                         msg);
206                 }
207             }
208             start = index + 1;
209         }
210
211         // check local part
212
if (!XMLChar.isNCNameStart(qname.charAt(start))) {
213             // REVISIT: add qname parameter to the message
214
String JavaDoc msg =
215                 DOMMessageFormatter.formatMessage(
216                     DOMMessageFormatter.DOM_DOMAIN,
217                     "INVALID_CHARACTER_ERR",
218                     null);
219             throw new DOMException JavaDoc(DOMException.INVALID_CHARACTER_ERR, msg);
220         }
221         for (int i = start + 1; i < length; i++) {
222             if (!XMLChar.isNCName(qname.charAt(i))) {
223                 String JavaDoc msg =
224                     DOMMessageFormatter.formatMessage(
225                         DOMMessageFormatter.DOM_DOMAIN,
226                         "INVALID_CHARACTER_ERR",
227                         null);
228                 throw new DOMException JavaDoc(DOMException.INVALID_CHARACTER_ERR, msg);
229             }
230         }
231     }
232
233
234     /**
235      * Introduced in DOM Level 2. <p>
236      *
237      * Creates an XML Document object of the specified type with its document
238      * element.
239      *
240      * @param namespaceURI The namespace URI of the document
241      * element to create, or null.
242      * @param qualifiedName The qualified name of the document
243      * element to create.
244      * @param doctype The type of document to be created or null.<p>
245      *
246      * When doctype is not null, its
247      * Node.ownerDocument attribute is set to
248      * the document being created.
249      * @return Document A new Document object.
250      * @throws DOMException WRONG_DOCUMENT_ERR: Raised if doctype has
251      * already been used with a different document.
252      * @since WD-DOM-Level-2-19990923
253      */

254     public Document JavaDoc createDocument(
255         String JavaDoc namespaceURI,
256         String JavaDoc qualifiedName,
257         DocumentType JavaDoc doctype)
258         throws DOMException JavaDoc {
259         if (doctype != null && doctype.getOwnerDocument() != null) {
260             String JavaDoc msg =
261                 DOMMessageFormatter.formatMessage(
262                     DOMMessageFormatter.DOM_DOMAIN,
263                     "WRONG_DOCUMENT_ERR",
264                     null);
265             throw new DOMException JavaDoc(DOMException.WRONG_DOCUMENT_ERR, msg);
266         }
267         CoreDocumentImpl doc = new CoreDocumentImpl(doctype);
268         Element JavaDoc e = doc.createElementNS(namespaceURI, qualifiedName);
269         doc.appendChild(e);
270         return doc;
271     }
272
273     /**
274      * DOM Level 3 WD - Experimental.
275      */

276     public Object JavaDoc getFeature(String JavaDoc feature, String JavaDoc version) {
277         if (singleton.hasFeature(feature, version)) {
278             if ((feature.equalsIgnoreCase("+XPath"))) {
279                 try {
280                     Class JavaDoc xpathClass = ObjectFactory.findProviderClass(
281                         "org.apache.xpath.domapi.XPathEvaluatorImpl",
282                         ObjectFactory.findClassLoader(), true);
283                     
284                     // Check if the DOM XPath implementation implements
285
// the interface org.w3c.dom.XPathEvaluator
286
Class JavaDoc interfaces[] = xpathClass.getInterfaces();
287                     for (int i = 0; i < interfaces.length; i++) {
288                         if (interfaces[i].getName().equals(
289                             "org.w3c.dom.xpath.XPathEvaluator")) {
290                             return xpathClass.newInstance();
291                         }
292                     }
293                 } catch (Exception JavaDoc e) {
294                     return null;
295                 }
296             } else {
297                 return singleton;
298             }
299         }
300         return null;
301     }
302
303     // DOM L3 LS
304

305     /**
306      * DOM Level 3 LS CR - Experimental.
307      * Create a new <code>LSParser</code>. The newly constructed parser may
308      * then be configured by means of its <code>DOMConfiguration</code>
309      * object, and used to parse documents by means of its <code>parse</code>
310      * method.
311      * @param mode The <code>mode</code> argument is either
312      * <code>MODE_SYNCHRONOUS</code> or <code>MODE_ASYNCHRONOUS</code>, if
313      * <code>mode</code> is <code>MODE_SYNCHRONOUS</code> then the
314      * <code>LSParser</code> that is created will operate in synchronous
315      * mode, if it's <code>MODE_ASYNCHRONOUS</code> then the
316      * <code>LSParser</code> that is created will operate in asynchronous
317      * mode.
318      * @param schemaType An absolute URI representing the type of the schema
319      * language used during the load of a <code>Document</code> using the
320      * newly created <code>LSParser</code>. Note that no lexical checking
321      * is done on the absolute URI. In order to create a
322      * <code>LSParser</code> for any kind of schema types (i.e. the
323      * LSParser will be free to use any schema found), use the value
324      * <code>null</code>.
325      * <p ><b>Note:</b> For W3C XML Schema [<a HREF='http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/'>XML Schema Part 1</a>]
326      * , applications must use the value
327      * <code>"http://www.w3.org/2001/XMLSchema"</code>. For XML DTD [<a HREF='http://www.w3.org/TR/2000/REC-xml-20001006'>XML 1.0</a>],
328      * applications must use the value
329      * <code>"http://www.w3.org/TR/REC-xml"</code>. Other Schema languages
330      * are outside the scope of the W3C and therefore should recommend an
331      * absolute URI in order to use this method.
332      * @return The newly created <code>LSParser</code> object. This
333      * <code>LSParser</code> is either synchronous or asynchronous
334      * depending on the value of the <code>mode</code> argument.
335      * <p ><b>Note:</b> By default, the newly created <code>LSParser</code>
336      * does not contain a <code>DOMErrorHandler</code>, i.e. the value of
337      * the "<a HREF='http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030609/core.html#parameter-error-handler'>
338      * error-handler</a>" configuration parameter is <code>null</code>. However, implementations
339      * may provide a default error handler at creation time. In that case,
340      * the initial value of the <code>"error-handler"</code> configuration
341      * parameter on the new created <code>LSParser</code> contains a
342      * reference to the default error handler.
343      * @exception DOMException
344      * NOT_SUPPORTED_ERR: Raised if the requested mode or schema type is
345      * not supported.
346      */

347         public LSParser JavaDoc createLSParser(short mode, String JavaDoc schemaType)
348         throws DOMException JavaDoc {
349         if (mode != DOMImplementationLS.MODE_SYNCHRONOUS || (schemaType !=null &&
350            !"http://www.w3.org/2001/XMLSchema".equals(schemaType) &&
351             !"http://www.w3.org/TR/REC-xml".equals(schemaType))) {
352             String JavaDoc msg =
353                 DOMMessageFormatter.formatMessage(
354                     DOMMessageFormatter.DOM_DOMAIN,
355                     "NOT_SUPPORTED_ERR",
356                     null);
357             throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
358         }
359         if (schemaType != null
360             && schemaType.equals("http://www.w3.org/TR/REC-xml")) {
361             return new DOMParserImpl(
362                 "org.apache.xerces.parsers.DTDConfiguration",
363                 schemaType);
364         }
365         else {
366             // create default parser configuration validating against XMLSchemas
367
return new DOMParserImpl(
368                 "org.apache.xerces.parsers.XIncludeAwareParserConfiguration",
369                 schemaType);
370         }
371     }
372
373     /**
374      * DOM Level 3 LS CR - Experimental.
375          * Create a new <code>LSSerializer</code> object.
376          * @return The newly created <code>LSSerializer</code> object.
377          * <p ><b>Note:</b> By default, the newly created
378          * <code>LSSerializer</code> has no <code>DOMErrorHandler</code>,
379          * i.e. the value of the <code>"error-handler"</code> configuration
380          * parameter is <code>null</code>. However, implementations may
381          * provide a default error handler at creation time. In that case, the
382          * initial value of the <code>"error-handler"</code> configuration
383          * parameter on the new created <code>LSSerializer</code> contains a
384          * reference to the default error handler.
385      */

386     public LSSerializer JavaDoc createLSSerializer() {
387         return new DOMSerializerImpl();
388     }
389     /**
390      * DOM Level 3 LS CR - Experimental.
391          * Create a new empty input source.
392          * @return The newly created input object.
393      */

394     public LSInput JavaDoc createLSInput() {
395         return new DOMInputImpl();
396     }
397
398     //
399
// Protected methods
400
//
401
/** NON-DOM: retrieve validator. */
402     synchronized RevalidationHandler getValidator(String JavaDoc schemaType) {
403         // REVISIT: implement retrieving DTD validator
404
if (schemaType == XMLGrammarDescription.XML_SCHEMA) {
405             // create new validator - we should not attempt
406
// to restrict the number of validation handlers being
407
// requested
408
if(freeValidatorIndex < 0) {
409                 return (RevalidationHandler) (ObjectFactory
410                             .newInstance(
411                                 "org.apache.xerces.impl.xs.XMLSchemaValidator",
412                                 ObjectFactory.findClassLoader(),
413                                 true));
414             }
415             // return first available validator
416
RevalidationHandler val = validators[freeValidatorIndex];
417             validators[freeValidatorIndex--] = null;
418             return val;
419         }
420         else if(schemaType == XMLGrammarDescription.XML_DTD) {
421             if(freeDTDValidatorIndex < 0) {
422                 return (RevalidationHandler) (ObjectFactory
423                             .newInstance(
424                                 "org.apache.xerces.impl.dtd.XMLDTDValidator",
425                                 ObjectFactory.findClassLoader(),
426                                 true));
427             }
428             // return first available validator
429
RevalidationHandler val = dtdValidators[freeDTDValidatorIndex];
430             dtdValidators[freeDTDValidatorIndex--] = null;
431             return val;
432         }
433         return null;
434     }
435
436     /** NON-DOM: release validator */
437     synchronized void releaseValidator(String JavaDoc schemaType,
438                                          RevalidationHandler validator) {
439        // REVISIT: implement support for DTD validators as well
440
if(schemaType == XMLGrammarDescription.XML_SCHEMA) {
441            ++freeValidatorIndex;
442            if (validators.length == freeValidatorIndex ){
443                 // resize size of the validators
444
currentSize+=SIZE;
445                 RevalidationHandler newarray[] = new RevalidationHandler[currentSize];
446                 System.arraycopy(validators, 0, newarray, 0, validators.length);
447                 validators = newarray;
448            }
449            validators[freeValidatorIndex]=validator;
450        }
451        else if(schemaType == XMLGrammarDescription.XML_DTD) {
452            ++freeDTDValidatorIndex;
453            if (dtdValidators.length == freeDTDValidatorIndex ){
454                 // resize size of the validators
455
currentSize+=SIZE;
456                 RevalidationHandler newarray[] = new RevalidationHandler[currentSize];
457                 System.arraycopy(dtdValidators, 0, newarray, 0, dtdValidators.length);
458                 dtdValidators = newarray;
459            }
460            dtdValidators[freeDTDValidatorIndex]=validator;
461        }
462     }
463
464        /** NON-DOM: increment document/doctype counter */
465        protected synchronized int assignDocumentNumber() {
466             return ++docAndDoctypeCounter;
467        }
468        /** NON-DOM: increment document/doctype counter */
469        protected synchronized int assignDocTypeNumber() {
470             return ++docAndDoctypeCounter;
471        }
472
473     /* DOM Level 3 LS CR - Experimental.
474      *
475      * Create a new empty output destination object where
476      * <code>LSOutput.characterStream</code>,
477      * <code>LSOutput.byteStream</code>, <code>LSOutput.systemId</code>,
478      * <code>LSOutput.encoding</code> are null.
479
480      * @return The newly created output object.
481      */

482        public LSOutput JavaDoc createLSOutput() {
483            return new DOMOutputImpl();
484        }
485
486 } // class DOMImplementationImpl
487
Popular Tags