001 package org.apache.torque.mojo; 002 003 import static org.apache.commons.io.FileUtils.forceMkdir; 004 005 import java.io.File; 006 import java.io.FileInputStream; 007 import java.io.FileOutputStream; 008 import java.io.IOException; 009 import java.util.ArrayList; 010 import java.util.List; 011 012 import org.apache.maven.plugin.MojoExecutionException; 013 import org.apache.torque.mojo.morph.DataMorpher; 014 import org.apache.torque.mojo.morph.MorphRequest; 015 import org.apache.torque.mojo.morph.Morpher; 016 import org.codehaus.plexus.util.DirectoryScanner; 017 import org.codehaus.plexus.util.StringUtils; 018 import org.kuali.core.db.torque.PrettyPrint; 019 import org.kuali.core.db.torque.Utils; 020 021 /** 022 * Converts Ant impex data XML files into maven-impex-plugin data XML files 023 * 024 * @goal morphdata 025 * @phase generate-sources 026 */ 027 public class MorphDataMojo extends BaseMojo { 028 Utils utils = new Utils(); 029 030 /** 031 * The directory in which the morphed XML will be generated. 032 * 033 * @parameter expression="${newDataOutputDir}" default-value="${project.build.directory}/generated-impex/xml" 034 * @required 035 */ 036 private File newDataOutputDir; 037 038 /** 039 * The directory containing the source (non-morphed) data XML files 040 * 041 * @parameter expression="${oldDataXMLDir}" default-value="${basedir}/src/main/impex" 042 * @required 043 */ 044 private File oldDataXMLDir; 045 046 /** 047 * The default set of files in that directory to include (ant style notation) 048 * 049 * @parameter expression="${oldDataXMLIncludes}" default-value="*.xml" 050 * @required 051 */ 052 private String oldDataXMLIncludes; 053 054 /** 055 * The default set of files in that directory to exclude (ant style notation) 056 * 057 * @parameter expression="${oldDataXMLExcludes}" default-value="schema.xml" 058 */ 059 private String oldDataXMLExcludes; 060 061 /** 062 * Use our configuration to determine the list of files we need to convert 063 */ 064 protected String[] getOldFiles() throws IOException { 065 DirectoryScanner ds = new DirectoryScanner(); 066 ds.setIncludes(new String[] { getOldDataXMLIncludes() }); 067 if (getOldDataXMLExcludes() != null) { 068 ds.setExcludes(new String[] { getOldDataXMLExcludes() }); 069 } 070 ds.setBasedir(getOldDataXMLDir()); 071 ds.scan(); 072 return ds.getIncludedFiles(); 073 } 074 075 protected boolean isMorphNeeded(final File oldFile, final File newFile) { 076 if (!newFile.exists()) { 077 return true; 078 } 079 080 long lastModifiedOld = oldFile.lastModified(); 081 long lastModifiedNew = newFile.lastModified(); 082 return lastModifiedOld > lastModifiedNew; 083 } 084 085 protected List<MorphRequest> getMorphRequests(final String[] oldFiles) throws IOException { 086 String inputPath = getOldDataXMLDir().getAbsolutePath(); 087 String outputPath = getNewDataOutputDir().getAbsolutePath(); 088 forceMkdir(getNewDataOutputDir()); 089 List<MorphRequest> requests = new ArrayList<MorphRequest>(); 090 for (String oldFilename : oldFiles) { 091 String oldFilePath = inputPath + FS + oldFilename; 092 String newFilePath = outputPath + FS + oldFilename; 093 File oldFile = new File(oldFilePath); 094 File newFile = new File(newFilePath); 095 if (!isMorphNeeded(oldFile, newFile)) { 096 getLog().debug("Skipping morph on " + oldFilename); 097 continue; 098 } 099 MorphRequest request = new MorphRequest(); 100 request.setEncoding(getEncoding()); 101 request.setName(oldFile.getName()); 102 request.setInputStream(new FileInputStream(oldFile)); 103 request.setOutputStream(new FileOutputStream(newFile)); 104 requests.add(request); 105 } 106 return requests; 107 } 108 109 protected void showConfig() { 110 getLog().info(StringUtils.repeat("-", utils.getDefaultPrintableConsoleWidth() - 7)); 111 getLog().info(" From: " + oldDataXMLDir.getAbsolutePath()); 112 getLog().info(" To: " + newDataOutputDir.getAbsolutePath()); 113 getLog().info("Include: " + oldDataXMLIncludes); 114 getLog().info("Exclude: " + oldDataXMLExcludes); 115 } 116 117 @Override 118 protected void executeMojo() throws MojoExecutionException { 119 try { 120 showConfig(); 121 String[] oldFiles = getOldFiles(); 122 PrettyPrint pp = new PrettyPrint("[INFO] Converting " + oldFiles.length + " data XML files"); 123 utils.left(pp); 124 List<MorphRequest> requests = getMorphRequests(oldFiles); 125 for (MorphRequest request : requests) { 126 Morpher morpher = new DataMorpher(request, getProject().getArtifactId()); 127 morpher.executeMorph(); 128 } 129 utils.right(pp); 130 int skipCount = oldFiles.length - requests.size(); 131 if (skipCount > 0) { 132 getLog().info("Skipped " + skipCount + " files that were unchanged."); 133 } 134 } catch (IOException e) { 135 throw new MojoExecutionException("Unexpected error", e); 136 } 137 } 138 139 public File getNewDataOutputDir() { 140 return newDataOutputDir; 141 } 142 143 public void setNewDataOutputDir(final File newDataOutputDir) { 144 this.newDataOutputDir = newDataOutputDir; 145 } 146 147 public File getOldDataXMLDir() { 148 return oldDataXMLDir; 149 } 150 151 public void setOldDataXMLDir(final File oldDataXMLDir) { 152 this.oldDataXMLDir = oldDataXMLDir; 153 } 154 155 public String getOldDataXMLIncludes() { 156 return oldDataXMLIncludes; 157 } 158 159 public void setOldDataXMLIncludes(final String oldDataXMLIncludes) { 160 this.oldDataXMLIncludes = oldDataXMLIncludes; 161 } 162 163 public String getOldDataXMLExcludes() { 164 return oldDataXMLExcludes; 165 } 166 167 public void setOldDataXMLExcludes(final String oldDataXMLExcludes) { 168 this.oldDataXMLExcludes = oldDataXMLExcludes; 169 } 170 }