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