[Mulgara-svn] r1817 - trunk/src/jar/query/java/org/mulgara/query/functions
pag at mulgara.org
pag at mulgara.org
Wed Oct 7 03:04:29 UTC 2009
Author: pag
Date: 2009-10-06 20:04:28 -0700 (Tue, 06 Oct 2009)
New Revision: 1817
Added:
trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraFunctionResolver.java
trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraUnsafeFunctionResolver.java
trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraXFunctionGroup.java
Log:
The new 'unsafe' set of functions that missed the checking in r1816. Documentation on the use of these functions is at http://mulgara.org/trac/wiki/UnsafeFunctions
Added: trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraFunctionResolver.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraFunctionResolver.java (rev 0)
+++ trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraFunctionResolver.java 2009-10-07 03:04:28 UTC (rev 1817)
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2009 DuraSpace.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.mulgara.query.functions;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathFunction;
+import javax.xml.xpath.XPathFunctionResolver;
+
+
+/**
+ * Retrieves Mulgara functions.
+ *
+ * @created Oct 6, 2009
+ * @author Paul Gearon
+ * @copyright © 2009 <a href="http://www.duraspace.org/">DuraSpace</a>
+ */
+public abstract class MulgaraFunctionResolver implements XPathFunctionResolver {
+
+ /**
+ * A mapping of namespace URIs to the map of (name->functions) in that namespace.
+ * This is used to look up the requested function object.
+ */
+ private final Map<String,Map<String,XPathFunction>> functionGroups = new HashMap<String,Map<String,XPathFunction>>();
+
+
+ /**
+ * @see javax.xml.xpath.XPathFunctionResolver#resolveFunction(javax.xml.namespace.QName, int)
+ */
+ public XPathFunction resolveFunction(QName functionName, int arity) {
+ XPathFunction result = null;
+ String namespace = functionName.getNamespaceURI();
+ if (namespace != null) {
+ Map<String,XPathFunction> fnGroupMap = functionGroups.get(namespace);
+ if (fnGroupMap != null) {
+ result = fnGroupMap.get(functionName.getLocalPart() + "/" + arity);
+ // fall back to multiple arity
+ if (result == null) result = fnGroupMap.get(functionName.getLocalPart() + "/*");
+ }
+ }
+ return result;
+ }
+
+
+ /**
+ * A helper method to create a mapping of function names to their implementing classes,
+ * and of namespaces to these mappings.
+ * @param fnGroup A group of functions to be added into a single namespace.
+ * This group also provides that namespace.
+ */
+ protected final void addFunctionGroup(MulgaraFunctionGroup fnGroup) {
+ // map the function names to the functions
+ Map<String,XPathFunction> functionMap = new HashMap<String,XPathFunction>();
+ for (MulgaraFunction fn: fnGroup.getAllFunctions()) functionMap.put(fn.getName(), fn);
+ // map the namespace to the name->function map
+ functionGroups.put(fnGroup.getNamespace(), functionMap);
+ }
+
+}
Added: trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraUnsafeFunctionResolver.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraUnsafeFunctionResolver.java (rev 0)
+++ trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraUnsafeFunctionResolver.java 2009-10-07 03:04:28 UTC (rev 1817)
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2009 DuraSpace.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.mulgara.query.functions;
+
+
+/**
+ * Retrieves Mulgara functions.
+ *
+ * @created Oct 6, 2009
+ * @author Paul Gearon
+ * @copyright © 2009 <a href="http://www.duraspace.org/">DuraSpace</a>
+ */
+public class MulgaraUnsafeFunctionResolver extends MulgaraFunctionResolver {
+
+ // initialize the maps of requested parameters to the function object being asked for
+ public MulgaraUnsafeFunctionResolver() {
+ addFunctionGroup(new MulgaraXFunctionGroup());
+ }
+
+}
Added: trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraXFunctionGroup.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraXFunctionGroup.java (rev 0)
+++ trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraXFunctionGroup.java 2009-10-07 03:04:28 UTC (rev 1817)
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2009 DuraSpace.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.mulgara.query.functions;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.xml.xpath.XPathFunctionException;
+
+import org.mulgara.query.functions.MulgaraFunction;
+import org.mulgara.query.functions.MulgaraFunctionGroup;
+
+/**
+ * Container for functions in the mulgarax domain.
+ *
+ * @created Oct 5, 2009
+ * @author Paul Gearon
+ * @copyright © 2009 <a href="http://www.duraspace.org/">DuraSpace</a>
+ */
+ at SuppressWarnings("unchecked")
+public class MulgaraXFunctionGroup implements MulgaraFunctionGroup {
+
+ /** The prefix for the mulgarax: namespace */
+ static final String PREFIX = "mulgarax";
+
+ /** The mulgarax: namespace */
+ static final String NAMESPACE = "http://mulgara.org/mulgarax#";
+
+ /** Internal buffer size for reading from external processes */
+ private static final int BUFFER_SIZE = 1024;
+
+ /**
+ * Get the prefix used for the namespace of these operations.
+ * @return The short string used for a prefix in a QName.
+ */
+ public String getPrefix() {
+ return PREFIX;
+ }
+
+ /**
+ * Get the namespace of these operations.
+ * @return The string of the namespace URI.
+ */
+ public String getNamespace() {
+ return NAMESPACE;
+ }
+
+ /**
+ * Get the set of MulgaraX functions.
+ * @return A set of MulgaraFunction for this entire group.
+ */
+ public Set<MulgaraFunction> getAllFunctions() {
+ Set<MulgaraFunction> functions = new HashSet<MulgaraFunction>();
+ functions.add(new System());
+ functions.add(new System2());
+ return functions;
+ }
+
+ /**
+ * Function to execute a command in a shell. Stdout is captured and returned as a string.
+ * @see http://www.w3.org/TR/xpath-functions/#func-matches
+ */
+ static private class System extends MulgaraFunction {
+ public Object eval(List args) throws XPathFunctionException {
+ String str = (String)args.get(0);
+ StringBuilder outputString = new StringBuilder();
+ try {
+ Process proc = Runtime.getRuntime().exec(str);
+ proc.getOutputStream().close();
+ BufferedReader procStdOut = new BufferedReader(new InputStreamReader(proc.getInputStream()));
+ char[] buffer = new char[BUFFER_SIZE];
+ int len;
+ while ((len = procStdOut.read(buffer)) >= 0) outputString.append(buffer, 0, len);
+ procStdOut.close();
+ proc.getErrorStream().close();
+ } catch (IOException e) {
+ throw new XPathFunctionException("I/O error communicating with external process.");
+ }
+ return outputString.toString();
+ }
+ }
+
+ /**
+ * Function to execute a command in a shell. A second parameter provides stdin for the process.
+ * Stdout is captured and returned as a string.
+ */
+ static private class System2 extends MulgaraFunction {
+ public String getName() { return "system/2"; }
+ public int getArity() { return 2; }
+ public Object eval(List args) throws XPathFunctionException {
+ // get the command and arguments
+ String str = (String)args.get(0);
+ // get the data to feed to stdin
+ String inputString = args.get(1).toString();
+
+ StringBuilder outputString = new StringBuilder();
+ try {
+ Process proc = Runtime.getRuntime().exec(str);
+ // get stdin for the process
+ Writer procStdIn = new OutputStreamWriter(proc.getOutputStream());
+ // get stdout for the process
+ BufferedReader procStdOut = new BufferedReader(new InputStreamReader(proc.getInputStream()));
+
+ // write to stdin
+ procStdIn.append(inputString);
+ procStdIn.close();
+ // read from stdout
+ char[] buffer = new char[BUFFER_SIZE];
+ int len;
+ while ((len = procStdOut.read(buffer)) >= 0) outputString.append(buffer, 0, len);
+ procStdOut.close();
+ proc.getErrorStream().close();
+ } catch (IOException e) {
+ throw new XPathFunctionException("I/O error communicating with external process.");
+ }
+ return outputString.toString();
+ }
+ }
+}
More information about the Mulgara-svn
mailing list