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 
17 package org.apache.harmony.xml.parsers;
18 
19 import java.util.HashMap;
20 import java.util.Map;
21 import javax.xml.parsers.ParserConfigurationException;
22 import javax.xml.parsers.SAXParser;
23 import javax.xml.parsers.SAXParserFactory;
24 import org.xml.sax.SAXNotRecognizedException;
25 
26 /**
27  * Provides a straightforward SAXParserFactory implementation based on
28  * Expat. The class is used internally only, thus only notable members
29  * that are not already in the abstract superclass are documented.
30  */
31 public class SAXParserFactoryImpl extends SAXParserFactory {
32 
33     private static final String NAMESPACES
34             = "http://xml.org/sax/features/namespaces";
35 
36     private static final String VALIDATION
37             = "http://xml.org/sax/features/validation";
38 
39     private Map<String, Boolean> features = new HashMap<String, Boolean>();
40 
41     @Override
getFeature(String name)42     public boolean getFeature(String name) throws SAXNotRecognizedException {
43         if (name == null) {
44             throw new NullPointerException("name == null");
45         }
46 
47         if (!name.startsWith("http://xml.org/sax/features/")) {
48             throw new SAXNotRecognizedException(name);
49         }
50 
51         return Boolean.TRUE.equals(features.get(name));
52     }
53 
54     @Override
isNamespaceAware()55     public boolean isNamespaceAware() {
56         try {
57             return getFeature(NAMESPACES);
58         } catch (SAXNotRecognizedException ex) {
59             throw new AssertionError(ex);
60         }
61     }
62 
63     @Override
isValidating()64     public boolean isValidating() {
65         try {
66             return getFeature(VALIDATION);
67         } catch (SAXNotRecognizedException ex) {
68             throw new AssertionError(ex);
69         }
70     }
71 
72     @Override
newSAXParser()73     public SAXParser newSAXParser() throws ParserConfigurationException {
74         if (isValidating()) {
75             throw new ParserConfigurationException(
76                     "No validating SAXParser implementation available");
77         }
78 
79         try {
80             return new SAXParserImpl(features);
81         } catch (Exception ex) {
82             throw new ParserConfigurationException(ex.toString());
83         }
84     }
85 
86     @Override
setFeature(String name, boolean value)87     public void setFeature(String name, boolean value) throws SAXNotRecognizedException {
88         if (name == null) {
89             throw new NullPointerException("name == null");
90         }
91 
92         if (!name.startsWith("http://xml.org/sax/features/")) {
93             throw new SAXNotRecognizedException(name);
94         }
95 
96         if (value) {
97             features.put(name, Boolean.TRUE);
98         } else {
99             // This is needed to disable features that are enabled by default.
100             features.put(name, Boolean.FALSE);
101         }
102     }
103 
104     @Override
setNamespaceAware(boolean value)105     public void setNamespaceAware(boolean value) {
106         try {
107             setFeature(NAMESPACES, value);
108         } catch (SAXNotRecognizedException ex) {
109             throw new AssertionError(ex);
110         }
111     }
112 
113     @Override
setValidating(boolean value)114     public void setValidating(boolean value) {
115         try {
116             setFeature(VALIDATION, value);
117         } catch (SAXNotRecognizedException ex) {
118             throw new AssertionError(ex);
119         }
120     }
121 }
122