    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *     http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.jxpath.xml;
    019
    020import java.io.InputStream;
    021
    022/**
    023 * The abstract superclass of XML parsers that produce DOM Documents. The features have the same defaults as {@link javax.xml.parsers.DocumentBuilderFactory}.
    024 */
    025public abstract class XMLParser2 implements XMLParser {
    026
    027    private boolean validating;
    028    private boolean namespaceAware = true;
    029    private boolean whitespace;
    030    private boolean expandEntityRef = true;
    031    private boolean ignoreComments;
    032    private boolean coalescing;
    033
    034
    035    /**
    036     * Constructs a new instance for subclasses.
    037     */
    038    public XMLParser2() {
    039        // empty
    040    }
    041
    042    /**
    043     * Tests whether the underlying parser is coalescing.
    044     *
    045     * @return boolean
    046     * @see javax.xml.parsers.DocumentBuilderFactory#isCoalescing()
    047     */
    048    public boolean isCoalescing() {
    049        return coalescing;
    050    }
    051
    052    /**
    053     * Tests whether the underlying parser expands entity references.
    054     *
    055     * @return boolean
    056     * @see javax.xml.parsers.DocumentBuilderFactory#isExpandEntityReferences()
    057     */
    058    public boolean isExpandEntityReferences() {
    059        return expandEntityRef;
    060    }
    061
    062    /**
    063     * Tests whether the underlying parser ignores comments.
    064     *
    065     * @return boolean
    066     * @see javax.xml.parsers.DocumentBuilderFactory#isIgnoringComments()
    067     */
    068    public boolean isIgnoringComments() {
    069        return ignoreComments;
    070    }
    071
    072    /**
    073     * Tests whether the underlying parser is ignoring whitespace.
    074     *
    075     * @return boolean
    076     * @see javax.xml.parsers.DocumentBuilderFactory#isIgnoringElementContentWhitespace()
    077     */
    078    public boolean isIgnoringElementContentWhitespace() {
    079        return whitespace;
    080    }
    081
    082    /**
    083     * Tests whether the underlying parser is ns-aware.
    084     *
    085     * @return boolean
    086     * @see javax.xml.parsers.DocumentBuilderFactory#isNamespaceAware()
    087     */
    088    public boolean isNamespaceAware() {
    089        return namespaceAware;
    090    }
    091
    092    /**
    093     * Tests whether the underlying parser is validating.
    094     *
    095     * @return boolean
    096     * @see javax.xml.parsers.DocumentBuilderFactory#isValidating()
    097     */
    098    public boolean isValidating() {
    099        return validating;
    100    }
    101
    102    @Override
    103    public abstract Object parseXML(InputStream stream);
    104
    105    /**
    106     * Sets whether the underlying parser is coalescing.
    107     *
    108     * @param coalescing flag
    109     * @see javax.xml.parsers.DocumentBuilderFactory#setCoalescing(boolean)
    110     */
    111    public void setCoalescing(final boolean coalescing) {
    112        this.coalescing = coalescing;
    113    }
    114
    115    /**
    116     * Sets whether the underlying parser expands entity references.
    117     *
    118     * @param expandEntityRef flag
    119     * @see javax.xml.parsers.DocumentBuilderFactory#setExpandEntityReferences(boolean)
    120     */
    121    public void setExpandEntityReferences(final boolean expandEntityRef) {
    122        this.expandEntityRef = expandEntityRef;
    123    }
    124
    125    /**
    126     * Sets whether the underlying parser ignores comments.
    127     *
    128     * @param ignoreComments flag
    129     * @see javax.xml.parsers.DocumentBuilderFactory#setIgnoringComments(boolean)
    130     */
    131    public void setIgnoringComments(final boolean ignoreComments) {
    132        this.ignoreComments = ignoreComments;
    133    }
    134
    135    /**
    136     * Sets whether the underlying parser is ignoring whitespace.
    137     *
    138     * @param whitespace flag
    139     * @see javax.xml.parsers.DocumentBuilderFactory#setIgnoringElementContentWhitespace(boolean)
    140     */
    141    public void setIgnoringElementContentWhitespace(final boolean whitespace) {
    142        this.whitespace = whitespace;
    143    }
    144
    145    /**
    146     * Sets whether the underlying parser is ns-aware.
    147     *
    148     * @param namespaceAware flag
    149     * @see javax.xml.parsers.DocumentBuilderFactory#setNamespaceAware(boolean)
    150     */
    151    public void setNamespaceAware(final boolean namespaceAware) {
    152        this.namespaceAware = namespaceAware;
    153    }
    154
    155    /**
    156     * Sets whether the underlying parser should be validating.
    157     *
    158     * @param validating flag
    159     * @see javax.xml.parsers.DocumentBuilderFactory#setValidating(boolean)
    160     */
    161    public void setValidating(final boolean validating) {
    162        this.validating = validating;
    163    }
    164}