Desktop tool for browsing account info from EVE-Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JAXPParserFactory.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*--
  2. $Id: JAXPParserFactory.java,v 1.6 2007/11/10 05:29:00 jhunter Exp $
  3. Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions, and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions, and the disclaimer that follows
  12. these conditions in the documentation and/or other materials
  13. provided with the distribution.
  14. 3. The name "JDOM" must not be used to endorse or promote products
  15. derived from this software without prior written permission. For
  16. written permission, please contact <request_AT_jdom_DOT_org>.
  17. 4. Products derived from this software may not be called "JDOM", nor
  18. may "JDOM" appear in their name, without prior written permission
  19. from the JDOM Project Management <request_AT_jdom_DOT_org>.
  20. In addition, we request (but do not require) that you include in the
  21. end-user documentation provided with the redistribution and/or in the
  22. software itself an acknowledgement equivalent to the following:
  23. "This product includes software developed by the
  24. JDOM Project (http://www.jdom.org/)."
  25. Alternatively, the acknowledgment may be graphical using the logos
  26. available at http://www.jdom.org/images/logos.
  27. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  28. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  34. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. SUCH DAMAGE.
  39. This software consists of voluntary contributions made by many
  40. individuals on behalf of the JDOM Project and was originally
  41. created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
  42. Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
  43. on the JDOM Project, please see <http://www.jdom.org/>.
  44. */
  45. package org.jdom.input;
  46. import java.util.*;
  47. import javax.xml.parsers.*;
  48. import org.jdom.*;
  49. import org.xml.sax.*;
  50. /**
  51. * A non-public utility class to allocate JAXP SAX parsers.
  52. *
  53. * @version $Revision: 1.6 $, $Date: 2007/11/10 05:29:00 $
  54. * @author Laurent Bihanic
  55. */
  56. class JAXPParserFactory { // package protected
  57. private static final String CVS_ID =
  58. "@(#) $RCSfile: JAXPParserFactory.java,v $ $Revision: 1.6 $ $Date: 2007/11/10 05:29:00 $ $Name: jdom_1_1 $";
  59. /** JAXP 1.2 schema language property id. */
  60. private static final String JAXP_SCHEMA_LANGUAGE_PROPERTY =
  61. "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
  62. /** JAXP 1.2 schema location property id. */
  63. private static final String JAXP_SCHEMA_LOCATION_PROPERTY =
  64. "http://java.sun.com/xml/jaxp/properties/schemaSource";
  65. /**
  66. * Private constructor to forbid allocating instances of this utility
  67. * class.
  68. */
  69. private JAXPParserFactory() {
  70. // Never called.
  71. }
  72. /* Implementor's note regarding createParser() design: The features and
  73. properties are normally set in SAXBuilder, but we take them in
  74. createParser() as well because some features or properties may need to be
  75. applied during the JAXP parser construction. Today, for example, properties
  76. is used as it's the only way to configure schema validation: JAXP defines
  77. schema validation properties but SAX does not. This reflects in the Apache
  78. Xerces implementation where the SAXParser implementation supports the JAXP
  79. properties but the XMLReader does not. Hence, configuring schema validation
  80. must be done on the SAXParser object which is only visible in
  81. JAXParserFactory. Features is also passed in case some future JAXP release
  82. defines JAXP-specific features.
  83. */
  84. /**
  85. * Creates a SAX parser allocated through the configured JAXP SAX
  86. * parser factory.
  87. *
  88. * @param validating whether a validating parser is requested.
  89. * @param features the user-defined SAX features.
  90. * @param properties the user-defined SAX properties.
  91. *
  92. * @return a configured XMLReader.
  93. *
  94. * @throws JDOMException if any error occurred when allocating or
  95. * configuring the JAXP SAX parser.
  96. */
  97. public static XMLReader createParser(boolean validating,
  98. Map features, Map properties) throws JDOMException {
  99. try {
  100. SAXParser parser = null;
  101. // Allocate and configure JAXP SAX parser factory.
  102. SAXParserFactory factory = SAXParserFactory.newInstance();
  103. factory.setValidating(validating);
  104. factory.setNamespaceAware(true);
  105. try {
  106. // Allocate parser.
  107. parser = factory.newSAXParser();
  108. }
  109. catch (ParserConfigurationException e) {
  110. throw new JDOMException("Could not allocate JAXP SAX Parser", e);
  111. }
  112. // Set user-defined JAXP properties (if any)
  113. setProperty(parser, properties, JAXP_SCHEMA_LANGUAGE_PROPERTY);
  114. setProperty(parser, properties, JAXP_SCHEMA_LOCATION_PROPERTY);
  115. // Return configured SAX XMLReader.
  116. return parser.getXMLReader();
  117. }
  118. catch (SAXException e) {
  119. throw new JDOMException("Could not allocate JAXP SAX Parser", e);
  120. }
  121. }
  122. /**
  123. * Sets a property on a JAXP SAX parser object if and only if it
  124. * is declared in the user-defined properties.
  125. *
  126. * @param parser the JAXP SAX parser to configure.
  127. * @param properties the user-defined SAX properties.
  128. * @param name the name of the property to set.
  129. *
  130. * @throws JDOMException if any error occurred while configuring
  131. * the property.
  132. */
  133. private static void setProperty(SAXParser parser,
  134. Map properties, String name) throws JDOMException {
  135. try {
  136. if (properties.containsKey(name)) {
  137. parser.setProperty(name, properties.get(name));
  138. }
  139. }
  140. catch (SAXNotSupportedException e) {
  141. throw new JDOMException(
  142. name + " property not supported for JAXP parser " +
  143. parser.getClass().getName());
  144. }
  145. catch (SAXNotRecognizedException e) {
  146. throw new JDOMException(
  147. name + " property not recognized for JAXP parser " +
  148. parser.getClass().getName());
  149. }
  150. }
  151. }