001    package com.saelist.stx.xpath;
002    
003    import org.apache.log4j.Logger;
004    
005    import org.jaxen.XPath;
006    import org.jaxen.DefaultNavigator;
007    import org.jaxen.FunctionCallException;
008    import org.jaxen.UnsupportedAxisException;
009    
010    import org.jaxen.util.SingleObjectIterator;
011    
012    
013    import org.saxpath.SAXPathException;
014    
015    import java.util.*;
016    import java.net.URL;
017    
018    import com.saelist.util.*;
019    import com.saelist.stx.parser.*;
020    import com.saelist.stx.*;
021    
022    
023    /** Interface for navigating around the Pair/PairList object model.
024     *
025     *  <p>
026     *  This class is not intended for direct usage, but is
027     *  used by the Jaxen engine during evaluation.
028     *  </p>
029     *
030     *  @see XPath
031     *
032     *  @author <a href="mailto:thor@peersoft.de">Thor Kristmundsson</a>
033     *
034     *  Derived from {@link org.jaxen.jdom.DocumentNavigator}.
035     */
036    public class PairNavigator extends DefaultNavigator {
037    
038      public static Logger logger = Logger.getLogger(PairNavigator.class);
039    
040        /** Singleton implementation.
041         */
042        private static class Singleton {
043          /** Singleton instance.
044           */
045          private static PairNavigator instance = new PairNavigator();
046        }
047    
048        public static PairNavigator getInstance() {
049          return Singleton.instance;
050        }
051    
052        public boolean isElement(Object obj) {
053          // return ((Pair) obj).getNodeType() == Pair.NT_ELEMENT;
054          return true;
055        }
056    
057        public boolean isComment(Object obj) { return false; }
058    
059        public boolean isText(Object obj) {
060          // return ((Pair) obj).getNodeType() == Pair.NT_TEXT;
061          return false;
062        }
063    
064        public boolean isAttribute(Object obj) {
065          // return ((Pair) obj).getNodeType() == Pair.NT_ATTRIBUTE;
066          return false;
067        }
068    
069        public boolean isProcessingInstruction(Object obj) { return false; }
070    
071        public boolean isDocument(Object obj) {
072          logger.info("isDocument() : obj=" + Strings.cutDown("" + obj, 30, "..."));
073          return ((Pair) obj).getParent() == null;
074        }
075    
076        public boolean isNamespace(Object obj) { return false; }
077    
078        public String getElementName(Object obj) {
079          logger.info("getElementName() : obj=" + Strings.cutDown("" + obj, 30, "..."));
080          return ((Pair) obj).getText();
081        }
082    
083        public String getElementNamespaceUri(Object obj) {   return null; }
084    
085        public String getAttributeName(Object obj) {
086          return ((Pair) obj).getText();
087        }
088    
089        public String getAttributeNamespaceUri(Object obj) { throw new UnsupportedOperationException(""); }
090    
091        public Iterator getChildAxisIterator(Object contextNode) {
092          logger.info("getChildAxisIterator() : contextNode=" + Strings.cutDown("" + contextNode, 30, "..."));
093          Pair pair = (Pair) contextNode;
094          return pair.getPairs();
095        }
096    
097        public Iterator getNamespaceAxisIterator(Object contextNode) throws UnsupportedAxisException { throw new UnsupportedAxisException(""); }
098    
099        public Iterator getParentAxisIterator(Object contextNode) {
100          logger.info("getParentAxisIterator() : contextNode=" + Strings.cutDown("" + contextNode, 30, "..."));
101          Pair parent = ((Pair) contextNode).getParent();
102          if ( parent != null )
103             return new SingleObjectIterator( parent );
104          return null;
105        }
106    
107        public Iterator getAttributeAxisIterator(Object contextNode) throws UnsupportedAxisException {
108          throw new UnsupportedAxisException("");
109          /* logger.info("getAttributeAxisIterator() : contextNode=" + Strings.cutDown("" + contextNode, 30, "..."));
110          Pair pair = (Pair) contextNode;
111          return pair.getPairs(Pair.NT_ATTRIBUTE); */
112        }
113    
114        /** Returns a parsed form of the given xpath string, which will be suitable
115         *  for queries on JDOM documents.
116         */
117        public XPath parseXPath (String xpath) throws SAXPathException {
118          return new PairXPath(xpath);
119        }
120    
121        public Object getDocumentNode(Object contextNode) {
122          logger.info("getDocumentNode() : contextNode=" + Strings.cutDown("" + contextNode, 30, "..."));
123          return ((Pair) contextNode).getRoot();
124        }
125    
126        public String getElementQName(Object obj) {
127          logger.info("getElementQName() : obj=" + Strings.cutDown("" + obj, 30, "..."));
128          return ((Pair) obj).getText();
129        }
130    
131        public String getAttributeQName(Object obj) {
132          logger.info("getElementQName() : obj=" + Strings.cutDown("" + obj, 30, "..."));
133          return ((Pair) obj).getText();
134        }
135    
136        public String getNamespaceStringValue(Object obj) { throw new UnsupportedOperationException(""); }
137    
138        public String getNamespacePrefix(Object obj) { throw new UnsupportedOperationException(""); }
139    
140        public String getTextStringValue(Object obj) {
141          logger.info("getTextStringValue() : obj=" + Strings.cutDown("" + obj, 30, "..."));
142          return ((Pair) obj).getText();
143        }
144    
145        public String getAttributeStringValue(Object obj) {
146          logger.info("getAttributeStringValue() : obj=" + Strings.cutDown("" + obj, 30, "..."));
147          return ((Pair) obj).getValue();
148        }
149    
150        public String getElementStringValue(Object obj) {
151          logger.info("getElementStringValue() : obj=" + Strings.cutDown("" + obj, 100, "..."));
152          Iterator it = (obj instanceof Pair) ? ((Pair) obj).getPairs() : ((Collection) obj).iterator();
153          StringBuffer buf = new StringBuffer();
154          while(it.hasNext()) {
155            Pair child = (Pair) it.next();
156            buf.append(child.getText()).append(" ");
157            buf.append(getElementStringValue(child));
158          }
159          return buf.toString();
160        }
161    
162        public String getProcessingInstructionTarget(Object obj) { throw new UnsupportedOperationException(""); }
163    
164        public String getProcessingInstructionData(Object obj) { throw new UnsupportedOperationException(""); }
165    
166        public String getCommentStringValue(Object obj) { throw new UnsupportedOperationException(""); }
167    
168        public String translateNamespacePrefixToUri(String prefix, Object context) { throw new UnsupportedOperationException(""); }
169    
170        public Object getDocument(String url) throws FunctionCallException  {
171          logger.info("getDocument() : url=" + Strings.cutDown("" + url, 30, "..."));
172          try {
173            throw new UnsupportedOperationException("To be implemented");
174            // return LstxParser2.parse(Net.loadFromURL(new URL(url)));
175          } catch (Exception e) {
176             throw new FunctionCallException( e.getMessage() );
177          }
178        }
179    }