chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1041837 - in /incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main: ./ antlr3/ antlr3/org/ antlr3/org/apache/ antlr3/org/apache/chemistry/ antlr3/org/apache/chemistry/opencmis/ antlr3/org/apache/chemistry/opencmis/or...
Date Fri, 03 Dec 2010 14:33:23 GMT
Author: jens
Date: Fri Dec  3 14:33:23 2010
New Revision: 1041837

URL: http://svn.apache.org/viewvc?rev=1041837&view=rev
Log:
restructure source code so that it follows project conventions

Added:
    incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/
    incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/antlr3/
    incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/antlr3/org/
    incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/antlr3/org/apache/
    incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/antlr3/org/apache/chemistry/
    incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/antlr3/org/apache/chemistry/opencmis/
    incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/antlr3/org/apache/chemistry/opencmis/orderly/
    incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/antlr3/org/apache/chemistry/opencmis/orderly/Orderly.g
    incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/java/
    incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/java/org/
    incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/java/org/apache/
    incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/java/org/apache/chemistry/
    incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/java/org/apache/chemistry/opencmis/
    incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/java/org/apache/chemistry/opencmis/orderly/
    incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/java/org/apache/chemistry/opencmis/orderly/validate/
    incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/java/org/apache/chemistry/opencmis/orderly/validate/Validator.java

