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 org.apache.harmony.xml.ExpatReader;
20
21import java.util.Map;
22
23import javax.xml.parsers.SAXParser;
24
25import org.xml.sax.Parser;
26import org.xml.sax.SAXException;
27import org.xml.sax.SAXNotRecognizedException;
28import org.xml.sax.SAXNotSupportedException;
29import org.xml.sax.XMLReader;
30import org.xml.sax.helpers.XMLReaderAdapter;
31
32/**
33 * Provides a straightforward SAXParser implementation based on ExpatReader.
34 * The class is used internally only, thus only notable members that are not
35 * already in the abstract superclass are documented. Hope that's ok.
36 */
37class SAXParserImpl extends SAXParser {
38
39    private XMLReader reader;
40
41    private Parser parser;
42
43    SAXParserImpl(Map<String, Boolean> features)
44            throws SAXNotRecognizedException, SAXNotSupportedException {
45        reader = new ExpatReader();
46
47        for (Map.Entry<String,Boolean> entry : features.entrySet()) {
48            reader.setFeature(entry.getKey(), entry.getValue());
49        }
50    }
51
52    @Override
53    public Parser getParser() {
54        if (parser == null) {
55            parser = new XMLReaderAdapter(reader);
56        }
57
58        return parser;
59    }
60
61    @Override
62    public Object getProperty(String name) throws SAXNotRecognizedException,
63            SAXNotSupportedException {
64        return reader.getProperty(name);
65    }
66
67    @Override
68    public XMLReader getXMLReader() {
69        return reader;
70    }
71
72    @Override
73    public boolean isNamespaceAware() {
74        try {
75            return reader.getFeature("http://xml.org/sax/features/namespaces");
76        } catch (SAXException ex) {
77            return false;
78        }
79    }
80
81    @Override
82    public boolean isValidating() {
83        return false;
84    }
85
86    @Override
87    public void setProperty(String name, Object value)
88            throws SAXNotRecognizedException, SAXNotSupportedException {
89        reader.setProperty(name, value);
90    }
91}
92