You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by je...@apache.org on 2010/11/26 18:04:13 UTC
svn commit: r1039466 - in
/incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator:
.classpath .project build.xml
src/antlr/org/apache/chemistry/opencmis/orderly/Orderly.g
src/java/org/apache/chemistry/opencmis/orderly/validate/Validator.java
Author: jens
Date: Fri Nov 26 17:04:12 2010
New Revision: 1039466
URL: http://svn.apache.org/viewvc?rev=1039466&view=rev
Log:
add validator validator code for Orderliy
Added:
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/.classpath
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/.project
Modified:
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/build.xml
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/antlr/org/apache/chemistry/opencmis/orderly/Orderly.g
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/java/org/apache/chemistry/opencmis/orderly/validate/Validator.java
Added: incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/.classpath
URL: http://svn.apache.org/viewvc/incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/.classpath?rev=1039466&view=auto
==============================================================================
--- incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/.classpath (added)
+++ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/.classpath Fri Nov 26 17:04:12 2010
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src/java"/>
+ <classpathentry kind="src" path="build/generated-sources/antlr3"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="lib" path="C:/Java/JUnit4/junit-4.6.jar"/>
+ <classpathentry kind="lib" path="C:/Java/jackson-1.5.6/jackson-core-asl-1.5.6.jar"/>
+ <classpathentry kind="lib" path="C:/Java/jackson-1.5.6/jackson-mapper-asl-1.5.6.jar"/>
+ <classpathentry kind="lib" path="C:/Java/maven-repository/org/antlr/antlr-runtime/3.2/antlr-runtime-3.2.jar" sourcepath="C:/Java/maven-repository/org/antlr/antlr-runtime/3.2/antlr-runtime-3.2-sources.jar"/>
+ <classpathentry kind="lib" path="lib/json-schema-validator-0.0.1-SNAPSHOT.jar" sourcepath="lib/json-schema-validator-0.0.1-SNAPSHOT-src-0.0.1.zip"/>
+ <classpathentry kind="lib" path="C:/Java/maven-repository/org/slf4j/slf4j-api/1.5.11/slf4j-api-1.5.11.jar"/>
+ <classpathentry kind="lib" path="C:/Java/maven-repository/ch/qos/logback/logback-classic/0.9.20/logback-classic-0.9.20.jar"/>
+ <classpathentry kind="lib" path="C:/Java/maven-repository/ch/qos/logback/logback-core/0.9.20/logback-core-0.9.20.jar"/>
+ <classpathentry kind="output" path="build/classes"/>
+</classpath>
Added: incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/.project
URL: http://svn.apache.org/viewvc/incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/.project?rev=1039466&view=auto
==============================================================================
--- incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/.project (added)
+++ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/.project Fri Nov 26 17:04:12 2010
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>OrderlyValidator</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
Modified: incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/build.xml
URL: http://svn.apache.org/viewvc/incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/build.xml?rev=1039466&r1=1039465&r2=1039466&view=diff
==============================================================================
--- incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/build.xml (original)
+++ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/build.xml Fri Nov 26 17:04:12 2010
@@ -1,4 +1,24 @@
-<project name="orderlyvalidator" basedir="." default="compile">
+<!--
+ *
+ * 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.
+ *
+ -->
+ <project name="orderlyvalidator" basedir="." default="compile">
<!-- properties for location of directories and files used in build -->
<property name="java.src.dir" value="src/java"/>
Modified: 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=1039466&r1=1039465&r2=1039466&view=diff
==============================================================================
--- incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/antlr/org/apache/chemistry/opencmis/orderly/Orderly.g (original)
+++ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/antlr/org/apache/chemistry/opencmis/orderly/Orderly.g Fri Nov 26 17:04:12 2010
@@ -58,6 +58,31 @@ import java.util.HashMap;
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 {
}
@@ -83,7 +108,6 @@ unnamed_entries returns [Object result]
}
@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); }
@@ -255,7 +279,12 @@ json_value returns [Object result]
json_string returns [String result]
:
- STRING {$result=$STRING.text;}
+ STRING
+ {
+ String s = $STRING.text;
+ s = s.substring(1, s.length()-1); // remove quotes
+ $result=s;
+ }
;
// ----------------------------------------
Modified: 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=1039466&r1=1039465&r2=1039466&view=diff
==============================================================================
--- incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/java/org/apache/chemistry/opencmis/orderly/validate/Validator.java (original)
+++ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/java/org/apache/chemistry/opencmis/orderly/validate/Validator.java Fri Nov 26 17:04:12 2010
@@ -20,56 +20,138 @@ package org.apache.chemistry.opencmis.or
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 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- if (args.length < 0 || args.length > 1) {
- System.err.println("Usage: Validator <file-name>");
- System.exit(1);
+ 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 {
- 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);
+ 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 parsing inout file " + args[0] + ": " + e);
- e.printStackTrace();
+ 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 vaildate <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);
+ }
}
+
}