001: package net.sourceforge.jaxor.parser;
002:
003: import com.thoughtworks.qdox.model.JavaClass;
004: import net.sourceforge.jaxor.util.SystemException;
005:
006: import java.io.File;
007: import java.util.Collections;
008: import java.util.Iterator;
009: import java.util.List;
010: import java.util.Properties;
011:
012: import org.apache.velocity.runtime.log.LogSystem;
013:
014: /*
015: * User: Mike
016: * Date: Oct 22, 2002
017: * Time: 12:39:56 AM
018: */
019:
020: public class Jaxor {
021: private Entity _entity;
022: private ImportSet _imports = new ImportSet();
023: private String _package;
024: private Generator _config;
025: private File _destDir;
026: private Properties _mapperProps;
027: private SourceMap _javaDocBuilder;
028: private String _source;
029: private net.sourceforge.jaxor.api.MapperRegistry _mapperRegistry = new net.sourceforge.jaxor.impl.MapperRegistryImpl();
030:
031: public void addImport(Import imp) {
032: _imports.add(imp);
033: }
034:
035: public java.util.Set getImports() {
036: ImportSet set = getImportsSet();
037: return set.getSet();
038: }
039:
040: private ImportSet getImportsSet() {
041: ImportSet set = new ImportSet();
042: set.add(_imports);
043: return set;
044: }
045:
046: public ImportSet getImportsWithPackage() {
047: ImportSet set = getImportsSet();
048: set.add(_package + ".*");
049: return set;
050: }
051:
052: public void setPackage(String aPackage) {
053: _package = aPackage;
054: }
055:
056: public void addEntity(Entity entity) {
057: if (_entity != null)
058: throw new SystemException(
059: "Only one entity allowed per jaxor file");
060: _entity = entity;
061: }
062:
063: public String getPackageNameAsDir() {
064: return _package.replace('.', File.separatorChar);
065: }
066:
067: public String getPackageName() {
068: return _package;
069: }
070:
071: public void setConfig(Generator config) {
072: _config = config;
073: }
074:
075: public void setDestDir(File dir) {
076: _destDir = dir;
077: }
078:
079: public void execute(EntityCollection coll, LogSystem logger) {
080: Entity entity = getEntity();
081: Iterator it = _config.getAttributes().iterator();
082: while (it.hasNext()) {
083: entity.addAttribute((Attribute) it.next());
084: }
085: File destdir = new File(_destDir.getPath() + File.separator
086: + getPackageNameAsDir());
087: long timestamp = getLastModified();
088: SourceGenerator gen = new SourceGenerator(this , destdir,
089: timestamp, logger);
090: gen.addToContext("entityCollection", coll);
091: gen.add("Finder.vm", getEntity().getFinderName());
092: gen.add("Impl.vm", getEntity().getImplName());
093: gen.add("Interface.vm", getEntity().getInterfaceName());
094: gen.add("List.vm", getEntity().getGeneratedListName());
095: gen.add("Iterator.vm", getEntity().getIteratorName());
096: gen.add("FinderImpl.vm", getEntity().getFinderImpl());
097: gen.add("Meta.vm", getEntity().getMetaName());
098: gen.add("EntityRow.vm", getEntity().getEntityRowName());
099: gen.add("ResultSet.vm", getEntity().getEntityResultSetClass());
100: gen.add("QueryResult.vm", getEntity()
101: .getEntityQueryResultClass());
102: gen.generate();
103: }
104:
105: private long getLastModified() {
106: String impl = getEntity().getImpl();
107: long metaModified = new File(_source).lastModified();
108: if (impl == null)
109: return metaModified;
110: else {
111: long timestamp = _javaDocBuilder.getTimestamp(impl);
112: return timestamp > metaModified ? timestamp : metaModified;
113: }
114: }
115:
116: public SourceClass getImplClass() {
117: String impl = getEntity().getImpl();
118: if (impl == null)
119: return null;
120: JavaClass daClass = _javaDocBuilder.get(impl);
121: if (daClass != null)
122: return new SourceClass(daClass);
123: throw new SystemException("Unable to find 'impl' class for "
124: + getEntity().getTableName() + " class " + impl);
125: }
126:
127: public List getImplMethods() {
128: SourceClass daClass = getImplClass();
129: if (daClass == null)
130: return Collections.EMPTY_LIST;
131: return daClass.getImplMethods();
132: }
133:
134: public void setMapperProps(Properties mapperProps) {
135: _mapperProps = mapperProps;
136: }
137:
138: public Entity getEntity() {
139: return _entity;
140: }
141:
142: public List getFields(EntityCollection coll) {
143: return getEntity().getFields(coll, this );
144: }
145:
146: public List getForeignFields(EntityCollection coll) {
147: return getEntity().getForeignFields(coll, this );
148: }
149:
150: public String getMapperName(String type) {
151: String map = (String) _mapperProps.get(type);
152: if (map == null)
153: map = _mapperRegistry.getName(type);
154: if (map == null)
155: throw new SystemException(
156: "Mapper not specified and mapper not found in registry for: "
157: + type);
158: return map;
159: }
160:
161: public String getExtends() {
162: return _config.getExtends(_entity);
163: }
164:
165: public String getImplements() {
166: return _config.getImplements(_entity);
167: }
168:
169: public List getQueries() {
170: return getEntity().getQueries(this );
171: }
172:
173: public void setJavaDocBuilders(SourceMap javaDocBuilders) {
174: _javaDocBuilder = javaDocBuilders;
175: }
176:
177: public void setSource(String src) {
178: _source = src;
179: }
180:
181: public String getFullName() {
182: return getPackageName() + "." + getEntity().getTableName();
183: }
184:
185: public Attribute findAttribute(String key) {
186: return getEntity().findAttribute(key);
187: }
188:
189: public String getListClassFull() {
190: return getPackageName() + "." + getEntity().getListClass();
191: }
192:
193: public String getEntityClassFull() {
194: return getPackageName() + "." + getEntity().getInterfaceName();
195: }
196:
197: public String getFinderImplFull() {
198: return getPackageName() + "." + getEntity().getFinderImpl();
199: }
200:
201: }
|