Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / xml / stream / XMLInputFactoryImpl.java
blob5f72e361e52e1e51b34890843a3e89de1f514cb8
1 /* XMLInputFactoryImpl.java --
2 Copyright (C) 2005,2006 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
92 return createXMLStreamReader(null, reader);
95 public XMLStreamReader createXMLStreamReader(Source source)
96 throws XMLStreamException
98 String systemId = source.getSystemId();
99 InputStream in = getInputStream(source);
100 XMLParser ret = new XMLParser(in, systemId,
101 validating,
102 namespaceAware,
103 coalescing,
104 replacingEntityReferences,
105 externalEntities,
106 supportDTD,
107 baseAware,
108 stringInterning,
109 false,
110 reporter,
111 resolver);
112 if (xIncludeAware)
113 return new XIncludeFilter(ret, systemId, namespaceAware, validating,
114 replacingEntityReferences);
115 return ret;
118 public XMLStreamReader createXMLStreamReader(InputStream in)
119 throws XMLStreamException
121 return createXMLStreamReader(null, in);
124 public XMLStreamReader createXMLStreamReader(InputStream in, String encoding)
125 throws XMLStreamException
127 return createXMLStreamReader(in);
130 public XMLStreamReader createXMLStreamReader(String systemId, InputStream in)
131 throws XMLStreamException
133 XMLParser ret = new XMLParser(in, systemId,
134 validating,
135 namespaceAware,
136 coalescing,
137 replacingEntityReferences,
138 externalEntities,
139 supportDTD,
140 baseAware,
141 stringInterning,
142 false,
143 reporter,
144 resolver);
145 if (xIncludeAware)
146 return new XIncludeFilter(ret, null, namespaceAware, validating,
147 replacingEntityReferences);
148 return ret;
151 public XMLStreamReader createXMLStreamReader(String systemId, Reader reader)
152 throws XMLStreamException
154 XMLParser ret = new XMLParser(reader, systemId,
155 validating,
156 namespaceAware,
157 coalescing,
158 replacingEntityReferences,
159 externalEntities,
160 supportDTD,
161 baseAware,
162 stringInterning,
163 false,
164 reporter,
165 resolver);
166 if (xIncludeAware)
167 return new XIncludeFilter(ret, null, namespaceAware, validating,
168 replacingEntityReferences);
169 return ret;
172 public XMLEventReader createXMLEventReader(Reader reader)
173 throws XMLStreamException
175 XMLStreamReader sr = createXMLStreamReader(reader);
176 return new XMLEventReaderImpl(sr, allocator, null);
179 public XMLEventReader createXMLEventReader(String systemId, Reader reader)
180 throws XMLStreamException
182 XMLStreamReader sr = createXMLStreamReader(systemId, reader);
183 return new XMLEventReaderImpl(sr, allocator, null);
186 public XMLEventReader createXMLEventReader(XMLStreamReader reader)
187 throws XMLStreamException
189 return new XMLEventReaderImpl(reader, allocator, null);
192 public XMLEventReader createXMLEventReader(Source source)
193 throws XMLStreamException
195 XMLStreamReader sr = createXMLStreamReader(source);
196 return new XMLEventReaderImpl(sr, allocator, null);
199 public XMLEventReader createXMLEventReader(InputStream in)
200 throws XMLStreamException
202 XMLStreamReader sr = createXMLStreamReader(in);
203 return new XMLEventReaderImpl(sr, allocator, null);
206 public XMLEventReader createXMLEventReader(InputStream in, String encoding)
207 throws XMLStreamException
209 XMLStreamReader sr = createXMLStreamReader(in, encoding);
210 return new XMLEventReaderImpl(sr, allocator, null);
213 public XMLEventReader createXMLEventReader(String systemId, InputStream in)
214 throws XMLStreamException
216 XMLStreamReader sr = createXMLStreamReader(systemId, in);
217 return new XMLEventReaderImpl(sr, allocator, null);
220 public XMLStreamReader createFilteredReader(XMLStreamReader reader,
221 StreamFilter filter)
222 throws XMLStreamException
224 return new FilteredStreamReader(reader, filter);
227 public XMLEventReader createFilteredReader(XMLEventReader reader,
228 EventFilter filter)
229 throws XMLStreamException
231 return new FilteredEventReader(reader, filter);
234 public XMLResolver getXMLResolver()
236 return resolver;
239 public void setXMLResolver(XMLResolver resolver)
241 this.resolver = resolver;
244 public XMLReporter getXMLReporter()
246 return reporter;
249 public void setXMLReporter(XMLReporter reporter)
251 this.reporter = reporter;
254 public void setProperty(String name, Object value)
255 throws IllegalArgumentException
257 if (name.equals(IS_NAMESPACE_AWARE))
258 namespaceAware = ((Boolean) value).booleanValue();
259 else if (name.equals(IS_VALIDATING))
260 validating = ((Boolean) value).booleanValue();
261 else if (name.equals(IS_COALESCING))
262 coalescing = ((Boolean) value).booleanValue();
263 else if (name.equals(IS_REPLACING_ENTITY_REFERENCES))
264 replacingEntityReferences = ((Boolean) value).booleanValue();
265 else if (name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES))
266 externalEntities = ((Boolean) value).booleanValue();
267 else if (name.equals(SUPPORT_DTD))
268 supportDTD = ((Boolean) value).booleanValue();
269 else if (name.equals(REPORTER))
270 reporter = (XMLReporter) value;
271 else if (name.equals(RESOLVER))
272 resolver = (XMLResolver) value;
273 else if (name.equals(ALLOCATOR))
274 allocator = (XMLEventAllocator) value;
275 else if (name.equals("gnu.xml.stream.stringInterning"))
276 stringInterning = ((Boolean) value).booleanValue();
277 else if (name.equals("gnu.xml.stream.baseAware"))
278 baseAware = ((Boolean) value).booleanValue();
279 else if (name.equals("gnu.xml.stream.xIncludeAware"))
280 xIncludeAware = ((Boolean) value).booleanValue();
281 else
282 throw new IllegalArgumentException(name);
285 public Object getProperty(String name)
286 throws IllegalArgumentException
288 if (name.equals(IS_NAMESPACE_AWARE))
289 return namespaceAware ? Boolean.TRUE : Boolean.FALSE;
290 if (name.equals(IS_VALIDATING))
291 return validating ? Boolean.TRUE : Boolean.FALSE;
292 if (name.equals(IS_COALESCING))
293 return coalescing ? Boolean.TRUE : Boolean.FALSE;
294 if (name.equals(IS_REPLACING_ENTITY_REFERENCES))
295 return replacingEntityReferences ? Boolean.TRUE : Boolean.FALSE;
296 if (name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES))
297 return externalEntities ? Boolean.TRUE : Boolean.FALSE;
298 if (name.equals(SUPPORT_DTD))
299 return supportDTD ? Boolean.TRUE : Boolean.FALSE;
300 if (name.equals(REPORTER))
301 return reporter;
302 if (name.equals(RESOLVER))
303 return resolver;
304 if (name.equals(ALLOCATOR))
305 return allocator;
306 if (name.equals("gnu.xml.stream.stringInterning"))
307 return stringInterning ? Boolean.TRUE : Boolean.FALSE;
308 if (name.equals("gnu.xml.stream.baseAware"))
309 return baseAware ? Boolean.TRUE : Boolean.FALSE;
310 if (name.equals("gnu.xml.stream.xIncludeAware"))
311 return xIncludeAware ? Boolean.TRUE : Boolean.FALSE;
312 throw new IllegalArgumentException(name);
315 public boolean isPropertySupported(String name)
317 return name.equals(IS_NAMESPACE_AWARE) ||
318 name.equals(IS_VALIDATING) ||
319 name.equals(IS_COALESCING) ||
320 name.equals(IS_REPLACING_ENTITY_REFERENCES) ||
321 name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES) ||
322 name.equals(SUPPORT_DTD) ||
323 name.equals(REPORTER) ||
324 name.equals(RESOLVER) ||
325 name.equals(ALLOCATOR) ||
326 name.equals("gnu.xml.stream.stringInterning") ||
327 name.equals("gnu.xml.stream.baseAware") ||
328 name.equals("gnu.xml.stream.xIncludeAware");
331 public void setEventAllocator(XMLEventAllocator allocator)
333 this.allocator = allocator;
336 public XMLEventAllocator getEventAllocator()
338 return allocator;
341 public void setCoalescing(boolean coalescing)
343 this.coalescing = coalescing;
346 public boolean isCoalescing()
348 return coalescing;
351 protected InputStream getInputStream(Source source)
352 throws XMLStreamException
354 InputStream in = null;
355 if (source instanceof StreamSource)
357 StreamSource streamSource = (StreamSource) source;
358 in = streamSource.getInputStream();
360 if (in == null)
362 String systemId = source.getSystemId();
365 URL url = new URL(systemId);
368 in = url.openStream();
370 catch (IOException e2)
372 XMLStreamException e3 = new XMLStreamException(e2);
373 e3.initCause(e2);
374 throw e3;
377 catch (MalformedURLException e)
379 // Fall back to relative file
380 if (File.separatorChar != '/')
381 systemId = systemId.replace('/', File.separatorChar);
384 in = new FileInputStream(systemId);
386 catch (FileNotFoundException e2)
388 XMLStreamException e3 = new XMLStreamException(e2);
389 e3.initCause(e2);
390 throw e3;
394 return in;