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.

DOMOutputter.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*--
  2. $Id: DOMOutputter.java,v 1.43 2007/11/10 05:29:01 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.output;
  46. import java.util.*;
  47. import org.jdom.*;
  48. import org.jdom.adapters.*;
  49. /**
  50. * Outputs a JDOM {@link org.jdom.Document org.jdom.Document} as a DOM {@link
  51. * org.w3c.dom.Document org.w3c.dom.Document}.
  52. *
  53. * @version $Revision: 1.43 $, $Date: 2007/11/10 05:29:01 $
  54. * @author Brett McLaughlin
  55. * @author Jason Hunter
  56. * @author Matthew Merlo
  57. * @author Dan Schaffer
  58. * @author Yusuf Goolamabbas
  59. * @author Bradley S. Huffman
  60. */
  61. public class DOMOutputter {
  62. private static final String CVS_ID =
  63. "@(#) $RCSfile: DOMOutputter.java,v $ $Revision: 1.43 $ $Date: 2007/11/10 05:29:01 $ $Name: jdom_1_1 $";
  64. /** Default adapter class */
  65. private static final String DEFAULT_ADAPTER_CLASS =
  66. "org.jdom.adapters.XercesDOMAdapter";
  67. /** Adapter to use for interfacing with the DOM implementation */
  68. private String adapterClass;
  69. /** Output a DOM with namespaces but just the empty namespace */
  70. private boolean forceNamespaceAware;
  71. /**
  72. * This creates a new DOMOutputter which will attempt to first locate
  73. * a DOM implementation to use via JAXP, and if JAXP does not exist or
  74. * there's a problem, will fall back to the default parser.
  75. */
  76. public DOMOutputter() {
  77. // nothing
  78. }
  79. /**
  80. * This creates a new DOMOutputter using the specified DOMAdapter
  81. * implementation as a way to choose the underlying parser.
  82. *
  83. * @param adapterClass <code>String</code> name of class
  84. * to use for DOM output
  85. */
  86. public DOMOutputter(String adapterClass) {
  87. this.adapterClass = adapterClass;
  88. }
  89. /**
  90. * Controls how NO_NAMESPACE nodes are handeled. If true the outputter
  91. * always creates a namespace aware DOM.
  92. * @param flag
  93. */
  94. public void setForceNamespaceAware(boolean flag) {
  95. this.forceNamespaceAware = flag;
  96. }
  97. /**
  98. * Returns whether DOMs will be constructed with namespaces even when
  99. * the source document has elements all in the empty namespace.
  100. * @return the forceNamespaceAware flag value
  101. */
  102. public boolean getForceNamespaceAware() {
  103. return forceNamespaceAware;
  104. }
  105. /**
  106. * This converts the JDOM <code>Document</code> parameter to a
  107. * DOM Document, returning the DOM version. The DOM implementation
  108. * is the one chosen in the constructor.
  109. *
  110. * @param document <code>Document</code> to output.
  111. * @return an <code>org.w3c.dom.Document</code> version
  112. */
  113. public org.w3c.dom.Document output(Document document)
  114. throws JDOMException {
  115. NamespaceStack namespaces = new NamespaceStack();
  116. org.w3c.dom.Document domDoc = null;
  117. try {
  118. // Assign DOCTYPE during construction
  119. DocType dt = document.getDocType();
  120. domDoc = createDOMDocument(dt);
  121. // Add content
  122. Iterator itr = document.getContent().iterator();
  123. while (itr.hasNext()) {
  124. Object node = itr.next();
  125. if (node instanceof Element) {
  126. Element element = (Element) node;
  127. org.w3c.dom.Element domElement =
  128. output(element, domDoc, namespaces);
  129. // Add the root element, first check for existing root
  130. org.w3c.dom.Element root = domDoc.getDocumentElement();
  131. if (root == null) {
  132. // Normal case
  133. domDoc.appendChild(domElement); // normal case
  134. }
  135. else {
  136. // Xerces 1.3 creates new docs with a <root />
  137. // XXX: Need to address DOCTYPE mismatch still
  138. domDoc.replaceChild(domElement, root);
  139. }
  140. }
  141. else if (node instanceof Comment) {
  142. Comment comment = (Comment) node;
  143. org.w3c.dom.Comment domComment =
  144. domDoc.createComment(comment.getText());
  145. domDoc.appendChild(domComment);
  146. }
  147. else if (node instanceof ProcessingInstruction) {
  148. ProcessingInstruction pi =
  149. (ProcessingInstruction) node;
  150. org.w3c.dom.ProcessingInstruction domPI =
  151. domDoc.createProcessingInstruction(
  152. pi.getTarget(), pi.getData());
  153. domDoc.appendChild(domPI);
  154. }
  155. else if (node instanceof DocType) {
  156. // We already dealt with the DocType above
  157. }
  158. else {
  159. throw new JDOMException(
  160. "Document contained top-level content with type:" +
  161. node.getClass().getName());
  162. }
  163. }
  164. }
  165. catch (Throwable e) {
  166. throw new JDOMException("Exception outputting Document", e);
  167. }
  168. return domDoc;
  169. }
  170. private org.w3c.dom.Document createDOMDocument(DocType dt)
  171. throws JDOMException {
  172. if (adapterClass != null) {
  173. // The user knows that they want to use a particular impl
  174. try {
  175. DOMAdapter adapter =
  176. (DOMAdapter)Class.forName(adapterClass).newInstance();
  177. // System.out.println("using specific " + adapterClass);
  178. return adapter.createDocument(dt);
  179. }
  180. catch (ClassNotFoundException e) {
  181. // e.printStackTrace();
  182. }
  183. catch (IllegalAccessException e) {
  184. // e.printStackTrace();
  185. }
  186. catch (InstantiationException e) {
  187. // e.printStackTrace();
  188. }
  189. }
  190. else {
  191. // Try using JAXP...
  192. try {
  193. DOMAdapter adapter =
  194. (DOMAdapter)Class.forName(
  195. "org.jdom.adapters.JAXPDOMAdapter").newInstance();
  196. // System.out.println("using JAXP");
  197. return adapter.createDocument(dt);
  198. }
  199. catch (ClassNotFoundException e) {
  200. // e.printStackTrace();
  201. }
  202. catch (IllegalAccessException e) {
  203. // e.printStackTrace();
  204. }
  205. catch (InstantiationException e) {
  206. // e.printStackTrace();
  207. }
  208. }
  209. // If no DOM doc yet, try to use a hard coded default
  210. try {
  211. DOMAdapter adapter = (DOMAdapter)
  212. Class.forName(DEFAULT_ADAPTER_CLASS).newInstance();
  213. return adapter.createDocument(dt);
  214. // System.out.println("Using default " +
  215. // DEFAULT_ADAPTER_CLASS);
  216. }
  217. catch (ClassNotFoundException e) {
  218. // e.printStackTrace();
  219. }
  220. catch (IllegalAccessException e) {
  221. // e.printStackTrace();
  222. }
  223. catch (InstantiationException e) {
  224. // e.printStackTrace();
  225. }
  226. throw new JDOMException("No JAXP or default parser available");
  227. }
  228. private org.w3c.dom.Element output(Element element,
  229. org.w3c.dom.Document domDoc,
  230. NamespaceStack namespaces)
  231. throws JDOMException {
  232. try {
  233. int previouslyDeclaredNamespaces = namespaces.size();
  234. org.w3c.dom.Element domElement = null;
  235. if (element.getNamespace() == Namespace.NO_NAMESPACE) {
  236. // No namespace, use createElement
  237. domElement = forceNamespaceAware ?
  238. domDoc.createElementNS(null, element.getQualifiedName())
  239. : domDoc.createElement(element.getQualifiedName()); }
  240. else {
  241. domElement = domDoc.createElementNS(
  242. element.getNamespaceURI(),
  243. element.getQualifiedName());
  244. }
  245. // Add namespace attributes, beginning with the element's own
  246. // Do this only if it's not the XML namespace and it's
  247. // not the NO_NAMESPACE with the prefix "" not yet mapped
  248. // (we do output xmlns="" if the "" prefix was already used
  249. // and we need to reclaim it for the NO_NAMESPACE)
  250. Namespace ns = element.getNamespace();
  251. if (ns != Namespace.XML_NAMESPACE &&
  252. !(ns == Namespace.NO_NAMESPACE &&
  253. namespaces.getURI("") == null)) {
  254. String prefix = ns.getPrefix();
  255. String uri = namespaces.getURI(prefix);
  256. if (!ns.getURI().equals(uri)) { // output a new namespace decl
  257. namespaces.push(ns);
  258. String attrName = getXmlnsTagFor(ns);
  259. domElement.setAttribute(attrName, ns.getURI());
  260. }
  261. }
  262. // Add additional namespaces also
  263. Iterator itr = element.getAdditionalNamespaces().iterator();
  264. while (itr.hasNext()) {
  265. Namespace additional = (Namespace)itr.next();
  266. String prefix = additional.getPrefix();
  267. String uri = namespaces.getURI(prefix);
  268. if (!additional.getURI().equals(uri)) {
  269. String attrName = getXmlnsTagFor(additional);
  270. domElement.setAttribute(attrName, additional.getURI());
  271. namespaces.push(additional);
  272. }
  273. }
  274. // Add attributes to the DOM element
  275. itr = element.getAttributes().iterator();
  276. while (itr.hasNext()) {
  277. Attribute attribute = (Attribute) itr.next();
  278. domElement.setAttributeNode(output(attribute, domDoc));
  279. Namespace ns1 = attribute.getNamespace();
  280. if ((ns1 != Namespace.NO_NAMESPACE) &&
  281. (ns1 != Namespace.XML_NAMESPACE)) {
  282. String prefix = ns1.getPrefix();
  283. String uri = namespaces.getURI(prefix);
  284. if (!ns1.getURI().equals(uri)) { // output a new decl
  285. String attrName = getXmlnsTagFor(ns1);
  286. domElement.setAttribute(attrName, ns1.getURI());
  287. namespaces.push(ns1);
  288. }
  289. }
  290. // Crimson doesn't like setAttributeNS() for non-NS attribs
  291. if (attribute.getNamespace() == Namespace.NO_NAMESPACE) {
  292. // No namespace, use setAttribute
  293. if (forceNamespaceAware) {
  294. domElement.setAttributeNS(null,
  295. attribute.getQualifiedName(),
  296. attribute.getValue());
  297. } else {
  298. domElement.setAttribute(attribute.getQualifiedName(),
  299. attribute.getValue());
  300. }
  301. }
  302. else {
  303. domElement.setAttributeNS(attribute.getNamespaceURI(),
  304. attribute.getQualifiedName(),
  305. attribute.getValue());
  306. }
  307. }
  308. // Add content to the DOM element
  309. itr = element.getContent().iterator();
  310. while (itr.hasNext()) {
  311. Object node = itr.next();
  312. if (node instanceof Element) {
  313. Element e = (Element) node;
  314. org.w3c.dom.Element domElt = output(e, domDoc, namespaces);
  315. domElement.appendChild(domElt);
  316. }
  317. else if (node instanceof String) {
  318. String str = (String) node;
  319. org.w3c.dom.Text domText = domDoc.createTextNode(str);
  320. domElement.appendChild(domText);
  321. }
  322. else if (node instanceof CDATA) {
  323. CDATA cdata = (CDATA) node;
  324. org.w3c.dom.CDATASection domCdata =
  325. domDoc.createCDATASection(cdata.getText());
  326. domElement.appendChild(domCdata);
  327. }
  328. else if (node instanceof Text) {
  329. Text text = (Text) node;
  330. org.w3c.dom.Text domText =
  331. domDoc.createTextNode(text.getText());
  332. domElement.appendChild(domText);
  333. }
  334. else if (node instanceof Comment) {
  335. Comment comment = (Comment) node;
  336. org.w3c.dom.Comment domComment =
  337. domDoc.createComment(comment.getText());
  338. domElement.appendChild(domComment);
  339. }
  340. else if (node instanceof ProcessingInstruction) {
  341. ProcessingInstruction pi =
  342. (ProcessingInstruction) node;
  343. org.w3c.dom.ProcessingInstruction domPI =
  344. domDoc.createProcessingInstruction(
  345. pi.getTarget(), pi.getData());
  346. domElement.appendChild(domPI);
  347. }
  348. else if (node instanceof EntityRef) {
  349. EntityRef entity = (EntityRef) node;
  350. org.w3c.dom.EntityReference domEntity =
  351. domDoc.createEntityReference(entity.getName());
  352. domElement.appendChild(domEntity);
  353. }
  354. else {
  355. throw new JDOMException(
  356. "Element contained content with type:" +
  357. node.getClass().getName());
  358. }
  359. }
  360. // Remove declared namespaces from stack
  361. while (namespaces.size() > previouslyDeclaredNamespaces) {
  362. namespaces.pop();
  363. }
  364. return domElement;
  365. }
  366. catch (Exception e) {
  367. throw new JDOMException("Exception outputting Element " +
  368. element.getQualifiedName(), e);
  369. }
  370. }
  371. private org.w3c.dom.Attr output(Attribute attribute,
  372. org.w3c.dom.Document domDoc)
  373. throws JDOMException {
  374. org.w3c.dom.Attr domAttr = null;
  375. try {
  376. if (attribute.getNamespace() == Namespace.NO_NAMESPACE) {
  377. // No namespace, use createAttribute
  378. if (forceNamespaceAware) {
  379. domAttr = domDoc.createAttributeNS(null, attribute.getQualifiedName());
  380. } else {
  381. domAttr = domDoc.createAttribute(attribute.getQualifiedName());
  382. }
  383. }
  384. else {
  385. domAttr = domDoc.createAttributeNS(attribute.getNamespaceURI(),
  386. attribute.getQualifiedName());
  387. }
  388. domAttr.setValue(attribute.getValue());
  389. } catch (Exception e) {
  390. throw new JDOMException("Exception outputting Attribute " +
  391. attribute.getQualifiedName(), e);
  392. }
  393. return domAttr;
  394. }
  395. /**
  396. * This will handle adding any <code>{@link Namespace}</code>
  397. * attributes to the DOM tree.
  398. *
  399. * @param ns <code>Namespace</code> to add definition of
  400. */
  401. private static String getXmlnsTagFor(Namespace ns) {
  402. String attrName = "xmlns";
  403. if (!ns.getPrefix().equals("")) {
  404. attrName += ":";
  405. attrName += ns.getPrefix();
  406. }
  407. return attrName;
  408. }
  409. }