Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2009-03-06 16:31:23 -0500
committerpelder2009-03-06 16:31:23 -0500
commiteaad1b4c4a50baa9119054a2d213172caa719b12 (patch)
tree47215e4330944b35b1895f8779ddecf43250203b
parent351fe3fe106ac81c45c005d8acf9986a4d58b664 (diff)
downloadorg.eclipse.jet-eaad1b4c4a50baa9119054a2d213172caa719b12.tar.gz
org.eclipse.jet-eaad1b4c4a50baa9119054a2d213172caa719b12.tar.xz
org.eclipse.jet-eaad1b4c4a50baa9119054a2d213172caa719b12.zip
[258996] Create an XPath function that takes a String containing an XPath expression and evaluates it
-rw-r--r--doc/org.eclipse.jet.doc/references/xpathFunctions/evalFunction.html76
-rw-r--r--doc/org.eclipse.jet.doc/references/xpathFunctions/overview.html24
-rw-r--r--plugins/org.eclipse.jet/plugin.xml6
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/extras/EvalFunction.java73
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/extras/Messages.java37
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/extras/messages.properties13
6 files changed, 221 insertions, 8 deletions
diff --git a/doc/org.eclipse.jet.doc/references/xpathFunctions/evalFunction.html b/doc/org.eclipse.jet.doc/references/xpathFunctions/evalFunction.html
new file mode 100644
index 0000000..4d86675
--- /dev/null
+++ b/doc/org.eclipse.jet.doc/references/xpathFunctions/evalFunction.html
@@ -0,0 +1,76 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<HTML>
+<HEAD>
+<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
+<META name="GENERATOR" content="IBM Software Development Platform">
+<link rel="STYLESHEET" href="../../book.css" charset="ISO-8859-1" type="text/css"/>
+<TITLE>eval XPath function</TITLE>
+</HEAD>
+<BODY>
+<TABLE border="0" cellpadding="0" cellspacing="1" width="100%">
+ <TBODY>
+ <TR>
+ <TH align="left"><P>eval()</P></TH>
+ <TH align="right"><A href="overview.html">Additional XPath Functions</A></TH>
+ </TR>
+ </TBODY>
+</TABLE>
+<HR>
+<H2>eval</H2>
+<P>Evaluate a dynamically created XPath function and return its result.</P>
+<HR>
+<TABLE border="0" width="100%">
+ <TBODY>
+ <TR>
+ <TH colspan="2" align="left">Function Summary<BR>&nbsp;</TH></TR>
+ <TR>
+ <TD width="20%" valign="top">minimal arguments</TD>
+ <TD><SPAN style="font-family: monospace" valign="top">
+ Object eval (String text)<BR>&nbsp;<BR>
+ </SPAN></TD>
+ </TR>
+ <TR>
+ <TD width="20%" valign="top">all arguments</TD>
+ <TD><SPAN style="font-family: monospace" valign="top">
+ Object eval (String text)<BR>&nbsp;<BR>
+ </SPAN></TD>
+ </TR>
+ </TBODY>
+</TABLE>
+<HR>
+<TABLE border="0" width="100%">
+ <TBODY>
+ <TR>
+ <TH colspan="2" align="left">Required Arguments<BR>&nbsp;</TH>
+ </TR>
+ <TR>
+ <TD width="20%" valign="top"><B>String text</B></TD>
+ <TD>any string</TD>
+ </TR>
+ </TBODY>
+</TABLE>
+<HR>
+<TABLE border="0" width="100%">
+ <TBODY>
+ <TR>
+ <TH colspan="2" align="left">There are no optional arguments<BR>&nbsp;</TD>
+ </TBODY>
+</TABLE>
+<HR>
+<TABLE border="0" width="100%">
+ <TBODY>
+ <TR>
+ <TH colspan="2" align="left">Example<BR>&nbsp;</TH></TR>
+ <TR>
+ <TD colspan="2"><SPAN style="font-family: monospace">
+<!-- Begin eval example -->
+ <B>eval</B>(concat('$var/@',$attrName))<br>
+ <P>if $attrName contains the string 'foo', then returns the result of '$var/@foo'</P>
+<!-- End eval example -->
+</SPAN></TD>
+ </TR>
+ </TBODY>
+</TABLE>
+<P><BR>
+<FONT color="navy" size="-2">&copy; Copyright IBM Corporation 2009. All Rights Reserved.</FONT></P></P></BODY>
+</HTML>
diff --git a/doc/org.eclipse.jet.doc/references/xpathFunctions/overview.html b/doc/org.eclipse.jet.doc/references/xpathFunctions/overview.html
index 4c8d7a1..42af6d3 100644
--- a/doc/org.eclipse.jet.doc/references/xpathFunctions/overview.html
+++ b/doc/org.eclipse.jet.doc/references/xpathFunctions/overview.html
@@ -48,6 +48,14 @@ The following functions are included in JET, in addition to the XPath functions
</TD>
</TR>
<TR>
+ <TD width="10%"><A href="evalFunction.html">eval</A></TD>
+ <TD>
+ <!-- Begin eval description -->
+ Evaluate a dynamically created XPath function and return its result.
+ <!-- End eval description -->
+ </TD>
+ </TR>
+ <TR>
<TD width="10%"><A href="isVariableDefined.html">isVariableDefined</A></TD>
<TD>
<!-- Begin escapeJavaWhitespace description -->
@@ -72,14 +80,6 @@ The following functions are included in JET, in addition to the XPath functions
</TD>
</TR>
<TR>
- <TD width="10%"><A href="xmlEncodeFunction.html">xmlEncode</A></TD>
- <TD>
- <!-- Begin xmlEncode description -->
- Replaces characters that cause problems when parsing XML streams with the equivalent XML entities.
- <!-- End xmlEncode description -->
- </TD>
- </TR>
- <TR>
<TD width="10%"><A href="packageNameFunction.html">packageName</A></TD>
<TD>
<!-- Begin packageName description -->
@@ -135,6 +135,14 @@ The following functions are included in JET, in addition to the XPath functions
<!-- End uppercaseFirst description -->
</TD>
</TR>
+ <TR>
+ <TD width="10%"><A href="xmlEncodeFunction.html">xmlEncode</A></TD>
+ <TD>
+ <!-- Begin xmlEncode description -->
+ Replaces characters that cause problems when parsing XML streams with the equivalent XML entities.
+ <!-- End xmlEncode description -->
+ </TD>
+ </TR>
</TBODY>
</TABLE>
diff --git a/plugins/org.eclipse.jet/plugin.xml b/plugins/org.eclipse.jet/plugin.xml
index 2b6f064..ba1673e 100644
--- a/plugins/org.eclipse.jet/plugin.xml
+++ b/plugins/org.eclipse.jet/plugin.xml
@@ -1347,6 +1347,12 @@ See the documentation for the Java class java.util.Locale for details on languag
minArgs="2"
name="sort">
</function>
+ <function
+ implementation="org.eclipse.jet.internal.xpath.functions.extras.EvalFunction"
+ maxArgs="1"
+ minArgs="1"
+ name="eval">
+ </function>
</extension>
<extension
point="org.eclipse.ant.core.antTasks">
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/extras/EvalFunction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/extras/EvalFunction.java
new file mode 100644
index 0000000..24956fc
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/extras/EvalFunction.java
@@ -0,0 +1,73 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2009 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id: EvalFunction.java,v 1.1 2009/03/06 21:31:23 pelder Exp $
+ */
+package org.eclipse.jet.internal.xpath.functions.extras;
+
+
+import java.util.List;
+
+import org.eclipse.jet.xpath.Context;
+import org.eclipse.jet.xpath.XPath;
+import org.eclipse.jet.xpath.XPathException;
+import org.eclipse.jet.xpath.XPathExpression;
+import org.eclipse.jet.xpath.XPathFactory;
+import org.eclipse.jet.xpath.XPathFunction;
+import org.eclipse.jet.xpath.XPathFunctionWithContext;
+import org.eclipse.jet.xpath.XPathRuntimeException;
+import org.eclipse.jet.xpath.XPathUtil;
+import org.eclipse.osgi.util.NLS;
+
+
+/**
+ * Implement eval(String xpathExpression)
+ */
+public class EvalFunction implements XPathFunction, XPathFunctionWithContext
+{
+
+ private Context context;
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.xpath.XPathFunction#evaluate(java.util.List)
+ */
+ public Object evaluate(List args)
+ {
+ // one and only argument is a string containing an unparsed XPath expression
+ String unparsedXPath = XPathUtil.xpathString(args.get(0));
+
+ // create an XPath instance, on configure it from the current XPath context.
+ XPath xpath = XPathFactory.newInstance().newXPath(context.getAnnotationManager());
+ xpath.setXPathFunctionResolver(context.getFunctionResolver());
+ xpath.setXPathVariableResolver(context.getVariableResolver());
+
+ try
+ {
+ // compile and return the result of the XPath
+ XPathExpression compiledXPath = xpath.compile(unparsedXPath);
+ return compiledXPath.evaluate(context.getContextNode());
+ }
+ catch (XPathException e)
+ {
+ throw new XPathRuntimeException(NLS.bind(Messages.EvalFunction_ExpressionCompileError, unparsedXPath), e);
+ }
+ }
+
+ public void setContext(Context context)
+ {
+ // save the current XPath context - used by evaluate(List)
+ this.context = context;
+ }
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/extras/Messages.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/extras/Messages.java
new file mode 100644
index 0000000..f1350d9
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/extras/Messages.java
@@ -0,0 +1,37 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2009 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id: Messages.java,v 1.1 2009/03/06 21:31:23 pelder Exp $
+ */
+package org.eclipse.jet.internal.xpath.functions.extras;
+
+
+import org.eclipse.osgi.util.NLS;
+
+
+public class Messages extends NLS
+{
+ private static final String BUNDLE_NAME = "org.eclipse.jet.internal.xpath.functions.extras.messages"; //$NON-NLS-1$
+
+ public static String EvalFunction_ExpressionCompileError;
+ static
+ {
+ // initialize resource bundle
+ NLS.initializeMessages(BUNDLE_NAME, Messages.class);
+ }
+
+ private Messages()
+ {
+ }
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/extras/messages.properties b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/extras/messages.properties
new file mode 100644
index 0000000..47c2c80
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/xpath/functions/extras/messages.properties
@@ -0,0 +1,13 @@
+###############################################################################
+# Copyright (c) 2009 IBM Corporation and others.
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Contributors:
+# IBM Corporation - initial API and implementation
+###############################################################################
+#
+# NLS_MESSAGEFORMAT_VAR
+EvalFunction_ExpressionCompileError=The XPath expression {0} has errors.

Back to the top