Permalink
Browse files

Implement a new concept of a thin DOM 'wrapper' around JDOM objects.

This makes it possible to feed JDOM content in to libraries that currently process DOM only, like the native Java XPath api, as well as Xalan.
Additionally, add two XPath implementations that use this new DOM layer to feed the native Java XPath API as well as the Xalan API
  • Loading branch information...
rolfl committed Feb 11, 2012
1 parent 4932b1d commit 3db8ae31cdc017c25eab1da3d40659635af56538
Showing with 4,527 additions and 0 deletions.
  1. +184 −0 contrib/src/java/org/jdom2/contrib/dom/DOM.java
  2. +177 −0 contrib/src/java/org/jdom2/contrib/dom/JAttribute.java
  3. +73 −0 contrib/src/java/org/jdom2/contrib/dom/JCDATA.java
  4. +74 −0 contrib/src/java/org/jdom2/contrib/dom/JComment.java
  5. +135 −0 contrib/src/java/org/jdom2/contrib/dom/JContent.java
  6. +101 −0 contrib/src/java/org/jdom2/contrib/dom/JDOMConfiguration.java
  7. +86 −0 contrib/src/java/org/jdom2/contrib/dom/JDOMImplementation.java
  8. +117 −0 contrib/src/java/org/jdom2/contrib/dom/JDocType.java
  9. +564 −0 contrib/src/java/org/jdom2/contrib/dom/JDocument.java
  10. +371 −0 contrib/src/java/org/jdom2/contrib/dom/JElement.java
  11. +87 −0 contrib/src/java/org/jdom2/contrib/dom/JEntityRef.java
  12. +169 −0 contrib/src/java/org/jdom2/contrib/dom/JNamespace.java
  13. +100 −0 contrib/src/java/org/jdom2/contrib/dom/JNamespaceAware.java
  14. +348 −0 contrib/src/java/org/jdom2/contrib/dom/JNode.java
  15. +83 −0 contrib/src/java/org/jdom2/contrib/dom/JNodeList.java
  16. +189 −0 contrib/src/java/org/jdom2/contrib/dom/JParent.java
  17. +102 −0 contrib/src/java/org/jdom2/contrib/dom/JProcessingInstruction.java
  18. +126 −0 contrib/src/java/org/jdom2/contrib/dom/JSimpleCharacterContent.java
  19. +82 −0 contrib/src/java/org/jdom2/contrib/dom/JSimpleContent.java
  20. +98 −0 contrib/src/java/org/jdom2/contrib/dom/JText.java
  21. +84 −0 contrib/src/java/org/jdom2/contrib/dom/Wrapper.java
  22. +5 −0 contrib/src/java/org/jdom2/contrib/dom/package.html
  23. +218 −0 contrib/src/java/org/jdom2/contrib/xpath/java/JavaXPathExpression.java
  24. +35 −0 contrib/src/java/org/jdom2/contrib/xpath/java/JavaXPathFactory.java
  25. +475 −0 contrib/src/java/org/jdom2/contrib/xpath/xalan/JDOM2DTM.java
  26. +114 −0 contrib/src/java/org/jdom2/contrib/xpath/xalan/NamespacePointer.java
  27. +218 −0 contrib/src/java/org/jdom2/contrib/xpath/xalan/XalanXPathExpression.java
  28. +25 −0 contrib/src/java/org/jdom2/contrib/xpath/xalan/XalanXPathFactory.java
  29. +51 −0 test/src/java/org/jdom2/test/cases/xpath/TestJavaCompiled.java
  30. +36 −0 test/src/java/org/jdom2/test/cases/xpath/TestXalanCompiled.java
