You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by jb...@apache.org on 2010/06/12 21:33:49 UTC

svn commit: r954048 - in /commons/sandbox/gsoc/2010/scxml-js/trunk/test: xpathEvaluator.js xpathEvaluator.sh

Author: jbeard
Date: Sat Jun 12 19:33:49 2010
New Revision: 954048

URL: http://svn.apache.org/viewvc?rev=954048&view=rev
Log:
Ported xalan example xpath expression evaluator to Rhino.

Added:
    commons/sandbox/gsoc/2010/scxml-js/trunk/test/xpathEvaluator.js   (with props)
    commons/sandbox/gsoc/2010/scxml-js/trunk/test/xpathEvaluator.sh   (with props)

Added: commons/sandbox/gsoc/2010/scxml-js/trunk/test/xpathEvaluator.js
URL: http://svn.apache.org/viewvc/commons/sandbox/gsoc/2010/scxml-js/trunk/test/xpathEvaluator.js?rev=954048&view=auto
==============================================================================
--- commons/sandbox/gsoc/2010/scxml-js/trunk/test/xpathEvaluator.js (added)
+++ commons/sandbox/gsoc/2010/scxml-js/trunk/test/xpathEvaluator.js Sat Jun 12 19:33:49 2010
@@ -0,0 +1,96 @@
+
+importClass(java.io.FileInputStream);
+importClass(java.io.OutputStreamWriter);
+
+importClass(javax.xml.parsers.DocumentBuilderFactory);
+importClass(javax.xml.transform.OutputKeys);
+importClass(javax.xml.transform.Transformer);
+importClass(javax.xml.transform.TransformerFactory);
+importClass(javax.xml.transform.dom.DOMSource);
+importClass(javax.xml.transform.stream.StreamResult);
+
+importClass(org.apache.xpath.XPathAPI);
+importClass(org.w3c.dom.Document);
+importClass(org.w3c.dom.Node);
+importClass(org.w3c.dom.traversal.NodeIterator);
+importClass(org.xml.sax.InputSource);
+
+/**
+ *	Very basic utility for applying an XPath epxression to an xml file and printing information
+ /	about the execution of the XPath object and the nodes it finds.
+ *	Takes 2 arguments:
+ *	 (1) an xml filename
+ *	 (2) an XPath expression to apply to the file
+ *	Examples:
+ *	 java ApplyXPath foo.xml /
+ *	 java ApplyXPath foo.xml /doc/name[1]/@last
+ * @see XPathAPI
+ */
+	
+/** Decide if the node is text, and so must be handled specially */
+function isTextNode(n) {
+	if (n == null)
+		return false;
+	nodeType = n.getNodeType();
+	return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE;
+}
+	
+
+
+var filename = arguments[0];
+var xpath = arguments[1];
+
+if ((filename != null) && (filename.length > 0)
+	&& (xpath != null) && (xpath.length > 0))
+{
+
+	// Tell that we're loading classes and parsing, so the time it 
+	// takes to do this doesn't get confused with the time to do 
+	// the actual query and serialization.
+	print("Loading classes, parsing "+filename+", and setting up serializer");
+	
+	// Set up a DOM tree to query.
+	var inputSource = new InputSource(new FileInputStream(filename));
+	dfactory = DocumentBuilderFactory.newInstance();
+	dfactory.setNamespaceAware(false);
+	var doc = dfactory.newDocumentBuilder().parse(inputSource);
+	
+	// Set up an identity transformer to use as serializer.
+	var serializer = TransformerFactory.newInstance().newTransformer();
+	serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+
+	// Use the simple XPath API to select a nodeIterator.
+	print("Querying DOM using "+xpath);
+	var nl = XPathAPI.selectNodeIterator(doc, xpath);
+
+	// Serialize the found nodes to System.out.
+	//print("<output>");
+				
+	var n;
+	while ((n = nl.nextNode())!= null)
+	{		 
+		if (isTextNode(n)) {
+			// DOM may have more than one node corresponding to a 
+			// single XPath text node.	Coalesce all contiguous text nodes
+			// at this level
+			var sb = new StringBuffer(n.getNodeValue());
+			for (
+				var nn = n.getNextSibling(); 
+				isTextNode(nn);
+				nn = nn.getNextSibling()
+			) {
+				sb.append(nn.getNodeValue());
+			}
+			print(sb);
+		}
+		else {
+			 serializer.transform(new DOMSource(n), new StreamResult(new OutputStreamWriter(java.lang.System.out)));
+		}
+	print()
+	}
+	//print("</output>");
+}
+else
+{
+	print("Bad input args: " + filename + ", " + xpath);
+}

Propchange: commons/sandbox/gsoc/2010/scxml-js/trunk/test/xpathEvaluator.js
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/gsoc/2010/scxml-js/trunk/test/xpathEvaluator.sh
URL: http://svn.apache.org/viewvc/commons/sandbox/gsoc/2010/scxml-js/trunk/test/xpathEvaluator.sh?rev=954048&view=auto
==============================================================================
--- commons/sandbox/gsoc/2010/scxml-js/trunk/test/xpathEvaluator.sh (added)
+++ commons/sandbox/gsoc/2010/scxml-js/trunk/test/xpathEvaluator.sh Sat Jun 12 19:33:49 2010
@@ -0,0 +1,2 @@
+#!/bin/bash
+java -cp ../lib/java/commons-cli.jar:../lib/java/js.jar:../lib/java/serializer.jar:../lib/java/xalan.jar:../lib/java/xercesImpl.jar:../lib/java/xml-apis.jar org.mozilla.javascript.tools.shell.Main -debug xpathEvaluator.js $1 $2

Propchange: commons/sandbox/gsoc/2010/scxml-js/trunk/test/xpathEvaluator.sh
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/gsoc/2010/scxml-js/trunk/test/xpathEvaluator.sh
------------------------------------------------------------------------------
    svn:executable = *