Added: incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/antlr3/org/apache/chemistry/opencmis/orderly/Orderly.g
URL: http://svn.apache.org/viewvc/incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/antlr3/org/apache/chemistry/opencmis/orderly/Orderly.g?rev=1041837&view=auto
==============================================================================
--- incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/antlr3/org/apache/chemistry/opencmis/orderly/Orderly.g
(added)
+++ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/antlr3/org/apache/chemistry/opencmis/orderly/Orderly.g
Fri Dec  3 14:33:23 2010
@@ -0,0 +1,334 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+grammar Orderly;
+
+options {
+  language = Java;
+  output = AST;
+}
+
+@header {
+/*
+ * THIS FILE IS AUTO-GENERATED, DO NOT EDIT.
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ *
+ * THIS FILE IS AUTO-GENERATED, DO NOT EDIT.
+ */
+package org.apache.chemistry.opencmis.orderly;
+import java.util.Map;
+import java.util.HashMap;
+}
+
+@lexer::header{ 
+package org.apache.chemistry.opencmis.orderly;
+}
+
+@members {
+    private List<String> errorMessages = new ArrayList<String>();
+    
+    public boolean hasErrors() {
+    	return errorMessages.size() > 0;
+    }
+
+	public String getErrorMessages() {
+		StringBuffer allMessages = new StringBuffer();
+		
+		for (String msg : errorMessages)
+			allMessages.append(msg).append('\n');
+			
+		return allMessages.toString();
+	}
+
+    @Override
+    // Instead of sending all errors to System.err collect them in a list
+	public void emitErrorMessage(String msg) {
+	    super.emitErrorMessage(msg);
+		errorMessages.add(msg);
+	}
+	
+}
+
+@main {
+}
+
+orderly_schema returns [Object result]
+	: e=unnamed_entries? ';'? {$result = $e.result; }
+	;
+
+named_entries returns [Map<Object,Object> result]
+@init {
+	Map<Object,Object> map = new HashMap<Object,Object>();
+}
+@after{
+$result = map;
+}
+	: e=named_entry[map] { } (
+      ';' e2=named_entry[map] { }
+      )*
+    ;
+
+unnamed_entries returns [Object result] 
+@init {
+	List<Object> arr = new ArrayList<Object>();
+}
+@after{
+$result = (arr.size() > 1 ? arr : arr.get(0));
+}
+	: e=unnamed_entry { arr.add($e.result); } (
+      ';' e2=unnamed_entry {  arr.add($e2.result); }
+      )*;
+
+named_entry [Map<Object,Object> parentMap]
+@init {
+	Map<Object,Object> map = new HashMap<Object,Object>();
+}
+  :  (definition_prefix[map] name=property_name definition_suffix[map]) {parentMap.put($name.result,
map);}
+  |  (string_prefix[map] name=property_name string_suffix[map])         {parentMap.put($name.result,
map);}
+  ;
+
+unnamed_entry returns [Map<Object,Object> result]
+@init {
+	Map<Object,Object> map = new HashMap<Object,Object>();
+}
+@after{
+    $result = map;
+}
+  :  definition_prefix[map] definition_suffix[map]
+  |  string_prefix[map] string_suffix[map]
+  ;
+
+definition_prefix[Map<Object, Object> map]
+  :  'integer' rng=optional_range[map]?   { map.put("type", "integer"); }
+  |  'number' rng=optional_range[map]?    { map.put("type", "number"); }
+  |  'boolean'                       { map.put("type", "boolean"); }
+  |  'null'                          { map.put("type", "null"); }
+  |  'any'                           { map.put("any", "null"); }
+  |  'array' (
+      ('{' e1=unnamed_entries? ';'? '}' o=optional_additional_marker[map]? rng=optional_range[map]?)
    
+      { 
+          map.put("type", "array"); 
+          map.put("items", $e1.result); 
+      }
+)//      | ('[' e2=unnamed_entry? ';'? ']' rng=optional_range[map]?)) { /*map.put("type",
"array"); map.put("items", $e2.result); */}
+  |  'object' '{' e3=named_entries? ';'? '}' o=optional_additional_marker[map]?  { map.put("type",
"object"); map.put("properties", $e3.result);}
+  |  'union'  '{' e1=unnamed_entries? ';'? '}'   { map.put("type", $e1.result); }    
+  ;
+ 
+string_prefix[Map<Object, Object> map]
+    :  
+	'string' rng=optional_range[map]? {map.put("type", "string");}
+	;
+
+string_suffix [Map<Object, Object> map]
+    :  
+	regex=OPTIONAL_PERL_REGEX? definition_suffix[map] { 
+		if ($regex != null) {
+	        String s = $OPTIONAL_PERL_REGEX.text;
+   	        s = s.substring(1, s.length()-1); // remove quotes	
+			map.put("pattern", s);
+		} 
+	}
+	;
+
+definition_suffix [Map<Object, Object> map]
+	:
+	enums=optional_enum_values?  { if (enums!= null && enums.result != null) map.put("enum",
enums.result);}
+	def=optional_default_value? { if (def!= null && def.result != null) map.put("default",
def.result);}
+	requires=optional_requires? { if (requires!= null &&requires.result != null) map.put("requires",
requires.result);}
+	optional=optional_optional_marker[map]?
+	extra=optional_extra_properties? { if (extra!= null && extra.result != null) map.putAll($extra.result);}
+	;
+
+csv_property_names returns [Object result]
+@init {
+	List<Object> csvs = new ArrayList<Object>();
+}	
+@after{
+$result = csvs;
+}
+   :
+    prop=property_name          { csvs.add($prop.result);}
+    (  ',' prop2=property_name  { csvs.add($prop2.result);}
+    ) * { }
+	;
+
+optional_extra_properties returns [Map<Object, Object> result]
+    :  ('`' obj=json_object '`') {$result=$obj.result;}
+	;
+
+optional_requires returns [Object result]
+    :  ('<' p=csv_property_names '>') {$result = $p.result;}
+	;
+
+optional_optional_marker [Map<Object, Object> map]
+	: OPTIONAL_OPTIONAL_MARKER {map.put("optional", true); }
+	;
+
+optional_additional_marker[Map<Object, Object> map]
+	: OPTIONAL_ADDITIONAL_MARKER {map.put("additionalProperties", true); };
+
+optional_enum_values returns [Object result]
+    : arr=json_array {$result = $arr.result;}
+	;
+
+optional_default_value returns [Object result]
+    : ('=' d=json_value) {$result = $d.result;}
+	;
+
+optional_range[Map<Object, Object> map]  
+    : '{' s=json_number? ',' e=json_number? '}' { map.put("minimum", $s.result); map.put("maximum",
$e.result); }
+	;
+
+property_name returns [Object result]
+  : s=json_string                             {$result = $s.result;}
+  | r=PROPERTY_NAME_TOKEN                     {$result = $r.text;}
+  ;
+
+///////////// JSON  //////////////////////////
+
+json_object returns [Map<Object, Object> result]
+   :  '{' m=members? '}' {$result = $m.result;};
+
+members  returns [Map<Object, Object> result]
+@init { 
+	Map<Object, Object> map = new HashMap<Object, Object>();
+}
+@after {
+ $result = map;
+}
+	:
+	pair[map] (',' pair[map] )* {$result = map;}
+	;
+
+pair[Map<Object, Object> map]
+	:  
+	k=json_string ':' v=json_value { map.put($k.result, $v.result);}
+	;
+
+json_array  returns [List<Object> result]
+    : 
+	'[' elem=elements? ']' {$result = $elem.result;}
+	;
+
+json_number  returns [Object result]
+	: 
+	NUM_LIT
+    {
+        try {
+            $result = Long.valueOf($NUM_LIT.text);
+        } catch (NumberFormatException e) {
+            $result = Double.valueOf($NUM_LIT.text);
+        }
+    }	
+	;
+
+elements returns [List<Object> result]
+@init {
+    List<Object> values = new ArrayList<Object>();
+}
+@after {
+    $result = values; 
+} 
+	: 
+	v=json_value {values.add($v.result); }
+	(',' v2=json_value {values.add($v2.result); }
+	)*
+	;
+
+json_value returns [Object result]
+  :  val1=json_string                          {$result = $val1.result;}
+  |  val2=json_number                          {$result = $val2.result;}
+  |  val3=json_object                          {$result = $val3.result;}
+  |  val4=json_array                           {$result = $val4.result;}
+  |  'true'                                    {$result = Boolean.TRUE;}
+  |  'false'                                   {$result = Boolean.FALSE;}
+  |  'null'                                    {$result = null;}
+  ;
+
+json_string returns [String result]
+	: 
+	STRING 
+	  {
+	    String s = $STRING.text;
+   	    s = s.substring(1, s.length()-1); // remove quotes
+	    $result=s;
+	  }
+	;
+
+// ----------------------------------------
+
+OPTIONAL_OPTIONAL_MARKER : '?' ;
+
+OPTIONAL_ADDITIONAL_MARKER : '*' ;
+
+PROPERTY_NAME_TOKEN : ('a' .. 'z' | 'A' .. 'Z' | '_' | '-' )+;
+
+OPTIONAL_PERL_REGEX : ('/' ( (~'/') | '\\/' )+ '/');
+
+WS : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+   { $channel = HIDDEN; } ;
+
+COMMENT : ('//' | '#') (~'\n')* {$channel = HIDDEN; };
+
+fragment
+QUOTE_OR_BACKSLASH_OR_CONTROL_CHARACTER
+  :  '"'
+  |  '\\'
+  |  '\\' .
+  ;
+
+STRING  :  '"' ~'"'* '"';
+
+fragment
+HEX  :  'a'..'f'
+  |  'A'..'F'
+  |  '0'..'9';
+
+ESC : '\\\\' ;
+
+/*
+integer : '-'? ('0'..'9')+ ;
+exp     : ('e' | 'E') ('-' | '+') ?  ('0' .. '9')+ ;
+frac    : '.' ('0' .. '9')+ ;
+NUM_LIT : integer frac? exp?;
+*/
+fragment Sign : ('+'|'-')?;
+fragment Digits : ('0'..'9')+;
+fragment FloatBase : Digits DOT Digits | Digits DOT | DOT Digits | Digits;
+fragment ExpNumLit : ('e'|'E') Sign Digits;
+DOT : '.';
+NUM_LIT : Sign FloatBase ExpNumLit?;

