Author: kmenard
Date: Thu Nov  2 20:01:58 2006
New Revision: 470646

URL: http://svn.apache.org/viewvc?view=rev&rev=470646
Log:
Refactoring to cut down on code duplication between cgen ant task and cgen 
maven2 mojo.  Also, isolated the ant dependencies to only the ant task.  This 
is in furtherance of the development of the cgen mojo (CAY-591).

Modified:
    
incubator/cayenne/main/trunk/other/maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java

Modified: 
incubator/cayenne/main/trunk/other/maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
URL: 
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/other/maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java?view=diff&rev=470646&r1=470645&r2=470646
==============================================================================
--- 
incubator/cayenne/main/trunk/other/maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
 (original)
+++ 
incubator/cayenne/main/trunk/other/maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
 Thu Nov  2 20:01:58 2006
@@ -22,7 +22,6 @@
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
-import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.project.MavenProject;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.ObjEntity;
@@ -191,113 +190,52 @@
     private String version;
 
     private DefaultClassGenerator generator;
+    protected CayenneGeneratorUtil generatorUtil;
 
     public void execute() throws MojoExecutionException, MojoFailureException
     {
         // Create the destination directory if necessary.
+       // TODO: (KJM 11/2/06) The destDir really should be added as a 
compilation resource for maven.
         if (!destDir.exists())
         {
             destDir.mkdirs();
         }
 
         generator = createGenerator();
+        generatorUtil = new CayenneGeneratorUtil();
+        
+        generatorUtil.setExcludeEntitiesPattern(excludeEntitiesPattern);
+        generatorUtil.setGenerator(generator);
+        generatorUtil.setIncludeEntitiesPattern(includeEntitiesPattern);
+        generatorUtil.setLogger(new MavenLogger(this));
+        generatorUtil.setMap(map);
 
         try {
-            processMap();
+               generatorUtil.setAdditionalMaps(convertAdditionalDataMaps());
+            generatorUtil.execute();
         }
-        catch (Throwable th) {
-            th = Util.unwindException(th);
-
-            String thMessage = th.getLocalizedMessage();
-            String message = "Error generating classes: ";
-            message += (!Util.isEmptyString(thMessage)) ? thMessage : th
-                    .getClass()
-                    .getName();
-
-            this.getLog().error(message);
-
-            throw new MojoExecutionException(message, th);
-        }
-    }
-
-
-    protected void processMap() throws Exception {
-
-        DataMap dataMap = loadDataMap();
-        DataMap additionalDataMaps[] = loadAdditionalDataMaps();
-
-        // Create MappingNamespace for maps.
-        EntityResolver entityResolver = new 
EntityResolver(Collections.singleton(dataMap));
-        dataMap.setNamespace(entityResolver);
-        for (int i = 0; i < additionalDataMaps.length; i++) {
-            entityResolver.addDataMap(additionalDataMaps[i]);
-            additionalDataMaps[i].setNamespace(entityResolver);
-        }
-
-        Collection allEntities = dataMap.getObjEntities();
-        List filteredEntities = new ArrayList(allEntities.size());
-
-        // filter client entities
-        if (generator.isClient()) {
-            if (dataMap.isClientSupported()) {
-                Iterator it = allEntities.iterator();
-                while (it.hasNext()) {
-                    ObjEntity entity = (ObjEntity) it.next();
-                    if (entity.isClientAllowed()) {
-                        filteredEntities.add(entity);
-                    }
-                }
-            }
-        } else {
-            filteredEntities.addAll(allEntities);
+        catch (Exception e) {
+            throw new MojoExecutionException("Error generating classes: ", e);
         }
-
-        // filter names according to the specified pattern
-        NamePatternMatcher namePatternMatcher = new NamePatternMatcher(
-                // TODO: (KJM 10/03/2006) NamePatternMatcher should be updated 
to not depend on Ant.
-                null,
-                includeEntitiesPattern,
-                excludeEntitiesPattern);
-        namePatternMatcher.filter(filteredEntities);
-
-        generator.setTimestamp(map.lastModified());
-        generator.setDataMap(dataMap);
-        generator.setObjEntities(filteredEntities);
-        generator.validateAttributes();
-        generator.execute();
-    }
-
-    /**
-     * Loads and returns a DataMap by File.
-     */
-    protected DataMap loadDataMap(File mapName) throws Exception {
-        InputSource in = new InputSource(mapName.getCanonicalPath());
-        return new MapLoader().loadDataMap(in);
-    }
-
-    /**
-     * Loads and returns DataMap based on <code>map</code> attribute.
-     */
-    protected DataMap loadDataMap() throws Exception {
-        return loadDataMap(map);
     }
 
     /**
      * Loads and returns DataMap based on <code>map</code> attribute.
      */
-    protected DataMap[] loadAdditionalDataMaps() throws Exception {
-        if (null == additionalMaps) {
-            return new DataMap[0];
+    protected File[] convertAdditionalDataMaps() throws Exception {
+       
+       if (null == additionalMaps) {
+            return new File[0];
         }
-
+       
         if (!additionalMaps.isDirectory()) {
             throw new MojoFailureException("'additionalMaps' must be a 
directory containing only datamap files.");
         }
 
         String[] maps = additionalMaps.list();
-        DataMap dataMaps[] = new DataMap[maps.length];
+        File[] dataMaps = new File[maps.length];
         for (int i = 0; i < maps.length; i++) {
-            dataMaps[i] = loadDataMap(new File(maps[i]));
+            dataMaps[i] = new File(maps[i]);
         }
         return dataMaps;
     }
@@ -324,3 +262,20 @@
         return gen;
     }
 }
+
+class MavenLogger implements ILog {
+       
+       private AbstractMojo parent;
+       
+       public MavenLogger(AbstractMojo parent) {
+               this.parent = parent;
+       }
+       
+       public void log(String msg) {
+               parent.getLog().info(msg);
+       }
+
+       public void log(String msg, int msgLevel) {
+               // TODO: (KJM 11/2/06) This should log at the appropriate level.
+       }
+}
\ No newline at end of file


Reply via email to