KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > ant > taskdefs > DODS_XMLBuilder


1 /*
2  * Title: DODS Ant task
3  * Description: DODS
4  */

5 package org.enhydra.ant.taskdefs;
6
7 import java.io.File JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import org.apache.tools.ant.BuildException;
10 import org.apache.tools.ant.taskdefs.Touch;
11 import org.apache.tools.ant.types.EnumeratedAttribute;
12 import org.enhydra.dods.Common;
13
14 /**
15  * @author Sinisa Milosevic
16  * Invoke DODS to generate a set of java classes from a doml file<BR>
17  * The files will only be regenerated/compiled if the date on the doml file is
18  * newer than at least one of the generated files<BR>
19  * This taskdef extends Ant's <javac> task; refer to that documentation for
20  * parameters that affect compilation.<BR>
21  * Typically made visible to an Ant build file with the following declaration:<PRE>
22  * &lt;taskdef name="dods" classname="org.enhydra.ant.taskdefs.Dods"/&gt;</PRE>
23  *
24  * <B>Parameters</B><PRE>
25  *
26  * doml - The doml input file describing data object mapping. Required = Yes
27  * outdir - Target for generated classes, expressed as a directory path. Required = Yes
28  * tablesonly - If this attribute is set to true dods tast generate only tables.xml. Required = No
29  * force - Force DODS to always be regenerated source files. Required = No</PRE>
30  *<BR>
31  * Examples<PRE>
32  * &lt;dods doml="${basedir}/discRack.doml"
33  * outdir="${basedir}/src"/&gt;</PRE>
34  *
35  */

