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.

JDOMSource.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*--
  2. $Id: JDOMSource.java,v 1.20 2007/11/10 05:29:02 jhunter Exp $
  3. Copyright (C) 2001-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.transform;
  46. import java.io.*;
  47. import java.util.*;
  48. import javax.xml.transform.sax.*;
  49. import org.jdom.*;
  50. import org.jdom.output.*;
  51. import org.xml.sax.*;
  52. /**
  53. * A holder for an XML Transformation source: a Document, Element, or list of
  54. * nodes.
  55. * <p>
  56. * The is provides input to a
  57. * {@link javax.xml.transform.Transformer JAXP TrAX Transformer}.
  58. * <p>
  59. * The following example shows how to apply an XSL Transformation
  60. * to a JDOM document and get the transformation result in the form
  61. * of a list of JDOM nodes:
  62. * <pre><code>
  63. * public static List transform(Document doc, String stylesheet)
  64. * throws JDOMException {
  65. * try {
  66. * Transformer transformer = TransformerFactory.newInstance()
  67. * .newTransformer(new StreamSource(stylesheet));
  68. * JDOMSource in = new JDOMSource(doc);
  69. * JDOMResult out = new JDOMResult();
  70. * transformer.transform(in, out);
  71. * return out.getResult();
  72. * }
  73. * catch (TransformerException e) {
  74. * throw new JDOMException("XSLT Transformation failed", e);
  75. * }
  76. * }
  77. * </code></pre>
  78. *
  79. * @see org.jdom.transform.JDOMResult
  80. *
  81. * @version $Revision: 1.20 $, $Date: 2007/11/10 05:29:02 $
  82. * @author Laurent Bihanic
  83. * @author Jason Hunter
  84. */
  85. public class JDOMSource extends SAXSource {
  86. private static final String CVS_ID =
  87. "@(#) $RCSfile: JDOMSource.java,v $ $Revision: 1.20 $ $Date: 2007/11/10 05:29:02 $ $Name: jdom_1_1 $";
  88. /**
  89. * If {@link javax.xml.transform.TransformerFactory#getFeature}
  90. * returns <code>true</code> when passed this value as an
  91. * argument, the Transformer natively supports JDOM.
  92. * <p>
  93. * <strong>Note</strong>: This implementation does not override
  94. * the {@link SAXSource#FEATURE} value defined by its superclass
  95. * to be considered as a SAXSource by Transformer implementations
  96. * not natively supporting JDOM.
  97. * </p>
  98. */
  99. public final static String JDOM_FEATURE =
  100. "http://org.jdom.transform.JDOMSource/feature";
  101. /**
  102. * The XMLReader object associated to this source or
  103. * <code>null</code> if no XMLReader has yet been requested.
  104. *
  105. * @see #getXMLReader
  106. */
  107. private XMLReader xmlReader = null;
  108. /**
  109. * Optional entity resolver associated to the source of
  110. * this document or <code>null</code> if no EntityResolver
  111. * was supplied with this JDOMSource.
  112. *
  113. * @see #buildDocumentReader()
  114. */
  115. private EntityResolver resolver = null;
  116. /**
  117. * Creates a JDOM TrAX source wrapping a JDOM document.
  118. *
  119. * @param source the JDOM document to use as source for the
  120. * transformations
  121. *
  122. * @throws IllegalArgumentException if <code>source</code> is
  123. * <code>null</code>.
  124. */
  125. public JDOMSource(Document source) {
  126. setDocument(source);
  127. }
  128. /**
  129. * Creates a JDOM TrAX source wrapping a list of JDOM nodes.
  130. *
  131. * @param source the JDOM nodes to use as source for the
  132. * transformations
  133. *
  134. * @throws IllegalArgumentException if <code>source</code> is
  135. * <code>null</code>.
  136. */
  137. public JDOMSource(List source) {
  138. setNodes(source);
  139. }
  140. /**
  141. * Creates a JDOM TrAX source wrapping a JDOM element.
  142. *
  143. * @param source the JDOM element to use as source for the
  144. * transformations
  145. *
  146. * @throws IllegalArgumentException if <code>source</code> is
  147. * <code>null</code>.
  148. */
  149. public JDOMSource(Element source) {
  150. List nodes = new ArrayList();
  151. nodes.add(source);
  152. setNodes(nodes);
  153. }
  154. /**
  155. * Creates a JDOM TrAX source wrapping a JDOM element with an
  156. * associated EntityResolver to resolve external entities.
  157. *
  158. * @param source The JDOM Element to use as source for the
  159. * transformations
  160. *
  161. * @param resolver Entity resolver to use for the source
  162. * transformation
  163. *
  164. * @throws IllegalArgumentException if<code>source</code> is
  165. * <code>null</code>
  166. */
  167. public JDOMSource(Document source, EntityResolver resolver) {
  168. setDocument(source);
  169. this.resolver = resolver;
  170. }
  171. /**
  172. * Sets the source document used by this TrAX source.
  173. *
  174. * @param source the JDOM document to use as source for the
  175. * transformations
  176. *
  177. * @throws IllegalArgumentException if <code>source</code> is
  178. * <code>null</code>.
  179. *
  180. * @see #getDocument
  181. */
  182. public void setDocument(Document source) {
  183. super.setInputSource(new JDOMInputSource(source));
  184. }
  185. /**
  186. * Returns the source document used by this TrAX source.
  187. *
  188. * @return the source document used by this TrAX source or
  189. * <code>null</code> if the source is a node list.
  190. *
  191. * @see #setDocument
  192. */
  193. public Document getDocument() {
  194. Object src = ((JDOMInputSource)getInputSource()).getSource();
  195. Document doc = null;
  196. if (src instanceof Document) {
  197. doc = (Document)src;
  198. }
  199. return doc;
  200. }
  201. /**
  202. * Sets the source node list used by this TrAX source.
  203. *
  204. * @param source the JDOM nodes to use as source for the
  205. * transformations
  206. *
  207. * @throws IllegalArgumentException if <code>source</code> is
  208. * <code>null</code>.
  209. *
  210. * @see #getNodes
  211. */
  212. public void setNodes(List source) {
  213. super.setInputSource(new JDOMInputSource(source));
  214. }
  215. /**
  216. * Returns the source node list used by this TrAX source.
  217. *
  218. * @return the source node list used by this TrAX source or
  219. * <code>null</code> if the source is a JDOM document.
  220. *
  221. * @see #setDocument
  222. */
  223. public List getNodes() {
  224. Object src = ((JDOMInputSource)getInputSource()).getSource();
  225. List nodes = null;
  226. if (src instanceof List) {
  227. nodes = (List)src;
  228. }
  229. return nodes;
  230. }
  231. //-------------------------------------------------------------------------
  232. // SAXSource overwritten methods
  233. //-------------------------------------------------------------------------
  234. /**
  235. * Sets the SAX InputSource to be used for the Source.
  236. * <p>
  237. * As this implementation only supports JDOM document as data
  238. * source, this method always throws an
  239. * {@link UnsupportedOperationException}.
  240. * </p>
  241. *
  242. * @param inputSource a valid InputSource reference.
  243. *
  244. * @throws UnsupportedOperationException always!
  245. */
  246. public void setInputSource(InputSource inputSource)
  247. throws UnsupportedOperationException {
  248. throw new UnsupportedOperationException();
  249. }
  250. /**
  251. * Set the XMLReader to be used for the Source.
  252. * <p>
  253. * As this implementation only supports JDOM document as data
  254. * source, this method throws an
  255. * {@link UnsupportedOperationException} if the provided reader
  256. * object does not implement the SAX {@link XMLFilter}
  257. * interface. Otherwise, the JDOM document reader will be
  258. * attached as parent of the filter chain.</p>
  259. *
  260. * @param reader a valid XMLReader or XMLFilter reference.
  261. *
  262. * @throws UnsupportedOperationException if <code>reader</code>
  263. * is not a SAX
  264. * {@link XMLFilter}.
  265. * @see #getXMLReader
  266. */
  267. public void setXMLReader(XMLReader reader)
  268. throws UnsupportedOperationException {
  269. if (reader instanceof XMLFilter) {
  270. // Connect the filter chain to a document reader.
  271. XMLFilter filter = (XMLFilter)reader;
  272. while (filter.getParent() instanceof XMLFilter) {
  273. filter = (XMLFilter)(filter.getParent());
  274. }
  275. filter.setParent(buildDocumentReader());
  276. // Read XML data from filter chain.
  277. this.xmlReader = reader;
  278. }
  279. else {
  280. throw new UnsupportedOperationException();
  281. }
  282. }
  283. /**
  284. * Returns the XMLReader to be used for the Source.
  285. * <p>
  286. * This implementation returns a specific XMLReader reading
  287. * the XML data from the source JDOM document.
  288. * </p>
  289. *
  290. * @return an XMLReader reading the XML data from the source
  291. * JDOM document.
  292. */
  293. public XMLReader getXMLReader() {
  294. if (this.xmlReader == null) {
  295. this.xmlReader = buildDocumentReader();
  296. }
  297. return this.xmlReader;
  298. }
  299. /**
  300. * Build an XMLReader to be used for the source. This will
  301. * create a new instance of DocumentReader with an
  302. * EntityResolver instance if available.
  303. *
  304. * @return XMLReader reading the XML data from the source
  305. * JDOM document with an optional EntityResolver
  306. */
  307. private XMLReader buildDocumentReader() {
  308. DocumentReader reader = new DocumentReader();
  309. if (resolver != null)
  310. reader.setEntityResolver(resolver);
  311. return reader;
  312. }
  313. //=========================================================================
  314. // JDOMInputSource nested class
  315. //=========================================================================
  316. /**
  317. * A subclass of the SAX InputSource interface that wraps a JDOM
  318. * Document.
  319. * <p>
  320. * This class is nested in JDOMSource as it is not intented to
  321. * be used independently of its friend: DocumentReader.
  322. * </p>
  323. *
  324. * @see org.jdom.Document
  325. */
  326. private static class JDOMInputSource extends InputSource {
  327. /**
  328. * The source as a JDOM document or a list of JDOM nodes.
  329. */
  330. private Object source = null;
  331. /**
  332. * Builds a InputSource wrapping the specified JDOM Document.
  333. *
  334. * @param document the source document.
  335. */
  336. public JDOMInputSource(Document document) {
  337. this.source = document;
  338. }
  339. /**
  340. * Builds a InputSource wrapping a list of JDOM nodes.
  341. *
  342. * @param nodes the source JDOM nodes.
  343. */
  344. public JDOMInputSource(List nodes) {
  345. this.source = nodes;
  346. }
  347. /**
  348. * Returns the source.
  349. *
  350. * @return the source as a JDOM document or a list of JDOM nodes.
  351. */
  352. public Object getSource() {
  353. return source;
  354. }
  355. //-------------------------------------------------------------------------
  356. // InputSource overwritten methods
  357. //-------------------------------------------------------------------------
  358. /**
  359. * Sets the character stream for this input source.
  360. * <p>
  361. * This implementation always throws an
  362. * {@link UnsupportedOperationException} as the only source
  363. * stream supported is the source JDOM document.
  364. * </p>
  365. *
  366. * @param characterStream a character stream containing
  367. * an XML document.
  368. *
  369. * @throws UnsupportedOperationException always!
  370. */
  371. public void setCharacterStream(Reader characterStream)
  372. throws UnsupportedOperationException {
  373. throw new UnsupportedOperationException();
  374. }
  375. /**
  376. * Gets the character stream for this input source.
  377. * <p>
  378. * Note that this method is only provided to make this
  379. * InputSource implementation acceptable by any XML
  380. * parser. As it generates an in-memory string representation
  381. * of the JDOM document, it is quite inefficient from both
  382. * speed and memory consumption points of view.
  383. * </p>
  384. *
  385. * @return a Reader to a string representation of the
  386. * source JDOM document.
  387. */
  388. public Reader getCharacterStream() {
  389. Object src = this.getSource();
  390. Reader reader = null;
  391. if (src instanceof Document) {
  392. // Get an in-memory string representation of the document
  393. // and return a reader on it.
  394. reader = new StringReader(
  395. new XMLOutputter().outputString((Document)src));
  396. }
  397. else {
  398. if (src instanceof List) {
  399. reader = new StringReader(
  400. new XMLOutputter().outputString((List)src));
  401. }
  402. // Else: No source, no reader!
  403. }
  404. return reader;
  405. }
  406. }
  407. //=========================================================================
  408. // DocumentReader nested class
  409. //=========================================================================
  410. /**
  411. * An implementation of the SAX2 XMLReader interface that presents
  412. * a SAX view of a JDOM Document. The actual generation of the
  413. * SAX events is delegated to JDOM's SAXOutputter.
  414. *
  415. * @see org.jdom.Document
  416. * @see org.jdom.output.SAXOutputter
  417. */
  418. private static class DocumentReader extends SAXOutputter
  419. implements XMLReader {
  420. /**
  421. * Public default constructor.
  422. */
  423. public DocumentReader() {
  424. super();
  425. }
  426. //----------------------------------------------------------------------
  427. // SAX XMLReader interface support
  428. //----------------------------------------------------------------------
  429. /**
  430. * Parses an XML document from a system identifier (URI).
  431. * <p>
  432. * This implementation does not support reading XML data from
  433. * system identifiers, only from JDOM documents. Hence,
  434. * this method always throws a {@link SAXNotSupportedException}.
  435. * </p>
  436. *
  437. * @param systemId the system identifier (URI).
  438. *
  439. * @throws SAXNotSupportedException always!
  440. */
  441. public void parse(String systemId) throws SAXNotSupportedException {
  442. throw new SAXNotSupportedException(
  443. "Only JDOM Documents are supported as input");
  444. }
  445. /**
  446. * Parses an XML document.
  447. * <p>
  448. * The methods accepts only <code>JDOMInputSource</code>s
  449. * instances as input sources.
  450. * </p>
  451. *
  452. * @param input the input source for the top-level of the
  453. * XML document.
  454. *
  455. * @throws SAXException any SAX exception,
  456. * possibly wrapping
  457. * another exception.
  458. * @throws SAXNotSupportedException if the input source does
  459. * not wrap a JDOM document.
  460. */
  461. public void parse(InputSource input) throws SAXException {
  462. if (input instanceof JDOMInputSource) {
  463. try {
  464. Object source = ((JDOMInputSource)input).getSource();
  465. if (source instanceof Document) {
  466. this.output((Document)source);
  467. }
  468. else {
  469. this.output((List)source);
  470. }
  471. }
  472. catch (JDOMException e) {
  473. throw new SAXException(e.getMessage(), e);
  474. }
  475. }
  476. else {
  477. throw new SAXNotSupportedException(
  478. "Only JDOM Documents are supported as input");
  479. }
  480. }
  481. }
  482. }