1 17 18 19 20 package org.apache.lenya.cms.ant; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.FilenameFilter ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.util.StringTokenizer ; 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 javaDir; 38 private String buildDir; 39 40 43 public void execute() throws BuildException { 44 int numberOfDirectoriesCreated = 0; 45 int numberOfFilesCopied = 0; 46 TwoTuple twoTuple = new TwoTuple(numberOfDirectoriesCreated, numberOfFilesCopied); 47 48 String translatedBuildDir = Project.translatePath(buildDir); 49 File absoluteBuildDir = null; 50 if (translatedBuildDir != null && translatedBuildDir.startsWith(File.separator)) { 51 absoluteBuildDir = new File (translatedBuildDir); 52 } else { 53 absoluteBuildDir = new File (getProject().getBaseDir(), 54 translatedBuildDir); 55 } 56 57 StringTokenizer st = new StringTokenizer (pubsRootDirs.toString(), File.pathSeparator); 58 59 while (st.hasMoreTokens()) { 60 String pubsRootDir = st.nextToken(); 61 62 File path = new File (pubsRootDir); 63 64 if (path.isDirectory()) { 65 if (new File (path, "publication.xml").isFile()) { 66 copyContentOfDir(new File (path, javaDir), absoluteBuildDir, twoTuple, new JavaFilenameFilter()); 67 } else { 68 String [] pubs = path.list(); 70 71 for (int i = 0; i < pubs.length; i++) { 72 File pubJavaDir = new File (path, new File (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 91 static public void copyDir(File source, File destination, TwoTuple twoTuple, FilenameFilter filenameFilter) { 92 File actualDestination = new File (destination, source.getName()); 93 actualDestination.mkdirs(); 94 copyContentOfDir(source, actualDestination, twoTuple, filenameFilter); 95 } 96 97 100 static public void copyContentOfDir(File source, File destination, TwoTuple twoTuple, FilenameFilter filenameFilter) { 101 if (source.isDirectory()) { 102 String [] 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 file = new File (source, files[i]); 112 113 if (file.isFile()) { 114 copyFile(file, new File (destination, files[i]), twoTuple); 115 } else if (file.isDirectory()) { 116 copyContentOfDir(file, new File (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 129 static public void copyFile(File source, File destination, TwoTuple twoTuple) { 130 if (source.isFile()) { 131 File parentDest = new File (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 in = new FileInputStream (source); 151 OutputStream out = new FileOutputStream (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 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 175 public void setPubsRootDirs(Path pubsRootDirs) { 176 this.pubsRootDirs = pubsRootDirs; 177 } 178 179 182 public void setJavaDir(String javaDir) { 183 this.javaDir = javaDir; 184 } 185 186 189 public void setBuildDir(String buildDir) { 190 this.buildDir = buildDir; 191 } 192 } 193 | Popular Tags |