avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1161749 - in /avro/branches/branch-1.5: ./ lang/java/avro/src/main/java/org/apache/avro/ lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/ lang/java/compiler/src/test/idl/input/ lang/java/compiler/src/test/idl/output/
Date Thu, 25 Aug 2011 20:49:18 GMT
Author: cutting
Date: Thu Aug 25 20:49:18 2011
New Revision: 1161749

URL: http://svn.apache.org/viewvc?rev=1161749&view=rev
Log:
Merge -c 1157245 from trunk to 1.5 branch.  Fixes AVRO-874.

Added:
    avro/branches/branch-1.5/lang/java/compiler/src/test/idl/input/baseball.avdl
      - copied unchanged from r1157245, avro/trunk/lang/java/compiler/src/test/idl/input/baseball.avdl
    avro/branches/branch-1.5/lang/java/compiler/src/test/idl/input/player.avsc
      - copied unchanged from r1157245, avro/trunk/lang/java/compiler/src/test/idl/input/player.avsc
    avro/branches/branch-1.5/lang/java/compiler/src/test/idl/input/position.avsc
      - copied unchanged from r1157245, avro/trunk/lang/java/compiler/src/test/idl/input/position.avsc
    avro/branches/branch-1.5/lang/java/compiler/src/test/idl/output/baseball.avpr
      - copied unchanged from r1157245, avro/trunk/lang/java/compiler/src/test/idl/output/baseball.avpr
Modified:
    avro/branches/branch-1.5/   (props changed)
    avro/branches/branch-1.5/CHANGES.txt
    avro/branches/branch-1.5/lang/java/avro/src/main/java/org/apache/avro/Schema.java
    avro/branches/branch-1.5/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj

Propchange: avro/branches/branch-1.5/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Aug 25 20:49:18 2011
@@ -1 +1 @@
-/avro/trunk:1075938,1075993,1078917,1079055,1079060,1079063,1079680,1083246,1085921,1086727,1086730,1086866,1087076,1087129,1087136,1087439-1087440,1087463,1087472,1087792,1089128,1089131,1089550,1094812,1095206-1095208,1095493,1095529,1095548,1095550,1096798,1097916,1097927,1097968,1097974,1099257,1102332,1102335,1124127,1124971,1129053,1129071,1129697-1129706,1129729,1129856,1130503,1136342,1141619,1141677,1141685,1141979-1141980,1142057,1142063,1151660,1151983,1161743
+/avro/trunk:1075938,1075993,1078917,1079055,1079060,1079063,1079680,1083246,1085921,1086727,1086730,1086866,1087076,1087129,1087136,1087439-1087440,1087463,1087472,1087792,1089128,1089131,1089550,1094812,1095206-1095208,1095493,1095529,1095548,1095550,1096798,1097916,1097927,1097968,1097974,1099257,1102332,1102335,1124127,1124971,1129053,1129071,1129697-1129706,1129729,1129856,1130503,1136342,1141619,1141677,1141685,1141979-1141980,1142057,1142063,1151660,1151983,1157245,1161743

Modified: avro/branches/branch-1.5/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/branches/branch-1.5/CHANGES.txt?rev=1161749&r1=1161748&r2=1161749&view=diff
==============================================================================
--- avro/branches/branch-1.5/CHANGES.txt (original)
+++ avro/branches/branch-1.5/CHANGES.txt Thu Aug 25 20:49:18 2011
@@ -4,6 +4,9 @@ Avro 1.5.3 (unreleased)
 
   IMPROVEMENTS
 
+    AVRO-874. Java: Improved Schema parsing API and permit IDL imports
+    to depend on names defined in prior imports. (cutting)
+
   BUG FIXES
 
 Avro 1.5.2 (12 August 2011)

Modified: avro/branches/branch-1.5/lang/java/avro/src/main/java/org/apache/avro/Schema.java
URL: http://svn.apache.org/viewvc/avro/branches/branch-1.5/lang/java/avro/src/main/java/org/apache/avro/Schema.java?rev=1161749&r1=1161748&r2=1161749&view=diff
==============================================================================
--- avro/branches/branch-1.5/lang/java/avro/src/main/java/org/apache/avro/Schema.java (original)
+++ avro/branches/branch-1.5/lang/java/avro/src/main/java/org/apache/avro/Schema.java Thu
Aug 25 20:49:18 2011
@@ -870,6 +870,73 @@ public abstract class Schema {
     public NullSchema() { super(Type.NULL); }
   }
 
