[Mulgara-svn] r1786 - trunk/src/jar/querylang/java/org/mulgara/protocol

pag at mulgara.org pag at mulgara.org
Sat Sep 12 05:39:39 UTC 2009


Author: pag
Date: 2009-09-11 22:39:37 -0700 (Fri, 11 Sep 2009)
New Revision: 1786

Modified:
   trunk/src/jar/querylang/java/org/mulgara/protocol/AbstractStreamedAnswer.java
   trunk/src/jar/querylang/java/org/mulgara/protocol/AbstractStreamedXMLAnswer.java
   trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlJSONAnswer.java
   trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlXMLAnswer.java
   trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlXMLAnswerUnitTest.java
Log:
Changed default encoding to UTF-8, added encoding to the XML header, and added constructor variants to set the encoding

Modified: trunk/src/jar/querylang/java/org/mulgara/protocol/AbstractStreamedAnswer.java
===================================================================
--- trunk/src/jar/querylang/java/org/mulgara/protocol/AbstractStreamedAnswer.java	2009-08-26 16:53:58 UTC (rev 1785)
+++ trunk/src/jar/querylang/java/org/mulgara/protocol/AbstractStreamedAnswer.java	2009-09-12 05:39:37 UTC (rev 1786)
@@ -21,6 +21,8 @@
 import java.io.OutputStreamWriter;
 import java.nio.charset.Charset;
 
+import org.apache.log4j.Logger;
+
 import org.jrdf.graph.BlankNode;
 import org.jrdf.graph.Literal;
 import org.jrdf.graph.URIReference;
