1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.InputStreamReader ; 24 import java.io.FileOutputStream ; 25 import java.io.OutputStreamWriter ; 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.util.Enumeration ; 29 30 import org.apache.tools.ant.BuildException; 31 import org.apache.tools.ant.Project; 32 import org.apache.tools.ant.Task; 33 import org.apache.tools.ant.util.FileUtils; 34 import org.apache.tools.ant.types.EnumeratedAttribute; 35 36 45 public class ManifestTask extends Task { 46 47 50 private Manifest nestedManifest = new Manifest(); 51 52 55 private File manifestFile; 56 57 60 private Mode mode; 61 62 65 private String encoding; 66 67 70 public static class Mode extends EnumeratedAttribute { 71 76 public String [] getValues() { 77 return new String [] {"update", "replace"}; 78 } 79 } 80 81 84 public ManifestTask() { 85 mode = new Mode(); 86 mode.setValue("replace"); 87 } 88 89 96 public void addConfiguredSection(Manifest.Section section) 97 throws ManifestException { 98 nestedManifest.addConfiguredSection(section); 99 } 100 101 108 public void addConfiguredAttribute(Manifest.Attribute attribute) 109 throws ManifestException { 110 nestedManifest.addConfiguredAttribute(attribute); 111 } 112 113 118 public void setFile(File f) { 119 manifestFile = f; 120 } 121 122 126 public void setEncoding(String encoding) { 127 this.encoding = encoding; 128 } 129 130 134 public void setMode(Mode m) { 135 mode = m; 136 } 137 138 143 public void execute() throws BuildException { 144 if (manifestFile == null) { 145 throw new BuildException("the file attribute is required"); 146 } 147 148 Manifest toWrite = Manifest.getDefaultManifest(); 149 Manifest current = null; 150 BuildException error = null; 151 152 if (manifestFile.exists()) { 153 FileInputStream fis = null; 154 InputStreamReader isr = null; 155 try { 156 fis = new FileInputStream (manifestFile); 157 if (encoding == null) { 158 isr = new InputStreamReader (fis, "UTF-8"); 159 } else { 160 isr = new InputStreamReader (fis, encoding); 161 } 162 current = new Manifest(isr); 163 } catch (ManifestException m) { 164 error = new BuildException("Existing manifest " + manifestFile 165 + " is invalid", m, getLocation()); 166 } catch (IOException e) { 167 error = new BuildException("Failed to read " + manifestFile, 168 e, getLocation()); 169 } finally { 170 FileUtils.close(isr); 171 } 172 } 173 174 for (Enumeration e = nestedManifest.getWarnings(); 176 e.hasMoreElements();) { 177 log("Manifest warning: " + (String ) e.nextElement(), 178 Project.MSG_WARN); 179 } 180 try { 181 if (mode.getValue().equals("update") && manifestFile.exists()) { 182 if (current != null) { 183 toWrite.merge(current); 184 } else if (error != null) { 185 throw error; 186 } 187 } 188 189 toWrite.merge(nestedManifest); 190 } catch (ManifestException m) { 191 throw new BuildException("Manifest is invalid", m, getLocation()); 192 } 193 194 if (toWrite.equals(current)) { 195 log("Manifest has not changed, do not recreate", 196 Project.MSG_VERBOSE); 197 return; 198 } 199 200 PrintWriter w = null; 201 try { 202 FileOutputStream fos = new FileOutputStream (manifestFile); 203 OutputStreamWriter osw = new OutputStreamWriter (fos, Manifest.JAR_ENCODING); 204 w = new PrintWriter (osw); 205 toWrite.write(w); 206 } catch (IOException e) { 207 throw new BuildException("Failed to write " + manifestFile, 208 e, getLocation()); 209 } finally { 210 if (w != null) { 211 w.close(); 212 } 213 } 214 } 215 216 } 217 | Popular Tags |