cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r600341 - in /cayenne/main/trunk/framework: cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/ maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/
Date Sun, 02 Dec 2007 17:15:26 GMT
Author: aadamchik
Date: Sun Dec  2 09:15:25 2007
New Revision: 600341

URL: http://svn.apache.org/viewvc?rev=600341&view=rev
Log:
CAY-926 Refactoring class generator classes
(replacing CayenneGeneratorUtil with CayenneGeneratorMapLoader to streamline generation flow)

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/TemplateProcessor.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/TemplateProcessor1_1.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/CayenneGenerationMapLoader.java
    cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/MavenLogger.java
Removed:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/CayenneGeneratorUtil.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/DefaultClassGenerator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/MapClassGenerator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/NamePatternMatcher.java
    cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerator.java?rev=600341&r1=600340&r2=600341&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/ClassGenerator.java
Sun Dec  2 09:15:25 2007
@@ -41,6 +41,7 @@
  * "classGen".
  * 
  * @author Andrus Adamchik
+ * @deprecated since 3.0 use {@link TemplateProcessor}.
  */
 public class ClassGenerator {
 
@@ -146,7 +147,7 @@
      * Generates Java code for the ObjEntity. Output is written to the provided Writer.
      */
     public void generateClass(Writer out, ObjEntity entity) throws Exception {
-        
+
         if (!VERSION_1_1.equals(versionString)) {
             throw new IllegalStateException(
                     "Illegal Version in generateClass(Writer,ObjEntity): "
@@ -167,7 +168,7 @@
             String fqnBaseClass,
             String fqnSuperClass,
             String fqnSubClass) throws Exception {
-        
+
         if (!VERSION_1_2.equals(versionString)) {
             throw new IllegalStateException(
                     "Illegal Version in generateClass(Writer,ObjEntity,String,String,String):
"

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/DefaultClassGenerator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/DefaultClassGenerator.java?rev=600341&r1=600340&r2=600341&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/DefaultClassGenerator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/DefaultClassGenerator.java
Sun Dec  2 09:15:25 2007
@@ -40,6 +40,8 @@
  */
 public class DefaultClassGenerator extends MapClassGenerator {
 
+    private static final String WILDCARD = "*";
+
     protected File destDir;
     protected boolean overwrite;
     protected boolean usePkgPath = true;
@@ -47,7 +49,6 @@
     protected String template;
     protected String superTemplate;
     protected long timestamp = System.currentTimeMillis();
-    private static final String WILDCARD = "*";
     protected String outputPattern = "*.java";
 
     /**

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/MapClassGenerator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/MapClassGenerator.java?rev=600341&r1=600340&r2=600341&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/MapClassGenerator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/MapClassGenerator.java
Sun Dec  2 09:15:25 2007
@@ -88,6 +88,9 @@
     public MapClassGenerator() {
     }
 
+    /**
+     * @deprecated since 3.0 - unused.
+     */
     public MapClassGenerator(DataMap dataMap) {
         this(dataMap, new ArrayList<ObjEntity>(dataMap.getObjEntities()));
     }
@@ -379,6 +382,8 @@
     /**
      * Runs class generation. Produces a single Java class for each ObjEntity in the map.
      * Uses default Cayenne templates for classes.
+     * 
+     * @deprecated since 3.0 - unused.
      */
     public void generateSingleClasses() throws Exception {
         generateSingleClasses(defaultSingleClassTemplate(), SUPERCLASS_PREFIX);

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/TemplateProcessor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/TemplateProcessor.java?rev=600341&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/TemplateProcessor.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/TemplateProcessor.java
Sun Dec  2 09:15:25 2007
@@ -0,0 +1,130 @@
+/*****************************************************************
+ *   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.cayenne.gen;
+
+import java.io.Writer;
+import java.util.Properties;
+
+import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.context.Context;
+import org.apache.velocity.runtime.RuntimeConstants;
+import org.apache.velocity.runtime.log.NullLogSystem;
+
+import foundrylogic.vpp.VPPConfig;
+
+/**
+ * Encapsulates a class generation template that can be used repeatedly to generate a set
+ * of classes.
+ * 
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+// This class is used for version 1.2 of the class generation. Backwards compatibility (v
+// 1.1) is implemented via subclasses.
+public class TemplateProcessor {
+
+    protected Template classTemplate;
+    protected Context velocityContext;
+
+    /**
+     * Only used by deprecated subclasses.
+     * 
+     * @deprecated since 3.0.
+     */
+    protected TemplateProcessor() throws Exception {
+    }
+
+    public TemplateProcessor(String template, VPPConfig vppConfig) throws Exception {
+
+        if (vppConfig != null) {
+            velocityContext = vppConfig.getVelocityContext();
+        }
+        else {
+            velocityContext = new VelocityContext();
+        }
+
+        initializeClassTemplate(template);
+    }
+
+    /**
+     * Sets up VelocityEngine properties, creates a VelocityEngine instance, and fetches
a
+     * template using the VelocityEngine instance.
+     * 
+     * @since 1.2
+     */
+    protected void initializeClassTemplate(String template)
+            throws CayenneRuntimeException {
+
+        Properties props = new Properties();
+
+        // null logger that will prevent velocity.log from being generated
+        props.put(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogSystem.class
+                .getName());
+        props.put("resource.loader", "cayenne");
+        props.put("cayenne.resource.loader.class", ClassGeneratorResourceLoader.class
+                .getName());
+
+        VelocityEngine velocityEngine = new VelocityEngine();
+        try {
+            velocityEngine.init(props);
+        }
+        catch (Exception ex) {
+            throw new CayenneRuntimeException("Can't initialize Velocity", ex);
+        }
+
+        try {
+            classTemplate = velocityEngine.getTemplate(template);
+        }
+        catch (Exception ex) {
+            throw new CayenneRuntimeException("Can't create template: " + template, ex);
+        }
+    }
+
+    /**
+     * Generates Java code for the ObjEntity, writing output to the provided Writer.
+     */
+    public void generateClass(
+            Writer out,
+            DataMap dataMap,
+            ObjEntity entity,
+            String fqnBaseClass,
+            String fqnSuperClass,
+            String fqnSubClass) throws Exception {
+
+        if (dataMap == null) {
+            throw new IllegalStateException("DataMap is null.");
+        }
+
+        velocityContext.put("objEntity", entity);
+        velocityContext.put("stringUtils", StringUtils.getInstance());
+        velocityContext.put("entityUtils", new EntityUtils(
+                dataMap,
+                entity,
+                fqnBaseClass,
+                fqnSuperClass,
+                fqnSubClass));
+        velocityContext.put("importUtils", new ImportUtils());
+        classTemplate.merge(velocityContext, out);
+    }
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/TemplateProcessor1_1.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/TemplateProcessor1_1.java?rev=600341&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/TemplateProcessor1_1.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/gen/TemplateProcessor1_1.java
Sun Dec  2 09:15:25 2007
@@ -0,0 +1,56 @@
+/*****************************************************************
+ *   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.cayenne.gen;
+
+import java.io.Writer;
+
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.velocity.VelocityContext;
+
+/**
+ * Encapsulates a legacy class generation template.
+ * 
+ * @deprecated since 3.0
+ * @author Andrus Adamchik
+ */
+class TemplateProcessor1_1 extends TemplateProcessor {
+
+    private ClassGenerationInfo classGenerationInfo;
+
+    TemplateProcessor1_1(String template) throws Exception {
+
+        this.classGenerationInfo = new ClassGenerationInfo();
+        this.velocityContext = new VelocityContext();
+        velocityContext.put("classGen", classGenerationInfo);
+
+        initializeClassTemplate(template);
+    }
+
+    /**
+     * Generates Java code for the ObjEntity. Output is written to the provided Writer.
+     */
+    void generateClass(Writer out, ObjEntity entity) throws Exception {
+        classGenerationInfo.setObjEntity(entity);
+        classTemplate.merge(velocityContext, out);
+    }
+
+    ClassGenerationInfo getClassGenerationInfo() {
+        return classGenerationInfo;
+    }
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/CayenneGenerationMapLoader.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/CayenneGenerationMapLoader.java?rev=600341&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/CayenneGenerationMapLoader.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/CayenneGenerationMapLoader.java
Sun Dec  2 09:15:25 2007
@@ -0,0 +1,120 @@
+/*****************************************************************
+ *   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.cayenne.tools;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.map.EntityResolver;
+import org.apache.cayenne.map.MapLoader;
+import org.apache.cayenne.map.ObjEntity;
+import org.xml.sax.InputSource;
+
+/**
+ * Provides DataMap loading and ObjEntity filtering functionality to the class generation
tasks.
+ * 
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+class CayenneGenerationMapLoader {
+
+    private File mainDataMapFile;
+    private File[] additionalDataMapFiles;
+    private NamePatternMatcher nameFilter;
+    private boolean client;
+
+    private DataMap mainDataMap;
+
+    DataMap getMainDataMap() throws MalformedURLException {
+        if (mainDataMap == null) {
+            MapLoader mapLoader = new MapLoader();
+
+            DataMap mainDataMap = loadDataMap(mapLoader, mainDataMapFile);
+
+            if (additionalDataMapFiles != null) {
+
+                EntityResolver entityResolver = new EntityResolver();
+                entityResolver.addDataMap(mainDataMap);
+                mainDataMap.setNamespace(entityResolver);
+
+                for (int i = 0; i < additionalDataMapFiles.length; i++) {
+
+                    DataMap dataMap = loadDataMap(mapLoader, additionalDataMapFiles[i]);
+                    entityResolver.addDataMap(dataMap);
+                    dataMap.setNamespace(entityResolver);
+                }
+            }
+
+            this.mainDataMap = mainDataMap;
+        }
+
+        return mainDataMap;
+    }
+
+    Collection<ObjEntity> getFilteredEntities() throws MalformedURLException {
+
+        List<ObjEntity> entities = new ArrayList<ObjEntity>(getMainDataMap()
+                .getObjEntities());
+
+        // filter out excluded entities...
+        Iterator<ObjEntity> it = entities.iterator();
+
+        while (it.hasNext()) {
+            ObjEntity e = it.next();
+            if (e.isGeneric()) {
+                it.remove();
+            }
+            else if (client && !e.isClientAllowed()) {
+                it.remove();
+            }
+            else if (!nameFilter.isIncluded(e.getName())) {
+                it.remove();
+            }
+        }
+
+        return entities;
+    }
+
+    protected DataMap loadDataMap(MapLoader mapLoader, File dataMapFile)
+            throws MalformedURLException {
+        InputSource in = new InputSource(dataMapFile.toURL().toString());
+        return mapLoader.loadDataMap(in);
+    }
+
+    void setMainDataMapFile(File mainDataMapFile) {
+        this.mainDataMapFile = mainDataMapFile;
+    }
+
+    void setAdditionalDataMapFiles(File[] additionalDataMapFiles) {
+        this.additionalDataMapFiles = additionalDataMapFiles;
+    }
+
+    void setClient(boolean client) {
+        this.client = client;
+    }
+
+    public void setNameFilter(NamePatternMatcher nameFilter) {
+        this.nameFilter = nameFilter;
+    }
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java?rev=600341&r1=600340&r2=600341&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java
Sun Dec  2 09:15:25 2007
@@ -19,9 +19,11 @@
 package org.apache.cayenne.tools;
 
 import java.io.File;
+import java.util.List;
 
 import org.apache.cayenne.gen.ClassGenerator;
 import org.apache.cayenne.gen.DefaultClassGenerator;
+import org.apache.cayenne.map.ObjEntity;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.types.Path;
 
@@ -42,11 +44,8 @@
     protected File additionalMaps[];
     protected DefaultClassGenerator generator;
 
-    protected CayenneGeneratorUtil generatorUtil;
-
     public CayenneGeneratorTask() {
         generator = createGenerator();
-        generatorUtil = new CayenneGeneratorUtil();
     }
 
     /**
@@ -70,16 +69,21 @@
             generator.setVppConfig(vppConfig);
         }
 
-        // Initialize the util generator state.
-        generatorUtil.setAdditionalMaps(additionalMaps);
-        generatorUtil.setExcludeEntitiesPattern(excludeEntitiesPattern);
-        generatorUtil.setGenerator(generator);
-        generatorUtil.setIncludeEntitiesPattern(includeEntitiesPattern);
-        generatorUtil.setLogger(new AntTaskLogger(this));
-        generatorUtil.setMap(map);
+        ILog logger = new AntTaskLogger(this);
+        CayenneGenerationMapLoader mapLoader = new CayenneGenerationMapLoader();
+        mapLoader.setNameFilter(new NamePatternMatcher(
+                logger,
+                includeEntitiesPattern,
+                excludeEntitiesPattern));
+        mapLoader.setMainDataMapFile(map);
+        mapLoader.setAdditionalDataMapFiles(additionalMaps);
 
         try {
-            generatorUtil.execute();
+            generator.setTimestamp(map.lastModified());
+            generator.setDataMap(mapLoader.getMainDataMap());
+            generator.setObjEntities((List<ObjEntity>) mapLoader.getFilteredEntities());
+            generator.validateAttributes();
+            generator.execute();
         }
         catch (Exception e) {
             throw new BuildException(e);
@@ -87,8 +91,8 @@
     }
 
     /**
-     * Validates attributes that are not related to internal DefaultClassGenerator.
-     * Throws BuildException if attributes are invalid.
+     * Validates attributes that are not related to internal DefaultClassGenerator. Throws
+     * BuildException if attributes are invalid.
      */
     protected void validateAttributes() throws BuildException {
         if (map == null && this.getProject() == null) {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/NamePatternMatcher.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/NamePatternMatcher.java?rev=600341&r1=600340&r2=600341&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/NamePatternMatcher.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/NamePatternMatcher.java
Sun Dec  2 09:15:25 2007
@@ -41,9 +41,8 @@
     protected Pattern[] itemIncludeFilters;
     protected Pattern[] itemExcludeFilters;
 
-    public NamePatternMatcher(ILog parentTask, String includePattern,
-            String excludePattern) {
-        this.logger = parentTask;
+    public NamePatternMatcher(ILog logger, String includePattern, String excludePattern)
{
+        this.logger = logger;
         this.itemIncludeFilters = createPatterns(includePattern);
         this.itemExcludeFilters = createPatterns(excludePattern);
     }
@@ -128,10 +127,34 @@
     }
 
     /**
+     * Returns true if a given object property satisfies the include/exclude patterns.
+     * 
+     * @since 3.0
+     */
+    public boolean isIncluded(String string) {
+
+        if ((itemIncludeFilters.length == 0) && (itemExcludeFilters.length == 0))
{
+            return true;
+        }
+
+        if (!passedIncludeFilter(string)) {
+            return false;
+        }
+
+        if (!passedExcludeFilter(string)) {
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
      * Applies preconfigured list of filters to the list, removing entities that do not
      * pass the filter.
+     * 
+     * @deprecated since 3.0
      */
-    protected List filter(List items) {
+    protected List<?> filter(List<?> items) {
         if (items == null || items.isEmpty()) {
             return items;
         }
@@ -140,7 +163,7 @@
             return items;
         }
 
-        Iterator it = items.iterator();
+        Iterator<?> it = items.iterator();
         while (it.hasNext()) {
             CayenneMapEntry entity = (CayenneMapEntry) it.next();
 
@@ -159,8 +182,50 @@
     }
 
     /**
+     * Returns true if an object matches any one of the "include" patterns, or if there is
+     * no "include" patterns defined.
+     * 
+     * @since 3.0
+     */
+    boolean passedIncludeFilter(String item) {
+        if (itemIncludeFilters.length == 0) {
+            return true;
+        }
+
+        for (int i = 0; i < itemIncludeFilters.length; i++) {
+            if (itemIncludeFilters[i].matcher(item).find()) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    /**
+     * Returns true if an object does not match any one of the "exclude" patterns, or if
+     * there is no "exclude" patterns defined.
+     * 
+     * @since 3.0
+     */
+    boolean passedExcludeFilter(String item) {
+        if (itemExcludeFilters.length == 0) {
+            return true;
+        }
+
+        for (int i = 0; i < itemExcludeFilters.length; i++) {
+            if (itemExcludeFilters[i].matcher(item).find()) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    /**
      * Returns true if the entity matches any one of the "include" patterns, or if there
      * is no "include" patterns defined.
+     * 
+     * @deprecated since 3.0
      */
     protected boolean passedIncludeFilter(CayenneMapEntry item) {
         if (itemIncludeFilters.length == 0) {
@@ -180,6 +245,8 @@
     /**
      * Returns true if the entity does not match any one of the "exclude" patterns, or if
      * there is no "exclude" patterns defined.
+     * 
+     * @deprecated since 3.0
      */
     protected boolean passedExcludeFilter(CayenneMapEntry item) {
         if (itemExcludeFilters.length == 0) {

Modified: cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java?rev=600341&r1=600340&r2=600341&view=diff
==============================================================================
--- cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
(original)
+++ cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
Sun Dec  2 09:15:25 2007
@@ -19,281 +19,256 @@
 
 package org.apache.cayenne.tools;
 
+import java.io.File;
+import java.util.List;
+
+import org.apache.cayenne.gen.DefaultClassGenerator;
+import org.apache.cayenne.map.ObjEntity;
 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.gen.DefaultClassGenerator;
-
-import java.io.File;
 
 /**
- * Maven mojo to perform class generation from data map. This class is an Maven adapter to
- * DefaultClassGenerator class.
- *
+ * Maven mojo to perform class generation from data map. This class is an Maven
+ * adapter to DefaultClassGenerator class.
+ * 
  * @author Andrus Adamchik, Kevin Menard
  * @since 3.0
- *
+ * 
  * @phase generate-sources
  * @goal cgen
  */
-public class CayenneGeneratorMojo extends AbstractMojo
-{
-    /**
-     * Project instance used to add generated source code to the build.
-     *
-     * @parameter default-value="${project}"
-     * @required
-     * @readonly
-     */
-    private MavenProject project;
-
-    /**
-     * Path to additional DataMap XML files to use for class generation.
-     *
-     * @parameter expression="${cgen.additionalMaps}"
-     */
-    private File additionalMaps;
-
-    /**
-     * Whether we are generating classes for the client tier in a
-     * Remote Object Persistence application. Default is <code>false</code>.
-     *
-     * @parameter expression="${cgen.client}" default-value="false"
-     */
-    private boolean client;
-
-    /**
-     * 	Destination directory for Java classes (ignoring their package names).
-     *
-     * @parameter expression="${cgen.destDir}" default-value="${project.build.sourceDirectory}/java/generated-sources/cayenne"
-     */
-    private File destDir;
-
-    /**
-     * Specify generated file encoding if different from the default on current platform.
-     * Target encoding must be supported by the JVM running Maven build. Standard encodings
-     * supported by Java on all platforms are US-ASCII, ISO-8859-1, UTF-8, UTF-16BE,
-     * UTF-16LE, UTF-16. See Sun Java Docs for java.nio.charset.Charset for more information.
-     *
-     * @parameter expression="${cgen.encoding}"
-     */
-    private String encoding;
-
-    /**
-     * Entities (expressed as a perl5 regex) to exclude from template
-     * generation. (Default is to include all entities in the DataMap).
-     *
-     * @parameter expression="${cgen.excludeEntitiesPattern}"
-     */
-    private String excludeEntitiesPattern;
-
-    /**
-     * Entities (expressed as a perl5 regex) to include in template
-     * generation. (Default is to include all entities in the DataMap).
-     *
-     * @parameter expression="${cgen.includeEntitiesPattern}"
-     */
-    private String includeEntitiesPattern;
-
-    /**
-     * If set to <code>true</code>, will generate subclass/superclass pairs,
-     * with all generated code included in superclass (default is <code>true</code>).
-     *
-     * @parameter expression="${cgen.makePairs}" default-value="true"
-     */
-    private boolean makePairs;
-
-    /**
-     * DataMap XML file to use as a base for class generation.
-     *
-     * @parameter expression="${cgen.map}"
-     * @required
-     */
-    private File map;
-
-    /**
-     * Specifies generator iteration target. &quot;entity&quot; performs one iteration
for each selected entity.
-     * &quot;datamap&quot; performs one iteration per datamap (This is always one
iteration since cgen
-     * currently supports specifying one-and-only-one datamap). (Default is &quot;entity&quot;)
-     *
-     * @parameter expression="${cgen.mode}" default-value="entity"
-     */
-    private String mode;
-
-    /**
-     * Name of file for generated output. (Default is &quot;*.java&quot;)
-     *
-     * @parameter expression="${cgen.outputPattern}" default-value="*.java"
-     */
-    private String outputPattern;
-
-    /**
-     * 	If set to <code>true</code>, will overwrite older versions of generated
classes.
-     *  Ignored unless makepairs is set to <code>false</code>.
-     *
-     * @parameter expression="${cgen.overwrite}" default-value="false"
-     */
-    private boolean overwrite;
-
-    /**
-     * Java package name of generated superclasses. Ignored unless <code>makepairs</code>
-     * set to <code>true</code>.  If omitted, each superclass will be assigned
the same
-     * package as subclass. Note that having superclass in a different package would only
-     * make sense when <code>usepkgpath</code> is set to <code>true</code>.
Otherwise
-     * classes from different packages will end up in the same directory.
-     *
-     * @parameter expression="${cgen.superPkg}"
-     */
-    private String superPkg;
-
-    /**
-     * Location of Velocity template file for Java superclass generation. Ignored unless
-     * <code>makepairs</code> set to <code>true</code>. If omitted,
default template is used.
-     *
-     * @parameter expression="${cgen.superTemplate}"
-     */
-    private String superTemplate;
-
-    /**
-     * Location of Velocity template file for Java class generation. If omitted, default
template is used.
-     *
-     * @parameter expression="${cgen.template}"
-     */
-    private String template;
-
-    /**
-     * If set to <code>true</code> (default), a directory tree will be generated
in "destDir"
-     * corresponding to the class package structure, if set to <code>false</code>,
classes will
-     * be generated in &quot;destDir&quot; ignoring their package.
-     *
-     * @parameter expression="${cgen.usePkgPath}" default-value="true"
-     */
-    private boolean usePkgPath;
-
-    /**
-     * Specifies template location and generator behavior. &quot;1.1&quot; is the
old behavior,
-     * with templates located in &quot;dotemplates&quot; and &quot;classgen&quot;
as the only
-     * velocity context attribute. &quot;1.2&quot; is the new behavior, with templates
located
-     * in &quot;dotemplates/v1.2&quot; and &quot;objEntity&quot;, &quot;entityUtils&quot;,
-     * &quot;stringUtils&quot;, and &quot;importUtils&quot; in the velocity
context. (Default is &quot;1.2&quot;.)
-     *
-     * @parameter expression="${cgen.version}" default-value="1.1"
-     */
-    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 {
-        	generatorUtil.setAdditionalMaps(convertAdditionalDataMaps());
-            generatorUtil.execute();
-        }
-        catch (Exception e) {
-            throw new MojoExecutionException("Error generating classes: ", e);
-        }
-    }
-
-    /**
-     * Loads and returns DataMap based on <code>map</code> attribute.
-     */
-    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();
-        File[] dataMaps = new File[maps.length];
-        for (int i = 0; i < maps.length; i++) {
-            dataMaps[i] = new File(maps[i]);
-        }
-        return dataMaps;
-    }
-
-    /**
-     * Factory method to create internal class generator. Called from constructor.
-     */
-    protected DefaultClassGenerator createGenerator() {
-        DefaultClassGenerator gen = new DefaultClassGenerator();
-
-        gen.setClient(client);
-        gen.setDestDir(destDir);
-        gen.setEncoding(encoding);
-        gen.setMakePairs(makePairs);
-        gen.setMode(mode);
-        gen.setOutputPattern(outputPattern);
-        gen.setOverwrite(overwrite);
-        gen.setSuperPkg(superPkg);
-        gen.setSuperTemplate(superTemplate);
-        gen.setTemplate(template);
-        gen.setUsePkgPath(usePkgPath);
-        gen.setVersionString(version);
+public class CayenneGeneratorMojo extends AbstractMojo {
+	/**
+	 * Project instance used to add generated source code to the build.
+	 * 
+	 * @parameter default-value="${project}"
+	 * @required
+	 * @readonly
+	 */
+	private MavenProject project;
+
+	/**
+	 * Path to additional DataMap XML files to use for class generation.
+	 * 
+	 * @parameter expression="${cgen.additionalMaps}"
+	 */
+	private File additionalMaps;
+
+	/**
+	 * Whether we are generating classes for the client tier in a Remote Object
+	 * Persistence application. Default is <code>false</code>.
+	 * 
+	 * @parameter expression="${cgen.client}" default-value="false"
+	 */
+	private boolean client;
+
+	/**
+	 * Destination directory for Java classes (ignoring their package names).
+	 * 
+	 * @parameter expression="${cgen.destDir}"
+	 *            default-value="${project.build.sourceDirectory}/java/generated-sources/cayenne"
+	 */
+	private File destDir;
+
+	/**
+	 * Specify generated file encoding if different from the default on current
+	 * platform. Target encoding must be supported by the JVM running Maven
+	 * build. Standard encodings supported by Java on all platforms are
+	 * US-ASCII, ISO-8859-1, UTF-8, UTF-16BE, UTF-16LE, UTF-16. See Sun Java
+	 * Docs for java.nio.charset.Charset for more information.
+	 * 
+	 * @parameter expression="${cgen.encoding}"
+	 */
+	private String encoding;
+
+	/**
+	 * Entities (expressed as a perl5 regex) to exclude from template
+	 * generation. (Default is to include all entities in the DataMap).
+	 * 
+	 * @parameter expression="${cgen.excludeEntitiesPattern}"
+	 */
+	private String excludeEntitiesPattern;
+
+	/**
+	 * Entities (expressed as a perl5 regex) to include in template generation.
+	 * (Default is to include all entities in the DataMap).
+	 * 
+	 * @parameter expression="${cgen.includeEntitiesPattern}"
+	 */
+	private String includeEntitiesPattern;
+
+	/**
+	 * If set to <code>true</code>, will generate subclass/superclass pairs,
+	 * with all generated code included in superclass (default is
+	 * <code>true</code>).
+	 * 
+	 * @parameter expression="${cgen.makePairs}" default-value="true"
+	 */
+	private boolean makePairs;
+
+	/**
+	 * DataMap XML file to use as a base for class generation.
+	 * 
+	 * @parameter expression="${cgen.map}"
+	 * @required
+	 */
+	private File map;
+
+	/**
+	 * Specifies generator iteration target. &quot;entity&quot; performs one
+	 * iteration for each selected entity. &quot;datamap&quot; performs one
+	 * iteration per datamap (This is always one iteration since cgen currently
+	 * supports specifying one-and-only-one datamap). (Default is
+	 * &quot;entity&quot;)
+	 * 
+	 * @parameter expression="${cgen.mode}" default-value="entity"
+	 */
+	private String mode;
+
+	/**
+	 * Name of file for generated output. (Default is &quot;*.java&quot;)
+	 * 
+	 * @parameter expression="${cgen.outputPattern}" default-value="*.java"
+	 */
+	private String outputPattern;
+
+	/**
+	 * If set to <code>true</code>, will overwrite older versions of
+	 * generated classes. Ignored unless makepairs is set to <code>false</code>.
+	 * 
+	 * @parameter expression="${cgen.overwrite}" default-value="false"
+	 */
+	private boolean overwrite;
+
+	/**
+	 * Java package name of generated superclasses. Ignored unless
+	 * <code>makepairs</code> set to <code>true</code>. If omitted,
each
+	 * superclass will be assigned the same package as subclass. Note that
+	 * having superclass in a different package would only make sense when
+	 * <code>usepkgpath</code> is set to <code>true</code>. Otherwise
+	 * classes from different packages will end up in the same directory.
+	 * 
+	 * @parameter expression="${cgen.superPkg}"
+	 */
+	private String superPkg;
+
+	/**
+	 * Location of Velocity template file for Java superclass generation.
+	 * Ignored unless <code>makepairs</code> set to <code>true</code>.
If
+	 * omitted, default template is used.
+	 * 
+	 * @parameter expression="${cgen.superTemplate}"
+	 */
+	private String superTemplate;
+
+	/**
+	 * Location of Velocity template file for Java class generation. If omitted,
+	 * default template is used.
+	 * 
+	 * @parameter expression="${cgen.template}"
+	 */
+	private String template;
+
+	/**
+	 * If set to <code>true</code> (default), a directory tree will be
+	 * generated in "destDir" corresponding to the class package structure, if
+	 * set to <code>false</code>, classes will be generated in
+	 * &quot;destDir&quot; ignoring their package.
+	 * 
+	 * @parameter expression="${cgen.usePkgPath}" default-value="true"
+	 */
+	private boolean usePkgPath;
+
+	/**
+	 * Specifies template location and generator behavior. &quot;1.1&quot; is
+	 * the old behavior, with templates located in &quot;dotemplates&quot; and
+	 * &quot;classgen&quot; as the only velocity context attribute.
+	 * &quot;1.2&quot; is the new behavior, with templates located in
+	 * &quot;dotemplates/v1.2&quot; and &quot;objEntity&quot;,
+	 * &quot;entityUtils&quot;, &quot;stringUtils&quot;, and
+	 * &quot;importUtils&quot; in the velocity context. (Default is
+	 * &quot;1.2&quot;.)
+	 * 
+	 * @parameter expression="${cgen.version}" default-value="1.1"
+	 */
+	private String version;
+
+	private DefaultClassGenerator generator;
+
+	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();
+		}
 
-        return gen;
-    }
-}
+		generator = createGenerator();
 
-class MavenLogger implements ILog {
-	
-	private Log logger;
-	
-	public MavenLogger(AbstractMojo parent) {
-		this.logger = parent.getLog();
-	}
-	
-	public void log(String msg) {
-		logger.debug(msg);
+		ILog logger = new MavenLogger(this);
+		CayenneGenerationMapLoader mapLoader = new CayenneGenerationMapLoader();
+		mapLoader.setNameFilter(new NamePatternMatcher(logger,
+				includeEntitiesPattern, excludeEntitiesPattern));
+		mapLoader.setMainDataMapFile(map);
+
+		try {
+			mapLoader.setAdditionalDataMapFiles(convertAdditionalDataMaps());
+
+			generator.setTimestamp(map.lastModified());
+			generator.setDataMap(mapLoader.getMainDataMap());
+			generator.setObjEntities((List<ObjEntity>) mapLoader
+					.getFilteredEntities());
+			generator.validateAttributes();
+			generator.execute();
+		} catch (Exception e) {
+			throw new MojoExecutionException("Error generating classes: ", e);
+		}
 	}
 
-	public void log(String msg, int msgLevel) {
-		
-		switch (msgLevel) {
-			case ILog.MSG_DEBUG:
-				logger.debug(msg);
-				break;
-				
-			case ILog.MSG_ERR:
-				logger.error(msg);
-				break;
-				
-			case ILog.MSG_INFO:
-				logger.info(msg);
-				break;
-				
-			case ILog.MSG_VERBOSE:
-				logger.info(msg);
-				break;
-				
-			case ILog.MSG_WARN:
-				logger.warn(msg);
-				break;
-				
-			default:
-				logger.debug(msg);
+	/**
+	 * Loads and returns DataMap based on <code>map</code> attribute.
+	 */
+	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();
+		File[] dataMaps = new File[maps.length];
+		for (int i = 0; i < maps.length; i++) {
+			dataMaps[i] = new File(maps[i]);
 		}
+		return dataMaps;
 	}
-}
\ No newline at end of file
+
+	/**
+	 * Factory method to create internal class generator. Called from
+	 * constructor.
+	 */
+	protected DefaultClassGenerator createGenerator() {
+		DefaultClassGenerator gen = new DefaultClassGenerator();
+
+		gen.setClient(client);
+		gen.setDestDir(destDir);
+		gen.setEncoding(encoding);
+		gen.setMakePairs(makePairs);
+		gen.setMode(mode);
+		gen.setOutputPattern(outputPattern);
+		gen.setOverwrite(overwrite);
+		gen.setSuperPkg(superPkg);
+		gen.setSuperTemplate(superTemplate);
+		gen.setTemplate(template);
+		gen.setUsePkgPath(usePkgPath);
+		gen.setVersionString(version);
+
+		return gen;
+	}
+}

Added: cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/MavenLogger.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/MavenLogger.java?rev=600341&view=auto
==============================================================================
--- cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/MavenLogger.java
(added)
+++ cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/MavenLogger.java
Sun Dec  2 09:15:25 2007
@@ -0,0 +1,66 @@
+/*****************************************************************
+ *   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.cayenne.tools;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.logging.Log;
+
+/**
+ * @since 3.0
+ */
+class MavenLogger implements ILog {
+
+	private Log logger;
+
+	public MavenLogger(AbstractMojo parent) {
+		this.logger = parent.getLog();
+	}
+
+	public void log(String msg) {
+		logger.debug(msg);
+	}
+
+	public void log(String msg, int msgLevel) {
+
+		switch (msgLevel) {
+		case ILog.MSG_DEBUG:
+			logger.debug(msg);
+			break;
+
+		case ILog.MSG_ERR:
+			logger.error(msg);
+			break;
+
+		case ILog.MSG_INFO:
+			logger.info(msg);
+			break;
+
+		case ILog.MSG_VERBOSE:
+			logger.info(msg);
+			break;
+
+		case ILog.MSG_WARN:
+			logger.warn(msg);
+			break;
+
+		default:
+			logger.debug(msg);
+		}
+	}
+}
\ No newline at end of file



Mime
View raw message