Open Source Repository

Home /xom/xom-1.1 | Repository Home



nu/xom/JaxenNavigator.java
/* Copyright 2005 Elliotte Rusty Harold
   
   This library is free software; you can redistribute it and/or modify
   it under the terms of version 2.1 of the GNU Lesser General Public 
   License as published by the Free Software Foundation.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
   GNU Lesser General Public License for more details.
   
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the 
   Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
   Boston, MA 02111-1307  USA
   
   You can contact Elliotte Rusty Harold by sending e-mail to
   [email protected]. Please include the word "XOM" in the
   subject line. The XOM home page is located at http://www.xom.nu/
*/

package nu.xom;

/**
 <p>
 * Interface between Jaxen and XOM.
 </p>
 
 @author Elliotte Rusty Harold
 @version 1.1b6
 *
 */

import org.jaxen.DefaultNavigator;
import org.jaxen.FunctionCallException;
import org.jaxen.JaxenConstants;
import org.jaxen.JaxenException;
import org.jaxen.NamedAccessNavigator;
import org.jaxen.UnsupportedAxisException;
import org.jaxen.XPath;
import org.jaxen.util.SingleObjectIterator;

import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.NoSuchElementException;


class JaxenNavigator extends DefaultNavigator implements NamedAccessNavigator {


    private static final long serialVersionUID = 7008740797833836742L;


