[Mulgara-svn] r1823 - in trunk/src/jar: query/java/org/mulgara/query/operation querylang/java/org/mulgara/itql querylang/java/org/mulgara/protocol

pag at mulgara.org pag at mulgara.org
Mon Oct 19 12:58:59 UTC 2009


Author: pag
Date: 2009-10-19 05:58:58 -0700 (Mon, 19 Oct 2009)
New Revision: 1823

Added:
   trunk/src/jar/query/java/org/mulgara/query/operation/ListAlias.java
Modified:
   trunk/src/jar/querylang/java/org/mulgara/itql/TqlInterpreter.java
   trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlJSONObject.java
   trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlXMLObject.java
Log:
Alias command now returned as a dictionary rather than text. Also fixed JSON encoding.

Added: trunk/src/jar/query/java/org/mulgara/query/operation/ListAlias.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/operation/ListAlias.java	                        (rev 0)
+++ trunk/src/jar/query/java/org/mulgara/query/operation/ListAlias.java	2009-10-19 12:58:58 UTC (rev 1823)
@@ -0,0 +1,72 @@
+/*
+ * 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.operation;
+
+import java.net.URI;
+import java.util.Map;
+
+import org.mulgara.connection.Connection;
+
+
+/**
+ * An AST element for the ALIAS command.
+ *
+ * @created 2009-10-19
+ * @author Paul Gearon
+ */
+public class ListAlias extends LocalCommand {
+
+  private final Map<String,URI> aliases;
+
+  /**
+   * Creates a new list alias command.
+   */
+  public ListAlias(Map<String,URI> aliases) {
+    this.aliases = aliases;
+    setResultMessage(buildAliasList(aliases));
+  }
+  
+  /**
+   * Indicates that this operation is for a UI.
+   * @return <code>true</code> as operation is for UI output only.
+   */
+  public boolean isUICommand() {
+    return true;
+  }
+
+  /**
+   * Asks for the dictionary of aliases.
+   * @param conn ignored.
+   * @return A Map object that maps namespace prefixes to the namespace URI.
+   */
+  public Object execute(Connection conn) {
+    return aliases;
+  }
+
+  /**
+   * Writes the alias map as a set of URI/namespace pairs to a string for printing.
+   * @return A String containing all the alias mappings.
+   */
+  private static String buildAliasList(Map<String,URI> aliases) {
+    StringBuilder buffer = new StringBuilder();
+    for (Map.Entry<String,URI> alias: aliases.entrySet()) {
+      buffer.append(alias.getKey()).append(":  <").append(alias.getValue()).append(">\n");
+    }
+    return buffer.toString();
+  }
+
+}

Modified: trunk/src/jar/querylang/java/org/mulgara/itql/TqlInterpreter.java
===================================================================
--- trunk/src/jar/querylang/java/org/mulgara/itql/TqlInterpreter.java	2009-10-19 12:58:01 UTC (rev 1822)
+++ trunk/src/jar/querylang/java/org/mulgara/itql/TqlInterpreter.java	2009-10-19 12:58:58 UTC (rev 1823)
@@ -45,6 +45,7 @@
 import org.mulgara.query.operation.Export;
 import org.mulgara.query.operation.Help;
 import org.mulgara.query.operation.Insertion;
+import org.mulgara.query.operation.ListAlias;
 import org.mulgara.query.operation.Load;
 import org.mulgara.query.operation.Modification;
 import org.mulgara.query.operation.Quit;
@@ -520,7 +521,7 @@
 
     // Return an AST element, for reporting on what happened.
     // Use a Help command, with the alias listing as the help text.
