KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > ant > ReplaceCopy


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.ant;
6
7 import java.io.File JavaDoc;
8 import java.net.MalformedURLException JavaDoc;
9 import java.util.Enumeration JavaDoc;
10 import java.util.Hashtable JavaDoc;
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 /**
24  * Extension of Ant's Copy task that uses XDoclet's template engine to copy instead of Ant's plain copy. It will scan
25  * each file for occurrences of <XDtAnt:property name="some.ant.property"/> and replace them with the
26  * associated Ant property value.
27  *
28  * <p>This is similar to using the standard &lt;copy&gt; Ant built-in task with a nested &lt;filterset&gt; element, but
29  * this task uses a pull mechanism (ant properties are pulled from the ant environment by the copied files) instead
30  * of a push mechanism (where Ant pushes explicit values into the copied files).</p>
31  *
32  * @author <a HREF="mailto:aslak.nospam@users.sf.net">Aslak Hellesøy</a>
33  * @created 5. januar 2002
34  * @version $Revision: 1.14 $
35  * @todo Write docs!!!!!!!!!!!!!!!!!
36  */

37 public class ReplaceCopy extends Copy
38 {
39     public ReplaceCopy()
40     {
41         ModuleFinder.initClasspath(getClass());
42     }
43
44     /**
45      * Overridden doFileOperations() as Ant suggests. It would be less copy-paste if fileUtils.copyFile in the
46      * superclass' method used getFileUtils().copyFile instead. Then we could just override getFileUtils() and return a
47      * FileUtils subclass which used XDoclet template engine. Until Ant fixes this, we'll do inheritance by copy/paste
48      * :-(
49      */

50     protected void doFileOperations()
51     {
52         Hashtable JavaDoc 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 JavaDoc e = fileCopyMap.keys();
70
71             while (e.hasMoreElements()) {
72                 String JavaDoc fromFile = (String JavaDoc) e.nextElement();
73                 String JavaDoc toFile = (String JavaDoc) 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 JavaDoc filterEnum = getFilterSets().elements(); filterEnum.hasMoreElements(); ) {
89                         executionFilters.addFilterSet((FilterSet) filterEnum.nextElement());
90                     }
91
92 // fileUtils.copyFile(fromFile, toFile, executionFilters,
93
// forceOverwrite, preserveLastModified);
94
replace(fromFile, toFile, engine);
95
96                 }
97                 catch (TemplateException ioe) {
98                     String JavaDoc msg = Translator.getString(XDocletAntMessages.class, XDocletAntMessages.FAILED_TO_COPY, new String JavaDoc[]{fromFile, toFile, ioe.getMessage()});
99
100                     throw new BuildException(msg, ioe, location);
101                 }
102             }
103         }
104
105         if (includeEmpty) {
106             Enumeration JavaDoc e = dirCopyMap.elements();
107             int count = 0;
108
109             while (e.hasMoreElements()) {
110                 File JavaDoc d = new File JavaDoc((String JavaDoc) 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     /**
132      * Describe what the method does
133      *
134      * @param fromFile Describe what the parameter does
135      * @param toFile Describe what the parameter does
136      * @param engine Describe what the parameter does
137      * @exception TemplateException Describe the exception
138      */

139     private void replace(String JavaDoc fromFile, String JavaDoc toFile, TemplateEngine engine) throws TemplateException
140     {
141         try {
142             engine.setTemplateURL(new File JavaDoc(fromFile).toURL());
143             engine.setOutput(new File JavaDoc(toFile));
144             engine.start();
145         }
146         catch (MalformedURLException JavaDoc e) {
147             throw new TemplateException(e.getMessage());
148         }
149     }
150 }
151
Popular Tags