directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r1527303 - in /directory/escimo/branches/json-schema-experiment/client: ./ src/main/java/org/apache/directory/scim/ src/main/resources/
Date Sun, 29 Sep 2013 11:13:43 GMT
Author: kayyagari
Date: Sun Sep 29 11:13:43 2013
New Revision: 1527303

URL: http://svn.apache.org/r1527303
Log:
generating java classes from scim schema

Added:
    directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/AttributeDetail.java
    directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/JsonToJava.java
    directory/escimo/branches/json-schema-experiment/client/src/main/resources/
    directory/escimo/branches/json-schema-experiment/client/src/main/resources/resource-class.st
Modified:
    directory/escimo/branches/json-schema-experiment/client/pom.xml

Modified: directory/escimo/branches/json-schema-experiment/client/pom.xml
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/client/pom.xml?rev=1527303&r1=1527302&r2=1527303&view=diff
==============================================================================
--- directory/escimo/branches/json-schema-experiment/client/pom.xml (original)
+++ directory/escimo/branches/json-schema-experiment/client/pom.xml Sun Sep 29 11:13:43 2013
@@ -39,6 +39,12 @@
      <artifactId>escimo-common</artifactId>
      <version>${project.version}</version>
    </dependency>   
-   
+
+   <dependency>
+     <groupId>org.antlr</groupId>
+     <artifactId>stringtemplate</artifactId>
+     <version>3.2.1</version>
+   </dependency>
+      
   </dependencies>
 </project>

