JAXPFactory.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* JAXPFactory.java --
  2. Copyright (C) 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.util.Enumeration;
  33. import java.util.Hashtable;
  34. import org.xml.sax.Parser;
  35. import org.xml.sax.XMLReader;
  36. import org.xml.sax.SAXException;
  37. import org.xml.sax.SAXNotRecognizedException;
  38. import org.xml.sax.SAXNotSupportedException;
  39. import org.xml.sax.helpers.XMLReaderAdapter;
  40. import javax.xml.parsers.ParserConfigurationException;
  41. import javax.xml.parsers.SAXParser;
  42. import javax.xml.parsers.SAXParserFactory;
  43. /**
  44. * Configurable factory to create an Ælfred2 JAXP parser; required
  45. * to bootstrap using JAXP. You should use SAX2 directly where possible,
  46. * rather than through JAXP, since that gives you better control.
  47. * This class would normally be configured as a platform default factory.
  48. *
  49. * @author David Brownell
  50. */
  51. public final class JAXPFactory
  52. extends SAXParserFactory
  53. {
  54. private Hashtable flags = new Hashtable();
  55. /**
  56. * Constructs a factory which normally returns a non-validating
  57. * parser.
  58. */
  59. public JAXPFactory()
  60. {
  61. }
  62. public SAXParser newSAXParser()
  63. throws ParserConfigurationException, SAXException
  64. {
  65. JaxpParser jaxp = new JaxpParser();
  66. Enumeration e = flags.keys();
  67. XMLReader parser = jaxp.getXMLReader();
  68. parser.setFeature(SAXDriver.FEATURE + "namespaces",
  69. isNamespaceAware());
  70. parser.setFeature(SAXDriver.FEATURE + "validation",
  71. isValidating());
  72. // that makes SAX2 feature flags trump JAXP
  73. while (e.hasMoreElements())
  74. {
  75. String uri = (String) e.nextElement();
  76. Boolean value = (Boolean) flags.get(uri);
  77. parser.setFeature(uri, value.booleanValue());
  78. }
  79. return jaxp;
  80. }
  81. // yes, this "feature transfer" mechanism doesn't play well
  82. public void setFeature(String name, boolean value)
  83. throws ParserConfigurationException, SAXNotRecognizedException,
  84. SAXNotSupportedException
  85. {
  86. try
  87. {
  88. // force "early" detection of errors where possible
  89. // (flags can't necessarily be set before parsing)
  90. new JaxpParser().getXMLReader().setFeature(name, value);
  91. flags.put(name, Boolean.valueOf(value));
  92. }
  93. catch (SAXNotRecognizedException e)
  94. {
  95. throw new SAXNotRecognizedException(name);
  96. }
  97. catch (SAXNotSupportedException e)
  98. {
  99. throw new SAXNotSupportedException(name);
  100. }
  101. catch (Exception e)
  102. {
  103. throw new ParserConfigurationException(e.getClass().getName()
  104. + ": "
  105. + e.getMessage());
  106. }
  107. }
  108. public boolean getFeature(String name)
  109. throws ParserConfigurationException, SAXNotRecognizedException,
  110. SAXNotSupportedException
  111. {
  112. Boolean value = (Boolean) flags.get(name);
  113. if (value != null)
  114. {
  115. return value.booleanValue();
  116. }
  117. else
  118. {
  119. try
  120. {
  121. return new JaxpParser().getXMLReader().getFeature(name);
  122. }
  123. catch (SAXNotRecognizedException e)
  124. {
  125. throw new SAXNotRecognizedException(name);
  126. }
  127. catch (SAXNotSupportedException e)
  128. {
  129. throw new SAXNotSupportedException(name);
  130. }
  131. catch (SAXException e)
  132. {
  133. throw new ParserConfigurationException(e.getClass().getName()
  134. + ": "
  135. + e.getMessage());
  136. }
  137. }
  138. }
  139. private static class JaxpParser
  140. extends SAXParser
  141. {
  142. private XmlReader ae2 = new XmlReader();
  143. private XMLReaderAdapter parser = null;
  144. JaxpParser()
  145. {
  146. }
  147. public void setProperty(String id, Object value)
  148. throws SAXNotRecognizedException, SAXNotSupportedException
  149. {
  150. ae2.setProperty(id, value);
  151. }
  152. public Object getProperty(String id)
  153. throws SAXNotRecognizedException, SAXNotSupportedException
  154. {
  155. return ae2.getProperty(id);
  156. }
  157. public Parser getParser()
  158. throws SAXException
  159. {
  160. if (parser == null)
  161. {
  162. parser = new XMLReaderAdapter(ae2);
  163. }
  164. return parser;
  165. }
  166. public XMLReader getXMLReader ()
  167. throws SAXException
  168. {
  169. return ae2;
  170. }
  171. public boolean isNamespaceAware()
  172. {
  173. try
  174. {
  175. return ae2.getFeature(SAXDriver.FEATURE + "namespaces");
  176. }
  177. catch (Exception e)
  178. {
  179. throw new Error();
  180. }
  181. }
  182. public boolean isValidating()
  183. {
  184. try
  185. {
  186. return ae2.getFeature(SAXDriver.FEATURE + "validation");
  187. }
  188. catch (Exception e)
  189. {
  190. throw new Error();
  191. }
  192. }
  193. // TODO isXIncludeAware()
  194. }
  195. }