View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jxpath.xml;
19  
20  import java.io.InputStream;
21  
22  /**
23   * The abstract superclass of XML parsers that produce DOM Documents. The features have the same defaults as {@link javax.xml.parsers.DocumentBuilderFactory}.
24   */
25  public abstract class XMLParser2 implements XMLParser {
26  
27      private boolean validating;
28      private boolean namespaceAware = true;
29      private boolean whitespace;
30      private boolean expandEntityRef = true;
31      private boolean ignoreComments;
32      private boolean coalescing;
33  
34  
35      /**
36       * Constructs a new instance for subclasses.
37       */
38      public XMLParser2() {
39          // empty
40      }
41  
42      /**
43       * Tests whether the underlying parser is coalescing.
44       *
45       * @return boolean
46       * @see javax.xml.parsers.DocumentBuilderFactory#isCoalescing()
47       */
48      public boolean isCoalescing() {
49          return coalescing;
50      }
51  
52      /**
53       * Tests whether the underlying parser expands entity references.
54       *
55       * @return boolean
56       * @see javax.xml.parsers.DocumentBuilderFactory#isExpandEntityReferences()
57       */
58      public boolean isExpandEntityReferences() {
59          return expandEntityRef;
60      }
61  
62      /**
63       * Tests whether the underlying parser ignores comments.
64       *
65       * @return boolean
66       * @see javax.xml.parsers.DocumentBuilderFactory#isIgnoringComments()
67       */
68      public boolean isIgnoringComments() {
69          return ignoreComments;
70      }
71  
72      /**
73       * Tests whether the underlying parser is ignoring whitespace.
74       *
75       * @return boolean
76       * @see javax.xml.parsers.DocumentBuilderFactory#isIgnoringElementContentWhitespace()
77       */
78      public boolean isIgnoringElementContentWhitespace() {
79          return whitespace;
80      }
81  
82      /**
83       * Tests whether the underlying parser is ns-aware.
84       *
85       * @return boolean
86       * @see javax.xml.parsers.DocumentBuilderFactory#isNamespaceAware()
87       */
88      public boolean isNamespaceAware() {
89          return namespaceAware;
90      }
91  
92      /**
93       * Tests whether the underlying parser is validating.
94       *
95       * @return boolean
96       * @see javax.xml.parsers.DocumentBuilderFactory#isValidating()
97       */
98      public boolean isValidating() {
99          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 }