[Mulgara-svn] r1381 - in trunk: . lib src/jar/query/java/org/jrdf/graph src/jar/querylang/java/org/mulgara/itql src/jar/resolver/java/org/mulgara/resolver src/jar/resolver-distributed/java/org/mulgara/resolver/distributed src/jar/store-stringpool-xa11/java/org/mulgara/store/stringpool/xa11 src/jar/util/java/org/mulgara/util src/jar/web/java/org/mulgara/webquery

pag at mulgara.org pag at mulgara.org
Mon Dec 1 19:53:58 UTC 2008


Author: pag
Date: 2008-12-01 11:53:56 -0800 (Mon, 01 Dec 2008)
New Revision: 1381

Added:
   trunk/lib/sesame-model-2.2.1.jar
   trunk/src/jar/util/java/org/mulgara/util/QueryParams.java
   trunk/src/jar/util/java/org/mulgara/util/URIUtil.java
Removed:
   trunk/lib/sesame-model-2.1.jar
   trunk/src/jar/querylang/java/org/mulgara/itql/URIUtil.java
   trunk/src/jar/web/java/org/mulgara/webquery/QueryParams.java
Modified:
   trunk/.project
   trunk/build.properties
   trunk/src/jar/query/java/org/jrdf/graph/AbstractURIReference.java
   trunk/src/jar/querylang/java/org/mulgara/itql/ModelExpressionBuilder.java
   trunk/src/jar/querylang/java/org/mulgara/itql/TqlInterpreter.java
   trunk/src/jar/resolver-distributed/java/org/mulgara/resolver/distributed/NetworkDelegator.java
   trunk/src/jar/resolver/java/org/mulgara/resolver/StringPoolSession.java
   trunk/src/jar/store-stringpool-xa11/java/org/mulgara/store/stringpool/xa11/XA11StringPoolImpl.java
   trunk/src/jar/web/java/org/mulgara/webquery/QueryResponsePage.java
Log:
Added in support for relative URIs in queries. This changes the URIReference implementation, so the sesame-model jar was updated so we could confirm it against the sesame sources.

Modified: trunk/.project
===================================================================
--- trunk/.project	2008-11-24 19:44:56 UTC (rev 1380)
+++ trunk/.project	2008-12-01 19:53:56 UTC (rev 1381)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <projectDescription>
-	<name>Mulgara-1.0</name>
+	<name>Mulgara-trunk</name>
 	<comment></comment>
 	<projects>
 	</projects>
@@ -18,7 +18,7 @@
 		<link>
 			<name>descriptor-client</name>
 			<type>2</type>
-			<location>obj/jar/descriptor-client</location>
+			<locationURI>obj/jar/descriptor-client</locationURI>
 		</link>
 	</linkedResources>
 </projectDescription>

Modified: trunk/build.properties
===================================================================
--- trunk/build.properties	2008-11-24 19:44:56 UTC (rev 1380)
+++ trunk/build.properties	2008-12-01 19:53:56 UTC (rev 1381)
@@ -121,7 +121,7 @@
 jid3.jar                 =jid3-0.34.jar
 jmdns.jar                =jmdns-0.2.jar
 joda-time.jar            =joda-time-1.5.2.jar
-sesame-model.jar         =sesame-model-2.1.jar
+sesame-model.jar         =sesame-model-2.2.1.jar
 jta.jar                  =jta-spec1_0_1.jar
 carol.jar                =carol-2.0.5.jar
 jotm.jrmp.jar            =jotm_jrmp_stubs-2.0.10.jar

Deleted: trunk/lib/sesame-model-2.1.jar
===================================================================
(Binary files differ)

Added: trunk/lib/sesame-model-2.2.1.jar
===================================================================
(Binary files differ)