@@ -37,6 +39,9 @@
  */
 public abstract class AbstractStreamedAnswer {
 
+  /** Logger. */
+  private final static Logger logger = Logger.getLogger(AbstractStreamedAnswer.class);
+
   /** The API {@link Answer} to convert to the stream. */
   protected final Answer answer;
 
@@ -53,7 +58,7 @@
   protected OutputStream output = null;
 
   /** The charset encoding to use when writing to the output stream. */
-  Charset charset = Charset.defaultCharset();
+  Charset charset = Charset.forName("UTF-8");
 
   /** Adds a literal to the stream */
   protected abstract void addLiteral(Literal literal) throws IOException;
@@ -99,6 +104,20 @@
   }
 
   /**
+   * Creates the object around the answer and output stream.
+   * @param answer The answer to encode.
+   * @param output The stream to write to.
+   */
+  public AbstractStreamedAnswer(Answer answer, OutputStream output, String charsetName) {
+    this(answer, output);
+    try {
+      charset = Charset.forName(charsetName);
+    } catch (Exception e) {
+      logger.error("Invalid charset. Using UTF-8: " + charsetName);
+    }
+  }
+
+  /**
    * @see org.mulgara.protocol.StreamedXMLAnswer#emit()
    */
   public void emit() throws TuplesException, IOException {

Modified: trunk/src/jar/querylang/java/org/mulgara/protocol/AbstractStreamedXMLAnswer.java
===================================================================
--- trunk/src/jar/querylang/java/org/mulgara/protocol/AbstractStreamedXMLAnswer.java	2009-08-26 16:53:58 UTC (rev 1785)
+++ trunk/src/jar/querylang/java/org/mulgara/protocol/AbstractStreamedXMLAnswer.java	2009-09-12 05:39:37 UTC (rev 1786)
@@ -74,6 +74,14 @@
   }
 
   /**
+   * Create an XMLAnswer based on a given {@link Answer}.
+   * @param answer The Answer with the data to convert.
+   */
+  public AbstractStreamedXMLAnswer(Answer answer, OutputStream output, String charsetName) {
+    super(answer, output, charsetName);
+  }
+
+  /**
    * @see org.mulgara.protocol.StreamedXMLAnswer#setCharacterEncoding(java.lang.String)
    */
   public void setCharacterEncoding(String encoding) {

Modified: trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlJSONAnswer.java
===================================================================
--- trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlJSONAnswer.java	2009-08-26 16:53:58 UTC (rev 1785)
+++ trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlJSONAnswer.java	2009-09-12 05:39:37 UTC (rev 1786)
@@ -49,8 +49,9 @@
   boolean prependComma = false;
 
   /**
-   * Creates an XML Answer conforming to SPARQL XML results.
+   * Creates a JSON Answer conforming to SPARQL JSON results.
    * @param answer The Answer to wrap.
+   * @param output The stream to write to.
    */
   public StreamedSparqlJSONAnswer(Answer answer, OutputStream output) {
     super((answer instanceof BooleanAnswer) ? null : answer, output);
@@ -58,9 +59,10 @@
   }
 
   /**
-   * Creates an XML Answer with additional metadata.
+   * Creates an JSON Answer with additional metadata.
    * @param answer The Answer to wrap.
    * @param metadata Additional metadata for the answer.
+   * @param output The stream to write to.
    */
   public StreamedSparqlJSONAnswer(Answer answer, URI metadata, OutputStream output) {
     this(answer, output);
@@ -68,8 +70,9 @@
   }
 
   /**
-   * Creates an XML Answer conforming to SPARQL XML results.
+   * Creates a JSON Answer conforming to SPARQL JSON results.
    * @param result The boolean result to encode.
+   * @param output The stream to write to.
    */
   public StreamedSparqlJSONAnswer(boolean result, OutputStream output) {
     super(null, output);
@@ -77,9 +80,10 @@
   }
 
   /**
-   * Creates an XML Answer with additional metadata.
+   * Creates a JSON Answer with additional metadata.
    * @param result The boolean result to encode.
    * @param metadata Additional metadata for the answer.
+   * @param output The stream to write to.
    */
   public StreamedSparqlJSONAnswer(boolean result, URI metadata, OutputStream output) {
     super(null, output);
@@ -87,6 +91,53 @@
     additionalMetadata = metadata;
   }
 
+  /**
+   * Creates a JSON Answer conforming to SPARQL JSON results.
+   * @param answer The Answer to wrap.
+   * @param output The stream to write to.
+   * @param charsetName The name of the character set to use, if not the default of UTF-8.
+   */
+  public StreamedSparqlJSONAnswer(Answer answer, OutputStream output, String charsetName) {
+    super((answer instanceof BooleanAnswer) ? null : answer, output, charsetName);
+    if (answer instanceof BooleanAnswer) booleanResult = ((BooleanAnswer)answer).getResult();
+  }
+
+  /**
+   * Creates an JSON Answer with additional metadata.
+   * @param answer The Answer to wrap.
+   * @param metadata Additional metadata for the answer.
+   * @param output The stream to write to.
+   * @param charsetName The name of the character set to use, if not the default of UTF-8.
+   */
+  public StreamedSparqlJSONAnswer(Answer answer, URI metadata, OutputStream output, String charsetName) {
+    this(answer, output, charsetName);
+    additionalMetadata = metadata;
+  }
+
+  /**
+   * Creates a JSON Answer conforming to SPARQL JSON results.
+   * @param result The boolean result to encode.
+   * @param output The stream to write to.
+   * @param charsetName The name of the character set to use, if not the default of UTF-8.
+   */
+  public StreamedSparqlJSONAnswer(boolean result, OutputStream output, String charsetName) {
+    super(null, output, charsetName);
+    booleanResult = result;
+  }
+
+  /**
+   * Creates a JSON Answer with additional metadata.
+   * @param result The boolean result to encode.
+   * @param metadata Additional metadata for the answer.
+   * @param output The stream to write to.
+   * @param charsetName The name of the character set to use, if not the default of UTF-8.
+   */
+  public StreamedSparqlJSONAnswer(boolean result, URI metadata, OutputStream output, String charsetName) {
+    super(null, output, charsetName);
+    booleanResult = result;
+    additionalMetadata = metadata;
+  }
+
   /** {@inheritDoc} */
   protected void addDocHeader() throws IOException {
     s.append("{ ");

Modified: trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlXMLAnswer.java
===================================================================
--- trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlXMLAnswer.java	2009-08-26 16:53:58 UTC (rev 1785)
+++ trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlXMLAnswer.java	2009-09-12 05:39:37 UTC (rev 1786)
@@ -102,7 +102,9 @@
 
   /** {@inheritDoc} */
   protected void addDocHeader() throws IOException {
-    s.append("<?xml version=\"1.0\"?>\n");
+    s.append("<?xml version=\"1.0\" encoding=\"");
+    s.append(charset.name());
+    s.append("\"?>\n");
     s.append("<sparql xmlns=\"http://www.w3.org/2005/sparql-results#\"");
     for (Map.Entry<String,URI> ns: namespaces.entrySet()) {
       s.append(prettyPrint ? HEADER_INDENT : " ");

Modified: trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlXMLAnswerUnitTest.java
===================================================================
--- trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlXMLAnswerUnitTest.java	2009-08-26 16:53:58 UTC (rev 1785)
+++ trunk/src/jar/querylang/java/org/mulgara/protocol/StreamedSparqlXMLAnswerUnitTest.java	2009-09-12 05:39:37 UTC (rev 1786)
@@ -641,7 +641,7 @@
     return meta ? HEAD_META_INDENT + HEAD_META + "\n" : "";
   }
 
-  static final String DOC_HEAD = "<?xml version=\"1.0\"?>\n";
+  static final String DOC_HEAD = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
 
   static final String SPARQL_HEAD = "<sparql xmlns=\"http://www.w3.org/2005/sparql-results#\"";
 




More information about the Mulgara-svn mailing list