clerezza-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m...@apache.org
Subject git commit: CLEREZZA-929: Support different text encodings for Csv and Tsv
Date Mon, 28 Jul 2014 10:17:43 GMT
Repository: clerezza
Updated Branches:
  refs/heads/master 55a6e4b9e -> 0250a90bc


CLEREZZA-929: Support different text encodings for Csv and Tsv


Project: http://git-wip-us.apache.org/repos/asf/clerezza/repo
Commit: http://git-wip-us.apache.org/repos/asf/clerezza/commit/0250a90b
Tree: http://git-wip-us.apache.org/repos/asf/clerezza/tree/0250a90b
Diff: http://git-wip-us.apache.org/repos/asf/clerezza/diff/0250a90b

Branch: refs/heads/master
Commit: 0250a90bc9dce6268e61ed0f64158ff9b17b2244
Parents: 55a6e4b
Author: Minto van der Sluis <misl@apache.org>
Authored: Mon Jul 28 11:59:46 2014 +0200
Committer: Minto van der Sluis <misl@apache.org>
Committed: Mon Jul 28 11:59:46 2014 +0200

----------------------------------------------------------------------
 jaxrs.rdf.providers/pom.xml                     |  2 +-
 .../ResultSetCsvMessageBodyWriter.java          | 60 +++++++++++++++++---
 .../ResultSetTsvMessageBodyWriter.java          | 58 ++++++++++++++++---
 3 files changed, 102 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/clerezza/blob/0250a90b/jaxrs.rdf.providers/pom.xml
----------------------------------------------------------------------
diff --git a/jaxrs.rdf.providers/pom.xml b/jaxrs.rdf.providers/pom.xml
index 400dab4..0f8940a 100644
--- a/jaxrs.rdf.providers/pom.xml
+++ b/jaxrs.rdf.providers/pom.xml
@@ -30,7 +30,7 @@
     <groupId>org.apache.clerezza</groupId>
     <artifactId>jaxrs.rdf.providers</artifactId>
     <packaging>bundle</packaging>
-    <version>0.15-SNAPSHOT</version>
+    <version>0.16-SNAPSHOT</version>
     <name>Clerezza - JAX-RS MessageBodyProviders for RDF</name>
     <dependencies>
         <dependency>

http://git-wip-us.apache.org/repos/asf/clerezza/blob/0250a90b/jaxrs.rdf.providers/src/main/java/org/apache/clerezza/jaxrs/sparql/providers/ResultSetCsvMessageBodyWriter.java
----------------------------------------------------------------------
diff --git a/jaxrs.rdf.providers/src/main/java/org/apache/clerezza/jaxrs/sparql/providers/ResultSetCsvMessageBodyWriter.java
b/jaxrs.rdf.providers/src/main/java/org/apache/clerezza/jaxrs/sparql/providers/ResultSetCsvMessageBodyWriter.java
index 0739e69..848ace0 100644
--- a/jaxrs.rdf.providers/src/main/java/org/apache/clerezza/jaxrs/sparql/providers/ResultSetCsvMessageBodyWriter.java
+++ b/jaxrs.rdf.providers/src/main/java/org/apache/clerezza/jaxrs/sparql/providers/ResultSetCsvMessageBodyWriter.java
@@ -63,18 +63,22 @@ public class ResultSetCsvMessageBodyWriter implements MessageBodyWriter<ResultSe
 
   private static final Logger logger = LoggerFactory
       .getLogger( ResultSetCsvMessageBodyWriter.class );
-  private static final String UTF_8 = "UTF-8";
-  private static byte[] separator;
 
-  static {
+  private String textEncoding = "UTF-8";
+  private byte[] separator;
+  
+  public ResultSetCsvMessageBodyWriter() {
     try {
-      separator = ",".getBytes( UTF_8 );
+      buildSeparatorConformEncoding( textEncoding );
     } catch( UnsupportedEncodingException e ) {
       logger.error( "Developer error", e );
     }
-
   }
-
+  
+  // --------------------------------------------------------------------------
+  // Implementing MessageBodyWriter
+  // --------------------------------------------------------------------------
+  
   @Override
   public boolean isWriteable( Class<?> type, Type genericType, Annotation[] annotations,
       MediaType mediaType ) {
@@ -99,6 +103,44 @@ public class ResultSetCsvMessageBodyWriter implements MessageBodyWriter<ResultSe
     }
   }
 
