View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.torque.mojo;
17  
18  import static org.apache.commons.io.FileUtils.forceMkdir;
19  
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.torque.mojo.morph.DataMorpher;
29  import org.apache.torque.mojo.morph.MorphRequest;
30  import org.apache.torque.mojo.morph.Morpher;
31  import org.codehaus.plexus.util.DirectoryScanner;
32  import org.codehaus.plexus.util.StringUtils;
33  import org.kuali.core.db.torque.PrettyPrint;
34  import org.kuali.core.db.torque.Utils;
35  
36  /**
37   * Converts Ant impex data XML files into maven-impex-plugin data XML files
38   *
39   * @goal morphdata
40   * @phase generate-sources
41   */
42  public class MorphDataMojo extends BaseMojo {
43      Utils utils = new Utils();
44  
45      /**
46       * The directory in which the morphed XML will be generated.
47       *
48       * @parameter expression="${newDataOutputDir}" default-value="${project.build.directory}/generated-impex/xml"
49       * @required
50       */
51      private File newDataOutputDir;
52  
53      /**
54       * The directory containing the source (non-morphed) data XML files
55       *
56       * @parameter expression="${oldDataXMLDir}" default-value="${basedir}/src/main/impex"
57       * @required
58       */
59      private File oldDataXMLDir;
60  
61      /**
62       * The default set of files in that directory to include (ant style notation)
63       *
64       * @parameter expression="${oldDataXMLIncludes}" default-value="*.xml"
65       * @required
66       */
67      private String oldDataXMLIncludes;
68  
69      /**
70       * The default set of files in that directory to exclude (ant style notation)
71       *
72       * @parameter expression="${oldDataXMLExcludes}" default-value="schema.xml"
73       */
74      private String oldDataXMLExcludes;
75  
76      /**
77       * Use our configuration to determine the list of files we need to convert
78       */
79      protected String[] getOldFiles() throws IOException {
80          DirectoryScanner ds = new DirectoryScanner();
81          ds.setIncludes(new String[] { getOldDataXMLIncludes() });
82          if (getOldDataXMLExcludes() != null) {
83              ds.setExcludes(new String[] { getOldDataXMLExcludes() });
84          }
85          ds.setBasedir(getOldDataXMLDir());
86          ds.scan();
87          return ds.getIncludedFiles();
88      }
89  
90      protected boolean isMorphNeeded(final File oldFile, final File newFile) {
91          if (!newFile.exists()) {
92              return true;
93          }
94  
95          long lastModifiedOld = oldFile.lastModified();
96          long lastModifiedNew = newFile.lastModified();
97          return lastModifiedOld > lastModifiedNew;
98      }
99  
100     protected List<MorphRequest> getMorphRequests(final String[] oldFiles) throws IOException {
101         String inputPath = getOldDataXMLDir().getAbsolutePath();
102         String outputPath = getNewDataOutputDir().getAbsolutePath();
103         forceMkdir(getNewDataOutputDir());
104         List<MorphRequest> requests = new ArrayList<MorphRequest>();
105         for (String oldFilename : oldFiles) {
106             String oldFilePath = inputPath + FS + oldFilename;
107             String newFilePath = outputPath + FS + oldFilename;
108             File oldFile = new File(oldFilePath);
109             File newFile = new File(newFilePath);
110             if (!isMorphNeeded(oldFile, newFile)) {
111                 getLog().debug("Skipping morph on " + oldFilename);
112                 continue;
113             }
114             MorphRequest request = new MorphRequest();
115             request.setEncoding(getEncoding());
116             request.setName(oldFile.getName());
117             request.setInputStream(new FileInputStream(oldFile));
118             request.setOutputStream(new FileOutputStream(newFile));
119             requests.add(request);
120         }
121         return requests;
122     }
123 
124     protected void showConfig() {
125         getLog().info(StringUtils.repeat("-", utils.getDefaultPrintableConsoleWidth() - 7));
126         getLog().info("   From: " + oldDataXMLDir.getAbsolutePath());
127         getLog().info("     To: " + newDataOutputDir.getAbsolutePath());
128         getLog().info("Include: " + oldDataXMLIncludes);
129         getLog().info("Exclude: " + oldDataXMLExcludes);
130     }
131 
132     @Override
133     protected void executeMojo() throws MojoExecutionException {
134         try {
135             showConfig();
136             String[] oldFiles = getOldFiles();
137             PrettyPrint pp = new PrettyPrint("[INFO] Converting " + oldFiles.length + " data XML files");
138             utils.left(pp);
139             List<MorphRequest> requests = getMorphRequests(oldFiles);
140             for (MorphRequest request : requests) {
141                 Morpher morpher = new DataMorpher(request, getProject().getArtifactId());
142                 morpher.executeMorph();
143             }
144             utils.right(pp);
145             int skipCount = oldFiles.length - requests.size();
146             if (skipCount > 0) {
147                 getLog().info("Skipped " + skipCount + " files that were unchanged.");
148             }
149         } catch (IOException e) {
150             throw new MojoExecutionException("Unexpected error", e);
151         }
152     }
153 
154     public File getNewDataOutputDir() {
155         return newDataOutputDir;
156     }
157 
158     public void setNewDataOutputDir(final File newDataOutputDir) {
159         this.newDataOutputDir = newDataOutputDir;
160     }
161 
162     public File getOldDataXMLDir() {
163         return oldDataXMLDir;
164     }
165 
166     public void setOldDataXMLDir(final File oldDataXMLDir) {
167         this.oldDataXMLDir = oldDataXMLDir;
168     }
169 
170     public String getOldDataXMLIncludes() {
171         return oldDataXMLIncludes;
172     }
173 
174     public void setOldDataXMLIncludes(final String oldDataXMLIncludes) {
175         this.oldDataXMLIncludes = oldDataXMLIncludes;
176     }
177 
178     public String getOldDataXMLExcludes() {
179         return oldDataXMLExcludes;
180     }
181 
182     public void setOldDataXMLExcludes(final String oldDataXMLExcludes) {
183         this.oldDataXMLExcludes = oldDataXMLExcludes;
184     }
185 }