Added: directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/AttributeDetail.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/AttributeDetail.java?rev=1527303&view=auto
==============================================================================
--- directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/AttributeDetail.java
(added)
+++ directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/AttributeDetail.java
Sun Sep 29 11:13:43 2013
@@ -0,0 +1,111 @@
+/*
+ *  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.directory.scim;
+
+
+/**
+ * 
+ * A simple pojo internally used while gerating the source code.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class AttributeDetail
+{
+    private String name;
+    private String javaType;
+    private boolean multiValued = false;
+
+
+    public AttributeDetail( String name, String javaType )
+    {
+        if ( name == null || javaType == null )
+        {
+            throw new IllegalArgumentException( "Null value cannot be accepted" );
+        }
+
+        this.name = name;
+        this.javaType = javaType;
+    }
+
+
+    public String getName()
+    {
+        return name;
+    }
+
+    public String getMethodName()
+    {
+        return Character.toUpperCase( name.charAt( 0 ) ) + name.substring( 1 );
+    }
+
+    public String getJavaType()
+    {
+        return javaType;
+    }
+
+
+    public boolean isMultiValued()
+    {
+        return multiValued;
+    }
+
+
+    public void setMultiValued( boolean isMultiValued )
+    {
+        this.multiValued = isMultiValued;
+    }
+
+    
+    public boolean isBoolean()
+    {
+        return "boolean".equalsIgnoreCase( javaType );
+    }
+    
+
+    public int compareTo( Object o )
+    {
+        AttributeDetail that = ( AttributeDetail ) o;
+
+        return getName().compareTo( that.getName() );
+    }
+
+
+    @Override
+    public boolean equals( Object obj )
+    {
+        return getName().equals( ( ( AttributeDetail ) obj ).getName() );
+    }
+
+
+    @Override
+    public int hashCode()
+    {
+        return name.hashCode();
+    }
+
+
+    @Override
+    public String toString()
+    {
+        return name + "=" + name;
+    }
+
+}

Added: directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/JsonToJava.java
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/JsonToJava.java?rev=1527303&view=auto
==============================================================================
--- directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/JsonToJava.java
(added)
+++ directory/escimo/branches/json-schema-experiment/client/src/main/java/org/apache/directory/scim/JsonToJava.java
Sun Sep 29 11:13:43 2013
@@ -0,0 +1,171 @@
+/*
+ *   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.directory.scim;
+
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.antlr.stringtemplate.StringTemplate;
+import org.antlr.stringtemplate.StringTemplateGroup;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+
+
+/**
+ * TODO JsonToJava.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class JsonToJava
+{
+    private static StringTemplateGroup stg = new StringTemplateGroup( "json" );
+    
+    public static void compile( String schema )
+    {
+        JsonParser parser = new JsonParser();
+        JsonObject json = ( JsonObject ) parser.parse( schema );
+
+        List<String> innerClasses = new ArrayList<String>();
+        
+        StringTemplate template = generateClass( json, innerClasses, null );
+        template.setAttribute( "allInnerClasses", innerClasses );
+        
+        System.out.println( template );
+    }
+
+    
+    private static StringTemplate generateClass( JsonObject json, List<String> innerClasses,
StringTemplate parent )
+    {
+        StringTemplate template = stg.getInstanceOf( "resource-class" );
+
+        if ( json.has( "id" ) )
+        {
+            String schemaId = json.get( "id" ).getAsString();
+            template.setAttribute( "schemaId", schemaId );
+
+            template.setAttribute( "package", "org.apache.directory.scim" );
+
+            template.setAttribute( "visibility", "public" );
+
+            String desc = json.get( "description" ).getAsString();
+            template.setAttribute( "resourceDesc", desc );
+        }
+
+        if( parent != null )
+        {
+            template.setAttribute( "static", "static" );
+        }
+
+        String className = json.get( "name" ).getAsString();
+        template.setAttribute( "className", className );
+
+        List<AttributeDetail> simpleAttributes = new ArrayList<AttributeDetail>();
+
+
+        JsonArray attributes;
+        
+        if( json.has( "attributes" ) )
+        {
+            attributes = json.get( "attributes" ).getAsJsonArray();
+        }
+        else
+        {
+            attributes = json.get( "subAttributes" ).getAsJsonArray();
+        }
+
+        Iterator<JsonElement> itr = attributes.iterator();
+        while ( itr.hasNext() )
+        {
+            JsonObject jo = ( JsonObject ) itr.next();
+            String type = jo.get( "type" ).getAsString();
+
+            String name = jo.get( "name" ).getAsString();
+
+            String javaType = "String";
+            if ( type.equals( "numeric" ) )
+            {
+                javaType = "Number";
+            }
+            else if ( type.equals( "boolean" ) )
+            {
+                javaType = "boolean";
+            }
+
+            if ( type.equals( "complex" ) )
+            {
+                javaType = Character.toUpperCase( name.charAt( 0 ) ) + name.substring( 1
);
+                
+                if( javaType.endsWith( "s" ) )
+                {
+                    javaType = javaType.substring( 0, javaType.length() - 1 );
+                }
+                
+                // replace the type's name
+                jo.remove( "name" );
+                jo.addProperty( "name", javaType );
+                
+                javaType = className + "." + javaType;
+                
+                boolean multiValued = jo.get( "multiValued" ).getAsBoolean();
+                
+                if ( multiValued )
+                {
+                    javaType = "java.util.List<" + javaType + ">";
+                }
+                
+                //how to add inner classes
+                StringTemplate inner = generateClass( jo, innerClasses, template );
+                innerClasses.add( inner.toString() );
+            }
+
+            AttributeDetail nc = new AttributeDetail( name, javaType );
+            simpleAttributes.add( nc );
+        }
+
+        template.setAttribute( "allAttrs", simpleAttributes );
+
+        return template;
+    }
+
+    public static void main( String[] args ) throws Exception
+    {
+        //InputStream in = JsonToJava.class.getClassLoader().getSystemClassLoader().getResourceAsStream(
"user-schema.json" );
+        BufferedReader br = new BufferedReader( new FileReader(
+            "/Users/dbugger/projects/json-schema-escimo/common/src/main/resources/user-schema.json"
) );
+        String s;
+
+        StringBuilder sb = new StringBuilder();
+        while ( ( s = br.readLine() ) != null )
+        {
+            sb.append( s );
+        }
+
+        JsonToJava.compile( sb.toString() );
+    }
+}

Added: directory/escimo/branches/json-schema-experiment/client/src/main/resources/resource-class.st
URL: http://svn.apache.org/viewvc/directory/escimo/branches/json-schema-experiment/client/src/main/resources/resource-class.st?rev=1527303&view=auto
==============================================================================
--- directory/escimo/branches/json-schema-experiment/client/src/main/resources/resource-class.st
(added)
+++ directory/escimo/branches/json-schema-experiment/client/src/main/resources/resource-class.st
Sun Sep 29 11:13:43 2013
@@ -0,0 +1,61 @@
+$!/*
+ *  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.
+ *
+ */!$
+$if(package)$
+package $package$;
+$endif$
+
+$visibility$ $static$ class $className$
+{
+
+    $if(schemaId)$
+    public static final String schemaId = "$schemaId$";
+    $endif$
+    
+    $if(resourceDesc)$
+    public static final String resourceDesc = "$resourceDesc$";
+    $endif$
+    
+    $allAttrs: { attr | private $attr.javaType$ $attr.name$;}; separator="\n\n"$
+    
+    public $className$()
+    {
+    
+    }
+   
+   $allAttrs: { attr | 
+    public void set$attr.methodName$( $attr.javaType$ $attr.name$ )
+    {      
+       this.$attr.name$ = $attr.name$;
+    }
+   
+    $if(attr.boolean)$
+    public $attr.javaType$ is$attr.methodName$()
+    $else$
+    public $attr.javaType$ get$attr.methodName$()
+    $endif$
+    {
+       return $attr.name$;
+    }
+    
+   }$
+    
+    
+    $allInnerClasses: { innerClass | $innerClass$ }; separator="\n\n"$
+}
\ No newline at end of file



Mime
View raw message