-    lastCommand = new Help(buildAliasList());
+    lastCommand = new ListAlias(Collections.unmodifiableMap(aliasMap));
   }
 
   /**
@@ -1413,19 +1414,6 @@
 
 
   /**
-   * Writes the alias map as a set of URI/namespace pairs to a string for printing.
-   * @return A String containing all the alias mappings.
-   */
-  private String buildAliasList() {
-    StringBuilder buffer = new StringBuilder();
-    for (Map.Entry<String,URI> alias: getAliasMap().entrySet()) {
-      buffer.append(alias.getKey()).append(":  <").append(alias.getValue()).append(">\n");
-    }
-    return buffer.toString();
-  }
-
-
-  /**
    * Log the TQL command to a specified file
    *
    * @param command The TQL command to be validated

Modified: trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlJSONObject.java
===================================================================
--- trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlJSONObject.java	2009-10-19 12:58:01 UTC (rev 1822)
+++ trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlJSONObject.java	2009-10-19 12:58:58 UTC (rev 1823)
@@ -19,6 +19,7 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
+import java.util.Map;
 
 
 /**
@@ -61,9 +62,30 @@
     s.flush();
   }
 
+  /**
+   * Create a JSON string for a single Map object.
+   * @param o The object to convert to a JSON string.
+   * @return The JSON string representing the parameter.
+   */
+  static String jsonHash(Map<?,?> o) {
+    StringBuilder s = new StringBuilder("{ ");
+    boolean first = true;
+    for (Map.Entry<?,?> entry: o.entrySet()) {
+      if (!first) s.append(", ");
+      else first = false;
+      s.append("\"");
+      s.append(entry.getKey());
+      s.append("\": ");
+      s.append(jsonEscape(entry.getValue()));
+    }
+    s.append(" }");
+    return s.toString();
+  }
+
   /** Trivial escaping. */
-  public static String jsonEscape(Object o) {
+  static String jsonEscape(Object o) {
     if (o instanceof Number) return o.toString();
+    if (o instanceof Map<?,?>) return jsonHash((Map<?,?>)o);
     String data = o.toString();
     data = data.replace("\"", "\\\"");
     data = data.replace("\\", "\\\\");
@@ -73,6 +95,6 @@
     data = data.replace("\n", "\\n");
     data = data.replace("\r", "\\r");
     data = data.replace("\t", "\\t");
-    return data;
+    return "\"" + data + "\"";
   }
 }

Modified: trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlXMLObject.java
===================================================================
--- trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlXMLObject.java	2009-10-19 12:58:01 UTC (rev 1822)
+++ trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlXMLObject.java	2009-10-19 12:58:58 UTC (rev 1823)
@@ -21,6 +21,7 @@
 import java.io.OutputStreamWriter;
 import java.net.URI;
 import java.nio.charset.Charset;
+import java.util.Map;
 
 import org.mulgara.util.StringUtil;
 
@@ -75,8 +76,8 @@
   /** {@inheritDoc} */
   protected void addDocHeader() throws IOException {
     s.append("<?xml version=\"1.0\"?>\n");
-    s.append("<sparql xmlns=\"http://www.w3.org/2005/sparql-results#\"");
-    s.append(">");
+    s.append("<sparql xmlns=\"http://www.w3.org/2005/sparql-results#\">");
+    if (prettyPrint) s.append("\n");
   }
 
   /** {@inheritDoc} */
@@ -86,9 +87,14 @@
 
   /** {@inheritDoc} */
   protected void addResults() throws IOException {
-    if (prettyPrint) s.append(INDENT_STR).append("<data>");
-    if (objectData != null) s.append(StringUtil.quoteAV(objectData.toString()));
+    if (prettyPrint) s.append(INDENT_STR);
+    s.append("<data>");
+    if (objectData != null) {
+      if (objectData instanceof Map<?,?>) s.append(encodeMap((Map<?,?>)objectData, INDENT_STR));
+      else s.append(StringUtil.quoteAV(objectData.toString()));
+    }
     s.append("</data>");
+    if (prettyPrint) s.append("\n");
   }
 
 
@@ -123,4 +129,18 @@
     this.prettyPrint = prettyPrint;
   }
 
+  public String encodeMap(Map<?,?> map, String indent) {
+    String i2 = prettyPrint ? "\n" + indent + INDENT_STR : "";
+    StringBuilder s = new StringBuilder();
+    for (Map.Entry<?,?> entry: map.entrySet()) {
+      s.append(i2).append("<key>");
+      s.append(StringUtil.quoteAV(entry.getKey().toString()));
+      s.append("</key>");
+      s.append(i2).append("<value>");
+      s.append(StringUtil.quoteAV(entry.getValue().toString()));
+      s.append("</value>");
+    }
+    if (prettyPrint) s.append("\n").append(indent);
+    return s.toString();
+  }
 }




More information about the Mulgara-svn mailing list