+  /** A parser for JSON-format schemas.  Each named schema parsed with a parser
+   * is added to the names known to the parser so that subsequently parsed
+   * schemas may refer to it by name. */
+  public static class Parser {
+    private Names names = new Names();
+    private boolean validate = true;
+
+    /** Adds the provided types to the set of defined, named types known to
+     * this parser. */
+    public Parser addTypes(Map<String,Schema> types) {
+      for (Schema s : types.values())
+        names.add(s);
+      return this;
+    }
+
+    /** Returns the set of defined, named types known to this parser. */
+    public Map<String,Schema> getTypes() {
+      Map<String,Schema> result = new LinkedHashMap<String,Schema>();
+      for (Schema s : names.values())
+        result.put(s.getFullName(), s);
+      return result;
+    }
+
+    /** Enable or disable name validation. */
+    public Parser setValidate(boolean validate) {
+      this.validate = validate;
+      return this;
+    }
+
+    /** True iff names are validated.  True by default. */
+    public boolean getValidate() { return this.validate; }
+
+    /** Parse a schema from the provided file.
+     * If named, the schema is added to the names known to this parser. */
+    public Schema parse(File file) throws IOException {
+      return parse(FACTORY.createJsonParser(file));
+    }
+
+    /** Parse a schema from the provided stream.
+     * If named, the schema is added to the names known to this parser. */
+    public Schema parse(InputStream in) throws IOException {
+      return parse(FACTORY.createJsonParser(in));
+    }
+
+    /** Parse a schema from the provided string.
+     * If named, the schema is added to the names known to this parser. */
+    public Schema parse(String s) {
+      try {
+        return parse(FACTORY.createJsonParser(new StringReader(s)));
+      } catch (IOException e) {
+        throw new SchemaParseException(e);
+      }
+    }
+
+    private Schema parse(JsonParser parser) throws IOException {
+      boolean saved = validateNames.get();
+      try {
+        validateNames.set(validate);
+        return Schema.parse(MAPPER.readTree(parser), names);
+      } catch (JsonParseException e) {
+        throw new SchemaParseException(e);
+      } finally {
+        validateNames.set(saved);
+      }
+    }
+  }
+
   /**
    * Constructs a Schema object from JSON schema file <tt>file</tt>.
    * The contents of <tt>file</tt> is expected to be in UTF-8 format.
@@ -877,14 +944,10 @@ public abstract class Schema {
    * @return  The freshly built Schema.
    * @throws IOException if there was trouble reading the contents
    * @throws JsonParseException if the contents are invalid
+   * @deprecated use {@link Schema.Parser} instead.
    */
   public static Schema parse(File file) throws IOException {
-    JsonParser parser = FACTORY.createJsonParser(file);
-    try {
-      return Schema.parse(MAPPER.readTree(parser), new Names());
-    } catch (JsonParseException e) {
-      throw new SchemaParseException(e);
-    }
+    return new Parser().parse(file);
   }
 
   /**
@@ -894,32 +957,25 @@ public abstract class Schema {
    * @return  The freshly built Schema.
    * @throws IOException if there was trouble reading the contents
    * @throws JsonParseException if the contents are invalid
+   * @deprecated use {@link Schema.Parser} instead.
    */
   public static Schema parse(InputStream in) throws IOException {
-    JsonParser parser = FACTORY.createJsonParser(in);
-    try {
-      return Schema.parse(MAPPER.readTree(parser), new Names());
-    } catch (JsonParseException e) {
-      throw new SchemaParseException(e);
-    }
+    return new Parser().parse(in);
   }
 
-  /** Construct a schema from <a href="http://json.org/">JSON</a> text. */
+  /** Construct a schema from <a href="http://json.org/">JSON</a> text.
+   * @deprecated use {@link Schema.Parser} instead.
+   */
   public static Schema parse(String jsonSchema) {
-    return parse(parseJson(jsonSchema), new Names());
+    return new Parser().parse(jsonSchema);
   }
 
   /** Construct a schema from <a href="http://json.org/">JSON</a> text.
    * @param validate true if names should be validated, false if not.
+   * @deprecated use {@link Schema.Parser} instead.
    */
   public static Schema parse(String jsonSchema, boolean validate) {
-    boolean saved = validateNames.get();
-    try {
-      validateNames.set(validate);
-      return parse(jsonSchema);
-    } finally {
-      validateNames.set(saved);
-    }
+    return new Parser().setValidate(validate).parse(jsonSchema);
   }
 
   static final Map<String,Type> PRIMITIVES = new HashMap<String,Type>();

Modified: avro/branches/branch-1.5/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj
URL: http://svn.apache.org/viewvc/avro/branches/branch-1.5/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj?rev=1161749&r1=1161748&r2=1161749&view=diff
==============================================================================
--- avro/branches/branch-1.5/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj
(original)
+++ avro/branches/branch-1.5/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj
Thu Aug 25 20:49:18 2011
@@ -1110,9 +1110,8 @@ void ProtocolBody(Protocol p):
          names.put(s.getFullName(), s);
        p.getMessages().putAll(importProtocol.getMessages());
      })
-     | schema = ImportSchema()  {
-     names.put(schema.getFullName(), schema);
-   })
+     | schema = ImportSchema()
+     )
       
    | message = MessageDeclaration(p) {
      p.getMessages().put(message.getName(), message);
@@ -1162,7 +1161,11 @@ Schema ImportSchema() : {
   <SCHEMA> importFile = JsonString() ";"
     {
       try {
-        return Schema.parse(new File(inputDir, importFile));
+        Parser parser = new Schema.Parser();
+        parser.addTypes(names);                   // inherit names
+        Schema value = parser.parse(new File(inputDir, importFile));
+        names = parser.getTypes();                // update names
+        return value;
       } catch (IOException e) {
         throw error("Error importing "+importFile+": "+e, token);
       }        



Mime
View raw message