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 java.util.Collections;
20import java.util.HashMap;
21import java.util.Map;
22import javax.xml.parsers.SAXParser;
23import org.apache.harmony.xml.ExpatReader;
24import org.xml.sax.Parser;
25import org.xml.sax.SAXException;
26import org.xml.sax.SAXNotRecognizedException;
27import org.xml.sax.SAXNotSupportedException;
28import org.xml.sax.XMLReader;
29import org.xml.sax.helpers.XMLReaderAdapter;
30
31/**
32 * A SAX parser based on Expat.
33 */
34final class SAXParserImpl extends SAXParser {
35
36    private Map<String, Boolean> initialFeatures;
37    private XMLReader reader;
38    private Parser parser;
39
40    SAXParserImpl(Map<String, Boolean> initialFeatures)
41            throws SAXNotRecognizedException, SAXNotSupportedException {
42        this.initialFeatures = initialFeatures.isEmpty()
43                ? Collections.<String, Boolean>emptyMap()
44                : new HashMap<String, Boolean>(initialFeatures);
45        resetInternal();
46    }
47
48    private void resetInternal()
49            throws SAXNotSupportedException, SAXNotRecognizedException {
50        reader = new ExpatReader();
51        for (Map.Entry<String,Boolean> entry : initialFeatures.entrySet()) {
52            reader.setFeature(entry.getKey(), entry.getValue());
53        }
54    }
55
56    @Override public void reset() {
57        /*
58         * The exceptions are impossible. If any features are unrecognized or
59         * unsupported, construction of this instance would have failed.
60         */
61        try {
62            resetInternal();
63        } catch (SAXNotRecognizedException e) {
64            throw new AssertionError();
65        } catch (SAXNotSupportedException e) {
66            throw new AssertionError();
67        }
68    }
69
70    @Override
71    public Parser getParser() {
72        if (parser == null) {
73            parser = new XMLReaderAdapter(reader);
74        }
75
76        return parser;
77    }
78
79    @Override
80    public Object getProperty(String name) throws SAXNotRecognizedException,
81            SAXNotSupportedException {
82        return reader.getProperty(name);
83    }
84
85    @Override
86    public XMLReader getXMLReader() {
87        return reader;
88    }
89
90    @Override
91    public boolean isNamespaceAware() {
92        try {
93            return reader.getFeature("http://xml.org/sax/features/namespaces");
94        } catch (SAXException ex) {
95            return false;
96        }
97    }
98
99    @Override
100    public boolean isValidating() {
101        return false;
102    }
103
104    @Override
105    public void setProperty(String name, Object value)
106            throws SAXNotRecognizedException, SAXNotSupportedException {
107        reader.setProperty(name, value);
108    }
109}
110