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.

JAXPDOMAdapter.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*--
  2. $Id: JAXPDOMAdapter.java,v 1.13 2007/11/10 05:28:59 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.adapters;
  46. import java.io.*;
  47. import java.lang.reflect.*;
  48. import org.jdom.*;
  49. import org.jdom.input.*;
  50. import org.w3c.dom.Document;
  51. /**
  52. * An adapter for any parser supporting the Sun JAXP APIs.
  53. *
  54. * @version $Revision: 1.13 $, $Date: 2007/11/10 05:28:59 $
  55. * @author Jason Hunter
  56. */
  57. public class JAXPDOMAdapter extends AbstractDOMAdapter {
  58. private static final String CVS_ID =
  59. "@(#) $RCSfile: JAXPDOMAdapter.java,v $ $Revision: 1.13 $ $Date: 2007/11/10 05:28:59 $ $Name: jdom_1_1 $";
  60. /**
  61. * This creates a new <code>{@link Document}</code> from an
  62. * existing <code>InputStream</code> by letting a JAXP
  63. * parser handle parsing using the supplied stream.
  64. *
  65. * @param in <code>InputStream</code> to parse.
  66. * @param validate <code>boolean</code> to indicate if validation
  67. * should occur.
  68. * @return <code>Document</code> - instance ready for use.
  69. * @throws IOException when I/O error occurs.
  70. * @throws JDOMException when errors occur in parsing.
  71. */
  72. public Document getDocument(InputStream in, boolean validate)
  73. throws IOException, JDOMException {
  74. try {
  75. // Try using JAXP...
  76. // Note we need DOM Level 2 and thus JAXP 1.1.
  77. Class.forName("javax.xml.transform.Transformer");
  78. // Try JAXP 1.1 calls to build the document
  79. Class factoryClass =
  80. Class.forName("javax.xml.parsers.DocumentBuilderFactory");
  81. // factory = DocumentBuilderFactory.newInstance();
  82. Method newParserInstance =
  83. factoryClass.getMethod("newInstance", null);
  84. Object factory = newParserInstance.invoke(null, null);
  85. // factory.setValidating(validate);
  86. Method setValidating =
  87. factoryClass.getMethod("setValidating",
  88. new Class[]{boolean.class});
  89. setValidating.invoke(factory,
  90. new Object[]{new Boolean(validate)});
  91. // factory.setNamespaceAware(true);
  92. Method setNamespaceAware =
  93. factoryClass.getMethod("setNamespaceAware",
  94. new Class[]{boolean.class});
  95. setNamespaceAware.invoke(factory,
  96. new Object[]{Boolean.TRUE});
  97. // jaxpParser = factory.newDocumentBuilder();
  98. Method newDocBuilder =
  99. factoryClass.getMethod("newDocumentBuilder", null);
  100. Object jaxpParser = newDocBuilder.invoke(factory, null);
  101. // jaxpParser.setErrorHandler(null);
  102. Class parserClass = jaxpParser.getClass();
  103. Method setErrorHandler =
  104. parserClass.getMethod("setErrorHandler",
  105. new Class[]{org.xml.sax.ErrorHandler.class});
  106. setErrorHandler.invoke(jaxpParser,
  107. new Object[]{new BuilderErrorHandler()});
  108. // domDoc = jaxpParser.parse(in);
  109. Method parse = parserClass.getMethod(
  110. "parse", new Class[]{InputStream.class});
  111. org.w3c.dom.Document domDoc = (org.w3c.dom.Document)
  112. parse.invoke(jaxpParser, new Object[]{in});
  113. return domDoc;
  114. } catch (InvocationTargetException e) {
  115. Throwable targetException = e.getTargetException();
  116. if (targetException instanceof IOException) {
  117. throw (IOException) targetException;
  118. } else {
  119. throw new JDOMException(targetException.getMessage(), targetException);
  120. }
  121. } catch (Exception e) {
  122. throw new JDOMException("Reflection failed while parsing a document with JAXP", e);
  123. }
  124. // Allow all exceptions to pass through
  125. }
  126. /**
  127. * This creates an empty <code>Document</code> object based
  128. * on a specific parser implementation.
  129. *
  130. * @return <code>Document</code> - created DOM Document.
  131. * @throws JDOMException when errors occur in parsing.
  132. */
  133. public Document createDocument()
  134. throws JDOMException {
  135. try {
  136. // We need DOM Level 2 and thus JAXP 1.1.
  137. // If JAXP 1.0 is all that's available then we error out.
  138. Class.forName("javax.xml.transform.Transformer");
  139. // Try JAXP 1.1 calls to build the document
  140. Class factoryClass =
  141. Class.forName("javax.xml.parsers.DocumentBuilderFactory");
  142. // factory = DocumentBuilderFactory.newInstance();
  143. Method newParserInstance =
  144. factoryClass.getMethod("newInstance", null);
  145. Object factory = newParserInstance.invoke(null, null);
  146. // jaxpParser = factory.newDocumentBuilder();
  147. Method newDocBuilder =
  148. factoryClass.getMethod("newDocumentBuilder", null);
  149. Object jaxpParser = newDocBuilder.invoke(factory, null);
  150. // domDoc = jaxpParser.newDocument();
  151. Class parserClass = jaxpParser.getClass();
  152. Method newDoc = parserClass.getMethod("newDocument", null);
  153. org.w3c.dom.Document domDoc =
  154. (org.w3c.dom.Document) newDoc.invoke(jaxpParser, null);
  155. return domDoc;
  156. } catch (Exception e) {
  157. throw new JDOMException("Reflection failed while creating new JAXP document", e);
  158. }
  159. }
  160. }