garyp 00/10/17 16:33:32
Modified: java/src/org/apache/xalan/lib Extensions.java
Log:
Implement evaluate extension function.
Revision Changes Path
1.3 +41 -1 xml-xalan/java/src/org/apache/xalan/lib/Extensions.java
Index: Extensions.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/lib/Extensions.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Extensions.java 2000/10/17 19:07:09 1.2
+++ Extensions.java 2000/10/17 23:33:32 1.3
@@ -64,6 +64,10 @@
import org.w3c.dom.Text;
import org.w3c.dom.traversal.NodeIterator;
import org.apache.xpath.NodeSet;
+import org.apache.xpath.objects.XObject;
+import org.apache.xpath.XPath;
+import org.apache.xpath.XPathContext;
+import org.xml.sax.SAXNotSupportedException;
import java.util.Hashtable;
@@ -169,7 +173,8 @@
{
Node n = ns.elementAt(i);
String key = n.getNodeValue();
- if (!stringTable.containsKey(key))
{
+ if (!stringTable.containsKey(key))
+ {
stringTable.put(key, n);
dist.addElement(n);
}
@@ -196,6 +201,41 @@
return false;
}
return true;
+ }
+
+ /**
+ * Returns the result of evaluating the argument as a string containing
+ * an XPath expression. Used where the XPath expression is not known until
+ * run-time. The expression is evaluated as if the run-time value of the
+ * argument appeared in place of the evaluate function call at compile
time.
+ * @param myContext an <code>ExpressionContext</code> passed in by the
+ * extension mechanism. This must be an XPathContext.
+ * @param xpathExtr The XPath expression to be evaluated.
+ * @return the XObject resulting from evaluating the XPath
+ */
+ public static XObject evaluate(ExpressionContext myContext, String
xpathExpr)
+ throws SAXNotSupportedException, Exception
+ {
+ if (myContext instanceof XPathContext)
+ {
+ try
+ {
+ XPathContext xctxt = (XPathContext) myContext;
+ XPath dynamicXPath = new XPath(xpathExpr,
+ xctxt.getSAXLocator(),
+ xctxt.getNamespaceContext(),
+ XPath.SELECT);
+ return dynamicXPath.execute(xctxt,
+ myContext.getContextNode(),
+ xctxt.getNamespaceContext());
+ }
+ catch (Exception e)
+ {
+ throw e;
+ }
+ }
+ else
+ throw new SAXNotSupportedException("Invalid context passed to evaluate
" + myContext);
}
}