cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r602672 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen: ClassGenerationAction.java ClassGenerationAction1_1.java ClientClassGenerationAction.java
Date Sun, 09 Dec 2007 14:03:43 GMT
Author: aadamchik
Date: Sun Dec  9 06:03:42 2007
New Revision: 602672

URL: http://svn.apache.org/viewvc?rev=602672&view=rev
Log:
CAY-926 Refactoring class generator classes

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerationAction1_1.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClientClassGenerationAction.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java?rev=602672&r1=602671&r2=602672&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java
Sun Dec  9 06:03:42 2007
@@ -20,7 +20,6 @@
 
 import java.io.File;
 import java.io.FileOutputStream;
-import java.io.IOException;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.util.ArrayList;
@@ -83,31 +82,65 @@
         this.templateCache = new HashMap<String, Template>(5);
     }
 
-    protected String defaultSingleClassTemplate() {
+    protected String defaultSingleClassTemplateName() {
         return ClassGenerationAction.SINGLE_CLASS_TEMPLATE;
     }
 
-    protected String defaultSubclassTemplate() {
+    protected String defaultSubclassTemplateName() {
         return ClassGenerationAction.SUBCLASS_TEMPLATE;
     }
 
-    protected String defaultSuperclassTemplate() {
+    protected String defaultSuperclassTemplateName() {
         return ClassGenerationAction.SUPERCLASS_TEMPLATE;
     }
 
     /**
+     * Returns template file path for Java class when generating single classes.
+     */
+    protected Template singleClassTemplate() throws Exception {
+        String name = (template != null) ? template : defaultSingleClassTemplateName();
+        return getTemplate(name);
+    }
+
+    /**
+     * Returns template file path for Java subclass when generating class pairs.
+     */
+    protected Template subclassTemplate() throws Exception {
+        String name = (template != null) ? template : defaultSubclassTemplateName();
+        return getTemplate(name);
+    }
+
+    /**
+     * Returns template file path for Java superclass when generating class pairs.
+     */
+    protected Template superclassTemplate() throws Exception {
+        String name = (superTemplate != null)
+                ? superTemplate
+                : defaultSuperclassTemplateName();
+        return getTemplate(name);
+    }
+
+    /**
+     * Returns a String used to prefix class name to create a generated superclass.
+     * Default value is "_".
+     */
+    protected String getSuperclassPrefix() {
+        return ClassGenerationAction.SUPERCLASS_PREFIX;
+    }
+
+    /**
      * Runs class generation. Produces a pair of Java classes for each ObjEntity in the
      * map. This allows developers to use generated <b>subclass </b> for their
custom
      * code, while generated <b>superclass </b> will contain Cayenne code. Superclass
will
      * be generated in the same package, its class name will be derived from the class
      * name by adding a <code>superPrefix</code>.
      */
-    public void generateClassPairs(
-            String classTemplate,
-            String superTemplate,
-            String superPrefix) throws Exception {
+    protected void generateClassPairs() throws Exception {
 
         StringUtils stringUtils = StringUtils.getInstance();
+        Template superTemplate = superclassTemplate();
+        Template classTemplate = subclassTemplate();
+        String superPrefix = getSuperclassPrefix();
 
         for (ObjEntity entity : entitiesForCurrentMode()) {
 
@@ -129,25 +162,33 @@
 
             Writer superOut = openWriter(superPackageName, superClassName);
             if (superOut != null) {
-                generate(
-                        superOut,
-                        superTemplate,
+                context.put("objEntity", entity);
+                context.put("stringUtils", StringUtils.getInstance());
+                context.put("entityUtils", new EntityUtils(
+                        dataMap,
                         entity,
                         fqnBaseClass,
                         fqnSuperClass,
-                        fqnSubClass);
+                        fqnSubClass));
+                context.put("importUtils", new ImportUtils());
+
+                superTemplate.merge(context, superOut);
                 superOut.close();
             }
 
             Writer mainOut = openWriter(subPackageName, subClassName);
             if (mainOut != null) {
-                generate(
-                        mainOut,
-                        classTemplate,
+                context.put("objEntity", entity);
+                context.put("stringUtils", StringUtils.getInstance());
+                context.put("entityUtils", new EntityUtils(
+                        dataMap,
                         entity,
                         fqnBaseClass,
                         fqnSuperClass,
-                        fqnSubClass);
+                        fqnSubClass));
+                context.put("importUtils", new ImportUtils());
+
+                classTemplate.merge(context, mainOut);
                 mainOut.close();
             }
         }
@@ -156,8 +197,10 @@
     /**
      * Runs class generation. Produces a single Java class for each ObjEntity in the map.
      */
-    public void generateSingleClasses(String classTemplate, String superPrefix)
-            throws Exception {
+    protected void generateSingleClasses() throws Exception {
+
+        Template classTemplate = singleClassTemplate();
+        String superPrefix = getSuperclassPrefix();
 
         for (ObjEntity entity : entitiesForCurrentMode()) {
 
@@ -178,13 +221,17 @@
 
             Writer out = openWriter(subPackageName, subClassName);
             if (out != null) {
-                generate(
-                        out,
-                        classTemplate,
+                context.put("objEntity", entity);
+                context.put("stringUtils", StringUtils.getInstance());
+                context.put("entityUtils", new EntityUtils(
+                        dataMap,
                         entity,
                         fqnBaseClass,
                         fqnSuperClass,
-                        fqnSubClass);
+                        fqnSubClass));
+                context.put("importUtils", new ImportUtils());
+
+                classTemplate.merge(context, out);
                 out.close();
             }
         }
@@ -198,12 +245,10 @@
 
         try {
             if (makePairs) {
-                String t = getTemplateForPairs();
-                String st = getSupertemplateForPairs();
-                generateClassPairs(t, st, SUPERCLASS_PREFIX);
+                generateClassPairs();
             }
             else {
-                generateSingleClasses(getTemplateForSingles(), SUPERCLASS_PREFIX);
+                generateSingleClasses();
             }
         }
         finally {
@@ -431,30 +476,6 @@
     }
 
     /**
-     * Merges a template with prebuilt context, writing output to provided writer.
-     */
-    protected void generate(
-            Writer out,
-            String template,
-            ObjEntity entity,
-            String fqnBaseClass,
-            String fqnSuperClass,
-            String fqnSubClass) throws Exception {
-
-        context.put("objEntity", entity);
-        context.put("stringUtils", StringUtils.getInstance());
-        context.put("entityUtils", new EntityUtils(
-                dataMap,
-                entity,
-                fqnBaseClass,
-                fqnSuperClass,
-                fqnSubClass));
-        context.put("importUtils", new ImportUtils());
-
-        getTemplate(template).merge(context, out);
-    }
-
-    /**
      * Returns a File object corresponding to a directory where files that belong to
      * <code>pkgName</code> package should reside. Creates any missing diectories
below
      * <code>dest</code>.
@@ -472,27 +493,6 @@
         }
 
         return fullPath;
-    }
-
-    /**
-     * Returns template file path for Java class when generating single classes.
-     */
-    protected String getTemplateForSingles() throws IOException {
-        return (template != null) ? template : defaultSingleClassTemplate();
-    }
-
-    /**
-     * Returns template file path for Java subclass when generating class pairs.
-     */
-    protected String getTemplateForPairs() throws IOException {
-        return (template != null) ? template : defaultSubclassTemplate();
-    }
-
-    /**
-     * Returns template file path for Java superclass when generating class pairs.
-     */
-    protected String getSupertemplateForPairs() throws IOException {
-        return (superTemplate != null) ? superTemplate : defaultSuperclassTemplate();
     }
 
     public void setTimestamp(long timestamp) {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerationAction1_1.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerationAction1_1.java?rev=602672&r1=602671&r2=602672&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerationAction1_1.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerationAction1_1.java
Sun Dec  9 06:03:42 2007
@@ -23,6 +23,7 @@
 import org.apache.cayenne.CayenneDataObject;
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.map.ObjEntity;
+import org.apache.velocity.Template;
 
 /**
  * @since 3.0
@@ -36,26 +37,27 @@
     public static final String SUPERCLASS_TEMPLATE = "dotemplates/superclass.vm";
 
     @Override
-    protected String defaultSingleClassTemplate() {
+    protected String defaultSingleClassTemplateName() {
         return ClassGenerationAction1_1.SINGLE_CLASS_TEMPLATE;
     }
 
     @Override
-    protected String defaultSubclassTemplate() {
+    protected String defaultSubclassTemplateName() {
         return ClassGenerationAction1_1.SUBCLASS_TEMPLATE;
     }
 
     @Override
-    protected String defaultSuperclassTemplate() {
+    protected String defaultSuperclassTemplateName() {
         return ClassGenerationAction1_1.SUPERCLASS_TEMPLATE;
     }
 
     @Override
-    public void generateClassPairs(
-            String classTemplate,
-            String superTemplate,
-            String superPrefix) throws Exception {
+    protected void generateClassPairs() throws Exception {
 
+        Template superTemplate = superclassTemplate();
+        Template classTemplate = subclassTemplate();
+        String superPrefix = getSuperclassPrefix();
+        
         ClassGenerationInfo mainGen = new ClassGenerationInfo();
         ClassGenerationInfo superGen = new ClassGenerationInfo();
 
@@ -72,7 +74,7 @@
                     + superGen.getClassName());
 
             if (superOut != null) {
-                generate(superOut, superTemplate, entity, null, null, null);
+                superTemplate.merge(context, superOut);
                 superOut.close();
             }
 
@@ -80,7 +82,7 @@
             initClassGenerator(mainGen, entity, false);
             Writer mainOut = openWriter(mainGen.getPackageName(), mainGen.getClassName());
             if (mainOut != null) {
-                generate(mainOut, classTemplate, entity, null, null, null);
+                classTemplate.merge(context, mainOut);
                 mainOut.close();
             }
         }
@@ -89,9 +91,10 @@
     }
 
     @Override
-    public void generateSingleClasses(String classTemplate, String superPrefix)
+    protected void generateSingleClasses()
             throws Exception {
 
+        Template classTemplate = singleClassTemplate();
         ClassGenerationInfo mainGen = new ClassGenerationInfo();
 
         for (ObjEntity entity : entitiesForCurrentMode()) {
@@ -99,7 +102,7 @@
             initClassGenerator(mainGen, entity, false);
             Writer out = openWriter(mainGen.getPackageName(), mainGen.getClassName());
             if (out != null) {
-                generate(out, classTemplate, entity, null, null, null);
+                classTemplate.merge(context, out);
                 out.close();
             }
         }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClientClassGenerationAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClientClassGenerationAction.java?rev=602672&r1=602671&r2=602672&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClientClassGenerationAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClientClassGenerationAction.java
Sun Dec  9 06:03:42 2007
@@ -22,6 +22,7 @@
 
 import org.apache.cayenne.PersistentObject;
 import org.apache.cayenne.map.ObjEntity;
+import org.apache.velocity.Template;
 
 /**
  * @since 3.0
@@ -33,33 +34,33 @@
     public static final String SUPERCLASS_TEMPLATE = "dotemplates/v1_2/client-superclass.vm";
 
     @Override
-    protected String defaultSingleClassTemplate() {
+    protected String defaultSingleClassTemplateName() {
         throw new IllegalStateException(
                 "Default generation for single classes on the client is not supported.");
     }
 
     @Override
-    protected String defaultSubclassTemplate() {
+    protected String defaultSubclassTemplateName() {
         return ClientClassGenerationAction.SUBCLASS_TEMPLATE;
     }
 
     @Override
-    protected String defaultSuperclassTemplate() {
+    protected String defaultSuperclassTemplateName() {
         return ClientClassGenerationAction.SUPERCLASS_TEMPLATE;
     }
 
     @Override
-    public void generateSingleClasses(String classTemplate, String superPrefix)
-            throws Exception {
+    protected void generateSingleClasses() throws Exception {
         throw new IllegalStateException(
                 "Single classes generation is not supported for the client.");
     }
 
     @Override
-    public void generateClassPairs(
-            String classTemplate,
-            String superTemplate,
-            String superPrefix) throws Exception {
+    protected void generateClassPairs() throws Exception {
+
+        Template superTemplate = superclassTemplate();
+        Template classTemplate = subclassTemplate();
+        String superPrefix = getSuperclassPrefix();
 
         for (ObjEntity entity : entitiesForCurrentMode()) {
 
@@ -87,25 +88,33 @@
             Writer superOut = openWriter(superPackageName, superClassName);
 
             if (superOut != null) {
-                generate(
-                        superOut,
-                        superTemplate,
+                context.put("objEntity", entity);
+                context.put("stringUtils", StringUtils.getInstance());
+                context.put("entityUtils", new EntityUtils(
+                        dataMap,
                         entity,
                         fqnBaseClass,
                         fqnSuperClass,
-                        fqnSubClass);
+                        fqnSubClass));
+                context.put("importUtils", new ImportUtils());
+
+                superTemplate.merge(context, superOut);
                 superOut.close();
             }
 
             Writer mainOut = openWriter(subPackageName, subClassName);
             if (mainOut != null) {
-                generate(
-                        mainOut,
-                        classTemplate,
+                context.put("objEntity", entity);
+                context.put("stringUtils", StringUtils.getInstance());
+                context.put("entityUtils", new EntityUtils(
+                        dataMap,
                         entity,
                         fqnBaseClass,
                         fqnSuperClass,
-                        fqnSubClass);
+                        fqnSubClass));
+                context.put("importUtils", new ImportUtils());
+
+                classTemplate.merge(context, mainOut);
                 mainOut.close();
             }
         }



Mime
View raw message