aboutsummaryrefslogtreecommitdiff
path: root/spss.slinterface/WEB-INF/src/at/gv/egovernment/moa/spss/slinterface/XPathUtils.java
diff options
context:
space:
mode:
authorgregor <gregor@d688527b-c9ab-4aba-bd8d-4036d912da1d>2003-12-03 08:54:36 +0000
committergregor <gregor@d688527b-c9ab-4aba-bd8d-4036d912da1d>2003-12-03 08:54:36 +0000
commit1d39a4fa10f410c5d4fdcc64413b9e0f0372f9e1 (patch)
tree83e060cf0c95e1ebfde2141bd19b9fe42e621188 /spss.slinterface/WEB-INF/src/at/gv/egovernment/moa/spss/slinterface/XPathUtils.java
parent221be8b8869ed6658b6acd0ba10d78998da33080 (diff)
downloadmoa-id-spss-1d39a4fa10f410c5d4fdcc64413b9e0f0372f9e1.tar.gz
moa-id-spss-1d39a4fa10f410c5d4fdcc64413b9e0f0372f9e1.tar.bz2
moa-id-spss-1d39a4fa10f410c5d4fdcc64413b9e0f0372f9e1.zip
Zwischenstand, compilierbar
git-svn-id: https://joinup.ec.europa.eu/svn/moa-idspss/trunk@69 d688527b-c9ab-4aba-bd8d-4036d912da1d
Diffstat (limited to 'spss.slinterface/WEB-INF/src/at/gv/egovernment/moa/spss/slinterface/XPathUtils.java')
-rw-r--r--spss.slinterface/WEB-INF/src/at/gv/egovernment/moa/spss/slinterface/XPathUtils.java162
1 files changed, 162 insertions, 0 deletions
diff --git a/spss.slinterface/WEB-INF/src/at/gv/egovernment/moa/spss/slinterface/XPathUtils.java b/spss.slinterface/WEB-INF/src/at/gv/egovernment/moa/spss/slinterface/XPathUtils.java
new file mode 100644
index 000000000..16e4938ed
--- /dev/null
+++ b/spss.slinterface/WEB-INF/src/at/gv/egovernment/moa/spss/slinterface/XPathUtils.java
@@ -0,0 +1,162 @@
+/*
+ * Created on 02.12.2003
+ *
+ * (c) Stabsstelle IKT-Strategie des Bundes
+ */
+package at.gv.egovernment.moa.spss.slinterface;
+
+import java.util.HashMap;
+import java.util.StringTokenizer;
+
+import org.apache.xml.utils.PrefixResolverDefault;
+import org.apache.xpath.XPath;
+import org.apache.xpath.XPathContext;
+import org.apache.xpath.objects.XObject;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * @author Gregor Karlinger (mailto:gregor.karlinger@cio.gv.at)
+ */
+public class XPathUtils
+{
+ /**
+ * The XPath context for the XPath engine.
+ */
+ protected XPathContext xPathContext_;
+
+ /**
+ * The prefix resolver for the XPath engine.
+ */
+ protected PrefixResolver prefixResolver_;
+
+ /**
+ * The XPath engine.
+ */
+ protected XPath xPath_;
+
+ /* ==================================================================================================== */
+
+ public void setupContext(String xPathExpr, Node namespaceNode, String additionalNSPrefixes)
+ throws Exception
+ {
+
+ try
+ {
+ // Set up a new evaluation context
+ xPathContext_ = new XPathContext();
+
+ // Set up the namespace prefix resolver for the XPath engine
+ prefixResolver_ = new PrefixResolver(namespaceNode, additionalNSPrefixes);
+
+ // Initialize XPath engine
+ xPath_ = new XPath(xPathExpr, null, prefixResolver_, XPath.SELECT, null);
+ }
+ catch (Exception e)
+ {
+ throw new Exception("Setting up XPath evaluation context failed.", e);
+ }
+ }
+
+ /* ---------------------------------------------------------------------------------------------------- */
+
+ public NodeList selectNodeSet(Node contextNode) throws Exception
+ {
+ XObject xObject;
+ try
+ {
+ xObject = xPath_.execute(xPathContext_, contextNode, prefixResolver_);
+ return xObject.nodelist();
+ }
+ catch (Exception e)
+ {
+ throw new Exception("Executing XPath expression failed.", e);
+ }
+ }
+
+ /* ---------------------------------------------------------------------------------------------------- */
+
+ public boolean selectBoolean(Node contextNode) throws Exception
+ {
+ XObject xObject;
+ try
+ {
+ xObject = xPath_.execute(xPathContext_, contextNode, prefixResolver_);
+ return xObject.bool();
+ }
+ catch (Exception e)
+ {
+ throw new Exception("Executing XPath expression failed.", e);
+ }
+ }
+
+ /* ==================================================================================================== */
+
+ /**
+ * Special extension of the {@link org.apache.xml.utils.PrefixResolverDefault} interface. Used to
+ * configure the Apache Xalan XPath engine which is employed as the backbone of this class.
+ */
+ protected class PrefixResolver extends PrefixResolverDefault
+ {
+
+ /**
+ * Contains the additionally specified namespace prefix (key) to namespace URI (value) attributions.
+ */
+ protected HashMap additionalNSPrefixesMap_;
+
+ /* ================================================================================================== */
+
+ /**
+ * Basic constructor.
+ *
+ * @param xpathExpressionContext The namespace declarations in scope for this node will be used to get
+ * the namespace uri for a prefix specified in the XPath expression.
+ *
+ * @param additionalNSPrefixes Allows the specification of additional prefix to uri attributions apart
+ * from the declarations in scope for the parameter <code>
+ * xpathExpressionContext</code>. May be <code>null</code>.
+ */
+ public PrefixResolver(Node xpathExpressionContext, String additionalNSPrefixes) throws Exception
+ {
+ super(xpathExpressionContext);
+ additionalNSPrefixesMap_ = new HashMap();
+
+ // Register the specified additional namespace prefix to namespace uri attributions
+ if (additionalNSPrefixes != null)
+ {
+ StringTokenizer tokenizer = new StringTokenizer(additionalNSPrefixes, " ");
+ while (tokenizer.hasMoreTokens())
+ {
+ String prefix = tokenizer.nextToken();
+ if (!tokenizer.hasMoreTokens())
+ {
+
+ // There must be an even number of tokens in the string
+ throw new Exception("Parameter \"additionalNSPrefixes\" must have an even number of tokens.");
+ }
+ String uri = tokenizer.nextToken();
+ additionalNSPrefixesMap_.put(prefix, uri);
+ }
+ }
+ }
+
+ /* -------------------------------------------------------------------------------------------------- */
+
+ /**
+ * Gets the namespace uri for the specified namespace prefix. The additionally specified prefixes
+ * overrule the prefixes found in the specified namespace node.
+ *
+ * @param prefix The namespace prefix for which a namespace uri should be found.
+ *
+ * @return the namespace uri for the specified namespace prefix.
+ */
+ public String getNamespaceForPrefix(String prefix)
+ {
+ String additionalURI = (String) additionalNSPrefixesMap_.get(prefix);
+ return (additionalURI != null)
+ ? additionalURI
+ : super.getNamespaceForPrefix(prefix);
+ }
+ }
+}
+