avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1577670 - in /avro/trunk: ./ lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/ lang/java/compiler/src/test/java/org/apache/avro/compiler/ lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/
Date Fri, 14 Mar 2014 19:31:21 GMT
Author: cutting
Date: Fri Mar 14 19:31:20 2014
New Revision: 1577670

URL: http://svn.apache.org/r1577670
Log:
AVRO-1471. Java: Permit writing generated code in different character encodings.  Contributed
by Eugene Mustaphin.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
    avro/trunk/lang/java/compiler/src/test/java/org/apache/avro/compiler/TestSpecificCompiler.java
    avro/trunk/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/SchemaMojo.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1577670&r1=1577669&r2=1577670&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Fri Mar 14 19:31:20 2014
@@ -8,6 +8,9 @@ Trunk (not yet released)
 
     AVRO-974. Add a Perl implementation of Avro. (Yann Kerhervé & John Karp)
 
+    AVRO-1471. Java: Permit writing generated code in different
+    character encodings. (Eugene Mustaphin via cutting)
+
   OPTIMIZATIONS
 
     AVRO-1455. Deep copy does not need to create new instances for primitives.

Modified: avro/trunk/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java?rev=1577670&r1=1577669&r2=1577670&view=diff
==============================================================================
--- avro/trunk/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
(original)
+++ avro/trunk/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
Fri Mar 14 19:31:20 2014
@@ -18,6 +18,9 @@
 package org.apache.avro.compiler.specific;
 
 import java.io.File;
+import java.io.FileOutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.StringWriter;
@@ -65,6 +68,7 @@ public class SpecificCompiler {
   private String templateDir;
   private FieldVisibility fieldVisibility = FieldVisibility.PUBLIC_DEPRECATED;
   private boolean createSetters = true;
+  private String outputCharacterEncoding;
 
   /* List of Java reserved words from
    * http://java.sun.com/docs/books/jls/third_edition/html/lexical.html. */
@@ -208,6 +212,7 @@ public class SpecificCompiler {
   static class OutputFile {
     String path;
     String contents;
+    String outputCharacterEncoding;
 
     /**
      * Writes output to path destination directory when it is newer than src,
@@ -218,7 +223,12 @@ public class SpecificCompiler {
       if (src != null && f.exists() && f.lastModified() >= src.lastModified())
         return f;                                 // already up to date: ignore
       f.getParentFile().mkdirs();
-      FileWriter fw = new FileWriter(f);
+      Writer fw;
+      if (outputCharacterEncoding != null) {
+        fw = new OutputStreamWriter(new FileOutputStream(f), outputCharacterEncoding);
+      } else {
+        fw = new FileWriter(f);
+      }
       try {
         fw.write(FILE_HEADER);
         fw.write(contents);
@@ -345,6 +355,7 @@ public class SpecificCompiler {
     String mangledName = mangle(protocol.getName());
     outputFile.path = makePath(mangledName, protocol.getNamespace());
     outputFile.contents = out;
+    outputFile.outputCharacterEncoding = outputCharacterEncoding;
     return outputFile;
   }
 
@@ -384,6 +395,7 @@ public class SpecificCompiler {
     String name = mangle(schema.getName());
     outputFile.path = makePath(name, schema.getNamespace());
     outputFile.contents = output;
+    outputFile.outputCharacterEncoding = outputCharacterEncoding;
     return outputFile;
   }
 
@@ -788,5 +800,11 @@ public class SpecificCompiler {
     }
   }
 
+  /** Sets character encoding for generated java file
+  * @param outputCharacterEncoding Character encoding for output files (defaults to system
encoding)
+  */
+  public void setOutputCharacterEncoding(String outputCharacterEncoding) {
+    this.outputCharacterEncoding = outputCharacterEncoding;
+  }
 }
 

Modified: avro/trunk/lang/java/compiler/src/test/java/org/apache/avro/compiler/TestSpecificCompiler.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/compiler/src/test/java/org/apache/avro/compiler/TestSpecificCompiler.java?rev=1577670&r1=1577669&r2=1577670&view=diff
==============================================================================
--- avro/trunk/lang/java/compiler/src/test/java/org/apache/avro/compiler/TestSpecificCompiler.java
(original)
+++ avro/trunk/lang/java/compiler/src/test/java/org/apache/avro/compiler/TestSpecificCompiler.java
Fri Mar 14 19:31:20 2014
@@ -20,12 +20,17 @@ package org.apache.avro.compiler;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertThat;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.equalTo;
 
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileReader;
 import java.io.IOException;
 import java.net.URISyntaxException;
+import java.nio.charset.Charset;
 
 import org.apache.avro.AvroTestUtil;
 import org.apache.avro.Schema;
@@ -172,4 +177,26 @@ public class TestSpecificCompiler {
         line.startsWith("public void setValue("));
     }
   }
+
+  @Test
+  public void testSettingOutputCharacterEncoding() throws Exception {
+    SpecificCompiler compiler = createCompiler();
+    // Generated file in default encoding
+    compiler.compileToDestination(this.src, this.outputDir);
+    byte[] fileInDefaultEncoding = new byte[(int) this.outputFile.length()];
+    new FileInputStream(this.outputFile).read(fileInDefaultEncoding);
+    this.outputFile.delete();
+    // Generate file in another encoding (make sure it has different number of bytes per
character)
+    String differentEncoding = Charset.defaultCharset().equals(Charset.forName("UTF-16"))
? "UTF-32" : "UTF-16";
+    compiler.setOutputCharacterEncoding(differentEncoding);
+    compiler.compileToDestination(this.src, this.outputDir);
+    byte[] fileInDifferentEncoding = new byte[(int) this.outputFile.length()];
+    new FileInputStream(this.outputFile).read(fileInDifferentEncoding);
+    // Compare as bytes
+    assertThat("Generated file should contain different bytes after setting non-default encoding",
+      fileInDefaultEncoding, not(equalTo(fileInDifferentEncoding)));
+    // Compare as strings
+    assertThat("Generated files should contain the same characters in the proper encodings",
+      new String(fileInDefaultEncoding), equalTo(new String(fileInDifferentEncoding, differentEncoding)));
+  }
 }

Modified: avro/trunk/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/SchemaMojo.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/SchemaMojo.java?rev=1577670&r1=1577669&r2=1577670&view=diff
==============================================================================
--- avro/trunk/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/SchemaMojo.java (original)
+++ avro/trunk/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/SchemaMojo.java Fri
Mar 14 19:31:20 2014
@@ -78,6 +78,7 @@ public class SchemaMojo extends Abstract
     compiler.setStringType(StringType.valueOf(stringType));
     compiler.setFieldVisibility(getFieldVisibility());
     compiler.setCreateSetters(createSetters);
+    compiler.setOutputCharacterEncoding(project.getProperties().getProperty("project.build.sourceEncoding"));
     compiler.compileToDestination(src, outputDirectory);
   }
 



Mime
View raw message