Return-Path: Delivered-To: apmail-incubator-chemistry-commits-archive@minotaur.apache.org Received: (qmail 39385 invoked from network); 26 Nov 2010 08:40:35 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 26 Nov 2010 08:40:35 -0000 Received: (qmail 31300 invoked by uid 500); 26 Nov 2010 08:40:35 -0000 Delivered-To: apmail-incubator-chemistry-commits-archive@incubator.apache.org Received: (qmail 31244 invoked by uid 500); 26 Nov 2010 08:40:34 -0000 Mailing-List: contact chemistry-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: chemistry-dev@incubator.apache.org Delivered-To: mailing list chemistry-commits@incubator.apache.org Received: (qmail 31236 invoked by uid 99); 26 Nov 2010 08:40:33 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 26 Nov 2010 08:40:33 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 26 Nov 2010 08:40:31 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id B1CDB23888D2; Fri, 26 Nov 2010 08:38:59 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1039269 - in /incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator: ./ src/ src/antlr/ src/antlr/org/ src/antlr/org/apache/ src/antlr/org/apache/chemistry/ src/antlr/org/apache/chemistry/opencmis/ src/antlr/org/apache/chemist... Date: Fri, 26 Nov 2010 08:38:59 -0000 To: chemistry-commits@incubator.apache.org From: jens@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20101126083859.B1CDB23888D2@eris.apache.org> Author: jens Date: Fri Nov 26 08:38:59 2010 New Revision: 1039269 URL: http://svn.apache.org/viewvc?rev=1039269&view=rev Log: start with creating an experimental validator for Orderliy in Java (useful for the browser binding integration) Added: incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/build.xml incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/antlr/ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/antlr/org/ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/antlr/org/apache/ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/antlr/org/apache/chemistry/ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/antlr/org/apache/chemistry/opencmis/ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/antlr/org/apache/chemistry/opencmis/orderly/ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/antlr/org/apache/chemistry/opencmis/orderly/Orderly.g incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/java/ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/java/org/ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/java/org/apache/ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/java/org/apache/chemistry/ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/java/org/apache/chemistry/opencmis/ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/java/org/apache/chemistry/opencmis/orderly/ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/java/org/apache/chemistry/opencmis/orderly/validate/ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/java/org/apache/chemistry/opencmis/orderly/validate/Validator.java Added: incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/build.xml URL: http://svn.apache.org/viewvc/incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/build.xml?rev=1039269&view=auto ============================================================================== --- incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/build.xml (added) +++ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/build.xml Fri Nov 26 08:38:59 2010 @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Added: incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/antlr/org/apache/chemistry/opencmis/orderly/Orderly.g URL: http://svn.apache.org/viewvc/incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/antlr/org/apache/chemistry/opencmis/orderly/Orderly.g?rev=1039269&view=auto ============================================================================== --- incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/antlr/org/apache/chemistry/opencmis/orderly/Orderly.g (added) +++ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/antlr/org/apache/chemistry/opencmis/orderly/Orderly.g Fri Nov 26 08:38:59 2010 @@ -0,0 +1,302 @@ +/* + * 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; +} + +@main { +} + +orderly_schema returns [Object result] + : e=unnamed_entries? ';'? {$result = $e.result; } + ; + +named_entries returns [Map result] +@init { + Map map = new HashMap(); +} +@after{ +$result = map; +} + : e=named_entry[map] { } ( + ';' e2=named_entry[map] { } + )* + ; + +unnamed_entries returns [Object result] +@init { + List arr = new ArrayList(); +} +@after{ +$result = (arr.size() > 1 ? arr : arr.get(0)); +System.out.println("unnamed_entries return type " + $result.getClass().getName()); +} + : e=unnamed_entry { arr.add($e.result); } ( + ';' e2=unnamed_entry { arr.add($e2.result); } + )*; + +named_entry [Map parentMap] +@init { + Map map = new HashMap(); +} + : (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 result] +@init { + Map map = new HashMap(); +} +@after{ + $result = map; +} + : definition_prefix[map] definition_suffix[map] + | string_prefix[map] string_suffix[map] + ; + +definition_prefix[Map 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 map] + : + 'string' rng=optional_range[map]? {map.put("type", "string");} + ; + +string_suffix [Map map] + : + regex=OPTIONAL_PERL_REGEX? definition_suffix[map] { + if ($regex != null) + map.put("pattern", $OPTIONAL_PERL_REGEX.text); + } + ; + +definition_suffix [Map 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 csvs = new ArrayList(); +} +@after{ +$result = csvs; +} + : + prop=property_name { csvs.add($prop.result);} + ( ',' prop2=property_name { csvs.add($prop2.result);} + ) * { } + ; + +optional_extra_properties returns [Map result] + : ('`' obj=json_object '`') {$result=$obj.result;} + ; + +optional_requires returns [Object result] + : ('<' p=csv_property_names '>') {$result = $p.result;} + ; + +optional_optional_marker [Map map] + : OPTIONAL_OPTIONAL_MARKER {map.put("optional", true); } + ; + +optional_additional_marker[Map 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 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 result] + : '{' m=members? '}' {$result = $m.result;}; + +members returns [Map result] +@init { + Map map = new HashMap(); +} +@after { + $result = map; +} + : + pair[map] (',' pair[map] )* {$result = map;} + ; + +pair[Map map] + : + k=json_string ':' v=json_value { map.put($k.result, $v.result);} + ; + +json_array returns [List 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 result] +@init { + List values = new ArrayList(); +} +@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 {$result=$STRING.text;} + ; + +// ---------------------------------------- + +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/java/org/apache/chemistry/opencmis/orderly/validate/Validator.java URL: http://svn.apache.org/viewvc/incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/java/org/apache/chemistry/opencmis/orderly/validate/Validator.java?rev=1039269&view=auto ============================================================================== --- incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/java/org/apache/chemistry/opencmis/orderly/validate/Validator.java (added) +++ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/java/org/apache/chemistry/opencmis/orderly/validate/Validator.java Fri Nov 26 08:38:59 2010 @@ -0,0 +1,75 @@ +/* + * 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.util.Map; + +import org.antlr.runtime.ANTLRInputStream; +import org.antlr.runtime.CharStream; +import org.antlr.runtime.CommonTokenStream; +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.map.ObjectMapper; + +public class Validator { + + /** + * @param args + */ + public static void main(String[] args) { + if (args.length < 0 || args.length > 1) { + System.err.println("Usage: Validator "); + System.exit(1); + } + try { + CharStream input = new ANTLRInputStream(new FileInputStream(args[0])); + TokenSource lexer = new OrderlyLexer(input); + TokenStream tokens = new CommonTokenStream(lexer); + OrderlyParser parser = new OrderlyParser(tokens); + + orderly_schema_return result = parser.orderly_schema(); + CommonTree parserTree = (CommonTree) result.getTree(); + System.out.println("tree="+parserTree.toStringTree()); + + // Extract hashmap from parsed tree + Object untyped = result.result; + System.err.println("Parser result map " + untyped); + + ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally + mapper.writeValue(System.out, untyped); + mapper.writeValue(new File("json-schema.json"), untyped); + + + } catch (Exception e) { + System.err.println("Error parsing inout file " + args[0] + ": " + e); + e.printStackTrace(); + } + + + } + +}