@@ -0,0 +1,184 @@
+/*--
+
+ Copyright (C) 2012 Jason Hunter & Brett McLaughlin.
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions, and the disclaimer that follows
+ these conditions in the documentation and/or other materials
+ provided with the distribution.
+
+ 3. The name "JDOM" must not be used to endorse or promote products
+ derived from this software without prior written permission. For
+ written permission, please contact <request_AT_jdom_DOT_org>.
+
+ 4. Products derived from this software may not be called "JDOM", nor
+ may "JDOM" appear in their name, without prior written permission
+ from the JDOM Project Management <request_AT_jdom_DOT_org>.
+
+ In addition, we request (but do not require) that you include in the
+ end-user documentation provided with the redistribution and/or in the
+ software itself an acknowledgement equivalent to the following:
+ "This product includes software developed by the
+ JDOM Project (http://www.jdom.org/)."
+ Alternatively, the acknowledgment may be graphical using the logos
+ available at http://www.jdom.org/images/logos.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
+
+ This software consists of voluntary contributions made by many
+ individuals on behalf of the JDOM Project and was originally
+ created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
+ Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
+ on the JDOM Project, please see <http://www.jdom.org/>.
+
+ */
+
+package org.jdom2.contrib.dom;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.CDATASection;
+import org.w3c.dom.Comment;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.Element;
+import org.w3c.dom.EntityReference;
+import org.w3c.dom.ProcessingInstruction;
+import org.w3c.dom.Text;
+
+import org.jdom2.Content;
+
+/**
+ * Access JDOM Content using a (Read-Only) DOM model
+ *
+ * @author Rolf Lear
+ *
+ */
+public final class DOM {
+ /**
+ * Wrap a JDOM Document in a org.w3c.dom.Document instance.
+ * @param doc The JDOM Document to wrap.
+ * @return the wrapped Document
+ */
+ public static final Document wrap(final org.jdom2.Document doc) {
+ return wrap(doc, false);
+ }
+
+ /**
+ * Wrap a JDOM Document in a org.w3c.dom.Document instance.
+ * @param doc The JDOM Document to wrap.
+ * @param scan Whether the entire document should be pre-processed
+ * @return the wrapped Document
+ */
+ public static final Document wrap(
+ final org.jdom2.Document doc, final boolean scan) {
+ final JDocument ret = new JDocument(doc);
+ if (scan) {
+ ret.scanAll();
+ }
+ return ret;
+ }
+
+ private static final JDocument makeDoc(final Content c) {
+ final org.jdom2.Document doc = c.getDocument();
+ return new JDocument(doc);
+ }
+
+ /**
+ * Wrap a JDOM Element in a org.w3c.dom.Element instance.
+ * @param emt The JDOM Element to wrap.
+ * @return the wrapped Element
+ */
+ public static final Element wrap(final org.jdom2.Element emt) {
+ return makeDoc(emt).find(emt);
+ }
+
+
+ /**
+ * Wrap a JDOM Attribute in a org.w3c.dom.Attr instance.
+ * @param att The JDOM Attribute to wrap.
+ * @return the wrapped Attribute
+ */
+ public static final Attr wrap(final org.jdom2.Attribute att) {
+ final org.jdom2.Document doc = att.getDocument();
+ final JDocument jd = new JDocument(doc);
+ return jd.find(att);
+ }
+
+
+
+ /**
+ * Wrap a JDOM Text in a org.w3c.dom.Text instance.
+ * @param text The JDOM Text to wrap.
+ * @return the wrapped Text
+ */
+ public static final Text wrap(final org.jdom2.Text text) {
+ return makeDoc(text).find(text);
+ }
+
+ /**
+ * Wrap a JDOM CDATA in a org.w3c.dom.CDATASection instance.
+ * @param cdata The JDOM CDATA to wrap.
+ * @return the wrapped CDATA
+ */
+ public static final CDATASection wrap(final org.jdom2.CDATA cdata) {
+ return makeDoc(cdata).find(cdata);
+ }
+
+ /**
+ * Wrap a JDOM EntityRef in a org.w3c.dom.EntityReference instance.
+ * @param eref The JDOM EntityRef to wrap.
+ * @return the wrapped EtityRef
+ */
+ public static final EntityReference wrap(final org.jdom2.EntityRef eref) {
+ return makeDoc(eref).find(eref);
+ }
+
+ /**
+ * Wrap a JDOM ProcessingInstruction in a org.w3c.dom.ProcessingInstruction
+ * instance.
+ * @param pi The JDOM ProcessingInstruction to wrap.
+ * @return the wrapped ProcessingInstrction
+ */
+ public static final ProcessingInstruction wrap(
+ final org.jdom2.ProcessingInstruction pi) {
+ return makeDoc(pi).find(pi);
+ }
+
+ /**
+ * Wrap a JDOM Comment in a org.w3c.dom.Comment instance.
+ * @param comment The JDOM Comment to wrap.
+ * @return the wrapped Comment
+ */
+ public static final Comment wrap(final org.jdom2.Comment comment) {
+ return makeDoc(comment).find(comment);
+ }
+
+ /**
+ * Wrap a JDOM DocType in a org.w3c.dom.DocumentType instance.
+ * @param dt The JDOM DocType to wrap.
+ * @return the wrapped DocType
+ */
+ public static final DocumentType wrap(final org.jdom2.DocType dt) {
+ return makeDoc(dt).find(dt);
+ }
+
+}
@@ -0,0 +1,177 @@
+/*--
+
+ Copyright (C) 2012 Jason Hunter & Brett McLaughlin.
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions, and the disclaimer that follows
+ these conditions in the documentation and/or other materials
+ provided with the distribution.
+
+ 3. The name "JDOM" must not be used to endorse or promote products
+ derived from this software without prior written permission. For
+ written permission, please contact <request_AT_jdom_DOT_org>.
+
+ 4. Products derived from this software may not be called "JDOM", nor
+ may "JDOM" appear in their name, without prior written permission
+ from the JDOM Project Management <request_AT_jdom_DOT_org>.
+
+ In addition, we request (but do not require) that you include in the
+ end-user documentation provided with the redistribution and/or in the
+ software itself an acknowledgement equivalent to the following:
+ "This product includes software developed by the
+ JDOM Project (http://www.jdom.org/)."
+ Alternatively, the acknowledgment may be graphical using the logos
+ available at http://www.jdom.org/images/logos.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
+
+ This software consists of voluntary contributions made by many
+ individuals on behalf of the JDOM Project and was originally
+ created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
+ Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
+ on the JDOM Project, please see <http://www.jdom.org/>.
+
+ */
+
+package org.jdom2.contrib.dom;
+
+import java.net.URISyntaxException;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.TypeInfo;
+
+import org.jdom2.Attribute;
+import org.jdom2.AttributeType;
+import org.jdom2.Namespace;
+import org.jdom2.util.XMLBase;
+
+final class JAttribute extends JNamespaceAware implements Attr {
+
+ final Attribute attribute;
+ public JAttribute(final JDocument topdoc, final JParent parent, final Attribute attribute,
+ final Namespace[] nstack) {
+ super(topdoc, parent, Node.ATTRIBUTE_NODE, nstack);
+ this.attribute = attribute;
+ }
+
+ @Override
+ public final Object getWrapped() {
+ return attribute;
+ }
+
+ @Override
+ public String getNodeName() {
+ return getName();
+ }
+ @Override
+ public String getNodeValue() throws DOMException {
+ return getValue();
+ }
+ @Override
+ public NodeList getChildNodes() {
+ return EMPTYLIST;
+ }
+ @Override
+ public Node getFirstChild() {
+ return null;
+ }
+ @Override
+ public Node getLastChild() {
+ return null;
+ }
+ @Override
+ public NamedNodeMap getAttributes() {
+ return null;
+ }
+ @Override
+ public boolean hasChildNodes() {
+ return false;
+ }
+ @Override
+ public String getNamespaceURI() {
+ return attribute.getNamespaceURI();
+ }
+ @Override
+ public String getPrefix() {
+ return attribute.getNamespacePrefix();
+ }
+ @Override
+ public String getLocalName() {
+ return attribute.getName();
+ }
+ @Override
+ public boolean hasAttributes() {
+ return false;
+ }
+ @Override
+ public String getBaseURI() {
+ try {
+ return XMLBase.xmlBase(attribute.getParent()).toASCIIString();
+ } catch (final URISyntaxException e) {
+ throw new IllegalStateException("Unable to process URI", e);
+ }
+ }
+ @Override
+ public String getTextContent() throws DOMException {
+ return "";
+ }
+ @Override
+ public String getName() {
+ return attribute.getQualifiedName();
+ }
+ @Override
+ public boolean getSpecified() {
+ return true;
+ }
+ @Override
+ public String getValue() {
+ return attribute.getValue();
+ }
+
+ @Override
+ public Element getOwnerElement() {
+ return (Element)getParentNode();
+ }
+
+ @Override
+ public TypeInfo getSchemaTypeInfo() {
+ return TYPEINFO;
+ }
+
+ @Override
+ public boolean isId() {
+ return attribute.getAttributeType() == AttributeType.ID;
+ }
+
+ @Override
+ public void setValue(final String value) throws DOMException {
+ throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
+ "Cannot modify JDOM Wrapper DOM objects.");
+ }
+
+
+}
Oops, something went wrong.

0 comments on commit 3db8ae3

Please sign in to comment.