1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60:
61:
66: public class XMLInputFactoryImpl
67: extends XMLInputFactory
68: {
69:
70: protected XMLResolver resolver;
71: protected XMLReporter reporter;
72: protected XMLEventAllocator allocator;
73:
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;
83:
84: public XMLInputFactoryImpl()
85: {
86: allocator = new XMLEventAllocatorImpl();
87: }
88:
89: public XMLStreamReader createXMLStreamReader(Reader reader)
90: throws XMLStreamException
91: {
92: return createXMLStreamReader(null, reader);
93: }
94:
95: public XMLStreamReader createXMLStreamReader(Source source)
96: throws XMLStreamException
97: {
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;
116: }
117:
118: public XMLStreamReader createXMLStreamReader(InputStream in)
119: throws XMLStreamException
120: {
121: return createXMLStreamReader(null, in);
122: }
123:
124: public XMLStreamReader createXMLStreamReader(InputStream in, String encoding)
125: throws XMLStreamException
126: {
127: return createXMLStreamReader(in);
128: }
129:
130: public XMLStreamReader createXMLStreamReader(String systemId, InputStream in)
131: throws XMLStreamException
132: {
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;
149: }
150:
151: public XMLStreamReader createXMLStreamReader(String systemId, Reader reader)
152: throws XMLStreamException
153: {
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;
170: }
171:
172: public XMLEventReader createXMLEventReader(Reader reader)
173: throws XMLStreamException
174: {
175: XMLStreamReader sr = createXMLStreamReader(reader);
176: return new XMLEventReaderImpl(sr, allocator, null);
177: }
178:
179: public XMLEventReader createXMLEventReader(String systemId, Reader reader)
180: throws XMLStreamException
181: {
182: XMLStreamReader sr = createXMLStreamReader(systemId, reader);
183: return new XMLEventReaderImpl(sr, allocator, null);
184: }
185:
186: public XMLEventReader createXMLEventReader(XMLStreamReader reader)
187: throws XMLStreamException
188: {
189: return new XMLEventReaderImpl(reader, allocator, null);
190: }
191:
192: public XMLEventReader createXMLEventReader(Source source)
193: throws XMLStreamException
194: {
195: XMLStreamReader sr = createXMLStreamReader(source);
196: return new XMLEventReaderImpl(sr, allocator, null);
197: }
198:
199: public XMLEventReader createXMLEventReader(InputStream in)
200: throws XMLStreamException
201: {
202: XMLStreamReader sr = createXMLStreamReader(in);
203: return new XMLEventReaderImpl(sr, allocator, null);
204: }
205:
206: public XMLEventReader createXMLEventReader(InputStream in, String encoding)
207: throws XMLStreamException
208: {
209: XMLStreamReader sr = createXMLStreamReader(in, encoding);
210: return new XMLEventReaderImpl(sr, allocator, null);
211: }
212:
213: public XMLEventReader createXMLEventReader(String systemId, InputStream in)
214: throws XMLStreamException
215: {
216: XMLStreamReader sr = createXMLStreamReader(systemId, in);
217: return new XMLEventReaderImpl(sr, allocator, null);
218: }
219:
220: public XMLStreamReader createFilteredReader(XMLStreamReader reader,
221: StreamFilter filter)
222: throws XMLStreamException
223: {
224: return new FilteredStreamReader(reader, filter);
225: }
226:
227: public XMLEventReader createFilteredReader(XMLEventReader reader,
228: EventFilter filter)
229: throws XMLStreamException
230: {
231: return new FilteredEventReader(reader, filter);
232: }
233:
234: public XMLResolver getXMLResolver()
235: {
236: return resolver;
237: }
238:
239: public void setXMLResolver(XMLResolver resolver)
240: {
241: this.resolver = resolver;
242: }
243:
244: public XMLReporter getXMLReporter()
245: {
246: return reporter;
247: }
248:
249: public void setXMLReporter(XMLReporter reporter)
250: {
251: this.reporter = reporter;
252: }
253:
254: public void setProperty(String name, Object value)
255: throws IllegalArgumentException
256: {
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);
283: }
284:
285: public Object getProperty(String name)
286: throws IllegalArgumentException
287: {
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);
313: }
314:
315: public boolean isPropertySupported(String name)
316: {
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");
329: }
330:
331: public void setEventAllocator(XMLEventAllocator allocator)
332: {
333: this.allocator = allocator;
334: }
335:
336: public XMLEventAllocator getEventAllocator()
337: {
338: return allocator;
339: }
340:
341: public void setCoalescing(boolean coalescing)
342: {
343: this.coalescing = coalescing;
344: }
345:
346: public boolean isCoalescing()
347: {
348: return coalescing;
349: }
350:
351: protected InputStream getInputStream(Source source)
352: throws XMLStreamException
353: {
354: InputStream in = null;
355: if (source instanceof StreamSource)
356: {
357: StreamSource streamSource = (StreamSource) source;
358: in = streamSource.getInputStream();
359: }
360: if (in == null)
361: {
362: String systemId = source.getSystemId();
363: try
364: {
365: URL url = new URL(systemId);
366: try
367: {
368: in = url.openStream();
369: }
370: catch (IOException e2)
371: {
372: XMLStreamException e3 = new XMLStreamException(e2);
373: e3.initCause(e2);
374: throw e3;
375: }
376: }
377: catch (MalformedURLException e)
378: {
379:
380: if (File.separatorChar != '/')
381: systemId = systemId.replace('/', File.separatorChar);
382: try
383: {
384: in = new FileInputStream(systemId);
385: }
386: catch (FileNotFoundException e2)
387: {
388: XMLStreamException e3 = new XMLStreamException(e2);
389: e3.initCause(e2);
390: throw e3;
391: }
392: }
393: }
394: return in;
395: }
396:
397: }