Property changes on: trunk/lib/sesame-model-2.2.1.jar
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Modified: trunk/src/jar/query/java/org/jrdf/graph/AbstractURIReference.java
===================================================================
--- trunk/src/jar/query/java/org/jrdf/graph/AbstractURIReference.java	2008-11-24 19:44:56 UTC (rev 1380)
+++ trunk/src/jar/query/java/org/jrdf/graph/AbstractURIReference.java	2008-12-01 19:53:56 UTC (rev 1381)
@@ -1,10 +1,4 @@
 /*
- * $Header$
- * $Revision: 624 $
- * $Date: 2006-06-24 21:02:12 +1000 (Sat, 24 Jun 2006) $
- *
- * ====================================================================
- *
  * The Apache Software License, Version 1.1
  *
  * Copyright (c) 2003, 2004 The JRDF Project.  All rights reserved.
@@ -63,18 +57,15 @@
 import java.io.Serializable;
 import java.net.URI;
 
-import org.openrdf.model.impl.URIImpl;
+import org.openrdf.model.util.URIUtil;
 
 /**
  * A base implementation of an RDF {@link URIReference}.
- *
  * @author <a href="http://staff.pisoftware.com/raboczi">Simon Raboczi</a>
  * @author Andrew Newman
- *
- * @version $Revision: 624 $
+ * @author Paul Gearon
  */