+  // --------------------------------------------------------------------------
+  // Public interface
+  // --------------------------------------------------------------------------
+  
+  /**
+   * Sets the text encoding for the resource. This setting must only used 
+   * if the resource response represents text.
+   * 
+   * @param textEncoding
+   *            character encoding of text body
+   * @throws UnsupportedEncodingException when the given encoding is not supported.
+   */
+  public void setTextEncoding(String textEncoding) throws UnsupportedEncodingException {
+    buildSeparatorConformEncoding( textEncoding );
+    this.textEncoding = textEncoding;
+  }
+
+  /**
+   * @return text encoding for resource
+   */
+  protected String getTextEncoding() {
+    return textEncoding;
+  }
+
+  // --------------------------------------------------------------------------
+  // Private methods
+  // --------------------------------------------------------------------------
+
+  /**
+   * Builds the column separator according to the given text encoding.
+   * 
+   * @param encoding the text encoding to be used.
+   * @throws UnsupportedEncodingException when the given encoding is not supported.
+   */  
+  private void buildSeparatorConformEncoding( String encoding ) throws UnsupportedEncodingException
{
+    separator = ",".getBytes( encoding );
+  }
+
   /**
    * Write resultset header to the given output stream.
    * 
@@ -117,7 +159,7 @@ public class ResultSetCsvMessageBodyWriter implements MessageBodyWriter<ResultSe
       writeEscaped( outputStream, header );
       first = false;
     }
-    outputStream.write( "\n".getBytes( UTF_8 ) );
+    outputStream.write( "\n".getBytes( textEncoding ) );
   }
 
   /**
@@ -144,7 +186,7 @@ public class ResultSetCsvMessageBodyWriter implements MessageBodyWriter<ResultSe
       }
       first = false;
     }
-    outputStream.write( "\n".getBytes( UTF_8 ) );
+    outputStream.write( "\n".getBytes( textEncoding ) );
   }
 
   /**
@@ -186,6 +228,6 @@ public class ResultSetCsvMessageBodyWriter implements MessageBodyWriter<ResultSe
       builder.append( '"' );
       line = builder.toString();
     }
-    outputStream.write( line.getBytes( UTF_8 ) );
+    outputStream.write( line.getBytes( textEncoding ) );
   }
 }

http://git-wip-us.apache.org/repos/asf/clerezza/blob/0250a90b/jaxrs.rdf.providers/src/main/java/org/apache/clerezza/jaxrs/sparql/providers/ResultSetTsvMessageBodyWriter.java
----------------------------------------------------------------------
diff --git a/jaxrs.rdf.providers/src/main/java/org/apache/clerezza/jaxrs/sparql/providers/ResultSetTsvMessageBodyWriter.java
b/jaxrs.rdf.providers/src/main/java/org/apache/clerezza/jaxrs/sparql/providers/ResultSetTsvMessageBodyWriter.java
index c051b43..bf9dcd4 100644
--- a/jaxrs.rdf.providers/src/main/java/org/apache/clerezza/jaxrs/sparql/providers/ResultSetTsvMessageBodyWriter.java
+++ b/jaxrs.rdf.providers/src/main/java/org/apache/clerezza/jaxrs/sparql/providers/ResultSetTsvMessageBodyWriter.java
@@ -63,17 +63,21 @@ public class ResultSetTsvMessageBodyWriter implements MessageBodyWriter<ResultSe
 
   private static final Logger logger = LoggerFactory
       .getLogger( ResultSetTsvMessageBodyWriter.class );
-  private static final String UTF_8 = "UTF-8";
-  private static byte[] separator;
 
-  static {
+  private String textEncoding = "UTF-8";
+  private byte[] separator;
+  
+  public ResultSetTsvMessageBodyWriter() {
     try {
-      separator = "\t".getBytes( UTF_8 );
+      buildSeparatorConformEncoding( textEncoding );
     } catch( UnsupportedEncodingException e ) {
       logger.error( "Developer error", e );
     }
-
   }
+  
+  // --------------------------------------------------------------------------
+  // Implementing MessageBodyWriter
+  // --------------------------------------------------------------------------
 
   @Override
   public boolean isWriteable( Class<?> type, Type genericType, Annotation[] annotations,
@@ -99,6 +103,44 @@ public class ResultSetTsvMessageBodyWriter implements MessageBodyWriter<ResultSe
     }
   }
 
+  // --------------------------------------------------------------------------
+  // Public interface
+  // --------------------------------------------------------------------------
+  
+  /**
+   * Sets the text encoding for the resource. This setting must only used 
+   * if the resource response represents text.
+   * 
+   * @param textEncoding
+   *            character encoding of text body
+   * @throws UnsupportedEncodingException when the given encoding is not supported.
+   */
+  public void setTextEncoding(String textEncoding) throws UnsupportedEncodingException {
+    buildSeparatorConformEncoding( textEncoding );
+    this.textEncoding = textEncoding;
+  }
+
+  /**
+   * @return text encoding for resource
+   */
+  protected String getTextEncoding() {
+    return textEncoding;
+  }
+
+  // --------------------------------------------------------------------------
+  // Private methods
+  // --------------------------------------------------------------------------
+
+  /**
+   * Builds the column separator according to the given text encoding.
+   * 
+   * @param encoding the text encoding to be used.
+   * @throws UnsupportedEncodingException when the given encoding is not supported.
+   */  
+  private void buildSeparatorConformEncoding( String encoding ) throws UnsupportedEncodingException
{
+    separator = ",".getBytes( encoding );
+  }
+
   /**
    * Write resultset header to the given output stream.
    * 
@@ -117,7 +159,7 @@ public class ResultSetTsvMessageBodyWriter implements MessageBodyWriter<ResultSe
       writeEscaped( outputStream, header );
       first = false;
     }
-    outputStream.write( "\n".getBytes( UTF_8 ) );
+    outputStream.write( "\n".getBytes( textEncoding ) );
   }
 
   /**
@@ -144,7 +186,7 @@ public class ResultSetTsvMessageBodyWriter implements MessageBodyWriter<ResultSe
       }
       first = false;
     }
-    outputStream.write( "\n".getBytes( UTF_8 ) );
+    outputStream.write( "\n".getBytes( textEncoding ) );
   }
 
   /**
@@ -191,7 +233,7 @@ public class ResultSetTsvMessageBodyWriter implements MessageBodyWriter<ResultSe
     if( text.contains( "\t" ) ) {
       line = text.replaceAll( "\t", "\\t" );
     }
-    outputStream.write( line.getBytes( UTF_8 ) );
+    outputStream.write( line.getBytes( textEncoding ) );
   }
 
   private String escapedDQuotes( String text ) {


Mime
View raw message