You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dm...@apache.org on 2002/08/27 00:29:49 UTC
cvs commit: jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml JDOMParser.java DocumentContainer.java
dmitri 2002/08/26 15:29:49
Modified: jxpath/src/java/org/apache/commons/jxpath/ri
JXPathContextReferenceImpl.java
jxpath/src/java/org/apache/commons/jxpath/xml
DocumentContainer.java
Added: jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom
package.html JDOMPointerFactory.java
JDOMNodePointer.java JDOMNodeIterator.java
JDOMNamespacePointer.java
JDOMNamespaceIterator.java
JDOMAttributePointer.java
JDOMAttributeIterator.java
jxpath/src/java/org/apache/commons/jxpath/xml
JDOMParser.java
Log:
Added JDOM support
Revision Changes Path
1.1 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/package.html
Index: package.html
===================================================================
<body>
Implementation of "model" APIs for JDOM (see jdom.org).
</body>
1.1 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMPointerFactory.java
Index: JDOMPointerFactory.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMPointerFactory.java,v 1.1 2002/08/26 22:29:48 dmitri Exp $
* $Revision: 1.1 $
* $Date: 2002/08/26 22:29:48 $
*
* ====================================================================
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999-2001 The Apache Software Foundation. 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 following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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 APACHE SOFTWARE FOUNDATION OR
* ITS 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 Apache Software Foundation and was
* originally based on software copyright (c) 2001, Plotnix, Inc,
* <http://www.plotnix.com/>.
* For more information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.jxpath.ri.model.jdom;
import java.util.Locale;
import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.ri.model.NodePointer;
import org.apache.commons.jxpath.ri.model.NodePointerFactory;
import org.jdom.Document;
import org.jdom.Element;
/**
* Implements NodePointerFactory for DOM elements.
*
* @author Dmitri Plotnikov
* @version $Revision: 1.1 $ $Date: 2002/08/26 22:29:48 $
*/
public class JDOMPointerFactory implements NodePointerFactory {
public static final int JDOM_POINTER_FACTORY_ORDER = 110;
public int getOrder(){
return JDOM_POINTER_FACTORY_ORDER;
}
public NodePointer createNodePointer(
QName name, Object bean, Locale locale)
{
if (bean instanceof Document){
return new JDOMNodePointer(bean, locale);
}
else if (bean instanceof Element){
return new JDOMNodePointer(bean, locale);
}
return null;
}
public NodePointer createNodePointer(
NodePointer parent, QName name, Object bean)
{
if (bean instanceof Document){
return new JDOMNodePointer(parent, bean);
}
else if (bean instanceof Element){
return new JDOMNodePointer(parent, bean);
}
return null;
}
}
1.1 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java
Index: JDOMNodePointer.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodePointer.java,v 1.1 2002/08/26 22:29:48 dmitri Exp $
* $Revision: 1.1 $
* $Date: 2002/08/26 22:29:48 $
*
* ====================================================================
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999-2001 The Apache Software Foundation. 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 following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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 APACHE SOFTWARE FOUNDATION OR
* ITS 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 Apache Software Foundation and was
* originally based on software copyright (c) 2001, Plotnix, Inc,
* <http://www.plotnix.com/>.
* For more information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.jxpath.ri.model.jdom;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.commons.jxpath.AbstractFactory;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathException;
import org.apache.commons.jxpath.ri.Compiler;
import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
import org.apache.commons.jxpath.ri.compiler.NodeTest;
import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
import org.apache.commons.jxpath.ri.compiler.ProcessingInstructionTest;
import org.apache.commons.jxpath.ri.model.NodeIterator;
import org.apache.commons.jxpath.ri.model.NodePointer;
import org.apache.commons.jxpath.util.TypeUtils;
import org.jdom.Attribute;
import org.jdom.CDATA;
import org.jdom.Comment;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.ProcessingInstruction;
import org.jdom.Text;
/**
* A Pointer that points to a DOM node.
*
* @author Dmitri Plotnikov
* @version $Revision: 1.1 $ $Date: 2002/08/26 22:29:48 $
*/
public class JDOMNodePointer extends NodePointer {
private Object node;
private Map namespaces;
private String defaultNamespace;
private String id;
public static final String XML_NAMESPACE_URI =
"http://www.w3.org/XML/1998/namespace";
public static final String XMLNS_NAMESPACE_URI =
"http://www.w3.org/2000/xmlns/";
public JDOMNodePointer(Object node, Locale locale){
super(null, locale);
this.node = node;
}
public JDOMNodePointer(Object node, Locale locale, String id){
super(null, locale);
this.node = node;
this.id = id;
}
public JDOMNodePointer(NodePointer parent, Object node){
super(parent);
this.node = node;
}
public NodeIterator childIterator(NodeTest test, boolean reverse,
NodePointer startWith) {
return new JDOMNodeIterator(this, test, reverse, startWith);
}
public NodeIterator attributeIterator(QName name){
return new JDOMAttributeIterator(this, name);
}
public NodeIterator namespaceIterator(){
return new JDOMNamespaceIterator(this);
}
public NodePointer namespacePointer(String prefix){
return new JDOMNamespacePointer(this, prefix);
}
public String getNamespaceURI(){
if (node instanceof Element){
Element element = (Element)node;
return element.getNamespaceURI();
}
return null;
}
public String getNamespaceURI(String prefix){
if (node instanceof Element){
Element element = (Element)node;
Namespace ns = element.getNamespace(prefix);
// System.err.println("PREFIX: " + prefix + " NS: " + ns);
if (ns == null){
return null;
}
return ns.getURI();
}
return null;
}
public int compareChildNodePointers(
NodePointer pointer1, NodePointer pointer2)
{
Object node1 = pointer1.getBaseValue();
Object node2 = pointer2.getBaseValue();
if (node1 == node2){
return 0;
}
if ((node1 instanceof Attribute) && !(node2 instanceof Attribute)){
return -1;
}
else if (!(node1 instanceof Attribute) && (node2 instanceof Attribute)){
return 1;
}
else if ((node1 instanceof Attribute) && (node2 instanceof Attribute)){
List list = ((Element)getNode()).getAttributes();
int length = list.size();
for (int i = 0; i < length; i++){
Object n = list.get(i);
if (n == node1){
return -1;
}
else if (n == node2){
return 1;
}
}
return 0; // Should not happen
}
if (!(node instanceof Element)){
throw new RuntimeException("JXPath internal error: " +
"compareChildNodes called for " + node);
}
List children = ((Element)node).getContent();
int length = children.size();
for (int i = 0; i < length; i++){
Object n = children.get(i);
if (n == node1){
return -1;
}
else if (n == node2){
return 1;
}
}
return 0;
}
/**
* @see org.apache.commons.jxpath.ri.model.NodePointer#getBaseValue()
*/
public Object getBaseValue() {
return node;
}
/**
* @see org.apache.commons.jxpath.ri.model.NodePointer#getName()
*/
public QName getName() {
String ns = null;
String ln = null;
if (node instanceof Element){
ns = ((Element)node).getNamespacePrefix();
if (ns != null && ns.equals("")){
ns = null;
}
ln = ((Element)node).getName();
}
else if (node instanceof ProcessingInstruction){
ln = ((ProcessingInstruction)node).getTarget();
}
return new QName(ns, ln);
}
public QName getExpandedName(){
return new QName(getNamespaceURI(), getName().getName());
}
/**
* @see org.apache.commons.jxpath.ri.model.NodePointer#getNode()
*/
public Object getNode() {
return node;
}
public Object getValue(){
if (node instanceof Element){
return ((Element)node).getTextTrim();
}
else if (node instanceof Comment){
String text = ((Comment)node).getText();
if (text != null){
text = text.trim();
}
return text;
}
else if (node instanceof Text){
return ((Text)node).getTextTrim();
}
else if (node instanceof CDATA){
return ((CDATA)node).getTextTrim();
}
else if (node instanceof ProcessingInstruction){
String text = ((ProcessingInstruction)node).getData();
if (text != null){
text = text.trim();
}
return text;
}
return null;
}
/**
* @see org.apache.commons.jxpath.Pointer#setValue(Object)
*/
public void setValue(Object value) {
String string = null;
if (value != null){
string = (String)TypeUtils.convert(value, String.class);
if (string.equals("")){
string = null;
}
}
if (node instanceof Text){
if (string != null){
((Text)node).setText(string);
}
else {
nodeParent(node).removeContent((Text)node);
}
}
else {
Element element = (Element)node;
// First remove all text from the element
List content = new ArrayList(element.getContent());
for (int i = content.size(); --i >= 0;){
Object child = content.get(i);
if (child instanceof Text){
element.removeContent((Text)node);
}
else if (child instanceof CDATA){
element.removeContent((CDATA)node);
}
}
if (string != null){
element.addContent(new Text(string));
}
}
}
public boolean testNode(NodeTest test){
return testNode(this, node, test);
}
public static boolean testNode(
NodePointer pointer, Object node, NodeTest test)
{
if (test == null){
return true;
}
else if (test instanceof NodeNameTest){
if (!(node instanceof Element)){
return false;
}
QName testName = ((NodeNameTest)test).getNodeName();
String testLocalName = testName.getName();
if (testLocalName.equals("*") ||
testLocalName.equals(
JDOMNodePointer.getLocalName((Element)node))){
String testPrefix = testName.getPrefix();
String nodePrefix = JDOMNodePointer.getPrefix((Element)node);
if (equalStrings(testPrefix, nodePrefix)){
return true;
}
String testNS = pointer.getNamespaceURI(testPrefix);
if (testNS == null){
return false;
}
String nodeNS = pointer.getNamespaceURI(nodePrefix);
return equalStrings(testNS, nodeNS);
}
}
else if (test instanceof NodeTypeTest){
switch (((NodeTypeTest)test).getNodeType()){
case Compiler.NODE_TYPE_NODE:
return node instanceof Element;
case Compiler.NODE_TYPE_TEXT:
return (node instanceof Text) ||
(node instanceof CDATA);
case Compiler.NODE_TYPE_COMMENT:
return node instanceof Comment;
case Compiler.NODE_TYPE_PI:
return node instanceof ProcessingInstruction;
}
return false;
}
else if (test instanceof ProcessingInstructionTest){
if (node instanceof ProcessingInstruction){
String testPI = ((ProcessingInstructionTest)test).getTarget();
String nodePI = ((ProcessingInstruction)node).getTarget();
return testPI.equals(nodePI);
}
}
return false;
}
private static boolean equalStrings(String s1, String s2){
if (s1 == null && s2 != null){
return false;
}
if (s1 != null && s2 == null){
return false;
}
if (s1 != null && !s1.trim().equals(s2.trim())){
return false;
}
return true;
}
public static String getPrefix(Object node){
if (node instanceof Element){
String prefix = ((Element)node).getNamespacePrefix();
return (prefix == null || prefix.equals("")) ? null : prefix;
}
else if (node instanceof Attribute){
String prefix = ((Attribute)node).getNamespacePrefix();
return (prefix == null || prefix.equals("")) ? null : prefix;
}
return null;
}
public static String getLocalName(Object node){
if (node instanceof Element){
return ((Element)node).getName();
}
else if (node instanceof Attribute){
return ((Attribute)node).getName();
}
return null;
}
/**
* Returns true if the xml:lang attribute for the current node
* or its parent has the specified prefix <i>lang</i>.
* If no node has this prefix, calls <code>super.isLanguage(lang)</code>.
*/
public boolean isLanguage(String lang){
String current = getLanguage();
if (current == null){
return super.isLanguage(lang);
}
return current.toUpperCase().startsWith(lang.toUpperCase());
}
protected String getLanguage(){
Object n = node;
while (n != null){
if (n instanceof Element){
Element e = (Element)n;
String attr = e.getAttributeValue("lang",
Namespace.XML_NAMESPACE);
if (attr != null && !attr.equals("")){
return attr;
}
}
n = nodeParent(n);
}
return null;
}
private Element nodeParent(Object node){
if (node instanceof Element){
return ((Element)node).getParent();
}
else if (node instanceof Text){
return ((Text)node).getParent();
}
else if (node instanceof CDATA){
return ((CDATA)node).getParent();
}
else if (node instanceof ProcessingInstruction){
return ((ProcessingInstruction)node).getParent();
}
else if (node instanceof Comment){
return ((Comment)node).getParent();
}
return null;
}
public NodePointer createChild(
JXPathContext context, QName name, int index)
{
if (index == WHOLE_COLLECTION){
index = 0;
}
if (!getAbstractFactory(context).
createObject(context, this, node, name.toString(), index)){
throw new JXPathException("Factory could not create " +
"a child node for path: " +
asPath() + "/" + name + "[" + (index+1) + "]");
}
NodeIterator it = childIterator(new NodeNameTest(name), false, null);
if (it == null || !it.setPosition(index + 1)){
throw new JXPathException("Factory could not create " +
"a child node for path: " +
asPath() + "/" + name + "[" + (index+1) + "]");
}
return it.getNodePointer();
}
public NodePointer createChild(
JXPathContext context, QName name, int index, Object value)
{
NodePointer ptr = createChild(context, name, index);
ptr.setValue(value);
return ptr;
}
public void remove(){
Element parent = nodeParent(node);
if (parent == null){
throw new JXPathException("Cannot remove root JDOM node");
}
parent.getContent().remove(node);
}
public String asPath(){
if (id != null){
return "id('" + escape(id) + "')";
}
StringBuffer buffer = new StringBuffer();
if (parent != null){
buffer.append(parent.asPath());
}
if (node instanceof Element){
// If the parent pointer is not a JDOMNodePointer, it is
// the parent's responsibility to produce the node test part
// of the path
if (parent instanceof JDOMNodePointer){
buffer.append('/');
buffer.append(getName());
buffer.append('[');
buffer.append(getRelativePositionByName());
buffer.append(']');
}
}
else if (node instanceof Text || node instanceof CDATA){
buffer.append("/text()");
buffer.append('[').
append(getRelativePositionOfTextNode()).
append(']');
}
else if (node instanceof ProcessingInstruction){
String target = ((ProcessingInstruction)node).getTarget();
buffer.append("/processing-instruction(\'").
append(target).
append("')");
buffer.append('[').
append(getRelativePositionOfPI(target)).
append(']');
}
return buffer.toString();
}
private String escape(String string){
int index = string.indexOf('\'');
while (index != -1){
string = string.substring(0, index) +
"'" + string.substring(index + 1);
index = string.indexOf('\'');
}
index = string.indexOf('\"');
while (index != -1){
string = string.substring(0, index) +
""" + string.substring(index + 1);
index = string.indexOf('\"');
}
return string;
}
private int getRelativePositionByName(){
if (node instanceof Element){
Element parent = ((Element)node).getParent();
if (parent == null){
return 1;
}
List children = parent.getContent();
int count = 0;
String name = ((Element)node).getQualifiedName();
for (int i = 0; i < children.size(); i++){
Object child = children.get(i);
if ((child instanceof Element) &&
((Element)child).getQualifiedName().equals(name)){
count++;
}
if (child == node){
break;
}
}
return count;
}
return 1;
}
private int getRelativePositionOfTextNode(){
Element parent;
if (node instanceof Text){
parent = ((Text)node).getParent();
}
else {
parent = ((CDATA)node).getParent();
}
if (parent == null){
return 1;
}
List children = parent.getContent();
int count = 0;
for (int i = 0; i < children.size(); i++){
Object child = children.get(i);
if (child instanceof Text || child instanceof CDATA){
count++;
}
if (child == node){
break;
}
}
return count;
}
private int getRelativePositionOfPI(String target){
Element parent = ((ProcessingInstruction)node).getParent();
if (parent == null){
return 1;
}
List children = parent.getContent();
int count = 0;
for (int i = 0; i < children.size(); i++){
Object child = children.get(i);
if (child instanceof ProcessingInstruction &&
(target == null ||
target.equals(((ProcessingInstruction)child).getTarget()))){
count++;
}
if (child == node){
break;
}
}
return count;
}
public int hashCode(){
return System.identityHashCode(node);
}
public boolean equals(Object object){
if (object == this){
return true;
}
if (!(object instanceof JDOMNodePointer)){
return false;
}
JDOMNodePointer other = (JDOMNodePointer)object;
return node == other.node;
}
private AbstractFactory getAbstractFactory(JXPathContext context){
AbstractFactory factory = context.getFactory();
if (factory == null){
throw new JXPathException(
"Factory is not set on the JXPathContext - " +
"cannot create path: " + asPath());
}
return factory;
}
}
1.1 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodeIterator.java
Index: JDOMNodeIterator.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNodeIterator.java,v 1.1 2002/08/26 22:29:48 dmitri Exp $
* $Revision: 1.1 $
* $Date: 2002/08/26 22:29:48 $
*
* ====================================================================
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999-2001 The Apache Software Foundation. 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 following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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 APACHE SOFTWARE FOUNDATION OR
* ITS 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 Apache Software Foundation and was
* originally based on software copyright (c) 2001, Plotnix, Inc,
* <http://www.plotnix.com/>.
* For more information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.jxpath.ri.model.jdom;
import java.util.Collections;
import java.util.List;
import org.apache.commons.jxpath.ri.compiler.NodeTest;
import org.apache.commons.jxpath.ri.model.NodeIterator;
import org.apache.commons.jxpath.ri.model.NodePointer;
import org.jdom.Document;
import org.jdom.Element;
/**
* An iterator of children of a JDOM Node.
*
* @author Dmitri Plotnikov
* @version $Revision: 1.1 $ $Date: 2002/08/26 22:29:48 $
*/
public class JDOMNodeIterator implements NodeIterator {
private NodePointer parent;
private NodeTest nodeTest;
private boolean reverse;
private int position = 0;
private int index = 0;
// private Element node;
private List children;
private Object child;
public JDOMNodeIterator(
NodePointer parent, NodeTest nodeTest,
boolean reverse, NodePointer startWith)
{
this.parent = parent;
if (startWith != null){
this.child = startWith.getNode();
}
// TBD: optimize me for different node tests
Object node = parent.getNode();
if (node instanceof Document){
this.children = ((Document)node).getContent();
}
else if (node instanceof Element){
this.children = ((Element)node).getContent();
}
else {
this.children = Collections.EMPTY_LIST;
}
this.nodeTest = nodeTest;
this.reverse = reverse;
}
public NodePointer getNodePointer(){
if (child == null){
if (!setPosition(1)){
return null;
}
position = 0;
}
return new JDOMNodePointer(parent, child);
}
public int getPosition(){
return position;
}
public boolean setPosition(int position){
while (this.position < position){
if (!next()){
return false;
}
}
while (this.position > position){
if (!previous()){
return false;
}
}
return true;
}
private boolean previous(){
System.err.println("PREVIOUS"); // TBD
// position--;
// if (!reverse){
// child = child.getPreviousSibling();
// while (child != null && !testChild()){
// child = child.getPreviousSibling();
// }
// }
// else {
// child = child.getNextSibling();
// while (child != null && !testChild()){
// child = child.getNextSibling();
// }
// }
// return child != null;
return false;
}
private boolean next(){
position++;
if (!reverse){
if (position == 1){
index = 0;
if (child != null){
index = children.indexOf(child) + 1;
}
}
else {
index++;
}
for (;index < children.size(); index++){
child = children.get(index);
if (testChild()){
return true;
}
}
return false;
}
else {
if (position == 1){
index = children.size() - 1;
if (child != null){
index = children.indexOf(child) - 1;
}
}
else {
index--;
}
for (;index >= 0; index--){
child = children.get(index);
if (testChild()){
return true;
}
}
return false;
}
}
private boolean testChild(){
return JDOMNodePointer.testNode(parent, child, nodeTest);
}
}
1.1 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNamespacePointer.java
Index: JDOMNamespacePointer.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNamespacePointer.java,v 1.1 2002/08/26 22:29:48 dmitri Exp $
* $Revision: 1.1 $
* $Date: 2002/08/26 22:29:48 $
*
* ====================================================================
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999-2001 The Apache Software Foundation. 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 following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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 APACHE SOFTWARE FOUNDATION OR
* ITS 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 Apache Software Foundation and was
* originally based on software copyright (c) 2001, Plotnix, Inc,
* <http://www.plotnix.com/>.
* For more information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.jxpath.ri.model.jdom;
import org.apache.commons.jxpath.ri.Compiler;
import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.ri.compiler.NodeTest;
import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* Represents a namespace node.
*
* @author Dmitri Plotnikov
* @version $Revision: 1.1 $ $Date: 2002/08/26 22:29:48 $
*/
public class JDOMNamespacePointer extends NodePointer {
private String prefix;
private String namespaceURI;
public JDOMNamespacePointer(NodePointer parent, String prefix){
super(parent);
this.prefix = prefix;
}
public JDOMNamespacePointer(NodePointer parent, String prefix, String namespaceURI){
super(parent);
this.prefix = prefix;
this.namespaceURI = namespaceURI;
}
public QName getName(){
return new QName(getNamespaceURI(), prefix);
}
public Object getBaseValue(){
return null;
}
public Object getNode(){
return getNamespaceURI();
}
public String getNamespaceURI(){
if (namespaceURI == null){
namespaceURI = parent.getNamespaceURI(prefix);
}
return namespaceURI;
}
public boolean isLeaf(){
return true;
}
/**
* Throws UnsupportedOperationException.
*/
public void setValue(Object value){
throw new UnsupportedOperationException("Cannot modify a namespace");
}
/*
public boolean testNode(NodeTest nodeTest){
return nodeTest == null ||
((nodeTest instanceof NodeTypeTest) &&
((NodeTypeTest)nodeTest).getNodeType() == Compiler.NODE_TYPE_NODE);
}
*/
public String asPath(){
StringBuffer buffer = new StringBuffer();
if (parent != null){
buffer.append(parent.asPath());
buffer.append('/');
}
buffer.append("namespace::");
buffer.append(prefix);
return buffer.toString();
}
public int hashCode(){
return prefix.hashCode();
}
public boolean equals(Object object){
if (object == this){
return true;
}
if (!(object instanceof JDOMNamespacePointer)){
return false;
}
JDOMNamespacePointer other = (JDOMNamespacePointer)object;
return prefix.equals(other.prefix);
}
public int compareChildNodePointers(NodePointer pointer1, NodePointer pointer2){
// Won't happen - namespaces don't have children
return 0;
}
}
1.1 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNamespaceIterator.java
Index: JDOMNamespaceIterator.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMNamespaceIterator.java,v 1.1 2002/08/26 22:29:48 dmitri Exp $
* $Revision: 1.1 $
* $Date: 2002/08/26 22:29:48 $
*
* ====================================================================
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999-2001 The Apache Software Foundation. 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 following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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 APACHE SOFTWARE FOUNDATION OR
* ITS 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 Apache Software Foundation and was
* originally based on software copyright (c) 2001, Plotnix, Inc,
* <http://www.plotnix.com/>.
* For more information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.jxpath.ri.model.jdom;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import org.apache.commons.jxpath.ri.model.NodeIterator;
import org.apache.commons.jxpath.ri.model.NodePointer;
import org.jdom.*;
/**
* An iterator of namespaces of a DOM Node.
*
* @author Dmitri Plotnikov
* @version $Revision: 1.1 $ $Date: 2002/08/26 22:29:48 $
*/
public class JDOMNamespaceIterator implements NodeIterator {
private NodePointer parent;
private List namespaces;
private Set prefixes;
private int position = 0;
public JDOMNamespaceIterator(NodePointer parent){
this.parent = parent;
Object node = parent.getNode();
if (node instanceof Element){
namespaces = new ArrayList();
prefixes = new HashSet();
collectNamespaces((Element)parent.getNode());
// System.err.println("NAMESPACES: " + namespaces);
}
}
private void collectNamespaces(Element element){
// NamedNodeMap map = node.getAttributes();
// int count = map.getLength();
// for (int i = 0; i < count; i++){
// Attr attr = (Attr)map.item(i);
// String prefix = DOMNodePointer.getPrefix(attr);
// String name = DOMNodePointer.getLocalName(attr);
// if ((prefix != null && prefix.equals("xmlns")) ||
// (prefix == null && name.equals("xmlns"))){
// attributes.add(attr);
// }
// }
Namespace ns = element.getNamespace();
if (ns != null && !prefixes.contains(ns.getPrefix())){
namespaces.add(ns);
prefixes.add(ns.getPrefix());
}
List others = element.getAdditionalNamespaces();
for (int i = 0; i < others.size(); i++){
ns = (Namespace)others.get(i);
if (ns != null && !prefixes.contains(ns.getPrefix())){
namespaces.add(ns);
prefixes.add(ns.getPrefix());
}
}
Element parent = element.getParent();
if (parent != null){
collectNamespaces(parent);
}
}
public NodePointer getNodePointer(){
if (position == 0){
if (!setPosition(1)){
return null;
}
position = 0;
}
int index = position - 1;
if (index < 0){
index = 0;
}
String prefix = "";
Namespace ns = (Namespace)namespaces.get(index);
// String name = attr.getPrefix();
// if (name != null && name.equals("xmlns")){
// prefix = JDOMNodePointer.getLocalName(attr);
// }
return new JDOMNamespacePointer(parent, ns.getPrefix(), ns.getURI());
}
public int getPosition(){
return position;
}
public boolean setPosition(int position){
this.position = position;
return position >= 1 && position <= namespaces.size();
}
}
1.1 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMAttributePointer.java
Index: JDOMAttributePointer.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMAttributePointer.java,v 1.1 2002/08/26 22:29:48 dmitri Exp $
* $Revision: 1.1 $
* $Date: 2002/08/26 22:29:48 $
*
* ====================================================================
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999-2001 The Apache Software Foundation. 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 following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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 APACHE SOFTWARE FOUNDATION OR
* ITS 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 Apache Software Foundation and was
* originally based on software copyright (c) 2001, Plotnix, Inc,
* <http://www.plotnix.com/>.
* For more information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.jxpath.ri.model.jdom;
import org.apache.commons.jxpath.ri.Compiler;
import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.ri.compiler.NodeTest;
import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
import org.apache.commons.jxpath.ri.model.NodePointer;
import org.apache.commons.jxpath.util.TypeUtils;
import org.jdom.Attribute;
/**
* A Pointer that points to a DOM node.
*
* @author Dmitri Plotnikov
* @version $Revision: 1.1 $ $Date: 2002/08/26 22:29:48 $
*/
public class JDOMAttributePointer extends NodePointer {
private Attribute attr;
public JDOMAttributePointer(NodePointer parent, Attribute attr){
super(parent);
this.attr = attr;
}
public QName getName(){
return new QName(JDOMNodePointer.getPrefix(attr), JDOMNodePointer.getLocalName(attr));
}
public QName getExpandedName(){
return new QName(getNamespaceURI(), attr.getName());
}
public String getNamespaceURI(){
String uri = attr.getNamespaceURI();
if (uri != null && uri.equals("")){
uri = null;
}
return uri;
// String prefix = DOMNodePointer.getPrefix(attr);
// if (prefix == null){
// return null;
// }
// return parent.getNamespaceURI(prefix);
}
public Object getBaseValue(){
return attr;
}
public Object getNode(){
String value = attr.getValue();
if (value == null){
return null;
}
// if (value.equals("") && !attr.getSpecified()){
// return null;
// }
return value;
}
public boolean isActual(){
return true;
}
public boolean isLeaf(){
return true;
}
// public boolean testNode(NodeTest nodeTest){
// return nodeTest == null ||
// ((nodeTest instanceof NodeTypeTest) &&
// ((NodeTypeTest)nodeTest).getNodeType() == Compiler.NODE_TYPE_NODE);
// }
/**
* Sets the value of this attribute.
*/
public void setValue(Object value){
attr.setValue((String)TypeUtils.convert(value, String.class));
}
public void remove(){
attr.getParent().removeAttribute(attr);
}
/**
*/
public String asPath(){
StringBuffer buffer = new StringBuffer();
if (parent != null){
buffer.append(parent.asPath());
buffer.append('/');
}
buffer.append('@');
buffer.append(getName());
return buffer.toString();
}
public int hashCode(){
return System.identityHashCode(attr);
}
public boolean equals(Object object){
if (object == this){
return true;
}
if (!(object instanceof JDOMAttributePointer)){
return false;
}
JDOMAttributePointer other = (JDOMAttributePointer)object;
return attr == other.attr;
}
public int compareChildNodePointers(NodePointer pointer1, NodePointer pointer2){
// Won't happen - attributes don't have children
return 0;
}
}
1.1 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMAttributeIterator.java
Index: JDOMAttributeIterator.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/jdom/JDOMAttributeIterator.java,v 1.1 2002/08/26 22:29:48 dmitri Exp $
* $Revision: 1.1 $
* $Date: 2002/08/26 22:29:48 $
*
* ====================================================================
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999-2001 The Apache Software Foundation. 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 following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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 APACHE SOFTWARE FOUNDATION OR
* ITS 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 Apache Software Foundation and was
* originally based on software copyright (c) 2001, Plotnix, Inc,
* <http://www.plotnix.com/>.
* For more information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.jxpath.ri.model.jdom;
import java.util.*;
import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.ri.model.NodeIterator;
import org.apache.commons.jxpath.ri.model.NodePointer;
import org.jdom.*;
/**
* An iterator of attributes of a DOM Node.
*
* @author Dmitri Plotnikov
* @version $Revision: 1.1 $ $Date: 2002/08/26 22:29:48 $
*/
public class JDOMAttributeIterator implements NodeIterator {
private NodePointer parent;
private QName name;
private List attributes;
private int position = 0;
public JDOMAttributeIterator(NodePointer parent, QName name){
this.parent = parent;
this.name = name;
if (parent.getNode() instanceof Element){
Element element = (Element)parent.getNode();
// System.err.println("ELEMENT: " + element.getName());
// List a = element.getAttributes();
// for (int i = 0; i < a.size(); i++){
// Attribute x = (Attribute)a.get(i);
// System.err.println("ATTR: " + x.getName() + " " + x.getNamespace());
// }
String prefix = name.getPrefix();
Namespace ns;
if (prefix != null){
if (prefix.equals("xml")){
ns = Namespace.XML_NAMESPACE;
}
else {
ns = element.getNamespace(prefix);
if (ns == null){
// TBD: no attributes
attributes = Collections.EMPTY_LIST;
return;
}
}
}
else {
ns = Namespace.NO_NAMESPACE;
}
String lname = name.getName();
if (!lname.equals("*")){
attributes = new ArrayList();
if (ns != null){
Attribute attr = element.getAttribute(lname, ns);
// System.err.println("LNAME=" + lname + " NS: " + ns + " ATTR: " + attr);
if (attr != null){
attributes.add(attr);
}
}
}
else {
attributes = new ArrayList();
List allAttributes = element.getAttributes();
for (int i = 0; i < allAttributes.size(); i++){
Attribute attr = (Attribute)allAttributes.get(i);
if (attr.getNamespace().equals(ns)){
attributes.add(attr);
}
}
}
}
}
/*
private boolean testAttr(Attr attr, QName testName){
String nodePrefix = DOMNodePointer.getPrefix(attr);
String nodeLocalName = DOMNodePointer.getLocalName(attr);
if (nodePrefix != null && nodePrefix.equals("xmlns")){
return false;
}
if (nodePrefix == null && nodeLocalName.equals("xmlns")){
return false;
}
String testLocalName = name.getName();
if (testLocalName.equals("*") || testLocalName.equals(nodeLocalName)){
String testPrefix = testName.getPrefix();
if (equalStrings(testPrefix, nodePrefix)){
return true;
}
String testNS = null;
if (testPrefix != null){
testNS = parent.getNamespaceURI(testPrefix);
}
String nodeNS = null;
if (nodePrefix != null){
nodeNS = parent.getNamespaceURI(nodePrefix);
}
return equalStrings(testNS, nodeNS);
}
return false;
}
private static boolean equalStrings(String s1, String s2){
if (s1 == null && s2 != null){
return false;
}
if (s1 != null && !s1.equals(s2)){
return false;
}
return true;
}
private Attr getAttribute(Element element, QName name){
String testPrefix = name.getPrefix();
String testNS = null;
if (testPrefix != null){
testNS = parent.getNamespaceURI(testPrefix);
}
if (testNS != null){
Attr attr = element.getAttributeNodeNS(testNS, name.getName());
if (attr == null){
// This may mean that the parser does not support NS for
// attributes, example - the version of Crimson bundled
// with JDK 1.4.0
NamedNodeMap nnm = element.getAttributes();
for (int i = 0; i < nnm.getLength(); i++){
attr = (Attr)nnm.item(i);
if (testAttr(attr, name)){
return attr;
}
}
}
return attr;
}
else {
return element.getAttributeNode(name.getName());
}
}
*/
public NodePointer getNodePointer(){
if (position == 0){
if (!setPosition(1)){
return null;
}
position = 0;
}
int index = position - 1;
if (index < 0){
index = 0;
}
return new JDOMAttributePointer(parent, (Attribute)attributes.get(index));
}
public int getPosition(){
return position;
}
public boolean setPosition(int position){
if (attributes == null){
return false;
}
this.position = position;
return position >= 1 && position <= attributes.size();
}
}
1.21 +6 -6 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java
Index: JXPathContextReferenceImpl.java
===================================================================
RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- JXPathContextReferenceImpl.java 10 Aug 2002 01:30:39 -0000 1.20
+++ JXPathContextReferenceImpl.java 26 Aug 2002 22:29:49 -0000 1.21
@@ -106,14 +106,14 @@
if (domFactory != null){
nodeFactories.add(domFactory);
}
-/* TBD
+
Object jdomFactory = allocateConditionally(
"org.apache.commons.jxpath.ri.model.jdom.JDOMPointerFactory",
"org.jdom.Document");
if (jdomFactory != null){
nodeFactories.add(jdomFactory);
}
-*/
+
nodeFactories.add(new ContainerPointerFactory());
createNodeFactoryArray();
}
1.2 +15 -14 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/DocumentContainer.java
Index: DocumentContainer.java
===================================================================
RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/DocumentContainer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DocumentContainer.java 10 Aug 2002 01:28:49 -0000 1.1
+++ DocumentContainer.java 26 Aug 2002 22:29:49 -0000 1.2
@@ -84,30 +84,31 @@
public class DocumentContainer implements Container {
public static final String MODEL_DOM = "DOM";
-// public static final String MODEL_JDOM = "JDOM";
+ public static final String MODEL_JDOM = "JDOM";
private Object document;
private URL xmlURL;
private String model;
-
+
private static HashMap parserClasses = new HashMap();
static {
- parserClasses.put(MODEL_DOM,
+ parserClasses.put(MODEL_DOM,
"org.apache.commons.jxpath.xml.DOMParser");
-// parserClasses.put(MODEL_JDOM,
-// "org.apache.commons.jxpath.xml.JDOMParser");
+ parserClasses.put(MODEL_JDOM,
+ "org.apache.commons.jxpath.xml.JDOMParser");
}
+
private static HashMap parsers = new HashMap();
/**
* Use this constructor if the desired model is DOM.
- *
- * @param URL is a URL for an XML file.
- * Use getClass().getResource(resourceName) to load XML from a
+ *
+ * @param URL is a URL for an XML file.
+ * Use getClass().getResource(resourceName) to load XML from a
* resource file.
*/
public DocumentContainer(URL xmlURL){
- this(xmlURL, MODEL_DOM);
+ this(xmlURL, MODEL_JDOM);
}
/**
@@ -159,7 +160,7 @@
public void setValue(Object value){
throw new UnsupportedOperationException();
}
-
+
/**
* Maps a model type to a parser.
*/
1.1 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/JDOMParser.java
Index: JDOMParser.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/xml/JDOMParser.java,v 1.1 2002/08/26 22:29:49 dmitri Exp $
* $Revision: 1.1 $
* $Date: 2002/08/26 22:29:49 $
*
* ====================================================================
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999-2001 The Apache Software Foundation. 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 following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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 APACHE SOFTWARE FOUNDATION OR
* ITS 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 Apache Software Foundation and was
* originally based on software copyright (c) 2001, Plotnix, Inc,
* <http://www.plotnix.com/>.
* For more information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.jxpath.xml;
import java.io.InputStream;
import org.apache.commons.jxpath.JXPathException;
import org.jdom.input.SAXBuilder;
/**
* An implementation of the XMLParser interface that produces a JDOM Document.
*
* @author Dmitri Plotnikov
* @version $Revision: 1.1 $ $Date: 2002/08/26 22:29:49 $
*/
public class JDOMParser implements XMLParser {
public Object parseXML(InputStream stream){
try {
SAXBuilder builder = new SAXBuilder();
return builder.build(stream);
}
catch (Exception ex){
throw new JXPathException("JDOM parser error", ex);
}
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>