1 19 20 package org.apache.cayenne.tools; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.Iterator ; 27 import java.util.List ; 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 ; 36 37 44 class CayenneGeneratorUtil { 45 46 protected ILog logger; 47 48 protected MapLoader mapLoader; 49 protected File map; 50 protected File additionalMaps[]; 51 protected DefaultClassGenerator generator; 52 53 protected String includeEntitiesPattern; 54 protected String excludeEntitiesPattern; 55 56 57 public DataMap loadDataMap(File mapName) throws Exception { 58 InputSource in = new InputSource (mapName.toURL().toString()); 59 if(mapLoader == null) { 60 mapLoader = new MapLoader(); 61 } 62 return mapLoader.loadDataMap(in); 63 } 64 65 66 public DataMap loadDataMap() throws Exception { 67 return loadDataMap(map); 68 } 69 70 71 protected DataMap[] loadAdditionalDataMaps() throws Exception { 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 { 84 85 DataMap dataMap = loadDataMap(); 86 DataMap additionalDataMaps[] = loadAdditionalDataMaps(); 87 88 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 allEntities = dataMap.getObjEntities(); 97 List filteredEntities = new ArrayList (allEntities.size()); 98 99 if (generator.isClient()) { 101 if (dataMap.isClientSupported()) { 102 Iterator 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 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 130 { 131 try { 132 processMap(); 133 } 134 catch (Throwable th) { 135 th = Util.unwindException(th); 136 137 String thMessage = th.getLocalizedMessage(); 138 String message = "Error generating classes: "; 139 message += (!Util.isEmptyString(thMessage)) ? thMessage : th 140 .getClass() 141 .getName(); 142 143 logger.log(message); 144 throw new Exception (message, th); 145 } 146 } 147 148 public void setAdditionalMaps(File [] additionalMaps) { 149 this.additionalMaps = additionalMaps; 150 } 151 152 public void setExcludeEntitiesPattern(String excludeEntitiesPattern) { 153 this.excludeEntitiesPattern = excludeEntitiesPattern; 154 } 155 156 public void setGenerator(DefaultClassGenerator generator) { 157 this.generator = generator; 158 } 159 160 public void setIncludeEntitiesPattern(String includeEntitiesPattern) { 161 this.includeEntitiesPattern = includeEntitiesPattern; 162 } 163 164 public void setLogger(ILog logger) { 165 this.logger = logger; 166 } 167 168 public void setMap(File map) { 169 this.map = map; 170 } 171 } 172 | Popular Tags |