net.sf.xsltmp.ManyToManyBase Maven / Gradle / Ivy
package net.sf.xsltmp;
import java.io.File;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import net.sf.xsltmp.util.AddSourcesUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
/**
* A base class for mojos with multiple destination files.
*/
public abstract class ManyToManyBase extends FromManyBase {
/**
* The destination directory. The output files are written based on this
* directory.
*
* @parameter
* expression="${project.build.directory}/generated-xml/xslt-generator/"
*/
private File destDir;
/**
* Where should the destination directory be added. Valid values are:
* sources, resources, test-sources, test-resources.
*
* This is a shortcut to avoid configuring the build-helper-maven-plugin.
*
* When adding to resources or test-resources, all .java files are excluded
* by default.
*
* @parameter
*/
private String addDestDirTo;
// Standard getters and setters for the properties
public File getDestDir() {
return destDir;
}
public void setDestDir(File destDir) {
this.destDir = destDir;
}
public String getAddDestDirTo() {
return addDestDirTo;
}
public void setAddDestDirTo(String addDestDirTo) {
this.addDestDirTo = addDestDirTo;
}
// Standard implementation of the transformation algorithm for multiple
// destination files.
/**
* The main MOJO execution method.
*
* Perform a generic XSL transformation algorithm. The mojo-specific parts
* are implemented in called abstract methods in the class descendants.
*/
public void execute() throws MojoExecutionException, MojoFailureException {
boolean didRun = false;
try {
if (!verifyXsltFileExist()) {
return;
}
if (!verifySrcDirExist()) {
return;
}
ensureDestDirExists();
if (shouldStopM2eBuild()) {
return;
}
boolean xslFileChanged = hasChanged(getXslFile());
String[] sourceFileNames = getSourceFiles();
for (int i = 0; i < sourceFileNames.length; i++) {
File srcFile = getSourceFile(sourceFileNames[i]);
File destFile = getDestFile(sourceFileNames[i]);
if (!xslFileChanged
&& (shouldSkip(srcFile, destFile) || !hasChanged(srcFile))) {
getLog().debug("File skipped: " + srcFile);
continue;
}
didRun = true;
ensureDestFileDirExists(destFile);
logExecution(srcFile);
getTransformer().transform(new StreamSource(srcFile),
new StreamResult(destFile));
cleanAfterFileTransformation(destFile);
}
if (!didRun) {
getLog().info("No sources to process.");
}
addDestDir();
} catch (MojoFailureException mfe) {
throw mfe;
} catch (Exception e) {
e.printStackTrace();
throw new MojoExecutionException(e.getMessage(), e);
}
getLog().info("Refreshing destDir: " + getDestDir());
getBuildContext().refresh(getDestDir());
}
// The abstract methods which defer implementation of the specifics of the
// transformation algorithm to descendants.
/**
* Return the destination file for a given source file name.
*
* @param sourceFileName
* @return File
*/
protected abstract File getDestFile(String sourceFileName);
/**
* Return true iff the transformation from the given source file to the
* given destination file should be skipped.
*
* @param srcFile
* @param destFile
* @return boolean
*/
protected abstract boolean shouldSkip(File srcFile, File destFile);
/**
* Perform any clean-up after a source file has been transformed into the
* given destination file.
*
* @param destFile
*/
protected void cleanAfterFileTransformation(File destFile) {
}
// Helper methods
protected void ensureDestDirExists() throws MojoFailureException {
if (!getDestDir().exists()) {
boolean dirCreationResult = getDestDir().mkdirs();
if (!dirCreationResult) {
throw new MojoFailureException(
"Destination directory structure could not be initialised. "
+ "Failed to create directory: " + getDestDir());
}
}
}
/**
* Add the destination directory to the project.
*/
protected void addDestDir() {
AddSourcesUtils addSourcesUtils = new AddSourcesUtils(getProject(),
getLog());
addSourcesUtils.addSources(getAddDestDirTo(), getDestDir());
addSourcesUtils.addResources(getAddDestDirTo(), getDestDir());
}
}