All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.harmony.xml.parsers.SAXParserImpl Maven / Gradle / Ivy

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.harmony.xml.parsers;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.SAXParser;
import org.apache.harmony.xml.ExpatReader;
import org.xml.sax.Parser;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderAdapter;

/**
 * A SAX parser based on Expat.
 */
final class SAXParserImpl extends SAXParser {

    private Map initialFeatures;
    private XMLReader reader;
    private Parser parser;

    SAXParserImpl(Map initialFeatures)
            throws SAXNotRecognizedException, SAXNotSupportedException {
        this.initialFeatures = initialFeatures.isEmpty()
                ? Collections.emptyMap()
                : new HashMap(initialFeatures);
        resetInternal();
    }

    private void resetInternal()
            throws SAXNotSupportedException, SAXNotRecognizedException {
        reader = new ExpatReader();
        for (Map.Entry entry : initialFeatures.entrySet()) {
            reader.setFeature(entry.getKey(), entry.getValue());
        }
    }

    @Override public void reset() {
        /*
         * The exceptions are impossible. If any features are unrecognized or
         * unsupported, construction of this instance would have failed.
         */
        try {
            resetInternal();
        } catch (SAXNotRecognizedException e) {
            throw new AssertionError();
        } catch (SAXNotSupportedException e) {
            throw new AssertionError();
        }
    }

    @Override
    public Parser getParser() {
        if (parser == null) {
            parser = new XMLReaderAdapter(reader);
        }

        return parser;
    }

    @Override
    public Object getProperty(String name) throws SAXNotRecognizedException,
            SAXNotSupportedException {
        return reader.getProperty(name);
    }

    @Override
    public XMLReader getXMLReader() {
        return reader;
    }

    @Override
    public boolean isNamespaceAware() {
        try {
            return reader.getFeature("http://xml.org/sax/features/namespaces");
        } catch (SAXException ex) {
            return false;
        }
    }

    @Override
    public boolean isValidating() {
        return false;
    }

    @Override
    public void setProperty(String name, Object value)
            throws SAXNotRecognizedException, SAXNotSupportedException {
        reader.setProperty(name, value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy