| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 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 | | | |
| 51 | | | |
| 52 | | | @author |
| 53 | | | |
| 54 | 0 | | public class XsltBatchProcessor |
| 55 | | | extends Task |
| 56 | | | { |
| 57 | 0 | | private String mStyleSheet = "default.xsl"; |
| 58 | | | |
| 59 | 0 | | private FileSet mFiles = new FileSet(); |
| 60 | | | |
| 61 | 0 | | private boolean mResolveExternalEntities = true; |
| 62 | | | |
| 63 | | | |
| 64 | 0 | | private boolean mFailOnError = false; |
| 65 | | | |
| 66 | | | |
| 67 | | | |
| 68 | | | |
| 69 | | | @param |
| 70 | | | |
| 71 | | | public void setFailonerror (boolean b) |
| 72 | | | { |
| 73 | 0 | | mFailOnError = b; |
| 74 | 0 | | } |
| 75 | | | |
| 76 | | | |
| 77 | | | |
| 78 | | | |
| 79 | | | @param |
| 80 | | | @see |
| 81 | | | |
| 82 | | | public void setXsl (String f) |
| 83 | | | { |
| 84 | 0 | | mStyleSheet = f; |
| 85 | 0 | | } |
| 86 | | | |
| 87 | | | |
| 88 | | | |
| 89 | | | |
| 90 | | | |
| 91 | | | @param |
| 92 | | | |
| 93 | | | public void addFiles (FileSet fs) |
| 94 | | | { |
| 95 | 0 | | mFiles = fs; |
| 96 | 0 | | } |
| 97 | | | |
| 98 | | | |
| 99 | | | {@inheritDoc} |
| 100 | | | |
| 101 | | | public void execute () |
| 102 | | | throws BuildException |
| 103 | | | { |
| 104 | | | try |
| 105 | | | { |
| 106 | 0 | | final XsltBasedTask xsltTask = new XsltBasedTask() |
| 107 | 0 | | { |
| 108 | | | String getDefaultStyleSheet () |
| 109 | | | { |
| 110 | 0 | | return mStyleSheet; |
| 111 | | | } |
| 112 | | | }; |
| 113 | 0 | | final Project myProject = getProject(); |
| 114 | 0 | | final DirectoryScanner ds = mFiles.getDirectoryScanner(myProject); |
| 115 | 0 | | final String[] includedFiles = ds.getIncludedFiles(); |
| 116 | 0 | | log("Transforming " + includedFiles.length + " files in directory " |
| 117 | | | + ds.getBasedir()); |
| 118 | 0 | | for (int i = 0; i < includedFiles.length; i++) |
| 119 | | | { |
| 120 | 0 | | final String f = includedFiles[i]; |
| 121 | 0 | | final File orig = new File(ds.getBasedir(), f); |
| 122 | | | final File out; |
| 123 | | | try |
| 124 | | | { |
| 125 | 0 | | out = File.createTempFile("jcoderz", "tmp"); |
| 126 | | | } |
| 127 | 0 | | catch (IOException e) |
| 128 | | | { |
| 129 | 0 | | throw new BuildException( |
| 130 | | | "Failed to create temp file: " + e, e); |
| 131 | 0 | | } |
| 132 | 0 | | xsltTask.setProject(myProject); |
| 133 | 0 | | xsltTask.setTaskName("xslt"); |
| 134 | 0 | | xsltTask.setIn(orig); |
| 135 | 0 | | xsltTask.setOut(out); |
| 136 | 0 | | xsltTask.setDestdir(myProject.getBaseDir()); |
| 137 | 0 | | xsltTask.setForce(true); |
| 138 | 0 | | xsltTask.setFailonerror(mFailOnError); |
| 139 | 0 | | xsltTask.setLogLevel(Project.MSG_VERBOSE); |
| 140 | 0 | | xsltTask.resolveExternalEntities(mResolveExternalEntities); |
| 141 | 0 | | log("Transforming file " + orig, Project.MSG_VERBOSE); |
| 142 | 0 | | xsltTask.execute(); |
| 143 | 0 | | if (out.exists()) |
| 144 | | | { |
| 145 | 0 | | if (!orig.delete()) |
| 146 | | | { |
| 147 | 0 | | throw new BuildException("Failed to delete " + orig); |
| 148 | | | } |
| 149 | 0 | | if (!out.renameTo(orig)) |
| 150 | | | { |
| 151 | | | |
| 152 | | | try |
| 153 | | | { |
| 154 | 0 | | safeMove(out, orig); |
| 155 | | | } |
| 156 | 0 | | catch (IOException e) |
| 157 | | | { |
| 158 | 0 | | throw new BuildException("Failed to move file " |
| 159 | | | + out, e); |
| 160 | 0 | | } |
| 161 | | | } |
| 162 | | | } |
| 163 | | | } |
| 164 | | | } |
| 165 | 0 | | catch (BuildException e) |
| 166 | | | { |
| 167 | 0 | | if (mFailOnError) |
| 168 | | | { |
| 169 | 0 | | throw e; |
| 170 | | | } |
| 171 | 0 | | log(e.getMessage(), Project.MSG_ERR); |
| 172 | 0 | | } |
| 173 | 0 | | } |
| 174 | | | |
| 175 | | | |
| 176 | | | <tt></tt> |
| 177 | | | |
| 178 | | | |
| 179 | | | @param |
| 180 | | | |
| 181 | | | public void resolveExternalEntities (boolean b) |
| 182 | | | { |
| 183 | 0 | | mResolveExternalEntities = b; |
| 184 | 0 | | } |
| 185 | | | |
| 186 | | | private void safeMove (File source, File dest) |
| 187 | | | throws IOException |
| 188 | | | { |
| 189 | 0 | (1) | final FileInputStream in = new FileInputStream(source); |
| 190 | 0 | | final FileOutputStream out = new FileOutputStream(dest); |
| 191 | | | try |
| 192 | | | { |
| 193 | 0 | | IoUtil.copy(in, out); |
| 194 | 0 | | if (!source.delete()) |
| 195 | | | { |
| 196 | 0 | | throw new BuildException("Failed to delete " + source); |
| 197 | | | } |
| 198 | | | } |
| 199 | | | finally |
| 200 | | | { |
| 201 | 0 | | IoUtil.close(in); |
| 202 | 0 | | IoUtil.close(out); |
| 203 | 0 | | } |
| 204 | 0 | | } |
| 205 | | | } |