36 public class DODS_XMLBuilder extends org.apache.tools.ant.taskdefs.Javac {
37     protected File JavaDoc domlFile = null;
38     protected File JavaDoc sourceoutDir = null;
39     protected String JavaDoc database = null;
40     protected boolean forceBuild = false;
41     protected boolean tablesOnly = false;
42     protected String JavaDoc configDir = null;
43     public static class BooleanAttribute extends EnumeratedAttribute {
44
45         /**
46          * Set value for Boolean parameters
47          */

48         public String JavaDoc[] getValues() {
49             return (new String JavaDoc[] {"yes", "no", "true", "false" });
50         }
51
52         public BooleanAttribute() {}
53     }
54     public DODS_XMLBuilder() {
55         domlFile = null;
56         sourceoutDir = null;
57         tablesOnly = false;
58     }
59
60     /**
61      * Simply invoke DODS Generator, and then compile the generated files
62      * @exception org.apache.tools.ant.BuildException
63      */

64     public void execute() throws org.apache.tools.ant.BuildException {
65         
66         try {
67             if (configDir != null) {
68                 Common.setConfigDir(configDir);
69             }
70             
71         } catch (Error JavaDoc e) {
72             throw new BuildException("path to dodsConf.xml must be valid");
73         }
74         if (domlFile == null) {
75             throw new BuildException("doml attribute must be set!");
76         }
77         if (sourceoutDir == null) {
78             throw new BuildException("outdir attribute must be set!");
79         }
80         if (!sourceoutDir.isDirectory()) {
81             throw new BuildException("outdir must be a valid directory!");
82         }
83         File JavaDoc domlTmp = null;
84
85         if (domlTmp == null) {
86             String JavaDoc s = domlFile.getAbsolutePath().toLowerCase();
87             int i = s.indexOf(".doml");
88
89             if (i > -1) {
90                 domlTmp = new File JavaDoc(domlFile.getAbsolutePath().substring(0, i)
91                         + ".uptodate");
92             } else {
93                 domlTmp = new File JavaDoc(domlFile.getAbsolutePath() + ".uptodate");
94             }
95         }
96         boolean regen = !sourceoutDir.exists() || !domlTmp.exists()
97                 || domlTmp.lastModified() < domlFile.lastModified();
98
99         if (regen) {
100             (this).project.log("Regenerating DODS files from " + domlFile
101                     + "... ");
102         } else {
103             (this).project.log(String.valueOf(((java.lang.Object JavaDoc) (domlFile)))
104                     + " up to date.",
105                     2);
106         }
107         if (regen || forceBuild) {
108  
109             java.util.ArrayList JavaDoc argsList = new ArrayList JavaDoc();
110
111             argsList.add((new Boolean JavaDoc(tablesOnly)).toString());
112             if (database != null) {
113                 argsList.add(database);
114             }
115             try {
116                 String JavaDoc dodsClassName = "org.enhydra.dods.trans.TransientXMLBuilderFactory";
117                 java.lang.reflect.Method JavaDoc m = null;
118                 java.lang.Class JavaDoc c = null;
119
120                 try {
121                     c = java.lang.Class.forName(dodsClassName);
122                     m = c.getMethod("main", new java.lang.Class JavaDoc[] {
123                         String JavaDoc[].class
124                     });
125                 } catch (java.lang.Exception JavaDoc e) {
126                     ((java.lang.Throwable JavaDoc) (e)).printStackTrace();
127                     throw new BuildException(((java.lang.Throwable JavaDoc) (e)));
128                 }
129                 String JavaDoc args[] = (String JavaDoc[]) argsList.toArray(((java.lang.Object JavaDoc[]) (
130                         new String JavaDoc[argsList.size()])));
131
132                 if (m != null) {
133                     m.invoke(((java.lang.Object JavaDoc) (null)),
134                             new java.lang.Object JavaDoc[] {
135                         args
136                     });
137                 }
138             } catch (java.lang.reflect.InvocationTargetException JavaDoc ite) {
139                 ite.printStackTrace();
140                 throw new BuildException(((java.lang.Throwable JavaDoc) (ite)));
141             } catch (java.lang.Exception JavaDoc e) {
142                 ((java.lang.Throwable JavaDoc) (e)).printStackTrace();
143                 throw new BuildException(((java.lang.Throwable JavaDoc) (e)));
144             }
145             if (!tablesOnly) {
146                 org.apache.tools.ant.taskdefs.Touch touch = new Touch();
147
148                 ((org.apache.tools.ant.ProjectComponent) (touch)).setProject((this).project);
149                 touch.setFile(domlTmp);
150                 touch.execute();
151             }
152         }
153     }
154
155     /**
156      * Sets the doml descriptor file.
157      * @param domlFile - The doml input file describing data object mapping
158      */

159     public void setDoml(File JavaDoc domlFile) {
160         this.domlFile = domlFile;
161     }
162
163     /**
164      * The destination directory into which the generated sources are written
165      * @param sourceout - Target for generated classes, expressed as a directory
166      * path
167      */

168     public void setOutdir(File JavaDoc sourceout) {
169         sourceoutDir = sourceout;
170     }
171
172     /**
173      * Set the database vendor for creating sql
174      *
175      * @param database database vendor for creating sql
176      */

177     public void setDatabase(String JavaDoc database) {
178         this.database = database;
179     }
180
181     /**
182      * Sets tablesOnly property.
183      * @param isTablesOnly - new tablesOnly value.
184      */

185     public void setTablesonly(boolean isTablesOnly) {
186         tablesOnly = isTablesOnly;
187     }
188
189     /**
190      * Force DODS to always be regenerated source files
191      * @param force - Set to "true" or "false"
192      */

193     public void setForce(String JavaDoc force) {
194         forceBuild = (new Boolean JavaDoc(force)).booleanValue();
195     }
196
197     /**
198      * Returns curent configurationDir (folder with dodsConf.xml file).
199      * @return Curent configurationDir.
200      */

201     public String JavaDoc getConfigDir() {
202         return configDir;
203     }
204
205     /**
206      * @param string
207      */

208     public void setConfigDir(String JavaDoc string) {
209         configDir = string;
210     }
211 }
212
Popular Tags