1 5 package xdoclet.ant; 6 7 import java.io.File ; 8 import java.net.MalformedURLException ; 9 import java.util.Enumeration ; 10 import java.util.Hashtable ; 11 12 import org.apache.tools.ant.BuildException; 13 import org.apache.tools.ant.Project; 14 import org.apache.tools.ant.taskdefs.Copy; 15 import org.apache.tools.ant.types.FilterSet; 16 import org.apache.tools.ant.types.FilterSetCollection; 17 import xdoclet.loader.ModuleFinder; 18 19 import xdoclet.template.TemplateEngine; 20 import xdoclet.template.TemplateException; 21 import xdoclet.util.Translator; 22 23 37 public class ReplaceCopy extends Copy 38 { 39 public ReplaceCopy() 40 { 41 ModuleFinder.initClasspath(getClass()); 42 } 43 44 50 protected void doFileOperations() 51 { 52 Hashtable properties = project.getProperties(); 53 AntPropertyTagsHandler antPropertyTagsHandler = new AntPropertyTagsHandler(properties); 54 TemplateEngine engine = null; 55 56 try { 57 engine = TemplateEngine.getEngineInstance(); 58 engine.setTagHandlerFor("Ant", antPropertyTagsHandler); 59 } 60 catch (TemplateException e) { 61 throw new BuildException(Translator.getString(XDocletAntMessages.class, XDocletAntMessages.ERROR_CREATING_TEMPLATEENGINE)); 62 } 63 64 if (fileCopyMap.size() > 0) { 65 log("Copying " + fileCopyMap.size() + 66 " file" + (fileCopyMap.size() == 1 ? "" : "s") + 67 " to " + destDir.getAbsolutePath()); 68 69 Enumeration e = fileCopyMap.keys(); 70 71 while (e.hasMoreElements()) { 72 String fromFile = (String ) e.nextElement(); 73 String toFile = (String ) fileCopyMap.get(fromFile); 74 75 if (fromFile.equals(toFile)) { 76 log("Skipping self-copy of " + fromFile, verbosity); 77 continue; 78 } 79 80 try { 81 log("Copying " + fromFile + " to " + toFile, verbosity); 82 83 FilterSetCollection executionFilters = new FilterSetCollection(); 84 85 if (filtering) { 86 executionFilters.addFilterSet(project.getGlobalFilterSet()); 87 } 88 for (Enumeration filterEnum = getFilterSets().elements(); filterEnum.hasMoreElements(); ) { 89 executionFilters.addFilterSet((FilterSet) filterEnum.nextElement()); 90 } 91 92 replace(fromFile, toFile, engine); 95 96 } 97 catch (TemplateException ioe) { 98 String msg = Translator.getString(XDocletAntMessages.class, XDocletAntMessages.FAILED_TO_COPY, new String []{fromFile, toFile, ioe.getMessage()}); 99 100 throw new BuildException(msg, ioe, location); 101 } 102 } 103 } 104 105 if (includeEmpty) { 106 Enumeration e = dirCopyMap.elements(); 107 int count = 0; 108 109 while (e.hasMoreElements()) { 110 File d = new File ((String ) e.nextElement()); 111 112 if (!d.exists()) { 113 if (!d.mkdirs()) { 114 log("Unable to create directory " + d.getAbsolutePath(), Project.MSG_ERR); 115 } 116 else { 117 count++; 118 } 119 } 120 } 121 122 if (count > 0) { 123 log("Copied " + count + 124 " empty director" + 125 (count == 1 ? "y" : "ies") + 126 " to " + destDir.getAbsolutePath()); 127 } 128 } 129 } 130 131 139 private void replace(String fromFile, String toFile, TemplateEngine engine) throws TemplateException 140 { 141 try { 142 engine.setTemplateURL(new File (fromFile).toURL()); 143 engine.setOutput(new File (toFile)); 144 engine.start(); 145 } 146 catch (MalformedURLException e) { 147 throw new TemplateException(e.getMessage()); 148 } 149 } 150 } 151 | Popular Tags |