XmlReader.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* XmlReader.java --
  2. Copyright (C) 1999,2000,2001 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.aelfred2;
  32. import java.io.IOException;
  33. import java.util.Locale;
  34. import org.xml.sax.*;
  35. import org.xml.sax.ext.*;
  36. import gnu.xml.pipeline.EventFilter;
  37. import gnu.xml.pipeline.ValidationConsumer;
  38. /**
  39. * This SAX2 parser optionally layers a validator over the Ælfred2
  40. * SAX2 parser. While this will not evaluate every XML validity constraint,
  41. * it does support all the validity constraints that are of any real utility
  42. * outside the strict SGML-compatible world. See the documentation for the
  43. * SAXDriver class for information about the SAX2 features and properties
  44. * that are supported, and documentation for the ValidationConsumer for
  45. * information about what validity constraints may not be supported.
  46. * (Ælfred2 tests some of those, even in non-validating mode, to
  47. * achieve better conformance.)
  48. *
  49. * <p> Note that due to its internal construction, you can't change most
  50. * handlers until parse() returns. This diverges slightly from SAX, which
  51. * expects later binding to be supported. Early binding involves less
  52. * runtime overhead, which is an issue for event pipelines as used inside
  53. * this parser. Rather than relying on the parser to handle late binding
  54. * to your own handlers, do it yourself.
  55. *
  56. * @see SAXDriver
  57. * @see gnu.xml.pipeline.ValidationConsumer
  58. *
  59. * @author David Brownell
  60. */
  61. public final class XmlReader
  62. implements XMLReader
  63. {
  64. static class FatalErrorHandler
  65. extends DefaultHandler2
  66. {
  67. public void error(SAXParseException e)
  68. throws SAXException
  69. {
  70. throw e;
  71. }
  72. }
  73. private SAXDriver aelfred2 = new SAXDriver();
  74. private EventFilter filter = new EventFilter();
  75. private boolean isValidating;
  76. private boolean active;
  77. /**
  78. * Constructs a SAX Parser.
  79. */
  80. public XmlReader()
  81. {
  82. }
  83. /**
  84. * Constructs a SAX Parser, optionally treating validity errors
  85. * as if they were fatal errors.
  86. */
  87. public XmlReader(boolean invalidIsFatal)
  88. {
  89. if (invalidIsFatal)
  90. {
  91. setErrorHandler(new FatalErrorHandler());
  92. }
  93. }
  94. /**
  95. * <b>SAX2</b>: Returns the object used to report the logical
  96. * content of an XML document.
  97. */
  98. public ContentHandler getContentHandler()
  99. {
  100. return filter.getContentHandler();
  101. }
  102. /**
  103. * <b>SAX2</b>: Assigns the object used to report the logical
  104. * content of an XML document.
  105. * @exception IllegalStateException if called mid-parse
  106. */
  107. public void setContentHandler(ContentHandler handler)
  108. {
  109. if (active)
  110. {
  111. throw new IllegalStateException("already parsing");
  112. }
  113. filter.setContentHandler(handler);
  114. }
  115. /**
  116. * <b>SAX2</b>: Returns the object used to process declarations related
  117. * to notations and unparsed entities.
  118. */
  119. public DTDHandler getDTDHandler()
  120. {
  121. return filter.getDTDHandler();
  122. }
  123. /**
  124. * <b>SAX1</b> Assigns DTD handler
  125. * @exception IllegalStateException if called mid-parse
  126. */
  127. public void setDTDHandler(DTDHandler handler)
  128. {
  129. if (active)
  130. {
  131. throw new IllegalStateException("already parsing");
  132. }
  133. filter.setDTDHandler(handler);
  134. }
  135. /**
  136. * <b>SAX2</b>: Returns the object used when resolving external
  137. * entities during parsing (both general and parameter entities).
  138. */
  139. public EntityResolver getEntityResolver()
  140. {
  141. return aelfred2.getEntityResolver();
  142. }
  143. /**
  144. * <b>SAX1</b> Assigns parser's entity resolver
  145. */
  146. public void setEntityResolver(EntityResolver handler)
  147. {
  148. aelfred2.setEntityResolver(handler);
  149. }
  150. /**
  151. * <b>SAX2</b>: Returns the object used to receive callbacks for XML
  152. * errors of all levels (fatal, nonfatal, warning); this is never null;
  153. */
  154. public ErrorHandler getErrorHandler()
  155. {
  156. return aelfred2.getErrorHandler();
  157. }
  158. /**
  159. * <b>SAX1</b> Assigns error handler
  160. * @exception IllegalStateException if called mid-parse
  161. */
  162. public void setErrorHandler(ErrorHandler handler)
  163. {
  164. if (active)
  165. {
  166. throw new IllegalStateException("already parsing");
  167. }
  168. aelfred2.setErrorHandler(handler);
  169. }
  170. /**
  171. * <b>SAX2</b>: Assigns the specified property.
  172. * @exception IllegalStateException if called mid-parse
  173. */
  174. public void setProperty(String propertyId, Object value)
  175. throws SAXNotRecognizedException, SAXNotSupportedException
  176. {
  177. if (active)
  178. {
  179. throw new IllegalStateException("already parsing");
  180. }
  181. if (getProperty(propertyId) != value)
  182. {
  183. filter.setProperty(propertyId, value);
  184. }
  185. }
  186. /**
  187. * <b>SAX2</b>: Returns the specified property.
  188. */
  189. public Object getProperty(String propertyId)
  190. throws SAXNotRecognizedException
  191. {
  192. if ((SAXDriver.PROPERTY + "declaration-handler").equals(propertyId)
  193. || (SAXDriver.PROPERTY + "lexical-handler").equals(propertyId))
  194. {
  195. return filter.getProperty(propertyId);
  196. }
  197. throw new SAXNotRecognizedException(propertyId);
  198. }
  199. private void forceValidating()
  200. throws SAXNotRecognizedException, SAXNotSupportedException
  201. {
  202. aelfred2.setFeature(SAXDriver.FEATURE + "namespace-prefixes",
  203. true);
  204. aelfred2.setFeature(SAXDriver.FEATURE + "external-general-entities",
  205. true);
  206. aelfred2.setFeature(SAXDriver.FEATURE + "external-parameter-entities",
  207. true);
  208. }
  209. /**
  210. * <b>SAX2</b>: Sets the state of features supported in this parser.
  211. * Note that this parser requires reporting of namespace prefixes when
  212. * validating.
  213. */
  214. public void setFeature(String featureId, boolean state)
  215. throws SAXNotRecognizedException, SAXNotSupportedException
  216. {
  217. boolean value = getFeature(featureId);
  218. if (state == value)
  219. {
  220. return;
  221. }
  222. if ((SAXDriver.FEATURE + "validation").equals(featureId))
  223. {
  224. if (active)
  225. {
  226. throw new SAXNotSupportedException("already parsing");
  227. }
  228. if (state)
  229. {
  230. forceValidating();
  231. }
  232. isValidating = state;
  233. }
  234. else
  235. {
  236. aelfred2.setFeature(featureId, state);
  237. }
  238. }
  239. /**
  240. * <b>SAX2</b>: Tells whether this parser supports the specified feature.
  241. * At this time, this directly parallels the underlying SAXDriver,
  242. * except that validation is optionally supported.
  243. *
  244. * @see SAXDriver
  245. */
  246. public boolean getFeature(String featureId)
  247. throws SAXNotRecognizedException, SAXNotSupportedException
  248. {
  249. if ((SAXDriver.FEATURE + "validation").equals(featureId))
  250. {
  251. return isValidating;
  252. }
  253. return aelfred2.getFeature(featureId);
  254. }
  255. /**
  256. * <b>SAX1</b>: Sets the locale used for diagnostics; currently,
  257. * only locales using the English language are supported.
  258. * @param locale The locale for which diagnostics will be generated
  259. */
  260. public void setLocale(Locale locale)
  261. throws SAXException
  262. {
  263. aelfred2.setLocale(locale);
  264. }
  265. /**
  266. * <b>SAX1</b>: Preferred API to parse an XML document, using a
  267. * system identifier (URI).
  268. */
  269. public void parse(String systemId)
  270. throws SAXException, IOException
  271. {
  272. parse(new InputSource(systemId));
  273. }
  274. /**
  275. * <b>SAX1</b>: Underlying API to parse an XML document, used
  276. * directly when no URI is available. When this is invoked,
  277. * and the parser is set to validate, some features will be
  278. * automatically reset to appropriate values: for reporting
  279. * namespace prefixes, and incorporating external entities.
  280. *
  281. * @param source The XML input source.
  282. *
  283. * @exception IllegalStateException if called mid-parse
  284. * @exception SAXException The handlers may throw any SAXException,
  285. * and the parser normally throws SAXParseException objects.
  286. * @exception IOException IOExceptions are normally through through
  287. * the parser if there are problems reading the source document.
  288. */
  289. public void parse(InputSource source)
  290. throws SAXException, IOException
  291. {
  292. EventFilter next;
  293. boolean nsdecls;
  294. synchronized (aelfred2)
  295. {
  296. if (active)
  297. {
  298. throw new IllegalStateException("already parsing");
  299. }
  300. active = true;
  301. }
  302. // set up the output pipeline
  303. if (isValidating)
  304. {
  305. forceValidating();
  306. next = new ValidationConsumer(filter);
  307. }
  308. else
  309. {
  310. next = filter;
  311. }
  312. // connect pipeline and error handler
  313. // don't let _this_ call to bind() affect xmlns* attributes
  314. nsdecls = aelfred2.getFeature(SAXDriver.FEATURE + "namespace-prefixes");
  315. EventFilter.bind(aelfred2, next);
  316. if (!nsdecls)
  317. {
  318. aelfred2.setFeature(SAXDriver.FEATURE + "namespace-prefixes",
  319. false);
  320. }
  321. // parse, clean up
  322. try
  323. {
  324. aelfred2.parse(source);
  325. }
  326. finally
  327. {
  328. active = false;
  329. }
  330. }
  331. }