Merge from mainline
[official-gcc.git] / libjava / classpath / gnu / xml / stream / XMLInputFactoryImpl.java
blob164774d8236ee327b520aad80054b20ef17fa159
1 /* XMLInputFactoryImpl.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.xml.stream;
40 import java.io.File;
41 import java.io.FileInputStream;
42 import java.io.FileNotFoundException;
43 import java.io.InputStream;
44 import java.io.IOException;
45 import java.io.Reader;
46 import java.net.MalformedURLException;
47 import java.net.URL;
49 import javax.xml.transform.Source;
50 import javax.xml.transform.stream.StreamSource;
51 import javax.xml.stream.EventFilter;
52 import javax.xml.stream.StreamFilter;
53 import javax.xml.stream.XMLEventReader;
54 import javax.xml.stream.XMLReporter;
55 import javax.xml.stream.XMLResolver;
56 import javax.xml.stream.XMLStreamException;
57 import javax.xml.stream.XMLStreamReader;
58 import javax.xml.stream.XMLInputFactory;
59 import javax.xml.stream.util.XMLEventAllocator;
61 /**
62 * Factory for creating parsers from various kinds of XML source.
64 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
66 public class XMLInputFactoryImpl
67 extends XMLInputFactory
70 protected XMLResolver resolver;
71 protected XMLReporter reporter;
72 protected XMLEventAllocator allocator;
74 protected boolean validating;
75 protected boolean namespaceAware = true;
76 protected boolean coalescing;
77 protected boolean replacingEntityReferences = true;
78 protected boolean externalEntities = true;
79 protected boolean supportDTD = true;
80 protected boolean xIncludeAware = false;
81 protected boolean baseAware = true;
82 protected boolean stringInterning = true;
84 public XMLInputFactoryImpl()
86 allocator = new XMLEventAllocatorImpl();
89 public XMLStreamReader createXMLStreamReader(Reader reader)
90 throws XMLStreamException
93 return new XMLStreamReaderImpl(reader, null, null,
94 resolver, reporter,
95 validating, namespaceAware,
96 coalescing, replacingEntityReferences,
97 externalEntities, supportDTD);
99 XMLParser ret = new XMLParser(reader, null,
100 validating,
101 namespaceAware,
102 coalescing,
103 replacingEntityReferences,
104 externalEntities,
105 supportDTD,
106 baseAware,
107 stringInterning,
108 reporter,
109 resolver);
110 if (xIncludeAware)
111 return new XIncludeFilter(ret, null, namespaceAware, validating,
112 replacingEntityReferences);
113 return ret;
116 public XMLStreamReader createXMLStreamReader(Source source)
117 throws XMLStreamException
119 String systemId = source.getSystemId();
120 InputStream in = getInputStream(source);
121 /*return new XMLStreamReaderImpl(in, null, systemId,
122 resolver, reporter,
123 validating, namespaceAware,
124 coalescing, replacingEntityReferences,
125 externalEntities, supportDTD);*/
126 XMLParser ret = new XMLParser(in, systemId,
127 validating,
128 namespaceAware,
129 coalescing,
130 replacingEntityReferences,
131 externalEntities,
132 supportDTD,
133 baseAware,
134 stringInterning,
135 reporter,
136 resolver);
137 if (xIncludeAware)
138 return new XIncludeFilter(ret, systemId, namespaceAware, validating,
139 replacingEntityReferences);
140 return ret;
143 public XMLStreamReader createXMLStreamReader(InputStream in)
144 throws XMLStreamException
146 /*return new XMLStreamReaderImpl(in, null, null,
147 resolver, reporter,
148 validating, namespaceAware,
149 coalescing, replacingEntityReferences,
150 externalEntities, supportDTD);*/
151 XMLParser ret = new XMLParser(in, null,
152 validating,
153 namespaceAware,
154 coalescing,
155 replacingEntityReferences,
156 externalEntities,
157 supportDTD,
158 baseAware,
159 stringInterning,
160 reporter,
161 resolver);
162 if (xIncludeAware)
163 return new XIncludeFilter(ret, null, namespaceAware, validating,
164 replacingEntityReferences);
165 return ret;
168 public XMLStreamReader createXMLStreamReader(InputStream in, String encoding)
169 throws XMLStreamException
171 return createXMLStreamReader(in);
174 public XMLEventReader createXMLEventReader(Reader reader)
175 throws XMLStreamException
177 XMLStreamReader sr = createXMLStreamReader(reader);
178 return new XMLEventReaderImpl(sr, allocator, null);
181 public XMLEventReader createXMLEventReader(XMLStreamReader reader)
182 throws XMLStreamException
184 return new XMLEventReaderImpl(reader, allocator, null);
187 public XMLEventReader createXMLEventReader(Source source)
188 throws XMLStreamException
190 XMLStreamReader sr = createXMLStreamReader(source);
191 return new XMLEventReaderImpl(sr, allocator, null);
194 public XMLEventReader createXMLEventReader(InputStream in)
195 throws XMLStreamException
197 XMLStreamReader sr = createXMLStreamReader(in);
198 return new XMLEventReaderImpl(sr, allocator, null);
201 public XMLEventReader createXMLEventReader(InputStream in, String encoding)
202 throws XMLStreamException
204 XMLStreamReader sr = createXMLStreamReader(in, encoding);
205 return new XMLEventReaderImpl(sr, allocator, null);
208 public XMLStreamReader createFilteredReader(XMLStreamReader reader,
209 StreamFilter filter)
210 throws XMLStreamException
212 return new FilteredStreamReader(reader, filter);
215 public XMLEventReader createFilteredReader(XMLEventReader reader,
216 EventFilter filter)
217 throws XMLStreamException
219 return new FilteredEventReader(reader, filter);
222 public XMLResolver getXMLResolver()
224 return resolver;
227 public void setXMLResolver(XMLResolver resolver)
229 this.resolver = resolver;
232 public XMLReporter getXMLReporter()
234 return reporter;
237 public void setXMLReporter(XMLReporter reporter)
239 this.reporter = reporter;
242 public void setProperty(String name, Object value)
243 throws IllegalArgumentException
245 if (name.equals(IS_NAMESPACE_AWARE))
246 namespaceAware = ((Boolean) value).booleanValue();
247 else if (name.equals(IS_VALIDATING))
248 validating = ((Boolean) value).booleanValue();
249 else if (name.equals(IS_COALESCING))
250 coalescing = ((Boolean) value).booleanValue();
251 else if (name.equals(IS_REPLACING_ENTITY_REFERENCES))
252 replacingEntityReferences = ((Boolean) value).booleanValue();
253 else if (name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES))
254 externalEntities = ((Boolean) value).booleanValue();
255 else if (name.equals(SUPPORT_DTD))
256 supportDTD = ((Boolean) value).booleanValue();
257 else if (name.equals(REPORTER))
258 reporter = (XMLReporter) value;
259 else if (name.equals(RESOLVER))
260 resolver = (XMLResolver) value;
261 else if (name.equals(ALLOCATOR))
262 allocator = (XMLEventAllocator) value;
263 else if (name.equals("gnu.xml.stream.stringInterning"))
264 stringInterning = ((Boolean) value).booleanValue();
265 else if (name.equals("gnu.xml.stream.baseAware"))
266 baseAware = ((Boolean) value).booleanValue();
267 else if (name.equals("gnu.xml.stream.xIncludeAware"))
268 xIncludeAware = ((Boolean) value).booleanValue();
269 else
270 throw new IllegalArgumentException(name);
273 public Object getProperty(String name)
274 throws IllegalArgumentException
276 if (name.equals(IS_NAMESPACE_AWARE))
277 return namespaceAware ? Boolean.TRUE : Boolean.FALSE;
278 if (name.equals(IS_VALIDATING))
279 return validating ? Boolean.TRUE : Boolean.FALSE;
280 if (name.equals(IS_COALESCING))
281 return coalescing ? Boolean.TRUE : Boolean.FALSE;
282 if (name.equals(IS_REPLACING_ENTITY_REFERENCES))
283 return replacingEntityReferences ? Boolean.TRUE : Boolean.FALSE;
284 if (name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES))
285 return externalEntities ? Boolean.TRUE : Boolean.FALSE;
286 if (name.equals(SUPPORT_DTD))
287 return supportDTD ? Boolean.TRUE : Boolean.FALSE;
288 if (name.equals(REPORTER))
289 return reporter;
290 if (name.equals(RESOLVER))
291 return resolver;
292 if (name.equals(ALLOCATOR))
293 return allocator;
294 if (name.equals("gnu.xml.stream.stringInterning"))
295 return stringInterning ? Boolean.TRUE : Boolean.FALSE;
296 if (name.equals("gnu.xml.stream.baseAware"))
297 return baseAware ? Boolean.TRUE : Boolean.FALSE;
298 if (name.equals("gnu.xml.stream.xIncludeAware"))
299 return xIncludeAware ? Boolean.TRUE : Boolean.FALSE;
300 throw new IllegalArgumentException(name);
303 public boolean isPropertySupported(String name)
305 return name.equals(IS_NAMESPACE_AWARE) ||
306 name.equals(IS_VALIDATING) ||
307 name.equals(IS_COALESCING) ||
308 name.equals(IS_REPLACING_ENTITY_REFERENCES) ||
309 name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES) ||
310 name.equals(SUPPORT_DTD) ||
311 name.equals(REPORTER) ||
312 name.equals(RESOLVER) ||
313 name.equals(ALLOCATOR) ||
314 name.equals("gnu.xml.stream.stringInterning") ||
315 name.equals("gnu.xml.stream.baseAware") ||
316 name.equals("gnu.xml.stream.xIncludeAware");
319 public void setEventAllocator(XMLEventAllocator allocator)
321 this.allocator = allocator;
324 public XMLEventAllocator getEventAllocator()
326 return allocator;
329 public void setCoalescing(boolean coalescing)
331 this.coalescing = coalescing;
334 public boolean isCoalescing()
336 return coalescing;
339 protected InputStream getInputStream(Source source)
340 throws XMLStreamException
342 InputStream in = null;
343 if (source instanceof StreamSource)
345 StreamSource streamSource = (StreamSource) source;
346 in = streamSource.getInputStream();
348 if (in == null)
350 String systemId = source.getSystemId();
353 URL url = new URL(systemId);
356 in = url.openStream();
358 catch (IOException e2)
360 XMLStreamException e3 = new XMLStreamException(e2);
361 e3.initCause(e2);
362 throw e3;
365 catch (MalformedURLException e)
367 // Fall back to relative file
368 if (File.separatorChar != '/')
369 systemId = systemId.replace('/', File.separatorChar);
372 in = new FileInputStream(systemId);
374 catch (FileNotFoundException e2)
376 XMLStreamException e3 = new XMLStreamException(e2);
377 e3.initCause(e2);
378 throw e3;
382 return in;