View Javadoc

1   /*   Copyright 2004 BEA Systems, Inc.
2    *
3    *   Licensed under the Apache License, Version 2.0 (the "License");
4    *   you may not use this file except in compliance with the License.
5    *   You may obtain a copy of the License at
6    *
7    *       http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *   Unless required by applicable law or agreed to in writing, software
10   *   distributed under the License is distributed on an "AS IS" BASIS,
11   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *   See the License for the specific language governing permissions and
13   *   limitations under the License.
14   */
15  
16  package com.bea.xml.stream;
17  
18  import java.util.Enumeration;
19  import java.util.Hashtable;
20  import java.util.HashSet;
21  
22  import javax.xml.stream.util.XMLEventAllocator;
23  import javax.xml.stream.EventFilter;
24  import javax.xml.stream.StreamFilter;
25  import javax.xml.stream.XMLReporter;
26  import javax.xml.stream.XMLResolver;
27  import javax.xml.stream.XMLInputFactory;
28  import javax.xml.stream.XMLOutputFactory;
29  
30  public class ConfigurationContextBase
31  
32  {
33    private static HashSet supportedFeatures;
34  
35    private static String EVENT_FILTER = "RI_EVENT_FILTER";
36    private static String STREAM_FILTER = "RI_STREAM_FILTER";
37    private static String NOTATIONS = "javax.xml.stream.notations";
38    private static String ENTITIES = "javax.xml.stream.entities";
39  
40    static {
41      supportedFeatures = new HashSet();
42      supportedFeatures.add(XMLInputFactory.IS_VALIDATING);
43      supportedFeatures.add(XMLInputFactory.IS_COALESCING);
44      supportedFeatures.add(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES);
45      supportedFeatures.add(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES);
46      supportedFeatures.add(XMLOutputFactory.IS_REPAIRING_NAMESPACES);
47      supportedFeatures.add(XMLInputFactory.IS_NAMESPACE_AWARE);
48      supportedFeatures.add(XMLInputFactory.SUPPORT_DTD);
49      supportedFeatures.add(XMLInputFactory.REPORTER);
50      supportedFeatures.add(XMLInputFactory.RESOLVER);
51      supportedFeatures.add(XMLInputFactory.ALLOCATOR);
52      supportedFeatures.add(NOTATIONS);
53      supportedFeatures.add(ENTITIES);
54  
55    };
56  
57    private Hashtable features = new Hashtable();
58    public ConfigurationContextBase() {
59      features.put(XMLInputFactory.IS_VALIDATING,
60                   Boolean.FALSE);
61      features.put(XMLInputFactory.IS_COALESCING,
62                   Boolean.FALSE);
63      features.put(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,
64                   Boolean.TRUE);
65      features.put(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
66                   Boolean.FALSE);
67      features.put(XMLInputFactory.IS_NAMESPACE_AWARE,
68                   Boolean.TRUE);
69      features.put(XMLInputFactory.SUPPORT_DTD,
70                   Boolean.FALSE);
71      features.put(XMLOutputFactory.IS_REPAIRING_NAMESPACES,
72                   Boolean.FALSE);
73  
74    }
75  
76    public void setEventAllocator(XMLEventAllocator a) {
77      features.put(XMLInputFactory.ALLOCATOR,a);
78    }
79    public XMLEventAllocator getEventAllocator() {
80      return (XMLEventAllocator) features.get(XMLInputFactory.ALLOCATOR);
81    }
82  
83    public void setProperty(String name, Object feature) {
84      check(name);
85      if (name.equals(XMLInputFactory.IS_VALIDATING) &&
86              Boolean.TRUE.equals(feature)){
87          throw new IllegalArgumentException("This implementation does not " +
88                  "support validation");
89      } else if (name.equals(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES) &&
90              Boolean.TRUE.equals(feature)) {
91          throw new IllegalArgumentException("This implementation does not " +
92                  "resolve external entities ");
93      }
94      features.put(name,feature);
95    }
96  
97    public void check(String name) {
98      if (!supportedFeatures.contains(name))
99        throw new IllegalArgumentException("Unable to access unsupported "+
100                                          "property "+name);
101   }
102 
103   public  Object getProperty(String name) {
104     check(name);
105     return features.get(name);
106   }
107 
108   public void setXMLReporter(XMLReporter r) {
109     features.put(XMLInputFactory.REPORTER,r);
110   }
111 
112   public  XMLReporter getXMLReporter() {
113     return (XMLReporter) features.get(XMLInputFactory.REPORTER);
114   }
115 
116   public void setXMLResolver(XMLResolver r) {
117     features.put(XMLInputFactory.RESOLVER,r);
118   }
119   public XMLResolver getXMLResolver() {
120     return (XMLResolver) features.get(XMLInputFactory.RESOLVER);
121   }
122 
123   public boolean getBool(String name) {
124     check(name);
125     Boolean val = (Boolean) features.get(name);
126     return val.booleanValue();
127   }
128 
129   public void setBool(String name, boolean val) {
130     check(name);
131     features.put(name,new Boolean(val));
132   }
133   public void setCoalescing(boolean val) {
134     setBool(XMLInputFactory.IS_COALESCING,val);
135   }
136   public boolean isCoalescing() {
137     return getBool(XMLInputFactory.IS_COALESCING);
138   }
139 
140 
141   public void setValidating(boolean val) {
142     setBool(XMLInputFactory.IS_VALIDATING,val);
143   }
144 
145   public boolean isValidating() {
146     return getBool(XMLInputFactory.IS_VALIDATING);
147   }
148 
149   public void setReplacingEntities(boolean val) {
150     setBool(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,val);
151   }
152 
153   public boolean isReplacingEntities() {
154     return getBool(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES);
155   }
156 
157 
158   public void setSupportExternalEntities(boolean val) {
159     setBool(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,val);
160   }
161 
162   public boolean isSupportingExternalEntities() {
163     return getBool(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES);
164   }
165 
166   public void setPrefixDefaulting(boolean val) {
167     setBool(XMLOutputFactory.IS_REPAIRING_NAMESPACES,val);
168   }
169 
170   public boolean isPrefixDefaulting() {
171     return getBool(XMLOutputFactory.IS_REPAIRING_NAMESPACES);
172   }
173 
174   public void setNamespaceAware(boolean val) {
175     setBool(XMLInputFactory.IS_NAMESPACE_AWARE,val);
176   }
177 
178   public boolean isNamespaceAware() {
179     return getBool(XMLInputFactory.IS_NAMESPACE_AWARE);
180   }
181 
182 
183   public String getVersion() {
184     return "1.0";
185   }
186 
187   public Enumeration getProperties() {
188     return features.keys();
189   }
190 
191   public boolean isPropertySupported(String name) {
192     return supportedFeatures.contains(name);
193   }
194 
195 }