KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > ant > CopyJavaSourcesTask


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
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  */

17
18 /* $Id: CopyJavaSourcesTask.java 55545 2004-10-26 00:34:51Z gregor $ */
19
20 package org.apache.lenya.cms.ant;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.FilenameFilter JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import org.apache.tools.ant.BuildException;
31 import org.apache.tools.ant.Project;
32 import org.apache.tools.ant.Task;
33 import org.apache.tools.ant.types.Path;
34
35 public class CopyJavaSourcesTask extends Task {
36     private Path pubsRootDirs;
37     private String JavaDoc javaDir;
38     private String JavaDoc buildDir;
39
40     /**
41      *
42      */

43     public void execute() throws BuildException {
44         int numberOfDirectoriesCreated = 0;
45         int numberOfFilesCopied = 0;
46         TwoTuple twoTuple = new TwoTuple(numberOfDirectoriesCreated, numberOfFilesCopied);
47
48         String JavaDoc translatedBuildDir = Project.translatePath(buildDir);
49         File JavaDoc absoluteBuildDir = null;
50         if (translatedBuildDir != null && translatedBuildDir.startsWith(File.separator)) {
51             absoluteBuildDir = new File JavaDoc(translatedBuildDir);
52         } else {
53         absoluteBuildDir = new File JavaDoc(getProject().getBaseDir(),
54                     translatedBuildDir);
55         }
56
57         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pubsRootDirs.toString(), File.pathSeparator);
58
59         while (st.hasMoreTokens()) {
60             String JavaDoc pubsRootDir = st.nextToken();
61
62             File JavaDoc path = new File JavaDoc(pubsRootDir);
63
64             if (path.isDirectory()) {
65                 if (new File JavaDoc(path, "publication.xml").isFile()) {
66                     copyContentOfDir(new File JavaDoc(path, javaDir), absoluteBuildDir, twoTuple, new JavaFilenameFilter());
67                 } else {
68                     // FIXME: Look for publications defined by the file "publication.xml"
69
String JavaDoc[] pubs = path.list();
70
71                     for (int i = 0; i < pubs.length; i++) {
72                         File JavaDoc pubJavaDir = new File JavaDoc(path, new File JavaDoc(pubs[i], javaDir).toString());
73
74                         copyContentOfDir(pubJavaDir, absoluteBuildDir, twoTuple, new JavaFilenameFilter());
75                     }
76                 }
77             } else {
78                 throw new BuildException("No such directory: " + path);
79             }
80         }
81
82         numberOfDirectoriesCreated = twoTuple.x;
83         numberOfFilesCopied = twoTuple.y;
84         System.out.println("Copying " + numberOfDirectoriesCreated + " directories to " + absoluteBuildDir);
85         System.out.println("Copying " + numberOfFilesCopied + " files to " + absoluteBuildDir);
86     }
87
88     /**
89      * Copies the directory "source" into the directory "destination"
90      */

91     static public void copyDir(File JavaDoc source, File JavaDoc destination, TwoTuple twoTuple, FilenameFilter JavaDoc filenameFilter) {
92         File JavaDoc actualDestination = new File JavaDoc(destination, source.getName());
93         actualDestination.mkdirs();
94         copyContentOfDir(source, actualDestination, twoTuple, filenameFilter);
95     }
96
97     /**
98      * Copies the content of a directory into another directory
99      */

100     static public void copyContentOfDir(File JavaDoc source, File JavaDoc destination, TwoTuple twoTuple, FilenameFilter JavaDoc filenameFilter) {
101         if (source.isDirectory()) {
102             String JavaDoc[] files;
103
104             if (filenameFilter != null) {
105                 files = source.list(filenameFilter);
106             } else {
107                 files = source.list();
108             }
109
110             for (int i = 0; i < files.length; i++) {
111                 File JavaDoc file = new File JavaDoc(source, files[i]);
112
113                 if (file.isFile()) {
114                     copyFile(file, new File JavaDoc(destination, files[i]), twoTuple);
115                 } else if (file.isDirectory()) {
116                     copyContentOfDir(file, new File JavaDoc(destination, files[i]), twoTuple, filenameFilter);
117                 } else {
118                     System.err.println("CopyJavaSourcesTask.copyDir(): Neither file nor directory: " + file);
119                 }
120             }
121         } else {
122         }
123     }
124
125     /**
126      * Copies the content of a file into another file
127      * @param destination File (not a directory!)
128      */

129     static public void copyFile(File JavaDoc source, File JavaDoc destination, TwoTuple twoTuple) {
130         if (source.isFile()) {
131             File JavaDoc parentDest = new File JavaDoc(destination.getParent());
132
133             if (!parentDest.exists()) {
134                 parentDest.mkdirs();
135
136                 int numberOfDirectoriesCreated = twoTuple.x;
137                 numberOfDirectoriesCreated++;
138                 twoTuple.x = numberOfDirectoriesCreated;
139             }
140
141             if (destination.isFile()) {
142                 if (destination.lastModified() > source.lastModified()) {
143                     return;
144                 }
145             }
146
147             try {
148                 byte[] buffer = new byte[1024];
149                 int bytesRead = -1;
150                 InputStream JavaDoc in = new FileInputStream JavaDoc(source);
151                 OutputStream JavaDoc out = new FileOutputStream JavaDoc(destination);
152
153                 while ((bytesRead = in.read(buffer)) >= 0) {
154                     out.write(buffer, 0, bytesRead);
155                 }
156
157                 out.close();
158                 in.close();
159
160                 int numberOfFilesCopied = twoTuple.y;
161                 numberOfFilesCopied++;
162                 twoTuple.y = numberOfFilesCopied;
163
164             } catch (Exception JavaDoc e) {
165                 System.err.println("CopyJavaSourcesTask.copyFile(): " + e);
166             }
167         } else {
168             System.err.println("CopyJavaSourcesTask.copyFile(): No such file: " + source);
169         }
170     }
171
172     /**
173      *
174      */

175     public void setPubsRootDirs(Path pubsRootDirs) {
176         this.pubsRootDirs = pubsRootDirs;
177     }
178
179     /**
180      *
181      */

182     public void setJavaDir(String JavaDoc javaDir) {
183         this.javaDir = javaDir;
184     }
185
186     /**
187      *
188      */

189     public void setBuildDir(String JavaDoc buildDir) {
190         this.buildDir = buildDir;
191     }
192 }
193
Popular Tags