Added: incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/java/org/apache/chemistry/opencmis/orderly/validate/Validator.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/java/org/apache/chemistry/opencmis/orderly/validate/Validator.java?rev=1041837&view=auto
==============================================================================
--- incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/java/org/apache/chemistry/opencmis/orderly/validate/Validator.java
(added)
+++ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/java/org/apache/chemistry/opencmis/orderly/validate/Validator.java
Fri Dec  3 14:33:23 2010
@@ -0,0 +1,159 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.chemistry.opencmis.orderly.validate;
+
+import java.io.FileInputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+
+
+import java.util.List;
+import java.util.Map;
+
+import org.antlr.runtime.ANTLRInputStream;
+import org.antlr.runtime.CharStream;
+import org.antlr.runtime.CommonTokenStream;
+import org.antlr.runtime.RecognitionException;
+import org.antlr.runtime.TokenSource;
+import org.antlr.runtime.TokenStream;
+import org.antlr.runtime.tree.CommonTree;
+import org.apache.chemistry.opencmis.orderly.OrderlyLexer;
+import org.apache.chemistry.opencmis.orderly.OrderlyParser;
+import org.apache.chemistry.opencmis.orderly.OrderlyParser.orderly_schema_return;
+import org.codehaus.jackson.JsonFactory;
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.JsonParser;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.map.SerializationConfig.Feature;
+
+import eu.vahlas.json.schema.JSONSchema;
+import eu.vahlas.json.schema.JSONSchemaProvider;
+import eu.vahlas.json.schema.impl.JacksonSchema;
+import eu.vahlas.json.schema.impl.JacksonSchemaProvider;
+
+public class Validator {
+
+    private static Object parseOrderly(String inFileName) throws Exception {
+        CharStream input = new ANTLRInputStream(new FileInputStream(inFileName));
+        TokenSource lexer = new OrderlyLexer(input);
+        TokenStream tokens = new CommonTokenStream(lexer);
+        OrderlyParser parser = new OrderlyParser(tokens);
+
+        orderly_schema_return result = parser.orderly_schema();
+        if (parser.hasErrors())
+            throw new RuntimeException(parser.getErrorMessages());
+        
+        CommonTree parserTree = (CommonTree) result.getTree();        
+
+        // Extract hashmap from parsed tree
+        Object parsedMap = result.result;
+        return parsedMap;
+    }
+    
+    private static void transform(String inFileName, String outFileName) {
+        try {
+            Object parsedMap = parseOrderly(inFileName);
+            
+            ObjectMapper mapper = new ObjectMapper();
+            mapper.configure(Feature.INDENT_OUTPUT, true);
+            
+            mapper.writeValue(System.out, parsedMap);
+            mapper.writeValue(new File(outFileName), parsedMap);
+        } catch (Exception e) {
+            System.err.println("Error parsing input file " + inFileName + ": " + e);
+            e.printStackTrace();
+        }
+    }
+
+    private static void validate(String jsonFile, String schemaFile) {        
+        try {
+            Object parsedMap = parseOrderly(schemaFile);
+            
+            ObjectMapper mapper = new ObjectMapper();
+
+            // Allows to retrieve a JSONSchema object on various sources
+            // supported by the ObjectMapper provided
+            JsonFactory factory = new JsonFactory();
+            
+            byte[] jsonSchemaAsBytes = mapper.writeValueAsBytes(parsedMap);
+            
+            JsonParser jsonParser = factory.createJsonParser(jsonSchemaAsBytes);
+            JsonNode schemaNode = mapper.readTree(jsonParser); 
+            
+            JacksonSchema schema = new JacksonSchema(mapper, schemaNode);
+
+            // Validates a JSON Instance object stored in a file
+            InputStream instanceIS = new FileInputStream(jsonFile);
+            List<String> errors = schema.validate(instanceIS);
+             
+            // Display the eventual errors
+            if (errors.size() > 0) {
+                for (String s : errors) {
+                    System.err.println(s);
+                }
+                System.exit(1);
+            } else
+                System.out.println("Validation passed.");
+            
+        } catch (Exception e) {
+            System.err.println("Error validation input file " + jsonFile + " for schema "
+ schemaFile + ": " + e);
+        }
+    }
+    
+    public static void usage() {
+            System.out.println("Usage: Validator transform <file-name>");
+            System.out.println("       Transform orderly file <file-name> to json-schema");
+            System.out.println("Usage: Validator validate <json-file-name> <orderly-file-name>");
+            System.out.println("       Validate json-file <file-name> against orderly
schema <orderly-file-name>");
+            System.exit(1);
+    }
+
+    public static void main(String[] args) {
+        if (args.length < 2 || args.length > 3) {
+            usage();
+        }
+         if(args[0].equals("transform")) {
+             String inFile = args[1];
+             String outFile;
+             if (args.length > 2)
+                 outFile = args[2];
+             else {
+                 if (inFile.endsWith(".orderly")) {
+                     outFile = inFile.substring(0, inFile.length()-8);
+                 } else {
+                     outFile = inFile;
+                 }
+                 outFile += "-json-schema.json";
+             }
+             transform(inFile, outFile);
+         } else if (args[0].equals("validate")) {
+             if (args.length < 3) 
+                 usage();
+             String jsonFile = args[1];
+             String schemaFile = args[2];
+             validate(jsonFile, schemaFile);
+         } else {
+             usage();
+         }
+    }
+
+
+}



Mime
View raw message