KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > tools > CayenneGeneratorUtil


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.tools;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.cayenne.gen.DefaultClassGenerator;
30 import org.apache.cayenne.map.DataMap;
31 import org.apache.cayenne.map.EntityResolver;
32 import org.apache.cayenne.map.MapLoader;
33 import org.apache.cayenne.map.ObjEntity;
34 import org.apache.cayenne.util.Util;
35 import org.xml.sax.InputSource JavaDoc;
36
37 /**
38  * Utility class to perform class generation from data map. This class is used by
39  * ant and Maven plugins.
40  *
41  * @author Andrus Adamchik, Kevin Menard
42  * @since 3.0
43  */

44 class CayenneGeneratorUtil {
45
46     protected ILog logger;
47     
48     protected MapLoader mapLoader;
49     protected File JavaDoc map;
50     protected File JavaDoc additionalMaps[];
51     protected DefaultClassGenerator generator;
52     
53     protected String JavaDoc includeEntitiesPattern;
54     protected String JavaDoc excludeEntitiesPattern;
55     
56     /** Loads and returns a DataMap by File. */
57     public DataMap loadDataMap(File JavaDoc mapName) throws Exception JavaDoc {
58         InputSource JavaDoc in = new InputSource JavaDoc(mapName.toURL().toString());
59         if(mapLoader == null) {
60             mapLoader = new MapLoader();
61         }
62         return mapLoader.loadDataMap(in);
63     }
64
65     /** Loads and returns DataMap based on <code>map</code> attribute. */
66     public DataMap loadDataMap() throws Exception JavaDoc {
67         return loadDataMap(map);
68     }
69     
70     /** Loads and returns DataMap based on <code>map</code> attribute. */
71     protected DataMap[] loadAdditionalDataMaps() throws Exception JavaDoc {
72         if (null == additionalMaps) {
73             return new DataMap[0];
74         }
75
76         DataMap dataMaps[] = new DataMap[additionalMaps.length];
77         for (int i = 0; i < additionalMaps.length; i++) {
78             dataMaps[i] = loadDataMap(additionalMaps[i]);
79         }
80         return dataMaps;
81     }
82     
83     public void processMap() throws Exception JavaDoc {
84
85         DataMap dataMap = loadDataMap();
86         DataMap additionalDataMaps[] = loadAdditionalDataMaps();
87
88         // Create MappingNamespace for maps.
89
EntityResolver entityResolver = new EntityResolver(Collections.singleton(dataMap));
90         dataMap.setNamespace(entityResolver);
91         for (int i = 0; i < additionalDataMaps.length; i++) {
92             entityResolver.addDataMap(additionalDataMaps[i]);
93             additionalDataMaps[i].setNamespace(entityResolver);
94         }
95
96         Collection JavaDoc allEntities = dataMap.getObjEntities();
97         List JavaDoc filteredEntities = new ArrayList JavaDoc(allEntities.size());
98
99         // filter client entities
100
if (generator.isClient()) {
101             if (dataMap.isClientSupported()) {
102                 Iterator JavaDoc it = allEntities.iterator();
103                 while (it.hasNext()) {
104                     ObjEntity entity = (ObjEntity) it.next();
105                     if (entity.isClientAllowed()) {
106                         filteredEntities.add(entity);
107                     }
108                 }
109             }
110         }
111         else {
112             filteredEntities.addAll(allEntities);
113         }
114
115         // filter names according to the specified pattern
116
NamePatternMatcher namePatternMatcher = new NamePatternMatcher(
117                 logger,
118                 includeEntitiesPattern,
119                 excludeEntitiesPattern);
120         namePatternMatcher.filter(filteredEntities);
121
122         generator.setTimestamp(map.lastModified());
123         generator.setDataMap(dataMap);
124         generator.setObjEntities(filteredEntities);
125         generator.validateAttributes();
126         generator.execute();
127     }
128     
129     public void execute() throws Exception JavaDoc
130     {
131         try {
132             processMap();
133         }
134         catch (Throwable JavaDoc th) {
135             th = Util.unwindException(th);
136
137             String JavaDoc thMessage = th.getLocalizedMessage();
138             String JavaDoc message = "Error generating classes: ";
139             message += (!Util.isEmptyString(thMessage)) ? thMessage : th
140                     .getClass()
141                     .getName();
142
143             logger.log(message);
144             throw new Exception JavaDoc(message, th);
145         }
146     }
147
148     public void setAdditionalMaps(File JavaDoc[] additionalMaps) {
149         this.additionalMaps = additionalMaps;
150     }
151     
152     public void setExcludeEntitiesPattern(String JavaDoc excludeEntitiesPattern) {
153         this.excludeEntitiesPattern = excludeEntitiesPattern;
154     }
155     
156     public void setGenerator(DefaultClassGenerator generator) {
157         this.generator = generator;
158     }
159     
160     public void setIncludeEntitiesPattern(String JavaDoc includeEntitiesPattern) {
161         this.includeEntitiesPattern = includeEntitiesPattern;
162     }
163     
164     public void setLogger(ILog logger) {
165         this.logger = logger;
166     }
167     
168     public void setMap(File JavaDoc map) {
169         this.map = map;
170     }
171 }
172
Popular Tags