[Mulgara-svn] r1816 - in trunk/src/jar/query/java/org/mulgara/query: . functions xpath

pag at mulgara.org pag at mulgara.org
Wed Oct 7 03:02:39 UTC 2009


Author: pag
Date: 2009-10-06 20:02:38 -0700 (Tue, 06 Oct 2009)
New Revision: 1816

Added:
   trunk/src/jar/query/java/org/mulgara/query/functions/
   trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraFunction.java
   trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraFunctionGroup.java
Removed:
   trunk/src/jar/query/java/org/mulgara/query/xpath/MulgaraFunction.java
   trunk/src/jar/query/java/org/mulgara/query/xpath/MulgaraFunctionGroup.java
Modified:
   trunk/src/jar/query/java/org/mulgara/query/xpath/FnFunctionGroup.java
   trunk/src/jar/query/java/org/mulgara/query/xpath/MulgaraXPathFunctionResolver.java
   trunk/src/jar/query/java/org/mulgara/query/xpath/OpFunctionGroup.java
   trunk/src/jar/query/java/org/mulgara/query/xpath/SparqlFunctionGroup.java
Log:
Refactored out common function loading code into MulgaraFunctionResolver. This meant changing static methods to instance methods (but that's OK, since they are only instantiated once). Also, took the completely abstract class of MulgaraFunctionGroup and converted to an interface. Common code was then moved to a new functions package. Finally, a new 'unsafe' set of functions was created. Documentation on the use of these functions is at http://mulgara.org/trac/wiki/UnsafeFunctions

Copied: trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraFunction.java (from rev 1797, trunk/src/jar/query/java/org/mulgara/query/xpath/MulgaraFunction.java)
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraFunction.java	                        (rev 0)
+++ trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraFunction.java	2009-10-07 03:02:38 UTC (rev 1816)
@@ -0,0 +1,120 @@
+/*
+ * 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.List;
+
+import javax.xml.xpath.XPathFunction;
+import javax.xml.xpath.XPathFunctionException;
+
+import org.jrdf.graph.Literal;
+import org.mulgara.query.QueryException;
+import org.mulgara.query.filter.value.SimpleLiteral;
+
+/**
+ * General class for providing management information for XPath function implementations.
+ *
+ * @created Oct 5, 2009
+ * @author Paul Gearon
+ * @copyright &copy; 2009 <a href="http://www.duraspace.org/">DuraSpace</a>
+ */
+ at SuppressWarnings("unchecked")
+public abstract class MulgaraFunction implements XPathFunction {
+
+  /**
+   * Evaluate the method represented by this class, using the supplied arguments.
+   * @see javax.xml.xpath.XPathFunction#evaluate(java.util.List)
+   */
+  public Object evaluate(List args) throws XPathFunctionException {
+    checkArgs(getArity(), args);
+    try {
+      return eval(args);
+    } catch (ClassCastException e) {
+      throw new XPathFunctionException("Incorrect parameter types passed to function: " + e.getMessage());
+    }
+  }
+
+  /**
+   * Test that the correct number of arguments were provided to the function call.
+   * @param expected The expected number of arguments.
+   * @param args The list of arguments.
+   * @throws XPathFunctionException Thrown if the argument list is the wrong length.
+   */
+  protected void checkArgs(int expected, List args) throws XPathFunctionException {
+    if (expected >= 0 && args.size() != expected) {
+      throw new XPathFunctionException("Incorrect number of parameters. Should be " + expected + ", but was: " + args.size());
+    }
+  }
+
+  /**
+   * Returns the number of arguments required to use this function. Default is 1. 
+   * @return The number of arguments for this function.
+   */
+  protected int getArity() { return 1; }
+
+  /**
+   * The name of this function, followed by its arity.
+   * If not overridden then this will be the function class's name, starting with a lower-case letter.
+   * @return The name/arity of this function.
+   */
+  public String getName() {
+    StringBuilder name = new StringBuilder(getClass().getSimpleName());
+    char c = name.charAt(0);
+    if (Character.isUpperCase(c)) {
+      name.setCharAt(0, Character.toLowerCase(c));
+    }
+    for (int i = 1; i < name.length(); i++) {
+      c = name.charAt(0);
+      if (Character.isUpperCase(c)) name.replace(i, i + 1, "-" + Character.toLowerCase(c));
+    }
+    name.append("/");
+    int arity = getArity();
+    if (arity >= 0) name.append(getArity());
+    else name.append("*");
+    return name.toString();
+  }
+
+  /**
+   * Evaluates the operation of the function. The argument list will be the correct length.
+   * @param args The arguments for the function.
+   * @return The return value of the function.
+   * @throws XPathFunctionException If there was an error executing the function.
+   */
+  protected abstract Object eval(List<?> args) throws XPathFunctionException;
+
+  /**
+   * Convert a singleton value into its effective boolean value (EBV).
+   * @param o The singleton to test.
+   * @return The EBV of the value.
+   * @throws XPathFunctionException If a complex evaluation throws an exception.
+   */
+  protected static final boolean toBool(Object o) throws XPathFunctionException {
+    if (o == null) return false;
+    if (o instanceof String) return ((String)o).length() != 0;
+    if (o instanceof Number) return ((Number)o).doubleValue() != 0 && ((Number)o).doubleValue() != Double.NaN;
+    if (o instanceof Boolean) return ((Boolean)o).booleanValue();
+    try {
+      if (o instanceof Literal) return new SimpleLiteral(((Literal)o).getLexicalForm(), ((Literal)o).getLanguage()).test(null);
+    } catch (NullPointerException e) {
+      throw new XPathFunctionException("Conversion of data to a simple literal requires a context: " + e.getMessage());
+    } catch (QueryException e) {
+      throw new XPathFunctionException("Unable to convert data to a simple literal: " + e.getMessage());
+    }
+    throw new XPathFunctionException("Type error: " + o + " [" + o.getClass() + "]");
+  }
+
+}

Copied: trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraFunctionGroup.java (from rev 1797, trunk/src/jar/query/java/org/mulgara/query/xpath/MulgaraFunctionGroup.java)
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraFunctionGroup.java	                        (rev 0)
+++ trunk/src/jar/query/java/org/mulgara/query/functions/MulgaraFunctionGroup.java	2009-10-07 03:02:38 UTC (rev 1816)
@@ -0,0 +1,49 @@
+/*
+ * 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.Set;
+
+
+/**
+ * Represents a group of functions exposed as XPathFunctions in a single namespace.
+ *
+ * @created Oct 5, 2009
+ * @author Paul Gearon
+ * @copyright &copy; 2009 <a href="http://www.duraspace.org/">DuraSpace</a>
+ */
+public interface MulgaraFunctionGroup {
+
+  /**
+   * Get the prefix used for the namespace of these operations.
+   * @return The short string used for a prefix in a QName.
+   */
+  public String getPrefix();
+
+  /**
+   * Get the namespace of these operations.
+   * @return The string of the namespace URI.
+   */
+  public String getNamespace();
+
+  /**
+   * Get the set of function in this group.
+   * @return A set of MulgaraFunction for this entire group.
+   */
+  public Set<MulgaraFunction> getAllFunctions();
+
+}

Modified: trunk/src/jar/query/java/org/mulgara/query/xpath/FnFunctionGroup.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/xpath/FnFunctionGroup.java	2009-10-07 02:57:04 UTC (rev 1815)
+++ trunk/src/jar/query/java/org/mulgara/query/xpath/FnFunctionGroup.java	2009-10-07 03:02:38 UTC (rev 1816)
@@ -25,6 +25,8 @@
 import javax.xml.xpath.XPathFunctionException;
 
 import org.apache.xerces.impl.xpath.regex.RegularExpression;
+import org.mulgara.query.functions.MulgaraFunction;
+import org.mulgara.query.functions.MulgaraFunctionGroup;
 
 /**
  * Container for functions in the fn domain.
@@ -34,7 +36,7 @@
  * @copyright &copy; 2009 <a href="http://www.duraspace.org/">DuraSpace</a>
  */
 @SuppressWarnings("unchecked")
-public class FnFunctionGroup extends MulgaraFunctionGroup {
+public class FnFunctionGroup implements MulgaraFunctionGroup {
 
   /** The prefix for the fn: namespace */
   static final String PREFIX = "fn";

Deleted: trunk/src/jar/query/java/org/mulgara/query/xpath/MulgaraFunction.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/xpath/MulgaraFunction.java	2009-10-07 02:57:04 UTC (rev 1815)
+++ trunk/src/jar/query/java/org/mulgara/query/xpath/MulgaraFunction.java	2009-10-07 03:02:38 UTC (rev 1816)
@@ -1,120 +0,0 @@
-/*
- * 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.xpath;
-
-import java.util.List;
-
-import javax.xml.xpath.XPathFunction;
-import javax.xml.xpath.XPathFunctionException;
-
-import org.jrdf.graph.Literal;
-import org.mulgara.query.QueryException;
-import org.mulgara.query.filter.value.SimpleLiteral;
-
-/**
- * General class for providing management information for XPath function implementations.
- *
- * @created Oct 5, 2009
- * @author Paul Gearon
- * @copyright &copy; 2009 <a href="http://www.duraspace.org/">DuraSpace</a>
- */
- at SuppressWarnings("unchecked")
-public abstract class MulgaraFunction implements XPathFunction {
-
-  /**
-   * Evaluate the method represented by this class, using the supplied arguments.
-   * @see javax.xml.xpath.XPathFunction#evaluate(java.util.List)
-   */
-  public Object evaluate(List args) throws XPathFunctionException {
-    checkArgs(getArity(), args);
-    try {
-      return eval(args);
-    } catch (ClassCastException e) {
-      throw new XPathFunctionException("Incorrect parameter types passed to function: " + e.getMessage());
-    }
-  }
-
-  /**
-   * Test that the correct number of arguments were provided to the function call.
-   * @param expected The expected number of arguments.
-   * @param args The list of arguments.
-   * @throws XPathFunctionException Thrown if the argument list is the wrong length.
-   */
-  protected void checkArgs(int expected, List args) throws XPathFunctionException {
-    if (expected >= 0 && args.size() != expected) {
-      throw new XPathFunctionException("Incorrect number of parameters. Should be " + expected + ", but was: " + args.size());
-    }
-  }
-
-  /**
-   * Returns the number of arguments required to use this function. Default is 1. 
-   * @return The number of arguments for this function.
-   */
-  protected int getArity() { return 1; }
-
-  /**
-   * The name of this function, followed by its arity.
-   * If not overridden then this will be the function class's name, starting with a lower-case letter.
-   * @return The name/arity of this function.
-   */
-  protected String getName() {
-    StringBuilder name = new StringBuilder(getClass().getSimpleName());
-    char c = name.charAt(0);
-    if (Character.isUpperCase(c)) {
-      name.setCharAt(0, Character.toLowerCase(c));
-    }
-    for (int i = 1; i < name.length(); i++) {
-      c = name.charAt(0);
-      if (Character.isUpperCase(c)) name.replace(i, i + 1, "-" + Character.toLowerCase(c));
-    }
-    name.append("/");
-    int arity = getArity();
-    if (arity >= 0) name.append(getArity());
-    else name.append("*");
-    return name.toString();
-  }
-
-  /**
-   * Evaluates the operation of the function. The argument list will be the correct length.
-   * @param args The arguments for the function.
-   * @return The return value of the function.
-   * @throws XPathFunctionException If there was an error executing the function.
-   */
-  protected abstract Object eval(List<?> args) throws XPathFunctionException;
-
-  /**
-   * Convert a singleton value into its effective boolean value (EBV).
-   * @param o The singleton to test.
-   * @return The EBV of the value.
-   * @throws XPathFunctionException If a complex evaluation throws an exception.
-   */
-  protected static final boolean toBool(Object o) throws XPathFunctionException {
-    if (o == null) return false;
-    if (o instanceof String) return ((String)o).length() != 0;
-    if (o instanceof Number) return ((Number)o).doubleValue() != 0 && ((Number)o).doubleValue() != Double.NaN;
-    if (o instanceof Boolean) return ((Boolean)o).booleanValue();
-    try {
-      if (o instanceof Literal) return new SimpleLiteral(((Literal)o).getLexicalForm(), ((Literal)o).getLanguage()).test(null);
-    } catch (NullPointerException e) {
-      throw new XPathFunctionException("Conversion of data to a simple literal requires a context: " + e.getMessage());
-    } catch (QueryException e) {
-      throw new XPathFunctionException("Unable to convert data to a simple literal: " + e.getMessage());
-    }
-    throw new XPathFunctionException("Type error: " + o + " [" + o.getClass() + "]");
-  }
-
-}

Deleted: trunk/src/jar/query/java/org/mulgara/query/xpath/MulgaraFunctionGroup.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/xpath/MulgaraFunctionGroup.java	2009-10-07 02:57:04 UTC (rev 1815)
+++ trunk/src/jar/query/java/org/mulgara/query/xpath/MulgaraFunctionGroup.java	2009-10-07 03:02:38 UTC (rev 1816)
@@ -1,49 +0,0 @@
-/*
- * 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.xpath;
-
-import java.net.URI;
-import java.util.Set;
-
-/**
- * Represents a group of functions exposed as XPathFunctions in a single namespace.
- *
- * @created Oct 5, 2009
- * @author Paul Gearon
- * @copyright &copy; 2009 <a href="http://www.duraspace.org/">DuraSpace</a>
- */
-public abstract class MulgaraFunctionGroup {
-
-  /**
-   * Get the prefix used for the namespace of these operations.
-   * @return The short string used for a prefix in a QName.
-   */
-  public abstract String getPrefix();
-
-  /**
-   * Get the namespace of these operations.
-   * @return The string of the namespace URI.
-   */
-  public abstract String getNamespace();
-
-  /**
-   * Get the set of function in this group.
-   * @return A set of MulgaraFunction for this entire group.
-   */
-  public abstract Set<MulgaraFunction> getAllFunctions();
-
-}

Modified: trunk/src/jar/query/java/org/mulgara/query/xpath/MulgaraXPathFunctionResolver.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/xpath/MulgaraXPathFunctionResolver.java	2009-10-07 02:57:04 UTC (rev 1815)
+++ trunk/src/jar/query/java/org/mulgara/query/xpath/MulgaraXPathFunctionResolver.java	2009-10-07 03:02:38 UTC (rev 1816)
@@ -16,13 +16,8 @@
 
 package org.mulgara.query.xpath;
 
-import java.util.HashMap;
-import java.util.Map;
+import org.mulgara.query.functions.MulgaraFunctionResolver;
 
-import javax.xml.namespace.QName;
-import javax.xml.xpath.XPathFunction;
-import javax.xml.xpath.XPathFunctionResolver;
-
 /**
  * Retrieves functions for SPARQL expressions.
  *
@@ -30,52 +25,15 @@
  * @author Paul Gearon
  * @copyright &copy; 2009 <a href="http://www.duraspace.org/">DuraSpace</a>
  */
-public class MulgaraXPathFunctionResolver implements XPathFunctionResolver {
+public class MulgaraXPathFunctionResolver extends MulgaraFunctionResolver {
 
   /**
-   * @see javax.xml.xpath.XPathFunctionResolver#resolveFunction(javax.xml.namespace.QName, int)
+   * Initialize the maps of requested parameters to the function object being asked for
    */
-  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 mapping of namespace URIs to the map of (name->functions) in that namespace.
-   * This is used to look up the requested function object.
-   */
-  private static final Map<String,Map<String,XPathFunction>> functionGroups = new HashMap<String,Map<String,XPathFunction>>();
-
-
-  // initialize the maps of requested parameters to the function object being asked for
-  static {
+  public MulgaraXPathFunctionResolver() {
     addFunctionGroup(new SparqlFunctionGroup());
     addFunctionGroup(new FnFunctionGroup());
     addFunctionGroup(new OpFunctionGroup());
   }
 
-  /**
-   * 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.
-   */
-  private static 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);
-  }
-
 }

Modified: trunk/src/jar/query/java/org/mulgara/query/xpath/OpFunctionGroup.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/xpath/OpFunctionGroup.java	2009-10-07 02:57:04 UTC (rev 1815)
+++ trunk/src/jar/query/java/org/mulgara/query/xpath/OpFunctionGroup.java	2009-10-07 03:02:38 UTC (rev 1816)
@@ -22,6 +22,9 @@
 import java.util.List;
 import java.util.Set;
 
+import org.mulgara.query.functions.MulgaraFunction;
+import org.mulgara.query.functions.MulgaraFunctionGroup;
+
 /**
  * Container for functions in the op pseudo-domain.
  *
@@ -30,7 +33,7 @@
  * @copyright &copy; 2009 <a href="http://www.duraspace.org/">DuraSpace</a>
  */
 @SuppressWarnings("unchecked")
-public class OpFunctionGroup extends MulgaraFunctionGroup {
+public class OpFunctionGroup implements MulgaraFunctionGroup {
 
   /** The prefix for the fn: namespace */
   static final String PREFIX = "op";

Modified: trunk/src/jar/query/java/org/mulgara/query/xpath/SparqlFunctionGroup.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/xpath/SparqlFunctionGroup.java	2009-10-07 02:57:04 UTC (rev 1815)
+++ trunk/src/jar/query/java/org/mulgara/query/xpath/SparqlFunctionGroup.java	2009-10-07 03:02:38 UTC (rev 1816)
@@ -27,6 +27,8 @@
 import org.jrdf.graph.Literal;
 import org.mulgara.query.QueryException;
 import org.mulgara.query.filter.value.SimpleLiteral;
+import org.mulgara.query.functions.MulgaraFunction;
+import org.mulgara.query.functions.MulgaraFunctionGroup;
 
 /**
  * Container for functions in the SPARQL domain.
@@ -36,7 +38,7 @@
  * @copyright &copy; 2009 <a href="http://www.duraspace.org/">DuraSpace</a>
  */
 @SuppressWarnings("unchecked")
-public class SparqlFunctionGroup extends MulgaraFunctionGroup {
+public class SparqlFunctionGroup implements MulgaraFunctionGroup {
 
   /** The prefix for the sparql: namespace */
   static final String PREFIX = "sparql";
@@ -162,7 +164,7 @@
 
   /** Function to perform a logical OR between 2 operands. */
   static private class LogicalOr extends LogicOp {
-    protected String getName() { return "logical-or"; }
+    public String getName() { return "logical-or"; }
     public boolean op(Object left, Object right) throws XPathFunctionException {
       return toBool(left) || toBool(right);
     }
@@ -170,7 +172,7 @@
 
   /** Function to perform a logical AND between 2 operands. */
   static private class LogicalAnd extends LogicOp {
-    protected String getName() { return "logical-and"; }
+    public String getName() { return "logical-and"; }
     public boolean op(Object left, Object right) throws XPathFunctionException {
       return toBool(left) && toBool(right);
     }




More information about the Mulgara-svn mailing list