KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > db > sqlmap > upgrade > ConvertTask


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Enumeration JavaDoc;
27
28 /**
29  * Extends the ant copy task to convert SQL Map files from v.1.x to v.2.x.
30  * <p/>
31  * Extends Ant version 1.5.4. Changes/extensions marked inline below
32  */

33 public class ConvertTask extends Copy {
34
35   private static final SqlMapXmlConverter CONVERTER = new SqlMapXmlConverter();
36
37   /**
38    * Overrides Ant Copy tasks file copy method.
39    */

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 JavaDoc e = fileCopyMap.keys();
47       while (e.hasMoreElements()) {
48         String JavaDoc fromFile = (String JavaDoc) e.nextElement();
49         String JavaDoc toFile = (String JavaDoc) 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 JavaDoc filterEnum = getFilterSets().elements();
66                filterEnum.hasMoreElements();) {
67             executionFilters
68                 .addFilterSet((FilterSet) filterEnum.nextElement());
69           }
70
71           // --------------------------------------
72

73           File JavaDoc temp = File.createTempFile("sql-map-", "-temp");
74
75           CONVERTER.convertFile(new File JavaDoc(fromFile), temp);
76
77           getFileUtils().copyFile(temp, new File JavaDoc(toFile), executionFilters,
78               getFilterChains(), forceOverwrite,
79               preserveLastModified, getEncoding(),
80               getProject());
81
82           // --------------------------------------
83

84         } catch (IOException JavaDoc ioe) {
85           String JavaDoc msg = "Failed to copy " + fromFile + " to " + toFile
86               + " due to " + ioe.getMessage();
87           File JavaDoc targetFile = new File JavaDoc(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 JavaDoc e = dirCopyMap.elements();
98       int count = 0;
99       while (e.hasMoreElements()) {
100         File JavaDoc d = new File JavaDoc((String JavaDoc) 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