1/*
2 * Copyright (C) 2007 The Android Open Source Project
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
17package org.apache.harmony.xml.dom;
18
19import org.w3c.dom.DOMException;
20import org.w3c.dom.DOMImplementation;
21import org.w3c.dom.Document;
22import org.w3c.dom.DocumentType;
23
24/**
25 * Provides a straightforward implementation of the corresponding W3C DOM
26 * interface. The class is used internally only, thus only notable members that
27 * are not in the original interface are documented (the W3C docs are quite
28 * extensive). Hope that's ok.
29 * <p>
30 * Some of the fields may have package visibility, so other classes belonging to
31 * the DOM implementation can easily access them while maintaining the DOM tree
32 * structure.
33 */
34public final class DOMImplementationImpl implements DOMImplementation {
35
36    // Singleton instance.
37    private static DOMImplementationImpl instance;
38
39    DOMImplementationImpl() {
40    }
41
42    public Document createDocument(String namespaceURI, String qualifiedName,
43            DocumentType doctype) throws DOMException {
44        return new DocumentImpl(this, namespaceURI, qualifiedName, doctype, null);
45    }
46
47    public DocumentType createDocumentType(String qualifiedName,
48            String publicId, String systemId) throws DOMException {
49        return new DocumentTypeImpl(null, qualifiedName, publicId, systemId);
50    }
51
52    public boolean hasFeature(String feature, String version) {
53        boolean anyVersion = version == null || version.length() == 0;
54        if (feature.startsWith("+")) {
55            feature = feature.substring(1);
56        }
57
58        // TODO: fully implement these APIs:
59        // "LS" (org.w3c.dom.ls) versions "3.0"
60        // "ElementTraversal" (org.w3c.dom.traversal) versions "1.0"
61
62        if (feature.equalsIgnoreCase("Core")) {
63            return anyVersion || version.equals("1.0") || version.equals("2.0") || version.equals("3.0");
64        } else if (feature.equalsIgnoreCase("XML")) {
65            return anyVersion || version.equals("1.0") || version.equals("2.0") || version.equals("3.0");
66        } else if (feature.equalsIgnoreCase("XMLVersion")) {
67            return anyVersion || version.equals("1.0") || version.equals("1.1");
68        } else {
69            return false;
70        }
71    }
72
73    /**
74     * Requests the singleton instance of the class. Creates it first, if
75     * necessary.
76     *
77     * @return The singleton Android DOMImplementationImpl instance.
78     */
79    public static DOMImplementationImpl getInstance() {
80        if (instance == null) {
81            instance = new DOMImplementationImpl();
82        }
83
84        return instance;
85    }
86
87    public Object getFeature(String feature, String version) {
88        return hasFeature(feature, version) ? this : null;
89    }
90}
91