DOMImplementationImpl.java revision 793:1d9850c1b35c
1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Copyright 1999-2002,2004 The Apache Software Foundation.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 *      http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21package com.sun.org.apache.xerces.internal.dom;
22
23import org.w3c.dom.DOMException;
24import org.w3c.dom.DOMImplementation;
25import org.w3c.dom.Document;
26import org.w3c.dom.DocumentType;
27import org.w3c.dom.Element;
28
29
30
31/**
32 * The DOMImplementation class is description of a particular
33 * implementation of the Document Object Model. As such its data is
34 * static, shared by all instances of this implementation.
35 * <P>
36 * The DOM API requires that it be a real object rather than static
37 * methods. However, there's nothing that says it can't be a singleton,
38 * so that's how I've implemented it.
39 *
40 * @xerces.internal
41 *
42 * @since  PR-DOM-Level-1-19980818.
43 */
44public class DOMImplementationImpl extends CoreDOMImplementationImpl
45    implements DOMImplementation {
46
47    //
48    // Data
49    //
50
51    // static
52
53    /** Dom implementation singleton. */
54    static DOMImplementationImpl singleton = new DOMImplementationImpl();
55
56
57    //
58    // Public methods
59    //
60
61    /** NON-DOM: Obtain and return the single shared object */
62    public static DOMImplementation getDOMImplementation() {
63        return singleton;
64    }
65
66    //
67    // DOMImplementation methods
68    //
69
70    /**
71     * Test if the DOM implementation supports a specific "feature" --
72     * currently meaning language and level thereof.
73     *
74     * @param feature      The package name of the feature to test.
75     * In Level 1, supported values are "HTML" and "XML" (case-insensitive).
76     * At this writing, com.sun.org.apache.xerces.internal.dom supports only XML.
77     *
78     * @param version      The version number of the feature being tested.
79     * This is interpreted as "Version of the DOM API supported for the
80     * specified Feature", and in Level 1 should be "1.0"
81     *
82     * @return    true iff this implementation is compatable with the
83     * specified feature and version.
84     */
85    public boolean hasFeature(String feature, String version) {
86        if (feature == null || feature.length() == 0) {
87            return false;
88        }
89
90        boolean result = super.hasFeature(feature, version);
91        if (!result) {
92            boolean anyVersion = version == null || version.length() == 0;
93            if (feature.startsWith("+")) {
94                feature = feature.substring(1);
95            }
96            return (
97                (feature.equalsIgnoreCase("Events")
98                    && (anyVersion || version.equals("2.0")))
99                    || (feature.equalsIgnoreCase("MutationEvents")
100                        && (anyVersion || version.equals("2.0")))
101                    || (feature.equalsIgnoreCase("Traversal")
102                        && (anyVersion || version.equals("2.0")))
103                    || (feature.equalsIgnoreCase("Range")
104                        && (anyVersion || version.equals("2.0")))
105                    || (feature.equalsIgnoreCase("MutationEvents")
106                        && (anyVersion || version.equals("2.0"))));
107        }
108        return result;
109    } // hasFeature(String,String):boolean
110
111
112
113    /**
114     * Introduced in DOM Level 2. <p>
115     *
116     * Creates an XML Document object of the specified type with its document
117     * element.
118     *
119     * @param namespaceURI     The namespace URI of the document
120     *                         element to create, or null.
121     * @param qualifiedName    The qualified name of the document
122     *                         element to create.
123     * @param doctype          The type of document to be created or null.<p>
124     *
125     *                         When doctype is not null, its
126     *                         Node.ownerDocument attribute is set to
127     *                         the document being created.
128     * @return Document        A new Document object.
129     * @throws DOMException    WRONG_DOCUMENT_ERR: Raised if doctype has
130     *                         already been used with a different document.
131     * @since WD-DOM-Level-2-19990923
132     */
133    public Document           createDocument(String namespaceURI,
134                                             String qualifiedName,
135                                             DocumentType doctype)
136                                             throws DOMException
137    {
138        if(namespaceURI == null && qualifiedName == null && doctype == null){
139        //if namespaceURI, qualifiedName and doctype are null, returned document is empty with
140        //no document element
141            return new DocumentImpl();
142        }
143        else if (doctype != null && doctype.getOwnerDocument() != null) {
144            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null);
145            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, msg);
146        }
147        DocumentImpl doc = new DocumentImpl(doctype);
148        Element e = doc.createElementNS( namespaceURI, qualifiedName);
149        doc.appendChild(e);
150        return doc;
151    }
152
153
154} // class DOMImplementationImpl
155