    public Iterator getSelfAxisIterator(Object contextNode) {
        
        if (contextNode instanceof Text) {
            // wrap text nodes in a list
            Text node = (TextcontextNode;
            ArrayList temp = new ArrayList();
            ParentNode parent = node.getParent();
            // parent is never null here due to DocumentFragment
            int index = parent.indexOf(node);
            int first = index;
            int last = index;
            while (first > && parent.getChild(first-1).isText()) {
                first--;
            }
            while (last < parent.getChildCount()-&& parent.getChild(last+1).isText()) {
                last++;
            }
            for (int i = first; i <= last; i++) {
                temp.add(parent.getChild(i));
            }
            contextNode = temp;
        }
        return new SingleObjectIterator(contextNode);
        
    }
    
    
    public Object getElementById(Object node, String id) {
        
        Node original;
        if (node instanceof ArrayList) {
            original = (Node) ((Listnode).get(0);
        }
        else {
            original = (Nodenode;
        }
        ParentNode parent;
        if (original.isElement() || original.isDocument()) {
            parent = (ParentNodeoriginal;
        }
        else {
            parent = original.getParent();
        }
        
        // find highest parent node
        ParentNode high = parent;
        while (parent != null) {
            high = parent;
            parent = parent.getParent();
        }
        
        // Now search down from the highest point for the requested ID
        Element root;
        if (high.isDocument()) {
            root = ((Documenthigh).getRootElement();
        }
        else // document fragment
            Node first = high.getChild(0);
            if (first.isElement()) {
                root = (Elementhigh.getChild(0);
            }
            else {
                return null;
            }
        }
        
        return findByID(root, id);
        
    }
    
    
    // ????remove recursion
    public static Element findByID(Element top, String id) {
        
        if (hasID(top, id)) return top;
        else {
            Elements children = top.getChildElements();
            for (int i = 0; i < children.size(); i++) {
                Element result = findByID(children.get(i), id);
                if (result != nullreturn result;
            }
        }
        return null;
        
    }
    
    
    private static boolean hasID(Element top, String id) {

        int count = top.getAttributeCount();
        for (int i = 0; i < count; i++) {
            Attribute a = top.getAttribute(i);
            if (Attribute.Type.ID == a.getType()) {
                // Do not need to fully normalize here
                // because if the value passed to the id() function
                // contains any spaces; then it is converted into a
                // search for multiple IDs, none of which have spaces
                return a.getValue().trim().equals(id);
            }
        }
        return false;
        
    }


    public String getNamespacePrefix(Object o) {
        Namespace ns = (Namespaceo;
        return ns.getPrefix();
    }
    
    
    public String getNamespaceStringValue(Object o) {
        Namespace ns = (Namespaceo;
        return ns.getValue();
    }

    
    public Iterator getNamespaceAxisIterator(Object contextNode) {
        
        try {
            Element element = (ElementcontextNode;
            // ???? can probably avoid this list copy
            Map bindings = element.getNamespacePrefixesInScope();
            Iterator iterator = bindings.entrySet().iterator();
            List result = new ArrayList(bindings.size()+1);
            result.add(new Namespace("xml"
              "http://www.w3.org/XML/1998/namespace", element));

            while (iterator.hasNext()) {
                Map.Entry binding = (Map.Entryiterator.next();
                String prefix = (Stringbinding.getKey();
                String uri = (Stringbinding.getValue();
                if ("".equals(prefix|| ! "".equals(uri)) {
                    Namespace ns = new Namespace(prefix, uri, element);
                    result.add(ns);
                }
            }
            return result.iterator();
        }
        catch (ClassCastException ex) {
            return JaxenConstants.EMPTY_ITERATOR;
        }
        
    }
    
    
    public Iterator getParentAxisIterator(Object contextNode)  {
        
        Node parent = (NodegetParentNode(contextNode);
        if (parent == nullreturn JaxenConstants.EMPTY_ITERATOR;
        else return new SingleObjectIterator(parent);
        
    }
    
    
    public Object getDocumentNode(Object o) {
    
        Node node = (Nodeo;
        return node.getRoot();
        
    }
    
        
    public Object getDocument(String urlthrows FunctionCallException {
        throw new FunctionCallException("document() function not supported");
    }
    
    public Iterator getAttributeAxisIterator(Object contextNode) {
        
        try {
            Element element = (ElementcontextNode;
            return element.attributeIterator();
        }
        catch (ClassCastException ex) {
            return JaxenConstants.EMPTY_ITERATOR;
        }
        
    }
    
    
    public Iterator getChildAxisIterator(Object o) {
        
        if (instanceof ParentNode) {
            return new ChildIterator((ParentNodeo);
        }
        else {
            return JaxenConstants.EMPTY_ITERATOR;
        }
        
    }
    
    
    public Iterator getFollowingSiblingAxisIterator(Object o) {
        
        Node start;
        if (instanceof ArrayList) {
            List l = (ArrayListo;
            start = (Nodel.get(l.size()-1);
        }
        else {
            start = (Nodeo;
        }
        ParentNode parent = start.getParent();
        if (parent == nullreturn JaxenConstants.EMPTY_ITERATOR;
        int startPos = parent.indexOf(start1;
        return new ChildIterator(parent, startPos);
        
    }
    
    
    public Object getParentNode(Object o) {
        
        Node n;
        if (instanceof ArrayList) {
            n = (Node) ((Listo).get(0);
        }
        else {
            n = (Nodeo;
        }
        return n.getParent();
        
    }

    
    public String getTextStringValue(Object o) {

        List texts = (Listo;
        if (texts.size() == 1) {
            return ((Texttexts.get(0)).getValue();
        }
        else {
            StringBuffer result = new StringBuffer();
            Iterator iterator = texts.iterator();
            while (iterator.hasNext()) {
                Text text = (Textiterator.next();
                result.append(text.getValue());
            }
            return result.toString();
        }
        
    }
    

    private static class ChildIterator implements Iterator {
    
        private final ParentNode parent;

        private int xomIndex = 0;
        private final int xomCount;
        
        ChildIterator(ParentNode parent) {
            this.parent = parent;
            this.xomCount = parent.getChildCount();
        }
      
        
        ChildIterator(ParentNode parent, int startNode) {
            this.parent = parent;
            this.xomIndex = startNode;
            this.xomCount = parent.getChildCount();
        }
      
        
        public boolean hasNext() {
            
            for (int i = xomIndex; i < xomCount; i++) {
                Node next = parent.getChild(i)
                if (next.isText()) {
                    if (((Textnext).isEmpty()) {
                        return true;
                    }
                }
                else return true;
            }
            return false;
            
        }
        

        public Object next() {
            
            Object result;
            Node next = parent.getChild(xomIndex++);
            if (next.isText()) {
                Text t = (Textnext;
                // Is this an empty text node?
                boolean empty = t.isEmpty();
                List texts = new ArrayList(1);
                texts.add(t);
                while (xomIndex < xomCount) {
                    Node nextText = parent.getChild(xomIndex);
                    if (! nextText.isText()) break;
                    xomIndex++;
                    texts.add(nextText);
                    if (empty) {
                        if (((TextnextText).isEmpty()) {
                            empty = false;
                        }
                    }
                }
                // need to make sure at least one of these texts is non-empty
                if (emptyreturn next();
                else result = texts;
            }
            else if (next.isDocType()) {
                return next();
            }
            else {
                result = next;
            }
            return result;
            
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
        
    }
    

    private static class NamedChildIterator implements Iterator {
    
        private final ParentNode parent;

        private int index = -1;
        private final int xomCount;
        private Element next;
        private final String localName;
        private final String URI;
        
        NamedChildIterator(ParentNode parent, String localName, String prefix, String namespaceURI) {
            this.parent = parent;
            this.xomCount = parent.getChildCount();
            this.localName = localName;
            if (namespaceURI == nullthis.URI = "";
            else this.URI = namespaceURI;
            
            findNext();
        }
      
        private void findNext() {
            
            while (++index < xomCount) {
                Node next = parent.getChild(index);
                if (next.isElement()) {
                    Element element = (Elementnext;
                    String elementNamespace = element.getNamespaceURI();
                    if (elementNamespace.equals(URI)) {
                        // I don't need to worry about the prefix here because XPath only iterates
                        // by local name and namespace URI. The prefix doesn't matter. 
                        // This does assume that this class is non-public.
                        if (element.getLocalName().equals(localName)) {
                            this.next = element;
                            return;
                        }
                    }
                }
            }
            next = null;
        }
        
        public boolean hasNext() {
            return next != null;
        }
        

        public Object next() {
            
            if (next == nullthrow new NoSuchElementException()// Correct? Yes. Necessary????
            Object result = next;
            findNext();
            return result;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
        
    }

    
    public String getElementNamespaceUri(Object element) {
        return ((Elementelement).getNamespaceURI();
    }

    
    // In Jaxen, name means the local name only 
    public String getElementName(Object element) {
        return ((Elementelement).getLocalName();
    }

    public String getElementQName(Object element) {
        return ((Elementelement).getQualifiedName();
    }

    
    public String getAttributeNamespaceUri(Object attr) {
        Attribute attribute = (Attributeattr;
        return attribute.getNamespaceURI();
    }

    
    // In Jaxen, name means the local name only 
    public String getAttributeName(Object attr) {
        Attribute attribute = (Attributeattr;
        return attribute.getLocalName();
    }

    
    public String getAttributeQName(Object attr) {
        Attribute attribute = (Attributeattr;
        return attribute.getQualifiedName();
    }

    public String getProcessingInstructionData(Object o) {
        ProcessingInstruction pi = (ProcessingInstructiono;
        return pi.getValue();
    }

   
    public String getProcessingInstructionTarget(Object o) {
        ProcessingInstruction pi = (ProcessingInstructiono;
        return pi.getTarget();
    }

    
    public boolean isDocument(Object object) {
        return object instanceof Document || object instanceof DocumentFragment;
    }

    
    public boolean isElement(Object object) {
        return object instanceof Element;
    }

    
    public boolean isAttribute(Object object) {
        return object instanceof Attribute;
    }

    
    public boolean isNamespace(Object object) {
        return object instanceof Namespace;
    }

    
    public boolean isComment(Object object) {
        return object instanceof Comment;
    }

    
    public boolean isText(Object object) {
        // ???? hack: need to use a separate special subclass of ArrayList I can identify.
        // But may not be necessary since this is not a public API. I don't 
        // think there's any way to get a different ArrayList into this method.
        if (object instanceof ArrayList) {
            Iterator iterator = ((Listobject).iterator();
            while (iterator.hasNext()) {
                if ((iterator.next() instanceof Text)) return false;
            }
            return true;
        }
        return false;
    }

    
    public boolean isProcessingInstruction(Object object) {
        return object instanceof ProcessingInstruction;
    }

    
    public String getCommentStringValue(Object comment) {
        return ((Commentcomment).getValue();
    }

    
    public String getElementStringValue(Object element) {
        return ((Elementelement).getValue();
    }

    
    public String getAttributeStringValue(Object attribute) {
        return ((Attributeattribute).getValue();
    }
    

    public XPath parseXPath(String expressionthrows JaxenException {
        return new JaxenConnector(expression);
    }


    public Iterator getChildAxisIterator(Object parent, String localName, String namespacePrefix, String namespaceURI
      throws UnsupportedAxisException {
        
        if (parent instanceof ParentNode) {
            return new NamedChildIterator((ParentNodeparent, localName, namespacePrefix, namespaceURI);
        }
        return JaxenConstants.EMPTY_ITERATOR;
        
    }


    public Iterator getAttributeAxisIterator(Object contextNode, String localName, String namespacePrefix, String namespaceURI
      throws UnsupportedAxisException {

        // I don't need to worry about the prefix here because XPath only iterates
        // by local name and namespace URI. The prefix doesn't matter. 
        // This does assume that this class is non-public.
        try {
            Element element = (ElementcontextNode;
            Attribute result = null;
            if (namespaceURI == null) {
                result = element.getAttribute(localName);
            }
            else {
                result = element.getAttribute(localName, namespaceURI);
            }
            
            if (result == nullreturn JaxenConstants.EMPTY_ITERATOR;
            
            return new SingleObjectIterator(result);
        }
        catch (ClassCastException ex) {
            return JaxenConstants.EMPTY_ITERATOR;
        }
        
    }
    
    
}