1 20 package org.enhydra.barracuda.taskdefs; 21 22 import java.util.*; 23 import java.io.*; 24 import org.apache.tools.ant.*; 25 import org.apache.tools.ant.taskdefs.*; 26 import org.apache.tools.ant.types.*; 27 28 61 public class CopyAndReplace extends Copy { 62 63 protected File mappingsFile = null; protected boolean ssi = false; 66 69 public void setMappings(File mappingsFile) { 70 this.mappingsFile = mappingsFile; 71 } 72 73 79 public void setSsi(BooleanAttribute issi) { 80 ssi = (issi.getValue().equals("yes") || issi.getValue().equals("true")); 81 } 82 83 89 protected void processSSI(String fromFile, String toFile) { 90 File targetFile = new File(toFile); 91 File fromDir = new File(fromFile).getParentFile(); 92 93 byte[] dstbytes = getFile(targetFile, fromDir); 95 if (dstbytes == null) 96 return; 97 98 try { 100 FileOutputStream fos = new FileOutputStream(targetFile); 101 fos.write(dstbytes); 102 fos.close(); 103 } catch (IOException e) { 104 System.out.println("Error writing file " + targetFile + ":" + e); 105 } 106 } 107 108 116 protected void processReplace(String fromFile, String toFile, List tokenList, List valueList) { 117 File targetFile = new File(toFile); 118 119 Replace replace = new Replace(); 122 replace.setProject(this.getProject()); 123 replace.setFile(targetFile); 124 for (int i=0, max=tokenList.size(); i<max; i++) { 125 Replace.Replacefilter rf = replace.createReplacefilter(); 126 rf.setToken((String) tokenList.get(i)); 127 rf.setValue((String) valueList.get(i)); 128 } 129 replace.execute(); 130 } 131 132 149 protected void doFileOperations() { 150 super.doFileOperations(); 152 153 159 if (ssi) { 161 if (fileCopyMap.size()>0) log("Processing copied files for server side includes"); 162 163 Enumeration enum = fileCopyMap.keys(); 164 while (enum.hasMoreElements()) { 165 String fromFile = (String) enum.nextElement(); 167 168 Object target = fileCopyMap.get(fromFile); 170 boolean isAnt15 = target instanceof String; 171 if (isAnt15) { 172 processSSI(fromFile, (String)target ); 173 } else { 174 String[] targetArray = (String[]) target; 175 for (int j = 0; j < targetArray.length; j++) { 176 processSSI(fromFile, targetArray[j] ); 177 } 178 } 179 181 } 182 } 183 185 if (mappingsFile==null) return; 187 List mappings = new ArrayList(); 188 if (!mappingsFile.exists()) { 189 log("Unable to find mappings file: "+mappingsFile+" ... files copied but no text replace occurred", Project.MSG_ERR); 190 return; 191 } 192 193 List tokenList = new ArrayList(); 195 List valueList = new ArrayList(); 196 try { 197 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(mappingsFile))); 198 while (true) { 199 String s = br.readLine(); 200 if (s==null) break; 201 String token = null; 202 String value = null; 203 204 int spos = s.indexOf("token="); 206 if (spos<0 || spos+6>=s.length()) continue; 207 spos+=6; 208 String delim = s.substring(spos,spos+1); 209 spos+=1; 210 int epos = s.indexOf(delim, spos+1); 211 if (epos<0 || epos+1>=s.length()) continue; 212 token = s.substring(spos, epos); 213 if (token==null) continue; 214 215 spos = s.indexOf("value=", epos+1); 217 if (spos<0 || spos+6>=s.length()) continue; 218 spos+=6; 219 delim = s.substring(spos,spos+1); 220 spos+=1; 221 epos = s.indexOf(delim, spos+1); 222 if (epos<0) continue; 223 value = s.substring(spos, epos); 224 if (value==null) continue; 225 226 tokenList.add(token); 227 valueList.add(value); 228 } 229 } catch (IOException e) { 230 System.out.println ("Error reading file "+mappingsFile+":"+e); 231 return; 232 } 233 234 if (fileCopyMap.size()>0) { 236 log("Replacing text in copied files"); 237 238 Enumeration enum = fileCopyMap.keys(); 239 while (enum.hasMoreElements()) { 240 String fromFile = (String) enum.nextElement(); 242 243 Object target = fileCopyMap.get(fromFile); 245 boolean isAnt15 = target instanceof String; 246 if (isAnt15) { 247 processReplace(fromFile, (String)target, tokenList, valueList ); 248 } else { 249 String[] targetArray = (String[]) target; 250 for (int j = 0; j < targetArray.length; j++) { 251 processReplace(fromFile, targetArray[j], tokenList, valueList ); 252 } 253 } 254 } 256 } 257 } 258 259 public static class BooleanAttribute extends EnumeratedAttribute { 260 public String[] getValues() { 261 return new String[] {"yes", "no", "true", "false"}; 262 } 263 } 264 265 266 private static String START_SSI = "<!--#include file=\""; 268 private static String END_SSI = "\"-->"; 269 270 public byte[] getFile(File targetFile, File fromDir) { 271 if (!targetFile.exists() && fromDir!=null) { 273 String targetFileName = targetFile.getName(); 274 targetFile = new File(fromDir, targetFileName); 275 } 276 277 byte[] dstbytes = new byte[10240]; 279 try { 280 FileInputStream fis = new FileInputStream(targetFile); 282 BufferedInputStream in = new BufferedInputStream(fis); 283 int offset = 0; 284 int len = 1024; 285 byte[] inbytes = new byte[len]; 286 while (true) { 287 int cnt = in.read(inbytes, 0, len); 288 if (cnt==-1) break; 289 if (offset+cnt>=dstbytes.length) { 290 byte[] newbytes = new byte[(int) (dstbytes.length+(len*5))]; 291 System.arraycopy(dstbytes, 0, newbytes, 0, dstbytes.length); 292 dstbytes = newbytes; 293 } 294 System.arraycopy(inbytes, 0, dstbytes, offset, cnt); 295 offset+=cnt; 296 } 297 in.close(); 298 299 byte[] newbytes = new byte[offset]; 301 System.arraycopy(dstbytes, 0, newbytes, 0, offset); 302 dstbytes = newbytes; 303 304 String result = new String(dstbytes); 309 int spos = 0; 310 int epos = 0; 311 while (true) { 312 spos = result.indexOf(START_SSI, epos); 313 if (spos<0) break; 314 epos = result.indexOf(END_SSI, spos); 315 if (epos<spos) break; 316 String ssiName = result.substring(spos+START_SSI.length(), epos); 317 File ssiFile = null; 318 if (ssiFile==null || !ssiFile.exists()) ssiFile = new File(targetFile.getParent(), ssiName); 319 if (ssiFile==null || !ssiFile.exists()) ssiFile = new File(fromDir, ssiName); 320 if (ssiFile==null || !ssiFile.exists()) ssiFile = new File(ssiName); 321 if (ssiFile==null || !ssiFile.exists()) { 322 System.out.println ("Error locating SSI:"+ssiName); 323 continue; 324 } 325 byte[] ssiBytes = getFile(ssiFile, null); 326 if (ssiBytes!=null) result = result.substring(0,spos)+new String(ssiBytes)+result.substring(epos+END_SSI.length()); 327 } 328 dstbytes = result.getBytes(); 329 330 } catch (IOException e) { 331 System.out.println ("Error reading file "+targetFile+":"+e); 332 return null; 333 } 334 return dstbytes; 335 } 336 } 338 | Popular Tags |