1 20 package org.enhydra.barracuda.taskdefs; 21 22 import org.apache.tools.ant.*; 23 import org.apache.tools.ant.types.*; 24 import org.apache.tools.ant.util.*; 25 26 import java.io.*; 27 import java.util.*; 28 29 54 public class GenerateSSIs extends Task { 55 protected File file = null; protected List filesets = new ArrayList(); 57 58 protected String touchpattern = "**/*.shtml"; 59 protected boolean force = false; 60 protected int verbosity = Project.MSG_VERBOSE; 61 62 65 public void setFile(File file) { 66 this.file = file; 67 } 68 69 73 public void setTouchpattern(String touchPattern) { 74 this.touchpattern = touchPattern; 75 } 76 77 80 public void setForce(boolean force) { 81 this.force = force; 82 } 83 84 87 public void setVerbose(boolean verbose) { 88 if (verbose) { 89 this.verbosity = Project.MSG_INFO; 90 } else { 91 this.verbosity = Project.MSG_VERBOSE; 92 } 93 } 94 95 98 public void addFileset(FileSet set) { 99 filesets.add(set); 100 } 101 102 105 public void execute() throws BuildException { 106 validateAttributes(); 108 109 List fileList = new ArrayList(); 110 111 if (file!=null) { 113 if (file.exists()) { 114 fileList.add(file); 115 } else { 116 String message = "Could not find file "+file.getAbsolutePath()+" to generate ssi files from."; 117 log(message); 118 throw new BuildException(message); 119 } 120 } else { 122 for (int i=0; i<filesets.size(); i++) { 123 FileSet fs = (FileSet) filesets.get(i); 124 DirectoryScanner ds = fs.getDirectoryScanner(this.getProject()); 125 File fromDir = fs.getDir(this.getProject()); 126 127 File baseDir = ds.getBasedir(); 128 String [] srcFiles = ds.getIncludedFiles(); 129 130 for (int j=0; j<srcFiles.length; j++) { 131 fileList.add(new File(baseDir, srcFiles[j])); 132 } 133 } 134 } 135 136 processFiles(fileList); 138 } 139 140 145 protected void validateAttributes() throws BuildException { 146 if (file==null && filesets.size()==0) { 147 throw new BuildException("Specify at least one source - a file or a fileset."); 148 } 149 150 if (file!=null && file.exists() && file.isDirectory()) { 151 throw new BuildException("Use a fileset to generate ssis on directories."); 152 } 153 } 154 155 165 protected void processFiles(List fileList) { 166 Iterator it = fileList.iterator(); 167 int writeCnt = 0; 168 int touchCnt = 0; 169 Map touchedDirs = new HashMap(); 170 while (it.hasNext()) { 171 File f = (File) it.next(); 172 boolean notified = false; 173 log("Processing file: "+f, verbosity); 174 175 try { 176 BufferedReader in = new BufferedReader(new FileReader(f)); 177 BufferedWriter out = null; 178 String nextLine = null; 179 String targetSSI = null; 180 while (true) { 181 nextLine=in.readLine(); 182 if (nextLine==null) { 183 if (out!=null) { 184 out.flush(); 185 out.close(); 186 out = null; 187 } 188 break; 189 } 190 191 if (out==null) { 194 int spos = nextLine.indexOf("<!-- start "); 196 if (spos<0) continue; 197 int epos = nextLine.indexOf(".ssi -->", spos); 198 if (epos<0) continue; 199 200 targetSSI = nextLine.substring(spos+11, epos+4); 202 204 File ssi = new File(f.getParent(), targetSSI); 206 if (force || !ssi.exists() || f.lastModified()>ssi.lastModified()) { 207 if (!notified) { 208 log("File "+f+" has been modified..."); 209 notified = true; 210 touchedDirs.put(f.getParent(), f.getParentFile()); 211 } 212 log("...Writing "+ssi, verbosity); 213 writeCnt++; 214 out = new BufferedWriter(new FileWriter(ssi)); 215 out.write(nextLine.substring(epos+8)); out.newLine(); 219 } 220 221 } else { 223 String eflg = "<!-- end "+targetSSI+" -->"; 225 int spos = nextLine.indexOf(eflg); 226 if (spos<0) { 227 out.write(nextLine); 228 out.newLine(); 229 } else { 230 out.write(nextLine.substring(0, spos)); out.newLine(); 235 out.flush(); 236 out.close(); 237 out = null; 238 continue; 239 } 240 } 241 } 242 in.close(); 243 244 } catch (IOException e) { 245 log("Unable to read file:"+f, Project.MSG_ERR); 246 log("IOException "+e, Project.MSG_ERR); 247 } 248 } 249 250 if (touchpattern!=null && touchedDirs.size()>0) { 252 it = touchedDirs.values().iterator(); 253 while (it.hasNext()) { 254 File baseDir = (File) it.next(); 255 log("Touching "+baseDir+", pattern=\""+touchpattern+"\""); 256 FileSet fs = new FileSet(); 257 fs.setDir(baseDir); 258 fs.setIncludes(touchpattern); 259 DirectoryScanner ds = fs.getDirectoryScanner(this.getProject()); 260 String [] touchFiles = ds.getIncludedFiles(); 261 long millis = System.currentTimeMillis(); 262 for (int j=0; j<touchFiles.length; j++) { 263 File tf = new File(baseDir, touchFiles[j]); 264 log("...Touching "+tf, verbosity); 265 tf.setLastModified(millis); 266 touchCnt++; 267 } 268 } 269 } 270 271 if (writeCnt>0 || touchCnt>0) { 273 log("Created "+writeCnt+" .ssi files, touched "+touchCnt+" "+touchpattern+" files."); 274 } 275 } 276 } 277 | Popular Tags |