-public abstract class AbstractURIReference extends URIImpl implements URIReference,
-    Serializable {
+public abstract class AbstractURIReference implements org.openrdf.model.URI, URIReference, Serializable {
 
   /**
    * Allow newer compiled version of the stub to operate when changes
@@ -84,27 +75,22 @@
    */
   private static final long serialVersionUID = 8034954863132812197L;
 
-  /**
-   * The URI of the node.
-   */
+  /** The URI of the node. */
   private URI uri;
 
+  /** An index indicating the first character of the local name in the URI string, -1 if not yet set. */
+  private int localNameIdx = -1;
+
   /**
    * Constructor.
-   *
    * Enforces a non-<code>null</code> and absolute <var>newUri</var> parameter.
-   *
    * @param newUri the URI to use in creation.
    * @throws IllegalArgumentException if <var>newUri</var> is <code>null</code> or
    *     not absolute
    */
   protected AbstractURIReference(URI newUri) {
-    super(newUri.toString());
-
     // Validate "newUri" parameter
-    if (null == newUri) {
-      throw new IllegalArgumentException("Null \"newUri\" parameter");
-    }
+    if (null == newUri) throw new IllegalArgumentException("Null \"newUri\" parameter");
 
     if (!newUri.isAbsolute()) {
       throw new IllegalArgumentException("\"" + newUri + "\" is not absolute");
@@ -116,22 +102,16 @@
 
   /**
    * Constructor.
-   *
    * Enforces a non-<code>null</code> parameter.  Use only for applications
    * where enforcement of valid URIs is too expensive or not necessary.
-   *
    * @param newUri the URI to use in creation.
    * @param validate whether to enforce valid RDF URIs.
    * @throws IllegalArgumentException if <var>newUri</var> is not absolute and
    *   validate is true.
    */
   protected AbstractURIReference(URI newUri, boolean validate) {
-    super(newUri.toString());
-
     // Validate "newUri" parameter
-    if (null == newUri) {
-      throw new IllegalArgumentException("Null \"newUri\" parameter");
-    }
+    if (null == newUri) throw new IllegalArgumentException("Null \"newUri\" parameter");
 
     if (validate && !newUri.isAbsolute()) {
       throw new IllegalArgumentException("\"" + newUri + "\" is not absolute");
@@ -143,7 +123,6 @@
 
   /**
    * The {@link URI} identifiying this resource.
-   *
    * @return the {@link URI} identifying this resource.
    */
   public URI getURI() {
@@ -152,10 +131,69 @@
 
   /**
    * Accept a call from a TypedNodeVisitor.
-   *
    * @param visitor the object doing the visiting.
    */
   public void accept(TypedNodeVisitor visitor) {
     visitor.visitURIReference(this);
   }
+
+  /**
+   * Returns the String-representation of this URI.
+   * @return The String-representation of this URI.
+   */
+  public String toString() {
+    return uri.toString();
+  }
+
+  /**
+   * Returns the String-representation of this URI.
+   * @return The String-representation of this URI.
+   */
+  public String stringValue() {
+    return uri.toString();
+  }
+
+  /**
+   * Gets the namespace of this URI. The namespace is defined as per the
+   * algorithm described in the class documentation.
+   * @return The URI's namespace.
+   */
+  public String getNamespace() {
+    if (!uri.isAbsolute()) return "";
+    if (localNameIdx < 0) localNameIdx = URIUtil.getLocalNameIndex(uri.toString());
+    return uri.toString().substring(0, localNameIdx);
+  }
+
+  /**
+   * Gets the local name of this URI. The local name is defined as per the
+   * algorithm described in the class documentation.
+   * @return The URI's local name.
+   */
+  public String getLocalName() {
+    if (!uri.isAbsolute()) return uri.toString();
+    if (localNameIdx < 0) localNameIdx = URIUtil.getLocalNameIndex(uri.toString());
+    return uri.toString().substring(localNameIdx);
+  }
+
+  /**
+   * Compares a URI object to another object.
+   * @param o The object to compare this URI to.
+   * @return <tt>true</tt> if the other object is an instance of {@link URI}
+   *         and their String-representations are equal, <tt>false</tt>
+   *         otherwise.
+   */
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (!(o instanceof org.openrdf.model.URI)) return false;
+    return toString().equals(o.toString());
+  }
+
+  /**
+   * The hash code of a URI is defined as the hash code of its
+   * String-representation: <tt>toString().hashCode</tt>.
+   * @return A hash code for the URI.
+   */
+  public int hashCode() {
+    return uri.hashCode();
+  }
 }

Modified: trunk/src/jar/querylang/java/org/mulgara/itql/ModelExpressionBuilder.java
===================================================================
--- trunk/src/jar/querylang/java/org/mulgara/itql/ModelExpressionBuilder.java	2008-11-24 19:44:56 UTC (rev 1380)
+++ trunk/src/jar/querylang/java/org/mulgara/itql/ModelExpressionBuilder.java	2008-12-01 19:53:56 UTC (rev 1381)
@@ -54,6 +54,7 @@
 import org.mulgara.query.ModelUnion;
 import org.mulgara.query.QueryException;
 import org.mulgara.util.ServerURIHandler;
+import org.mulgara.util.URIUtil;
 
 /**
  * Builds model expressions using input from the iTQL command interpreter.

Modified: trunk/src/jar/querylang/java/org/mulgara/itql/TqlInterpreter.java
===================================================================
--- trunk/src/jar/querylang/java/org/mulgara/itql/TqlInterpreter.java	2008-11-24 19:44:56 UTC (rev 1380)
+++ trunk/src/jar/querylang/java/org/mulgara/itql/TqlInterpreter.java	2008-12-01 19:53:56 UTC (rev 1381)
@@ -59,6 +59,7 @@
 import org.mulgara.query.*;
 import org.mulgara.query.rdf.*;
 import org.mulgara.server.Session;
+import org.mulgara.util.URIUtil;
 
 
 /**
@@ -1079,7 +1080,6 @@
    * @throws QueryException if the <code>rawVariableList</code> cannot be parsed
    *      into a list of {@link org.mulgara.query.Variable}s
    */
-  @SuppressWarnings("unchecked")
   List<SelectElement> buildVariableList(LinkedList<PElement> rawVariableList) throws
       QueryException, URISyntaxException {
   

Deleted: trunk/src/jar/querylang/java/org/mulgara/itql/URIUtil.java
===================================================================
--- trunk/src/jar/querylang/java/org/mulgara/itql/URIUtil.java	2008-11-24 19:44:56 UTC (rev 1380)
+++ trunk/src/jar/querylang/java/org/mulgara/itql/URIUtil.java	2008-12-01 19:53:56 UTC (rev 1381)
@@ -1,54 +0,0 @@
-/*
- * Copyright 2008 Fedora Commons, Inc.
- *
- * 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.itql;
-
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.Map;
-
-/**
- * A set of methods for managing common URI operations.
- *
- * @created 2007-08-09
- * @author Paul Gearon
- * @copyright &copy; 2007 <a href="http://www.fedora-commons.org/">Fedora Commons</a>
- */
-public class URIUtil {
-
-  /**
-   * Replace an alias in a URI, if one is recognized.
-   * @param uriString A string with the initial uri to check for aliases.
-   * @param aliasMap The map of known aliases to the associated URIs
-   * @return A new URI with the alias replaced, or the original if no alias is found.
-   */
-  public static URI convertToURI(String uriString, Map<String,URI> aliasMap) {
-    try {
-      URI uri = new URI(uriString);
-      if (uri.isOpaque()) {
-        // Attempt qname-to-URI substitution for aliased namespace prefixes
-        URI mapping = aliasMap.get(uri.getScheme());
-        if (mapping != null) {
-          uri = new URI(mapping + uri.getSchemeSpecificPart());
-        }
-      }
-      return uri;
-    } catch (URISyntaxException e) {
-      throw new RuntimeException("Bad URI syntax in resource", e);
-    }
-  }
-  
-}

Modified: trunk/src/jar/resolver/java/org/mulgara/resolver/StringPoolSession.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/StringPoolSession.java	2008-11-24 19:44:56 UTC (rev 1380)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/StringPoolSession.java	2008-12-01 19:53:56 UTC (rev 1381)
@@ -55,6 +55,7 @@
 import org.mulgara.store.xa.XAResolverSession;
 import org.mulgara.store.xa.XAStringPool;
 import org.mulgara.util.LongMapper;
+import org.mulgara.util.QueryParams;
 import org.mulgara.util.StackTrace;
 
 /**
@@ -99,6 +100,9 @@
   /** Extracts STORE_FLAG */
   static final int STORE_MASK = 2;
 
+  /** The name of the graph parameter in a URI */
+  static final String GRAPH = "graph";
+
   /** The unique {@link URI} naming this database. */
   private final URI databaseURI;
 
@@ -693,8 +697,11 @@
             // Construct a new relative uri with just the fragment and
             // optional query string.
             SPObjectFactory spObjectFactory = persistentStringPool.getSPObjectFactory();
+            QueryParams query = QueryParams.decode(uri);
+            String gName = query.get(GRAPH);
             try {
-              spObject = spObjectFactory.newSPURI(new URI(null, null, null, uri.getQuery(), fragment));
+              if (gName != null) spObject = spObjectFactory.newSPURI(new URI(gName));
+              else spObject = spObjectFactory.newSPURI(new URI(null, null, null, uri.getQuery(), fragment));
             } catch (URISyntaxException ex) {
               logger.warn("Cannot create relative URI with fragment:\"" + fragment + "\"", ex);
             }

Modified: trunk/src/jar/resolver-distributed/java/org/mulgara/resolver/distributed/NetworkDelegator.java
===================================================================
--- trunk/src/jar/resolver-distributed/java/org/mulgara/resolver/distributed/NetworkDelegator.java	2008-11-24 19:44:56 UTC (rev 1380)
+++ trunk/src/jar/resolver-distributed/java/org/mulgara/resolver/distributed/NetworkDelegator.java	2008-12-01 19:53:56 UTC (rev 1381)
@@ -32,6 +32,7 @@
 import org.mulgara.server.ServerInfo;
 import org.mulgara.server.NonRemoteSessionException;
 import org.mulgara.server.driver.SessionFactoryFinderException;
+import org.mulgara.util.URIUtil;
 import org.mulgara.resolver.distributed.remote.StatementSetFactory;
 import org.mulgara.resolver.spi.GlobalizeException;
 import org.mulgara.resolver.spi.Resolution;
@@ -106,13 +107,16 @@
     URIReferenceImpl modelRef = getModelRef(localModel);
 
     URI serverUri = getServerUri(modelRef);
-    logger.debug("Querying for: " + localConstraint + " in model: " + modelRef + " on server: " + serverUri);
-
-    Answer ans = getServerSession(serverUri).query(globalizedQuery(localConstraint, modelRef));
     try {
+      modelRef = new URIReferenceImpl(URIUtil.localizeGraphUri(modelRef.getURI()));
+      logger.debug("Querying for: " + localConstraint + " in model: " + modelRef + " on server: " + serverUri);
+  
+      Answer ans = getServerSession(serverUri).query(globalizedQuery(localConstraint, modelRef));
       return new AnswerResolution(serverUri, session, ans, localConstraint);
     } catch (TuplesException te) {
       throw new ResolverException("Localization failed", te);
+    } catch (URISyntaxException qe) {
+      throw new QueryException("Bad graph URI provided for resolution", qe);
     }
   }
 

Modified: trunk/src/jar/store-stringpool-xa11/java/org/mulgara/store/stringpool/xa11/XA11StringPoolImpl.java
===================================================================
--- trunk/src/jar/store-stringpool-xa11/java/org/mulgara/store/stringpool/xa11/XA11StringPoolImpl.java	2008-11-24 19:44:56 UTC (rev 1380)
+++ trunk/src/jar/store-stringpool-xa11/java/org/mulgara/store/stringpool/xa11/XA11StringPoolImpl.java	2008-12-01 19:53:56 UTC (rev 1381)
@@ -311,6 +311,8 @@
     if (node >= nextGNodeValue) return null;
     try {
       return new DataStruct(gNodeToDataReadOnly, node).getSPObject();
+    } catch (IllegalArgumentException iae) {
+      throw new StringPoolException("Bad node data. gNode = " + node, iae);
     } catch (IOException ioe) {
       throw new StringPoolException("Unable to load data from data pool.", ioe);
     }

Copied: trunk/src/jar/util/java/org/mulgara/util/QueryParams.java (from rev 1380, trunk/src/jar/web/java/org/mulgara/webquery/QueryParams.java)
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/QueryParams.java	                        (rev 0)
+++ trunk/src/jar/util/java/org/mulgara/util/QueryParams.java	2008-12-01 19:53:56 UTC (rev 1381)
@@ -0,0 +1,191 @@
+/*
+ * Copyright 2008 Fedora Commons, Inc.
+ *
+ * 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.util;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.log4j.Logger;
+import org.mulgara.util.functional.Pair;
+
+/**
+ * Represents the parameters used in an HTTP request.
+ * Note: This does NOT permit duplicate parameters.
+ *
+ * @created Aug 5, 2008
+ * @author Paul Gearon
+ * @copyright &copy; 2008 <a href="http://www.fedora-commons.org/">Fedora Commons</a>
+ */
+public class QueryParams {
+
+  /** Logger. */
+  private static Logger logger = Logger.getLogger(QueryParams.class.getName());
+
+  /** The encoding to use for a URL */
+  static final String ENCODING = "UTF-8";
+
+  /** The parameter names and values. */
+  private Map<String,String> params = new LinkedHashMap<String,String>();
+
+  /**
+   * Create an empty set of parameters.
+   */
+  public QueryParams() {
+  }
+
+  /**
+   * Create a set of parameters from a single name/value pair.
+   * @param name The name of the parameter.
+   * @param value The value for the parameter.
+   */
+  public QueryParams(String name, Object value) {
+    params.put(name, value.toString());
+  }
+
+  /**
+   * Create a set of parameters from an array of name/value pairs.
+   * @param pairs An array of name/value pairs.
+   */
+  public QueryParams(Pair<String,String>... pairs) {
+    for (Pair<String,String> p: pairs) params.put(p.first(), p.second());
+  }
+
+  /**
+   * Create a set of parameters from a collection of name/value pairs.
+   * @param pairs A collection of name/value pairs.
+   */
+  public QueryParams(Collection<Pair<String,String>> pairs) {
+    for (Pair<String,String> p: pairs) params.put(p.first(), p.second());
+  }
+
+  /**
+   * Decode a URI into a set of parameters.
+   * @param u The URI to decode.
+   * @return A set of parameters. This may be empty if the URI does not include a query.
+   */
+  public static QueryParams decode(URI u) {
+    QueryParams p = new QueryParams();
+    String query = u.getRawQuery();
+    if (query != null) {
+      String[] nvList = query.split("&");
+      for (String nv: nvList) {
+        int eqIdx = nv.indexOf('=');
+        try {
+          String name;
+          String value;
+          if (eqIdx >= 0) {
+            name = URLDecoder.decode(nv.substring(0, eqIdx), ENCODING);
+            value = URLDecoder.decode(nv.substring(eqIdx + 1), ENCODING);
+          } else {
+            // no name=value, so map the element to itself
+            name = nv;
+            value = nv;
+          }
+          p.add(name, value);
+        } catch (UnsupportedEncodingException e) {
+          logger.error("Unhandled exception during URI decoding", e);
+        }
+      }
+    }
+    return p;
+  }
+
+  /**
+   * Adds a name/value to the parameters.
+   * @param pair The name/value pair to be added.
+   * @return This parameters object.
+   */
+  public QueryParams add(Pair<String,String> pair) {
+    params.put(pair.first(), pair.second());
+    return this;
+  }
+
+  /**
+   * Adds a name/value to the parameters.
+   * @param name The name of the parameter to be added.
+   * @param value The valueof the parameter to be added.
+   * @return This parameters object.
+   */
+  public QueryParams add(String name, String value) {
+    params.put(name, value);
+    return this;
+  }
+
+  /**
+   * Adds an array of name/values to the parameters.
+   * @param pairs The name/value array to be added.
+   * @return This parameters object.
+   */
+  public QueryParams addAll(Pair<String,String>... pairs) {
+    for (Pair<String,String> p: pairs) params.put(p.first(), p.second());
+    return this;
+  }
+
+  /**
+   * Adds a collection of name/values to the parameters.
+   * @param pairs The name/value collection to be added.
+   * @return This parameters object.
+   */
+  public QueryParams addAll(Collection<Pair<String,String>> pairs) {
+    for (Pair<String,String> p: pairs) params.put(p.first(), p.second());
+    return this;
+  }
+
+  /**
+   * Retrieves a value for a given parameter.
+   * @param param The parameter name.
+   * @return The decoded string for this value.
+   */
+  public String get(String param) {
+    return params.get(param);
+  }
+
+  /**
+   * Retrieves a value for a given parameter.
+   * @param param The parameter name.
+   * @return The decoded string for this value.
+   */
+  public Set<String> getNames() {
+    return params.keySet();
+  }
+
+  /**
+   * Converts this set of parameters to the query portion of a URL.
+   * @return a query to be added to a URL.
+   */
+  public String toString() {
+    try {
+      boolean first = true;
+      StringBuilder b = new StringBuilder();
+      for (Map.Entry<String,String> e: params.entrySet()) {
+        if (first) first = false;
+        else b.append("&amp;");
+        b.append(URLEncoder.encode(e.getKey(), ENCODING));
+        b.append("=").append(URLEncoder.encode(e.getValue(), ENCODING));
+      }
+      return b.toString();
+    } catch (UnsupportedEncodingException e) {
+      throw new IllegalArgumentException("Unable to encode with " + ENCODING + ": " + e.getMessage());
+    }
+  }
+}


Property changes on: trunk/src/jar/util/java/org/mulgara/util/QueryParams.java
___________________________________________________________________
Name: svn:mergeinfo
   + 

Added: trunk/src/jar/util/java/org/mulgara/util/URIUtil.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/URIUtil.java	                        (rev 0)
+++ trunk/src/jar/util/java/org/mulgara/util/URIUtil.java	2008-12-01 19:53:56 UTC (rev 1381)
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2008 Fedora Commons, Inc.
+ *
+ * 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.util;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Map;
+
+/**
+ * Utilities for working with URIs in Mulgara.
+ *
+ * @created Nov 26, 2008
+ * @author Paul Gearon
+ */
+public class URIUtil {
+
+  /** The parameter name for the graph. */
+  private static final String GRAPH = "graph";
+
+  /**
+   * Convert a graph to a localized form, if it is defined for localizing.
+   * @param uri The URI to convert.
+   * @return A reference to a local graph URI. This will be the uriRef if it does not require localizing.
+   * @throws QueryException If the local graph has an illegal name.
+   */
+  public static URI localizeGraphUri(URI uri) throws URISyntaxException {
+    QueryParams params = QueryParams.decode(uri);
+    String graphName = params.get(GRAPH);
+    return (graphName == null) ? uri : new URI(graphName);
+  }
+
+
+  /**
+   * Replace an alias in a URI, if one is recognized.
+   * @param uriString A string with the initial uri to check for aliases.
+   * @param aliasMap The map of known aliases to the associated URIs
+   * @return A new URI with the alias replaced, or the original if no alias is found.
+   */
+  public static URI convertToURI(String uriString, Map<String,URI> aliasMap) {
+    try {
+      URI uri = new URI(uriString);
+      if (uri.isOpaque()) {
+        // Attempt qname-to-URI substitution for aliased namespace prefixes
+        URI mapping = aliasMap.get(uri.getScheme());
+        if (mapping != null) {
+          uri = new URI(mapping + uri.getSchemeSpecificPart());
+        }
+      }
+      return uri;
+    } catch (URISyntaxException e) {
+      throw new RuntimeException("Bad URI syntax in resource", e);
+    }
+  }
+  
+}

Deleted: trunk/src/jar/web/java/org/mulgara/webquery/QueryParams.java
===================================================================
--- trunk/src/jar/web/java/org/mulgara/webquery/QueryParams.java	2008-11-24 19:44:56 UTC (rev 1380)
+++ trunk/src/jar/web/java/org/mulgara/webquery/QueryParams.java	2008-12-01 19:53:56 UTC (rev 1381)
@@ -1,133 +0,0 @@
-/*
- * Copyright 2008 Fedora Commons, Inc.
- *
- * 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.webquery;
-
-import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
-import java.util.Collection;
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-import org.mulgara.util.functional.Pair;
-
-/**
- * Represents the parameters used in an HTTP request.
- *
- * @created Aug 5, 2008
- * @author Paul Gearon
- * @copyright &copy; 2008 <a href="http://www.fedora-commons.org/">Fedora Commons</a>
- */
-public class QueryParams {
-
-  /** The encoding to use for a URL */
-  static final String ENCODING = "UTF-8";
-
-  /** The parameter names and values. */
-  private Map<String,String> params = new LinkedHashMap<String,String>();
-
-  /**
-   * Create an empty set of parameters.
-   */
-  public QueryParams() {
-  }
-
-  /**
-   * Create a set of parameters from a single name/value pair.
-   * @param name The name of the parameter.
-   * @param value The value for the parameter.
-   */
-  public QueryParams(String name, Object value) {
-    params.put(name, value.toString());
-  }
-
-  /**
-   * Create a set of parameters from an array of name/value pairs.
-   * @param pairs An array of name/value pairs.
-   */
-  public QueryParams(Pair<String,String>... pairs) {
-    for (Pair<String,String> p: pairs) params.put(p.first(), p.second());
-  }
-
-  /**
-   * Create a set of parameters from a collection of name/value pairs.
-   * @param pairs A collection of name/value pairs.
-   */
-  public QueryParams(Collection<Pair<String,String>> pairs) {
-    for (Pair<String,String> p: pairs) params.put(p.first(), p.second());
-  }
-
-  /**
-   * Adds a name/value to the parameters.
-   * @param pair The name/value pair to be added.
-   * @return This parameters object.
-   */
-  public QueryParams add(Pair<String,String> pair) {
-    params.put(pair.first(), pair.second());
-    return this;
-  }
-
-  /**
-   * Adds a name/value to the parameters.
-   * @param name The name of the parameter to be added.
-   * @param value The valueof the parameter to be added.
-   * @return This parameters object.
-   */
-  public QueryParams add(String name, String value) {
-    params.put(name, value);
-    return this;
-  }
-
-  /**
-   * Adds an array of name/values to the parameters.
-   * @param pairs The name/value array to be added.
-   * @return This parameters object.
-   */
-  public QueryParams addAll(Pair<String,String>... pairs) {
-    for (Pair<String,String> p: pairs) params.put(p.first(), p.second());
-    return this;
-  }
-
-  /**
-   * Adds a collection of name/values to the parameters.
-   * @param pairs The name/value collection to be added.
-   * @return This parameters object.
-   */
-  public QueryParams addAll(Collection<Pair<String,String>> pairs) {
-    for (Pair<String,String> p: pairs) params.put(p.first(), p.second());
-    return this;
-  }
-
-  /**
-   * Converts this set of parameters to the query portion of a URL.
-   * @return a query to be added to a URL.
-   */
-  public String toString() {
-    try {
-      boolean first = true;
-      StringBuilder b = new StringBuilder();
-      for (Map.Entry<String,String> e: params.entrySet()) {
-        if (first) first = false;
-        else b.append("&amp;");
-        b.append(URLEncoder.encode(e.getKey(), ENCODING));
-        b.append("=").append(URLEncoder.encode(e.getValue(), ENCODING));
-      }
-      return b.toString();
-    } catch (UnsupportedEncodingException e) {
-      throw new IllegalArgumentException("Unable to encode with " + ENCODING + ": " + e.getMessage());
-    }
-  }
-}

Modified: trunk/src/jar/web/java/org/mulgara/webquery/QueryResponsePage.java
===================================================================
--- trunk/src/jar/web/java/org/mulgara/webquery/QueryResponsePage.java	2008-11-24 19:44:56 UTC (rev 1380)
+++ trunk/src/jar/web/java/org/mulgara/webquery/QueryResponsePage.java	2008-12-01 19:53:56 UTC (rev 1381)
@@ -39,6 +39,7 @@
 import org.mulgara.query.TuplesException;
 import org.mulgara.query.Variable;
 import org.mulgara.query.operation.Command;
+import org.mulgara.util.QueryParams;
 import org.mulgara.util.functional.Pair;
 import org.mulgara.webquery.html.Anchor;
 import org.mulgara.webquery.html.HtmlElement;




More information about the Mulgara-svn mailing list