| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.taskdefs; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | import java.io.File; |
|---|
| 37 | import java.io.FileInputStream; |
|---|
| 38 | import java.io.FileOutputStream; |
|---|
| 39 | import java.io.IOException; |
|---|
| 40 | |
|---|
| 41 | import org.apache.tools.ant.BuildException; |
|---|
| 42 | import org.apache.tools.ant.DirectoryScanner; |
|---|
| 43 | import org.apache.tools.ant.Project; |
|---|
| 44 | import org.apache.tools.ant.Task; |
|---|
| 45 | import org.apache.tools.ant.types.FileSet; |
|---|
| 46 | import org.jcoderz.commons.util.IoUtil; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * Ant task that performs XSL transformation for a set of input files. |
|---|
| 51 | * |
|---|
| 52 | * @author Michael Griffel |
|---|
| 53 | */ |
|---|
| 54 | public class XsltBatchProcessor |
|---|
| 55 | extends Task |
|---|
| 56 | { |
|---|
| 57 | private String mStyleSheet = "default.xsl"; |
|---|
| 58 | |
|---|
| 59 | private FileSet mFiles = new FileSet(); |
|---|
| 60 | |
|---|
| 61 | private boolean mResolveExternalEntities = true; |
|---|
| 62 | |
|---|
| 63 | /** terminate ant build on error. */ |
|---|
| 64 | private boolean mFailOnError = false; |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | * Set whether we should fail on an error. |
|---|
| 68 | * |
|---|
| 69 | * @param b Whether we should fail on an error. |
|---|
| 70 | */ |
|---|
| 71 | public void setFailonerror (boolean b) |
|---|
| 72 | { |
|---|
| 73 | mFailOnError = b; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | /** |
|---|
| 77 | * Set the XSL Stylesheet to use. |
|---|
| 78 | * |
|---|
| 79 | * @param f The name of the XSL Stylesheet file. |
|---|
| 80 | * @see XsltBasedTask#getDefaultStyleSheet() |
|---|
| 81 | */ |
|---|
| 82 | public void setXsl (String f) |
|---|
| 83 | { |
|---|
| 84 | mStyleSheet = f; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * XML files that are used as input documents for the |
|---|
| 89 | * transformation. |
|---|
| 90 | * |
|---|
| 91 | * @param fs fileset of XML files. |
|---|
| 92 | */ |
|---|
| 93 | public void addFiles (FileSet fs) |
|---|
| 94 | { |
|---|
| 95 | mFiles = fs; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * {@inheritDoc} |
|---|
| 100 | */ |
|---|
| 101 | public void execute () |
|---|
| 102 | throws BuildException |
|---|
| 103 | { |
|---|
| 104 | try |
|---|
| 105 | { |
|---|
| 106 | final XsltBasedTask xsltTask = new XsltBasedTask() |
|---|
| 107 | { |
|---|
| 108 | String getDefaultStyleSheet () |
|---|
| 109 | { |
|---|
| 110 | return mStyleSheet; |
|---|
| 111 | } |
|---|
| 112 | }; |
|---|
| 113 | final Project myProject = getProject(); |
|---|
| 114 | final DirectoryScanner ds = mFiles.getDirectoryScanner(myProject); |
|---|
| 115 | final String[] includedFiles = ds.getIncludedFiles(); |
|---|
| 116 | log("Transforming " + includedFiles.length + " files in directory " |
|---|
| 117 | + ds.getBasedir()); |
|---|
| 118 | for (int i = 0; i < includedFiles.length; i++) |
|---|
| 119 | { |
|---|
| 120 | final String f = includedFiles[i]; |
|---|
| 121 | final File orig = new File(ds.getBasedir(), f); |
|---|
| 122 | final File out; |
|---|
| 123 | try |
|---|
| 124 | { |
|---|
| 125 | out = File.createTempFile("jcoderz", "tmp"); |
|---|
| 126 | } |
|---|
| 127 | catch (IOException e) |
|---|
| 128 | { |
|---|
| 129 | throw new BuildException( |
|---|
| 130 | "Failed to create temp file: " + e, e); |
|---|
| 131 | } |
|---|
| 132 | xsltTask.setProject(myProject); |
|---|
| 133 | xsltTask.setTaskName("xslt"); |
|---|
| 134 | xsltTask.setIn(orig); |
|---|
| 135 | xsltTask.setOut(out); |
|---|
| 136 | xsltTask.setDestdir(myProject.getBaseDir()); |
|---|
| 137 | xsltTask.setForce(true); |
|---|
| 138 | xsltTask.setFailonerror(mFailOnError); |
|---|
| 139 | xsltTask.setLogLevel(Project.MSG_VERBOSE); |
|---|
| 140 | xsltTask.resolveExternalEntities(mResolveExternalEntities); |
|---|
| 141 | log("Transforming file " + orig, Project.MSG_VERBOSE); |
|---|
| 142 | xsltTask.execute(); |
|---|
| 143 | if (out.exists()) |
|---|
| 144 | { |
|---|
| 145 | if (!orig.delete()) |
|---|
| 146 | { |
|---|
| 147 | throw new BuildException("Failed to delete " + orig); |
|---|
| 148 | } |
|---|
| 149 | if (!out.renameTo(orig)) |
|---|
| 150 | { |
|---|
| 151 | // try copy && delete |
|---|
| 152 | try |
|---|
| 153 | { |
|---|
| 154 | safeMove(out, orig); |
|---|
| 155 | } |
|---|
| 156 | catch (IOException e) |
|---|
| 157 | { |
|---|
| 158 | throw new BuildException("Failed to move file " |
|---|
| 159 | + out, e); |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | catch (BuildException e) |
|---|
| 166 | { |
|---|
| 167 | if (mFailOnError) |
|---|
| 168 | { |
|---|
| 169 | throw e; |
|---|
| 170 | } |
|---|
| 171 | log(e.getMessage(), Project.MSG_ERR); |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | /** |
|---|
| 176 | * If set to <tt>false</tt>, external entities will not be |
|---|
| 177 | * resolved. |
|---|
| 178 | * |
|---|
| 179 | * @param b new value. |
|---|
| 180 | */ |
|---|
| 181 | public void resolveExternalEntities (boolean b) |
|---|
| 182 | { |
|---|
| 183 | mResolveExternalEntities = b; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | private void safeMove (File source, File dest) |
|---|
| 187 | throws IOException |
|---|
| 188 | { |
|---|
| 189 | final FileInputStream in = new FileInputStream(source); |
|---|
| 190 | final FileOutputStream out = new FileOutputStream(dest); |
|---|
| 191 | try |
|---|
| 192 | { |
|---|
| 193 | IoUtil.copy(in, out); |
|---|
| 194 | if (!source.delete()) |
|---|
| 195 | { |
|---|
| 196 | throw new BuildException("Failed to delete " + source); |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| 199 | finally |
|---|
| 200 | { |
|---|
| 201 | IoUtil.close(in); |
|---|
| 202 | IoUtil.close(out); |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | } |
|---|