XMLInputFactory.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* XMLInputFactory.java --
  2. Copyright (C) 2005,2006,2009 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 javax.xml.stream;
  32. import java.io.BufferedReader;
  33. import java.io.File;
  34. import java.io.FileInputStream;
  35. import java.io.InputStream;
  36. import java.io.InputStreamReader;
  37. import java.io.IOException;
  38. import java.io.Reader;
  39. import java.util.Properties;
  40. import javax.xml.stream.util.XMLEventAllocator;
  41. import javax.xml.transform.Source;
  42. /**
  43. * Factory for creating stream and event readers from various kinds of input
  44. * source.
  45. * <h3>Parameters</h3>
  46. * <table>
  47. * <tr>
  48. * <th>Name</th>
  49. * <th>Description</th>
  50. * <th>Type</th>
  51. * <th>Default</th>
  52. * <th>Required</th>
  53. * </tr>
  54. * <tr>
  55. * <td>javax.xml.stream.isValidating</td>
  56. * <td>Controls DTD validation</td>
  57. * <td>Boolean</td>
  58. * <td>Boolean.FALSE</td>
  59. * <td>no</td>
  60. * </tr>
  61. * <tr>
  62. * <td>javax.xml.stream.isNamespaceAware</td>
  63. * <td>Controls namespace processing for XML 1.0</td>
  64. * <td>Boolean</td>
  65. * <td>Boolean.TRUE</td>
  66. * <td>true is required, false is optional</td>
  67. * </tr>
  68. * <tr>
  69. * <td>javax.xml.stream.isCoalescing</td>
  70. * <td>Controls coalescing (normalization of adjacent character data)</td>
  71. * <td>Boolean</td>
  72. * <td>Boolean.FALSE</td>
  73. * <td>yes</td>
  74. * </tr>
  75. * <tr>
  76. * <td>javax.xml.stream.isReplacingEntityReferences</td>
  77. * <td>Controls replacement of entity references with their replacement
  78. * text</td>
  79. * <td>Boolean</td>
  80. * <td>Boolean.TRUE</td>
  81. * <td>yes</td>
  82. * </tr>
  83. * <tr>
  84. * <td>javax.xml.stream.isSupportingExternalEntities</td>
  85. * <td>Controls whether to resolve external entities</td>
  86. * <td>Boolean</td>
  87. * <td>not specified</td>
  88. * <td>yes</td>
  89. * </tr>
  90. * <tr>
  91. * <td>javax.xml.stream.supportDTD</td>
  92. * <td>Controls whether to support DTDs</td>
  93. * <td>Boolean</td>
  94. * <td>Boolean.TRUE</td>
  95. * <td>yes</td>
  96. * </tr>
  97. * <tr>
  98. * <td>javax.xml.stream.reporter</td>
  99. * <td></td>
  100. * <td>javax.xml.stream.XMLReporter</td>
  101. * <td></td>
  102. * <td>yes</td>
  103. * </tr>
  104. * <tr>
  105. * <td>javax.xml.stream.resolver</td>
  106. * <td></td>
  107. * <td>javax.xml.stream.XMLResolver</td>
  108. * <td></td>
  109. * <td>yes</td>
  110. * </tr>
  111. * <tr>
  112. * <td>javax.xml.stream.allocator</td>
  113. * <td></td>
  114. * <td>javax.xml.stream.util.XMLEventAllocator</td>
  115. * <td></td>
  116. * <td>yes</td>
  117. * </tr>
  118. * </table>
  119. */
  120. public abstract class XMLInputFactory
  121. {
  122. /**
  123. * Property used to control namespace support.
  124. */
  125. public static final String IS_NAMESPACE_AWARE =
  126. "javax.xml.stream.isNamespaceAware";
  127. /**
  128. * Property used to control DTD validation.
  129. */
  130. public static final String IS_VALIDATING = "javax.xml.stream.isValidating";
  131. /**
  132. * Property used to control whether to coalesce adjacent text events.
  133. */
  134. public static final String IS_COALESCING = "javax.xml.stream.isCoalescing";
  135. /**
  136. * Property used to control whether to replace entity references with
  137. * their replacement text.
  138. */
  139. public static final String IS_REPLACING_ENTITY_REFERENCES =
  140. "javax.xml.stream.isReplacingEntityReferences";
  141. /**
  142. * Property used to control whether to resolve external entities.
  143. */
  144. public static final String IS_SUPPORTING_EXTERNAL_ENTITIES =
  145. "javax.xml.stream.isSupportingExternalEntities";
  146. /**
  147. * Property used to indicate whether to support DTDs.
  148. */
  149. public static final String SUPPORT_DTD = "javax.xml.stream.supportDTD";
  150. /**
  151. * Property used to control the error reporter implementation.
  152. */
  153. public static final String REPORTER = "javax.xml.stream.reporter";
  154. /**
  155. * Property used to control the entity resolver implementation.
  156. */
  157. public static final String RESOLVER = "javax.xml.stream.resolver";
  158. /**
  159. * Property used to control the event allocator implementation.
  160. */
  161. public static final String ALLOCATOR = "javax.xml.stream.allocator";
  162. protected XMLInputFactory()
  163. {
  164. }
  165. /**
  166. * Creates a new factory instance.
  167. * @see #newInstance(String,ClassLoader)
  168. */
  169. public static XMLInputFactory newInstance()
  170. throws FactoryConfigurationError
  171. {
  172. return newInstance(null, null);
  173. }
  174. /**
  175. * Creates a new factory instance.
  176. * The implementation class to load is the first found in the following
  177. * locations:
  178. * <ol>
  179. * <li>the <code>javax.xml.stream.XMLInputFactory</code> system
  180. * property</li>
  181. * <li>the above named property value in the
  182. * <code><i>$JAVA_HOME</i>/lib/stax.properties</code> file</li>
  183. * <li>the class name specified in the
  184. * <code>META-INF/services/javax.xml.stream.XMLInputFactory</code>
  185. * system resource</li>
  186. * <li>the default factory class</li>
  187. * </ol>
  188. * @param factoryId name of the factory, same as a property name
  189. * @param classLoader the class loader to use
  190. * @return the factory implementation
  191. * @exception FactoryConfigurationError if an instance of this factory
  192. * cannot be loaded
  193. */
  194. public static XMLInputFactory newInstance(String factoryId,
  195. ClassLoader classLoader)
  196. throws FactoryConfigurationError
  197. {
  198. ClassLoader loader = classLoader;
  199. if (loader == null)
  200. {
  201. loader = Thread.currentThread().getContextClassLoader();
  202. }
  203. if (loader == null)
  204. {
  205. loader = XMLInputFactory.class.getClassLoader();
  206. }
  207. String className = null;
  208. int count = 0;
  209. do
  210. {
  211. className = getFactoryClassName(loader, count++);
  212. if (className != null)
  213. {
  214. try
  215. {
  216. Class<?> t = (loader != null) ? loader.loadClass(className) :
  217. Class.forName(className);
  218. return (XMLInputFactory) t.newInstance();
  219. }
  220. catch (ClassNotFoundException e)
  221. {
  222. className = null;
  223. }
  224. catch (Exception e)
  225. {
  226. throw new FactoryConfigurationError(e,
  227. "error instantiating class " + className);
  228. }
  229. }
  230. }
  231. while (className == null && count < 3);
  232. return new gnu.xml.stream.XMLInputFactoryImpl();
  233. }
  234. private static String getFactoryClassName(ClassLoader loader, int attempt)
  235. {
  236. final String propertyName = "javax.xml.stream.XMLInputFactory";
  237. switch (attempt)
  238. {
  239. case 0:
  240. return System.getProperty(propertyName);
  241. case 1:
  242. try
  243. {
  244. File file = new File(System.getProperty("java.home"));
  245. file = new File(file, "lib");
  246. file = new File(file, "stax.properties");
  247. InputStream in = new FileInputStream(file);
  248. Properties props = new Properties();
  249. props.load(in);
  250. in.close();
  251. return props.getProperty(propertyName);
  252. }
  253. catch (IOException e)
  254. {
  255. return null;
  256. }
  257. case 2:
  258. try
  259. {
  260. String serviceKey = "/META-INF/services/" + propertyName;
  261. InputStream in = (loader != null) ?
  262. loader.getResourceAsStream(serviceKey) :
  263. XMLInputFactory.class.getResourceAsStream(serviceKey);
  264. if (in != null)
  265. {
  266. BufferedReader r =
  267. new BufferedReader(new InputStreamReader(in));
  268. String ret = r.readLine();
  269. r.close();
  270. return ret;
  271. }
  272. }
  273. catch (IOException e)
  274. {
  275. }
  276. return null;
  277. default:
  278. return null;
  279. }
  280. }
  281. /**
  282. * Creates a new stream reader.
  283. */
  284. public abstract XMLStreamReader createXMLStreamReader(Reader reader)
  285. throws XMLStreamException;
  286. /**
  287. * Creates a new stream reader.
  288. */
  289. public abstract XMLStreamReader createXMLStreamReader(Source source)
  290. throws XMLStreamException;
  291. /**
  292. * Creates a new stream reader.
  293. */
  294. public abstract XMLStreamReader createXMLStreamReader(InputStream stream)
  295. throws XMLStreamException;
  296. /**
  297. * Creates a new stream reader.
  298. */
  299. public abstract XMLStreamReader createXMLStreamReader(InputStream stream,
  300. String encoding)
  301. throws XMLStreamException;
  302. /**
  303. * Creates a new stream reader.
  304. */
  305. public abstract XMLStreamReader createXMLStreamReader(String systemId,
  306. InputStream stream)
  307. throws XMLStreamException;
  308. /**
  309. * Creates a new stream reader.
  310. */
  311. public abstract XMLStreamReader createXMLStreamReader(String systemId,
  312. Reader reader)
  313. throws XMLStreamException;
  314. /**
  315. * Creates a new event reader.
  316. */
  317. public abstract XMLEventReader createXMLEventReader(Reader reader)
  318. throws XMLStreamException;
  319. /**
  320. * Creates a new event reader.
  321. */
  322. public abstract XMLEventReader createXMLEventReader(String systemId,
  323. Reader reader)
  324. throws XMLStreamException;
  325. /**
  326. * Creates a new event reader.
  327. */
  328. public abstract XMLEventReader createXMLEventReader(XMLStreamReader reader)
  329. throws XMLStreamException;
  330. /**
  331. * Creates a new event reader.
  332. */
  333. public abstract XMLEventReader createXMLEventReader(Source source)
  334. throws XMLStreamException;
  335. /**
  336. * Creates a new event reader.
  337. */
  338. public abstract XMLEventReader createXMLEventReader(InputStream stream)
  339. throws XMLStreamException;
  340. /**
  341. * Creates a new event reader.
  342. */
  343. public abstract XMLEventReader createXMLEventReader(InputStream stream,
  344. String encoding)
  345. throws XMLStreamException;
  346. /**
  347. * Creates a new event reader.
  348. */
  349. public abstract XMLEventReader createXMLEventReader(String systemId,
  350. InputStream stream)
  351. throws XMLStreamException;
  352. /**
  353. * Create a new filtered reader.
  354. */
  355. public abstract XMLStreamReader createFilteredReader(XMLStreamReader reader,
  356. StreamFilter filter)
  357. throws XMLStreamException;
  358. /**
  359. * Create a new filtered reader.
  360. */
  361. public abstract XMLEventReader createFilteredReader(XMLEventReader reader,
  362. EventFilter filter)
  363. throws XMLStreamException;
  364. /**
  365. * Returns the entity resolver.
  366. */
  367. public abstract XMLResolver getXMLResolver();
  368. /**
  369. * Sets the entity resolver.
  370. */
  371. public abstract void setXMLResolver(XMLResolver resolver);
  372. /**
  373. * Returns the error reporter.
  374. */
  375. public abstract XMLReporter getXMLReporter();
  376. /**
  377. * Sets the error reporter.
  378. */
  379. public abstract void setXMLReporter(XMLReporter reporter);
  380. /**
  381. * Sets the implementation-specific property of the given name.
  382. * @exception IllegalArgumentException if the property is not supported
  383. */
  384. public abstract void setProperty(String name, Object value)
  385. throws IllegalArgumentException;
  386. /**
  387. * Returns the implementation-specific property of the given name.
  388. * @exception IllegalArgumentException if the property is not supported
  389. */
  390. public abstract Object getProperty(String name)
  391. throws IllegalArgumentException;
  392. /**
  393. * Indicates whether the specified property is supported.
  394. */
  395. public abstract boolean isPropertySupported(String name);
  396. /**
  397. * Sets the event allocator.
  398. */
  399. public abstract void setEventAllocator(XMLEventAllocator allocator);
  400. /**
  401. * Returns the event allocator.
  402. */
  403. public abstract XMLEventAllocator getEventAllocator();
  404. }