XMLInputFactoryImpl.java 13 KB

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