001: package com.rimfaxe.xml.compatibility.fatpath;
002:
003: import java.io.IOException;
004: import java.util.*;
005: import org.w3c.dom.*;
006: import com.rimfaxe.xml.xmlreader.xpath.*;
007:
008: /**
009: * Facade class providing a convenient API to the XPath parser and
010: * evaluator. For efficiency, parsed XPath expressions are cached.
011: * For ease of migration from Xalan XPath this class has the same name
012: * and has the same method names as the Xalan XPathAPI class. See <a
013: * href="http://home.earthlink.net/~huston2/dp/facade.html">Facade
014: * Pattern</a>
015:
016: <blockquote><small> Copyright (C) 2002 Hewlett-Packard Company.
017: This file is part of Sparta, an XML Parser, DOM, and XPath library.
018: This library is free software; you can redistribute it and/or
019: modify it under the terms of the <a href="doc-files/LGPL.txt">GNU
020: Lesser General Public License</a> as published by the Free Software
021: Foundation; either version 2.1 of the License, or (at your option)
022: any later version. This library is distributed in the hope that it
023: will be useful, but WITHOUT ANY WARRANTY; without even the implied
024: warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
025: PURPOSE. </small></blockquote>
026: @version $Date: 2002/12/13 23:49:24 $ $Revision: 1.2 $
027: @author Eamonn O'Brien-Strain
028: * @see org.apache.xpath.XPathAPI
029: */
030:
031: public class XPathAPI {
032:
033: /** @return all the elements that match the relative XPath
034: expression with respect to the context element. */
035: static public Enumeration selectElementIterator(Element element,
036: String xpathString) throws XPathException, IOException {
037: XPath xpath = XPath.get(xpathString);
038: if (xpath.isStringValue())
039: throw new XPathException(xpath, "\"" + xpathString
040: + "\" evaluates to string not element");
041: XPathVisitor visitor = new XPathVisitor(element, xpath);
042: return visitor.getResult();
043: }
044:
045: /** @return all the strings that match the relative XPath
046: expression with respect to the context element. */
047: static public Enumeration selectStringIterator(Element element,
048: String xpathString) throws XPathException, IOException {
049: XPath xpath = XPath.get(xpathString);
050: if (!xpath.isStringValue())
051: throw new XPathException(xpath, "\"" + xpathString
052: + "\" evaluates to element not string");
053: XPathVisitor visitor = new XPathVisitor(element, xpath);
054: return visitor.getResult();
055: }
056:
057: /** @return all the elements in the document that match the
058: absolute XPath expression. */
059: static public Enumeration selectElementIterator(Document doc,
060: String xpathString) throws XPathException, IOException {
061: if (xpathString.charAt(0) != '/')
062: xpathString = "/" + xpathString;
063: XPath xpath = XPath.get(xpathString);
064: if (xpath.isStringValue())
065: throw new XPathException(xpath, "\"" + xpathString
066: + "\" evaluates to string not element");
067: XPathVisitor visitor = new XPathVisitor(doc, xpath);
068: return visitor.getResult();
069: }
070:
071: /** @return all the strings in the document that match the
072: absolute XPath expression. */
073: static public Enumeration selectStringIterator(Document doc,
074: String xpathString) throws XPathException, IOException {
075: if (xpathString.charAt(0) != '/')
076: xpathString = "/" + xpathString;
077: XPath xpath = XPath.get(xpathString);
078: if (!xpath.isStringValue())
079: throw new XPathException(xpath, "\"" + xpathString
080: + "\" evaluates to element not string");
081: XPathVisitor visitor = new XPathVisitor(doc, xpath);
082: return visitor.getResult();
083: }
084:
085: /** @return the first element that matches the relative XPath
086: expression with respect to the context element, or null if
087: there is no match.
088:
089: @todo make more efficient by short-circuiting the search. */
090: static public Element selectSingleElement(Element element,
091: String xpathString) throws XPathException, IOException {
092: Enumeration iter = selectElementIterator(element, xpathString);
093: if (iter.hasMoreElements())
094: return (Element) iter.nextElement();
095: else
096: return null;
097: }
098:
099: /** @return the first element in this document that matches the
100: absolute XPath expression, or null if there is no match.
101:
102: * @todo make more efficient by short-circuiting the search. */
103: static public Element selectSingleElement(Document doc,
104: String xpathString) throws XPathException, IOException {
105: if (xpathString.charAt(0) != '/')
106: xpathString = "/" + xpathString;
107: Enumeration iter = selectElementIterator(doc, xpathString);
108: if (iter.hasMoreElements())
109: return (Element) iter.nextElement();
110: else
111: return null;
112: }
113:
114: /** @return the first element that matches the relative XPath
115: expression with respect to the context element, or null if
116: there is no match.
117:
118: @todo make more efficient by short-circuiting the search. */
119: static public String selectSingleString(Element element,
120: String xpathString) throws XPathException, IOException {
121: Enumeration iter = selectStringIterator(element, xpathString);
122: if (iter.hasMoreElements())
123: return (String) iter.nextElement();
124: else
125: return null;
126: }
127:
128: /** @return the first string in this document that matches the
129: absolute XPath expression, or null if there is no match.
130:
131: * @todo make more efficient by short-circuiting the search. */
132: static public String selectSingleString(Document doc,
133: String xpathString) throws XPathException, IOException {
134: if (xpathString.charAt(0) != '/')
135: xpathString = "/" + xpathString;
136: Enumeration iter = selectStringIterator(doc, xpathString);
137: if (iter.hasMoreElements())
138: return (String) iter.nextElement();
139: else
140: return null;
141: }
142:
143: }
144:
145: // $Log: XPathAPI.java,v $
146: // Revision 1.2 2002/12/13 23:49:24 eobrain
147: // Fix javadoc.
148: //
149: // Revision 1.1.1.1 2002/08/19 05:04:23 eobrain
150: // import from HP Labs internal CVS
151: //
152: // Revision 1.6 2002/08/19 00:19:14 eob
153: // Add copyright and other formatting and commenting in preparation for
154: // release to SourceForge.
155: //
156: // Revision 1.5 2002/08/18 04:27:22 eob
157: // Remove deprecated method.
158: //
159: // Revision 1.4 2002/05/23 21:08:23 eob
160: // Better error reporting.
161: //
162: // Revision 1.3 2002/03/25 22:50:49 eob
163: // Move XPath object caching to XPath class.
164: //
165: // Revision 1.2 2002/02/04 22:05:48 eob
166: // Add handling of attribute xpath expressions that return strings.
167: //
168: // Revision 1.1 2002/02/01 19:20:40 eob
169: // initial
|