avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r764978 - in /hadoop/avro/trunk: CHANGES.txt src/java/org/apache/avro/specific/ProtocolTask.java src/java/org/apache/avro/specific/SchemaTask.java src/java/org/apache/avro/specific/SpecificCompiler.java
Date Tue, 14 Apr 2009 22:11:22 GMT
Author: cutting
Date: Tue Apr 14 22:11:22 2009
New Revision: 764978

URL: http://svn.apache.org/viewvc?rev=764978&view=rev
Log:
Fix so that specific code generation works under Eclipse.  Contributed by Pat Hunt.

Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/src/java/org/apache/avro/specific/ProtocolTask.java
    hadoop/avro/trunk/src/java/org/apache/avro/specific/SchemaTask.java
    hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java

Modified: hadoop/avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=764978&r1=764977&r2=764978&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Tue Apr 14 22:11:22 2009
@@ -19,3 +19,5 @@
     AVRO-3. Fix ValueReader to throw an exception at EOF.
     (Pat Hunt via cutting)
     
+    AVRO-4. Fix so that specific code generation works under Eclipse.
+    (Pat Hunt via cutting)

Modified: hadoop/avro/trunk/src/java/org/apache/avro/specific/ProtocolTask.java
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/specific/ProtocolTask.java?rev=764978&r1=764977&r2=764978&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/specific/ProtocolTask.java (original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/specific/ProtocolTask.java Tue Apr 14 22:11:22
2009
@@ -17,12 +17,18 @@
  */
 package org.apache.avro.specific;
 
-import java.io.*;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
 import java.util.ArrayList;
-import org.apache.tools.ant.*;
-import org.apache.tools.ant.types.FileSet;
 
-import org.apache.avro.*;
+import org.apache.avro.AvroRuntimeException;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.DirectoryScanner;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.Task;
+import org.apache.tools.ant.types.FileSet;
 
 /** Ant task to generate Java interface and classes for a protocol. */
 public class ProtocolTask extends Task {
@@ -59,17 +65,32 @@
     }
   }
   
-  protected String doCompile(File file) throws IOException {
+  protected SpecificCompiler doCompile(File file) throws IOException {
     return SpecificCompiler.compileProtocol(file);
   }
 
   private void compile(File file) throws BuildException {
     try {
-      String text = doCompile(file);
+      SpecificCompiler compiler = doCompile(file);
+      String namespace = compiler.getNamespace();
+      String text = compiler.getCode();
       String name = file.getName();
       name = name.substring(0, name.indexOf('.'))+".java";
       name = SpecificCompiler.cap(name);
-      Writer out = new FileWriter(new File(dest, name));
+      File outputFile;
+      if (namespace == null || namespace.length() == 0) {
+        outputFile = new File(dest, name);
+      } else {
+        File packageDir =
+            new File(dest, namespace.replace('.', File.separatorChar));
+        if (!packageDir.exists()) {
+            if (!packageDir.mkdirs()) {
+                throw new BuildException("Unable to create " + packageDir);
+            }
+        }
+        outputFile = new File(packageDir, name);
+      }
+      Writer out = new FileWriter(outputFile);
       try {
         out.write(text);
       } finally {

Modified: hadoop/avro/trunk/src/java/org/apache/avro/specific/SchemaTask.java
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/specific/SchemaTask.java?rev=764978&r1=764977&r2=764978&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/specific/SchemaTask.java (original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/specific/SchemaTask.java Tue Apr 14 22:11:22
2009
@@ -17,12 +17,12 @@
  */
 package org.apache.avro.specific;
 
-import java.io.*;
-import org.apache.tools.ant.*;
+import java.io.File;
+import java.io.IOException;
 
 /** Ant task to generate Java interface and classes for a protocol. */
 public class SchemaTask extends ProtocolTask {
-  protected String doCompile(File file) throws IOException {
+  protected SpecificCompiler doCompile(File file) throws IOException {
     return SpecificCompiler.compileSchema(file);
   }
 }

Modified: hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java?rev=764978&r1=764977&r2=764978&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java (original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java Tue Apr 14 22:11:22
2009
@@ -32,29 +32,38 @@
   private static final JsonTypeMapper MAPPER = new JsonTypeMapper();
   private static final JsonFactory FACTORY = new JsonFactory();
 
+  private String namespace;
   private StringBuilder buffer = new StringBuilder();
 
   private SpecificCompiler() {}                        // no public ctor
 
   /** Returns generated Java interface for a protocol. */
-  public static String compileProtocol(File file) throws IOException {
+  public static SpecificCompiler compileProtocol(File file) throws IOException {
     SpecificCompiler compiler = new SpecificCompiler();
     Protocol protocol = Protocol.parse(file);
-    return compiler.compile(protocol);
+    compiler.compile(protocol);
+    return compiler;
   }
 
   /** Returns generated Java class for a schema. */
-  public static String compileSchema(File file) throws IOException {
+  public static SpecificCompiler compileSchema(File file) throws IOException {
     SpecificCompiler compiler = new SpecificCompiler();
     Schema schema = Schema.parse(file);
     compiler.header(schema.getNamespace());
+    compiler.namespace = schema.getNamespace();
     compiler.compile(schema, schema.getName(), 0);
-    return compiler.buffer.toString();
+    return compiler;
   }
 
-  /** Return generated Java code for a protocol. */
-  public String compile(Protocol protocol) {
-    header(protocol.getNamespace());
+  /** Return namespace for compiled code. */
+  public String getNamespace() { return namespace; }
+
+  /** Return generated code. */
+  public String getCode() { return buffer.toString(); }
+  
+  private void compile(Protocol protocol) {
+    namespace = protocol.getNamespace();
+    header(namespace);
 
     // define an interface
     line(0, "public interface "+protocol.getName()+" {");
@@ -74,7 +83,6 @@
       line(2,"throws AvroRemoteException"+errors(message.getErrors())+";");
     }
     line(0, "}");
-    return buffer.toString();
   }
 
   private void header(String namespace) {
@@ -220,7 +228,7 @@
   }
 
   public static void main(String args[]) throws Exception {
-    System.out.println(compileProtocol(new File(args[0])));
+    System.out.println(compileProtocol(new File(args[0])).getCode());
   }
 
 }



Mime
View raw message