View Javadoc
1   package org.melati.poem.prepro;
2   
3   /*
4    * $Source$
5    * $Revision$
6    *
7    * Copyright (C) 2006 Tim Pizey
8    *
9    * Part of Melati (http://melati.org), a framework for the rapid
10   * development of clean, maintainable web applications.
11   *
12   * Melati is free software; Permission is granted to copy, distribute
13   * and/or modify this software under the terms either:
14   *
15   * a) the GNU General Public License as published by the Free Software
16   *    Foundation; either version 2 of the License, or (at your option)
17   *    any later version,
18   *
19   *    or
20   *
21   * b) any version of the Melati Software License, as published
22   *    at http://melati.org
23   *
24   * You should have received a copy of the GNU General Public License and
25   * the Melati Software License along with this program;
26   * if not, write to the Free Software Foundation, Inc.,
27   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
28   * GNU General Public License and visit http://melati.org to obtain the
29   * Melati Software License.
30   *
31   * Feel free to contact the Developers of Melati (http://melati.org),
32   * if you would like to work out a different arrangement than the options
33   * outlined here.  It is our intention to allow Melati to be used by as
34   * wide an audience as possible.
35   *
36   * This program is distributed in the hope that it will be useful,
37   * but WITHOUT ANY WARRANTY; without even the implied warranty of
38   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39   * GNU General Public License for more details.
40   *
41   * Contact details for copyright holder:
42   *
43   *     Tim Pizey <timp At paneris.org>
44   *     http://paneris.org/~timp
45   */
46  
47  import org.apache.maven.plugin.AbstractMojo;
48  import org.apache.maven.plugin.MojoExecutionException;
49  
50  import java.io.File;
51  /**
52   * Process a DSD file.
53   * 
54   * @requiresDependencyResolution compile
55   * @goal generate
56   * @description Process a DSD file to generate java sources.
57   * @phase process-sources
58   */
59  public class MelatiDsdProcessorMojo extends AbstractMojo {
60    /**
61     * Location of the dsd.
62     * 
63     * @parameter expression=
64     */
65    private String dsdPackage;
66  
67    /**
68     * DSD file name.
69     * 
70     * @parameter expression=
71     */
72    private String dsdFile;
73  
74    /**
75     * Location of the source.
76     * 
77     * @parameter expression="${project.build.sourceDirectory}"
78     * @required
79     */
80    private File sourceDirectory;
81  
82    /**
83     * Location of the test source.
84     * 
85     * @parameter expression="${project.build.testSourceDirectory}"
86     * @required
87     */
88    private File testSourceDirectory;
89  
90    /**
91     * @parameter expression="${project.groupId}"
92     * @required
93     */
94    private String groupId;
95  
96    /**
97     * @parameter expression="${project.artifactId}"
98     * @required
99     */
100   private String artifactId;
101   
102   /**
103    * @parameter expression="${checkUptodate}" default-value=true
104    */
105   private boolean checkUptodate;
106   
107   /**
108    * @parameter expression="${isMain}" default-value=true
109    */
110   private boolean isMain = true;
111   
112 
113   private String searchedLocations = "";
114 
115   public void execute()
116       throws MojoExecutionException {
117     
118     //Get the System Classloader
119     ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();
120 
121     File f = null;
122     if(isMain) 
123       f = sourceDirectory;
124     else
125       f = testSourceDirectory;
126     if (f == null || !f.exists()) {
127       throw new MojoExecutionException("Source directory (" + f + ")could not be found");
128     }
129 
130     String dsdPath = null;
131     if (dsdPackage != null) {
132       String lookupDir = f.getPath() + "/"
133           + dsdPackage.replace('.', '/') + "/";
134       dsdPath = dsdFileName(lookupDir, dsdFile);
135       if (dsdPath == null) {
136         if (dsdFile != null) {
137           throw new MojoExecutionException("Configured DSD file " + lookupDir
138               + dsdFile + " could not be found.");
139         } else {
140           throw new MojoExecutionException(
141               "DSD file could not be found on configured path "
142               + dsdPackage
143               + " in any of: \n"
144               + searchedLocations
145               + "Add an explicit dsdPackage and/or dsdFile parameter to your configuration.");
146 
147         }
148       }
149     } else {
150       String groupDir = groupId.replace('.', '/');
151       String sourceDir = f.getPath() + "/" + groupDir + "/";
152       String foundDsdName = existingDsdFileName(sourceDir, dsdFile);
153       if (foundDsdName == null)
154         foundDsdName = existingDsdFileName(f + artifactId + "/",
155             dsdFile);
156       if (foundDsdName == null)
157         throw new MojoExecutionException(
158              "DSD file could not be found in any of: \n"
159              + searchedLocations
160              + "Add an explicit dsdPackage and/or dsdFile parameter to your configuration.");
161       getLog().info("Found DSD at " + foundDsdName + ":");
162       dsdPath = foundDsdName;
163     }
164     String modelDir = dsdPath.substring(0, dsdPath.lastIndexOf('/') + 1);
165     String modelName = capitalised(dsdPath.substring(dsdPath.lastIndexOf('/') + 1, dsdPath
166         .lastIndexOf('.')));
167     String databaseTablesFileName = modelDir + "generated/" + modelName
168         + "DatabaseBase.java";
169 
170     File databaseTablesFile = new File(databaseTablesFileName);
171     long dsdTimestamp = new File(dsdPath).lastModified();
172     long databaseTablesTimestamp = 1;
173     if (databaseTablesFile.exists()) {
174       databaseTablesTimestamp = databaseTablesFile.lastModified();
175     }
176     boolean doIt = true;
177     if (checkUptodate) { 
178       getLog().info(" Checking " + databaseTablesFileName);
179       if (dsdTimestamp < databaseTablesTimestamp) {
180         getLog().info("Generated files are uptodate. No action required.");
181         doIt = false;
182       } 
183     } else 
184       getLog().info(" Not checking - doing regardless");
185     if (doIt) {
186       DSD dsd;
187       try {
188         dsd = new DSD(dsdPath);
189         dsd.generateJava();
190       } catch (Exception e) {
191         throw new MojoExecutionException("Error processing DSD", e);
192       }
193     }
194   }
195 
196   private String existingDsdFileName(String dir, String dsdFileName) {
197     String modelDirName = dir + "poem/";
198     File modelDir = new File(modelDirName);
199     if (!modelDir.exists()) {
200       searchedLocations += " " + modelDirName + "\n";
201       modelDirName = dir + "model/";
202       modelDir = new File(modelDirName);
203     }
204     if (!modelDir.exists()) {
205       searchedLocations += " " + modelDirName + "\n";
206       modelDirName = dir;
207       modelDir = new File(modelDirName);
208     }
209     if (!modelDir.exists()) {
210       searchedLocations += " " + modelDirName + "\n";
211       return null;
212     }
213     return dsdFileName(modelDirName, dsdFileName);
214   }
215 
216   private String dsdFileName(String dir, String dsdFileName) {
217     if (dsdFileName != null) {
218       dsdFileName = dir + dsdFileName;
219       File foundDsdFile = new File(dsdFileName);
220       if ( !foundDsdFile.exists()) {
221         searchedLocations += " " + dsdFileName + "\n";
222         return null;
223       }
224     } else {
225       dsdFileName = dir + artifactId + ".dsd";
226       File foundDsdFile = new File(dsdFileName);
227       if (!foundDsdFile.exists()) {
228         searchedLocations += " " + dsdFileName + "\n";
229         dsdFileName = dir + capitalised(artifactId) + ".dsd";
230         foundDsdFile = new File(dsdFileName);
231       }
232       if (!foundDsdFile.exists()) {
233         searchedLocations += " " + dsdFileName + "\n";
234         return null;
235       }
236     }
237     return dsdFileName;
238   }
239 
240   /**
241    * Capitalise the first character of the input string.
242    * 
243    * @param name the string to capitalise
244    * @return the capitalised string
245    */
246   public static String capitalised(String name) {
247     char suffix[] = name.toCharArray();
248     suffix[0] = Character.toUpperCase(suffix[0]);
249     return new String(suffix);
250   }
251 }