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.parsers;
18
19import javax.xml.parsers.DocumentBuilder;
20import javax.xml.parsers.DocumentBuilderFactory;
21import javax.xml.parsers.ParserConfigurationException;
22
23/**
24 * Provides a straightforward DocumentBuilderFactory implementation based on
25 * XMLPull/KXML. The class is used internally only, thus only notable members
26 * that are not already in the abstract superclass are documented. Hope that's
27 * ok.
28 */
29public class DocumentBuilderFactoryImpl extends DocumentBuilderFactory {
30
31    private static final String NAMESPACES =
32            "http://xml.org/sax/features/namespaces";
33
34    private static final String VALIDATION =
35            "http://xml.org/sax/features/validation";
36
37    @Override
38    public Object getAttribute(String name) throws IllegalArgumentException {
39        throw new IllegalArgumentException(name);
40    }
41
42    @Override
43    public boolean getFeature(String name) throws ParserConfigurationException {
44        if (name == null) {
45            throw new NullPointerException("name == null");
46        }
47
48        if (NAMESPACES.equals(name)) {
49            return isNamespaceAware();
50        } else if (VALIDATION.equals(name)) {
51            return isValidating();
52        } else {
53            throw new ParserConfigurationException(name);
54        }
55    }
56
57    @Override
58    public DocumentBuilder newDocumentBuilder()
59            throws ParserConfigurationException {
60        if (isValidating()) {
61            throw new ParserConfigurationException(
62                    "No validating DocumentBuilder implementation available");
63        }
64
65        /**
66         * TODO If Android is going to support a different DocumentBuilder
67         * implementations, this should be wired here. If we wanted to
68         * allow different implementations, these could be distinguished by
69         * a special feature (like http://www.org.apache.harmony.com/xml/expat)
70         * or by throwing the full SPI monty at it.
71         */
72        DocumentBuilderImpl builder = new DocumentBuilderImpl();
73        builder.setCoalescing(isCoalescing());
74        builder.setIgnoreComments(isIgnoringComments());
75        builder.setIgnoreElementContentWhitespace(isIgnoringElementContentWhitespace());
76        builder.setNamespaceAware(isNamespaceAware());
77
78        // TODO What about expandEntityReferences?
79
80        return builder;
81    }
82
83    @Override
84    public void setAttribute(String name, Object value)
85            throws IllegalArgumentException {
86        throw new IllegalArgumentException(name);
87    }
88
89    @Override
90    public void setFeature(String name, boolean value)
91            throws ParserConfigurationException {
92        if (name == null) {
93            throw new NullPointerException("name == null");
94        }
95
96        if (NAMESPACES.equals(name)) {
97            setNamespaceAware(value);
98        } else if (VALIDATION.equals(name)) {
99            setValidating(value);
100        } else {
101            throw new ParserConfigurationException(name);
102        }
103    }
104
105}
106