1 16 package com.ibatis.db.sqlmap.upgrade; 17 18 import org.apache.tools.ant.BuildException; 19 import org.apache.tools.ant.Project; 20 import org.apache.tools.ant.taskdefs.Copy; 21 import org.apache.tools.ant.types.FilterSet; 22 import org.apache.tools.ant.types.FilterSetCollection; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.Enumeration ; 27 28 33 public class ConvertTask extends Copy { 34 35 private static final SqlMapXmlConverter CONVERTER = new SqlMapXmlConverter(); 36 37 40 protected void doFileOperations() { 41 if (fileCopyMap.size() > 0) { 42 log("Copying " + fileCopyMap.size() 43 + " file" + (fileCopyMap.size() == 1 ? "" : "s") 44 + " to " + destDir.getAbsolutePath()); 45 46 Enumeration e = fileCopyMap.keys(); 47 while (e.hasMoreElements()) { 48 String fromFile = (String ) e.nextElement(); 49 String toFile = (String ) fileCopyMap.get(fromFile); 50 51 if (fromFile.equals(toFile)) { 52 log("Skipping self-copy of " + fromFile, verbosity); 53 continue; 54 } 55 56 try { 57 log("Copying " + fromFile + " to " + toFile, verbosity); 58 59 FilterSetCollection executionFilters = 60 new FilterSetCollection(); 61 if (filtering) { 62 executionFilters 63 .addFilterSet(getProject().getGlobalFilterSet()); 64 } 65 for (Enumeration filterEnum = getFilterSets().elements(); 66 filterEnum.hasMoreElements();) { 67 executionFilters 68 .addFilterSet((FilterSet) filterEnum.nextElement()); 69 } 70 71 73 File temp = File.createTempFile("sql-map-", "-temp"); 74 75 CONVERTER.convertFile(new File (fromFile), temp); 76 77 getFileUtils().copyFile(temp, new File (toFile), executionFilters, 78 getFilterChains(), forceOverwrite, 79 preserveLastModified, getEncoding(), 80 getProject()); 81 82 84 } catch (IOException ioe) { 85 String msg = "Failed to copy " + fromFile + " to " + toFile 86 + " due to " + ioe.getMessage(); 87 File targetFile = new File (toFile); 88 if (targetFile.exists() && !targetFile.delete()) { 89 msg += " and I couldn't delete the corrupt " + toFile; 90 } 91 throw new BuildException(msg, ioe, getLocation()); 92 } 93 } 94 } 95 96 if (includeEmpty) { 97 Enumeration e = dirCopyMap.elements(); 98 int count = 0; 99 while (e.hasMoreElements()) { 100 File d = new File ((String ) e.nextElement()); 101 if (!d.exists()) { 102 if (!d.mkdirs()) { 103 log("Unable to create directory " 104 + d.getAbsolutePath(), Project.MSG_ERR); 105 } else { 106 count++; 107 } 108 } 109 } 110 111 if (count > 0) { 112 log("Copied " + count + 113 " empty director" + 114 (count == 1 ? "y" : "ies") + 115 " to " + destDir.getAbsolutePath()); 116 } 117 } 118 } 119 120 } 121 | Popular Tags |