View Javadoc

1   package pl.aislib.tools.mapping;
2   
3   import java.io.File;
4   import java.io.IOException;
5   
6   import org.apache.tools.ant.BuildException;
7   import org.apache.tools.ant.Project;
8   import org.apache.tools.ant.Task;
9   
10  import pl.aislib.tools.mapping.structure.Database;
11  import pl.aislib.tools.mapping.structure.StructureParser;
12  
13  /***
14   * @author <a href="mailto:milosz@ais.pl">Milosz Tylenda</a>, AIS.PL
15   * @author <a href="mailto:wswiatek@ais.pl">Wojciech Swiatek</a>, AIS.PL
16   * @version $Revision: 1.7 $
17   */
18  public abstract class Generator extends Task {
19  
20    // Fields
21  
22    protected Database database;
23  
24    /***
25     * Name of base package.
26     */
27    protectedg> String packageName;
28  
29    /***
30     * Base directory files will be generated into.
31     */
32    protected File destinationDir;
33  
34    /***
35     * Mapping file with the structure.
36     */
37    protected File mappingFile;
38  
39    /***
40     * Defines subpackage of objects and objects.base.
41     */
42    protectedg> String objectsSubpackage = "objects";
43  
44    /***
45     * Defines subdirectory of objects and objects.base.
46     */
47    protected String objectsSubdir = "objects";
48  
49    /***
50     * Subpackage of database handlers.
51     */
52    protectedg> String dbHandlersSubpackage = "dbhandlers";
53  
54    /***
55     * Subdirectory of database handlers.
56     */
57    protected String dbHandlersSubdir = "dbhandlers";
58  
59    /***
60     * Subpackage of map handlers.
61     */
62    protectedg> String mapHandlersSubpackage = "handlers";
63  
64    /***
65     * Subdirectory of map handlers.
66     */
67    protected String mapHandlersSubdir = "handlers";
68  
69  
70    // Public methods
71  
72    /***
73     * Sets name of base package.
74     *
75     * @param packageName name of base package.
76     */
77    publicg> void setPackageName(String packageName) {
78      this.packageName = packageName;
79    }
80  
81    /***
82     * Sets base directory files will be generated into.
83     *
84     * @param destinationDir directory as object.
85     */
86    public void setDestinationDir(File destinationDir) {
87      this.destinationDir = destinationDir;
88    }
89  
90    /***
91     * Sets mapping file with the structure.
92     *
93     * @param mappingFile file as object.
94     */
95    public void setMappingFile(File mappingFile) {
96      this.mappingFile = mappingFile;
97    }
98  
99    /***
100    * Sets the subpackage of objects and objects.base.
101    *
102    * Defaults to "objects".
103    *
104    * @param objectsSubpackage subpackage of objects.
105    */
106   publicg> void setObjectsSubpackage(String objectsSubpackage) {
107     this.objectsSubpackage = objectsSubpackage;
108     objectsSubdir = packageToDirectory(objectsSubpackage);
109   }
110 
111   /***
112    * Sets the subpackage of database handlers.
113    *
114    * Defaults to "dbhandlers".
115    *
116    * @param dbHandlersSubpackage subpackage of database handlers.
117    */
118   publicg> void setDbHandlersSubpackage(String dbHandlersSubpackage) {
119     this.dbHandlersSubpackage = dbHandlersSubpackage;
120     dbHandlersSubdir = packageToDirectory(dbHandlersSubpackage);
121   }
122 
123   /***
124    * Sets the subpackage of map handlers.
125    *
126    * Defaults to "handlers".
127    *
128    * @param mapHandlersSubpackage subpackage of map handlers.
129    */
130   publicg> void setMapHandlersSubpackage(String mapHandlersSubpackage) {
131     this.mapHandlersSubpackage = mapHandlersSubpackage;
132     mapHandlersSubdir = packageToDirectory(mapHandlersSubpackage);
133   }
134 
135   /***
136    *
137    */
138   public void execute() throws BuildException {
139 
140     ifong> (packageName == null) {
141       throw new BuildException("packageName must be specified");
142     }
143     if (mappingFile == null) {
144       throw new BuildException("mappingFile must be specified");
145     }
146     if (destinationDir == null) {
147       throw new BuildException("destinationDir must be specified");
148     }
149     if (!destinationDir.exists()) {
150       throw new BuildException("directory: " + destinationDir + " doesn't exist");
151     }
152     if (!destinationDir.isDirectory()) {
153       throw new BuildException(destinationDir + "is not a directory");
154     }
155 
156     try {
157       StructureParser sp = new StructureParser(mappingFile);
158       database           = sp.createDatabase();
159     } catch (Exception e) {
160       throw new BuildException(e);
161     }
162     try {
163       if (database != null) {
164         generate();
165       } else {
166         log("Error: cannot parse mappingFile.", Project.MSG_ERR);
167       }
168     } catch (IOException ioe) {
169       throw new BuildException(ioe);
170     }
171   }
172 
173   public abstract void generate() throws IOException;
174 
175   /***
176    * Replaces all dots by file separator in the given String.
177    *
178    * @param pack <code>String</code> with dots.
179    * @return <code>String</code> with file separator.
180    */
181   privateg> String packageToDirectory(String pack) {
182     return pack.replace('.', File.separatorChar);
183   }
184 
185   /***
186    * It is overridden to allow use outside Ant.
187    *
188    * @param msg log message.
189    */
190   public void log(String msg) {
191     try {
192       super.log(msg);
193     } catch (RuntimeException e) {
194       System.out.println(msg);
195     }
196   }
197 
198 } // pl.aislib.tools.mapping.Generator class
199 
200 /***
201  * $Log: Generator.java,v $
202  * Revision 1.7  2004/08/11 12:41:01  wswiatek
203  * Database and map handlers location are now parametrizable.
204  * Maven plugin version changed to 1.0.1
205  *
206  * Revision 1.6  2004/01/19 14:37:42  milosz
207  * log method overridden.
208  *
209  */