001 /** 002 * Copyright 2004-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.apache.torque.mojo; 017 018 import static org.apache.commons.io.FileUtils.forceMkdir; 019 020 import java.io.File; 021 import java.io.FileInputStream; 022 import java.io.FileOutputStream; 023 import java.io.IOException; 024 import java.util.ArrayList; 025 import java.util.List; 026 027 import org.apache.maven.plugin.MojoExecutionException; 028 import org.apache.torque.mojo.morph.DataMorpher; 029 import org.apache.torque.mojo.morph.MorphRequest; 030 import org.apache.torque.mojo.morph.Morpher; 031 import org.codehaus.plexus.util.DirectoryScanner; 032 import org.codehaus.plexus.util.StringUtils; 033 import org.kuali.core.db.torque.PrettyPrint; 034 import org.kuali.core.db.torque.Utils; 035 036 /** 037 * Converts Ant impex data XML files into maven-impex-plugin data XML files 038 * 039 * @goal morphdata 040 * @phase generate-sources 041 */ 042 public class MorphDataMojo extends BaseMojo { 043 Utils utils = new Utils(); 044 045 /** 046 * The directory in which the morphed XML will be generated. 047 * 048 * @parameter expression="${newDataOutputDir}" default-value="${project.build.directory}/generated-impex/xml" 049 * @required 050 */ 051 private File newDataOutputDir; 052 053 /** 054 * The directory containing the source (non-morphed) data XML files 055 * 056 * @parameter expression="${oldDataXMLDir}" default-value="${basedir}/src/main/impex" 057 * @required 058 */ 059 private File oldDataXMLDir; 060 061 /** 062 * The default set of files in that directory to include (ant style notation) 063 * 064 * @parameter expression="${oldDataXMLIncludes}" default-value="*.xml" 065 * @required 066 */ 067 private String oldDataXMLIncludes; 068 069 /** 070 * The default set of files in that directory to exclude (ant style notation) 071 * 072 * @parameter expression="${oldDataXMLExcludes}" default-value="schema.xml" 073 */ 074 private String oldDataXMLExcludes; 075 076 /** 077 * Use our configuration to determine the list of files we need to convert 078 */ 079 protected String[] getOldFiles() throws IOException { 080 DirectoryScanner ds = new DirectoryScanner(); 081 ds.setIncludes(new String[] { getOldDataXMLIncludes() }); 082 if (getOldDataXMLExcludes() != null) { 083 ds.setExcludes(new String[] { getOldDataXMLExcludes() }); 084 } 085 ds.setBasedir(getOldDataXMLDir()); 086 ds.scan(); 087 return ds.getIncludedFiles(); 088 } 089 090 protected boolean isMorphNeeded(final File oldFile, final File newFile) { 091 if (!newFile.exists()) { 092 return true; 093 } 094 095 long lastModifiedOld = oldFile.lastModified(); 096 long lastModifiedNew = newFile.lastModified(); 097 return lastModifiedOld > lastModifiedNew; 098 } 099 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 }