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.

DOMBuilder.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*--
  2. $Id: DOMBuilder.java,v 1.60 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 org.jdom.*;
  47. import org.jdom.Document;
  48. import org.jdom.Element;
  49. import org.w3c.dom.*;
  50. /**
  51. * Builds a JDOM {@link org.jdom.Document org.jdom.Document} from a pre-existing
  52. * DOM {@link org.w3c.dom.Document org.w3c.dom.Document}. Also handy for testing
  53. * builds from files to sanity check {@link SAXBuilder}.
  54. *
  55. * @version $Revision: 1.60 $, $Date: 2007/11/10 05:29:00 $
  56. * @author Brett McLaughlin
  57. * @author Jason Hunter
  58. * @author Philip Nelson
  59. * @author Kevin Regan
  60. * @author Yusuf Goolamabbas
  61. * @author Dan Schaffer
  62. * @author Bradley S. Huffman
  63. */
  64. public class DOMBuilder {
  65. private static final String CVS_ID =
  66. "@(#) $RCSfile: DOMBuilder.java,v $ $Revision: 1.60 $ $Date: 2007/11/10 05:29:00 $ $Name: jdom_1_1 $";
  67. /** Adapter class to use */
  68. private String adapterClass;
  69. /** The factory for creating new JDOM objects */
  70. private JDOMFactory factory = new DefaultJDOMFactory();
  71. /**
  72. * This creates a new DOMBuilder which will attempt to first locate
  73. * a parser via JAXP, then will try to use a set of default parsers.
  74. * The underlying parser will not validate.
  75. */
  76. public DOMBuilder() {
  77. }
  78. /**
  79. * This creates a new DOMBuilder using the specified DOMAdapter
  80. * implementation as a way to choose the underlying parser.
  81. * The underlying parser will not validate.
  82. *
  83. * @param adapterClass <code>String</code> name of class
  84. * to use for DOM building.
  85. */
  86. public DOMBuilder(String adapterClass) {
  87. this.adapterClass = adapterClass;
  88. }
  89. /*
  90. * This sets a custom JDOMFactory for the builder. Use this to build
  91. * the tree with your own subclasses of the JDOM classes.
  92. *
  93. * @param factory <code>JDOMFactory</code> to use
  94. */
  95. public void setFactory(JDOMFactory factory) {
  96. this.factory = factory;
  97. }
  98. /**
  99. * Returns the current {@link org.jdom.JDOMFactory} in use.
  100. * @return the factory in use
  101. */
  102. public JDOMFactory getFactory() {
  103. return factory;
  104. }
  105. /**
  106. * This will build a JDOM tree from an existing DOM tree.
  107. *
  108. * @param domDocument <code>org.w3c.dom.Document</code> object
  109. * @return <code>Document</code> - JDOM document object.
  110. */
  111. public Document build(org.w3c.dom.Document domDocument) {
  112. Document doc = factory.document(null);
  113. buildTree(domDocument, doc, null, true);
  114. return doc;
  115. }
  116. /**
  117. * This will build a JDOM Element from an existing DOM Element
  118. *
  119. * @param domElement <code> org.w3c.dom.Element</code> object
  120. * @return <code>Element</code> - JDOM Element object
  121. */
  122. public org.jdom.Element build(org.w3c.dom.Element domElement) {
  123. Document doc = factory.document(null);
  124. buildTree(domElement, doc, null, true);
  125. return doc.getRootElement();
  126. }
  127. /**
  128. * This takes a DOM <code>Node</code> and builds up
  129. * a JDOM tree, recursing until the DOM tree is exhausted
  130. * and the JDOM tree results.
  131. *
  132. * @param node <code>Code</node> to examine.
  133. * @param doc JDOM <code>Document</code> being built.
  134. * @param current <code>Element</code> that is current parent.
  135. * @param atRoot <code>boolean</code> indicating whether at root level.
  136. */
  137. private void buildTree(Node node,
  138. Document doc,
  139. Element current,
  140. boolean atRoot) {
  141. // Recurse through the tree
  142. switch (node.getNodeType()) {
  143. case Node.DOCUMENT_NODE:
  144. NodeList nodes = node.getChildNodes();
  145. for (int i=0, size=nodes.getLength(); i<size; i++) {
  146. buildTree(nodes.item(i), doc, current, true);
  147. }
  148. break;
  149. case Node.ELEMENT_NODE:
  150. String nodeName = node.getNodeName();
  151. String prefix = "";
  152. String localName = nodeName;
  153. int colon = nodeName.indexOf(':');
  154. if (colon >= 0) {
  155. prefix = nodeName.substring(0, colon);
  156. localName = nodeName.substring(colon + 1);
  157. }
  158. // Get element's namespace
  159. Namespace ns = null;
  160. String uri = node.getNamespaceURI();
  161. if (uri == null) {
  162. ns = (current == null) ? Namespace.NO_NAMESPACE
  163. : current.getNamespace(prefix);
  164. }
  165. else {
  166. ns = Namespace.getNamespace(prefix, uri);
  167. }
  168. Element element = factory.element(localName, ns);
  169. if (atRoot) {
  170. // If at root, set as document root
  171. doc.setRootElement(element); // XXX should we use a factory call?
  172. } else {
  173. // else add to parent element
  174. factory.addContent(current, element);
  175. }
  176. // Add namespaces
  177. NamedNodeMap attributeList = node.getAttributes();
  178. int attsize = attributeList.getLength();
  179. for (int i = 0; i < attsize; i++) {
  180. Attr att = (Attr) attributeList.item(i);
  181. String attname = att.getName();
  182. if (attname.startsWith("xmlns")) {
  183. String attPrefix = "";
  184. colon = attname.indexOf(':');
  185. if (colon >= 0) {
  186. attPrefix = attname.substring(colon + 1);
  187. }
  188. String attvalue = att.getValue();
  189. Namespace declaredNS =
  190. Namespace.getNamespace(attPrefix, attvalue);
  191. // Add as additional namespaces if it's different
  192. // than this element's namespace (perhaps we should
  193. // also have logic not to mark them as additional if
  194. // it's been done already, but it probably doesn't
  195. // matter)
  196. if (prefix.equals(attPrefix)) {
  197. element.setNamespace(declaredNS);
  198. }
  199. else {
  200. factory.addNamespaceDeclaration(element, declaredNS);
  201. }
  202. }
  203. }
  204. // Add attributes
  205. for (int i = 0; i < attsize; i++) {
  206. Attr att = (Attr) attributeList.item(i);
  207. String attname = att.getName();
  208. if ( !attname.startsWith("xmlns")) {
  209. String attPrefix = "";
  210. String attLocalName = attname;
  211. colon = attname.indexOf(':');
  212. if (colon >= 0) {
  213. attPrefix = attname.substring(0, colon);
  214. attLocalName = attname.substring(colon + 1);
  215. }
  216. String attvalue = att.getValue();
  217. // Get attribute's namespace
  218. Namespace attns = null;
  219. if ("".equals(attPrefix)) {
  220. attns = Namespace.NO_NAMESPACE;
  221. }
  222. else {
  223. attns = element.getNamespace(attPrefix);
  224. }
  225. Attribute attribute =
  226. factory.attribute(attLocalName, attvalue, attns);
  227. factory.setAttribute(element, attribute);
  228. }
  229. }
  230. // Recurse on child nodes
  231. // The list should never be null nor should it ever contain
  232. // null nodes, but some DOM impls are broken
  233. NodeList children = node.getChildNodes();
  234. if (children != null) {
  235. int size = children.getLength();
  236. for (int i = 0; i < size; i++) {
  237. Node item = children.item(i);
  238. if (item != null) {
  239. buildTree(item, doc, element, false);
  240. }
  241. }
  242. }
  243. break;
  244. case Node.TEXT_NODE:
  245. String data = node.getNodeValue();
  246. factory.addContent(current, factory.text(data));
  247. break;
  248. case Node.CDATA_SECTION_NODE:
  249. String cdata = node.getNodeValue();
  250. factory.addContent(current, factory.cdata(cdata));
  251. break;
  252. case Node.PROCESSING_INSTRUCTION_NODE:
  253. if (atRoot) {
  254. factory.addContent(doc,
  255. factory.processingInstruction(node.getNodeName(),
  256. node.getNodeValue()));
  257. } else {
  258. factory.addContent(current,
  259. factory.processingInstruction(node.getNodeName(),
  260. node.getNodeValue()));
  261. }
  262. break;
  263. case Node.COMMENT_NODE:
  264. if (atRoot) {
  265. factory.addContent(doc, factory.comment(node.getNodeValue()));
  266. } else {
  267. factory.addContent(current, factory.comment(node.getNodeValue()));
  268. }
  269. break;
  270. case Node.ENTITY_REFERENCE_NODE:
  271. EntityRef entity = factory.entityRef(node.getNodeName());
  272. factory.addContent(current, entity);
  273. break;
  274. case Node.ENTITY_NODE:
  275. // ??
  276. break;
  277. case Node.DOCUMENT_TYPE_NODE:
  278. DocumentType domDocType = (DocumentType)node;
  279. String publicID = domDocType.getPublicId();
  280. String systemID = domDocType.getSystemId();
  281. String internalDTD = domDocType.getInternalSubset();
  282. DocType docType = factory.docType(domDocType.getName());
  283. docType.setPublicID(publicID);
  284. docType.setSystemID(systemID);
  285. docType.setInternalSubset(internalDTD);
  286. factory.addContent(doc, docType);
  287. break;
  288. }
  289. }
  290. }