1 58 59 package net.sourceforge.cobertura.ant; 60 61 import java.io.IOException ; 62 import java.net.URL ; 63 import java.net.URLClassLoader ; 64 import java.util.Iterator ; 65 import java.util.LinkedList ; 66 import java.util.List ; 67 68 import net.sourceforge.cobertura.util.CommandLineBuilder; 69 import net.sourceforge.cobertura.util.StringUtil; 70 71 import org.apache.tools.ant.AntClassLoader; 72 import org.apache.tools.ant.DirectoryScanner; 73 import org.apache.tools.ant.Project; 74 import org.apache.tools.ant.taskdefs.Java; 75 import org.apache.tools.ant.taskdefs.MatchingTask; 76 import org.apache.tools.ant.types.FileSet; 77 import org.apache.tools.ant.types.Path; 78 import org.apache.tools.ant.types.Reference; 79 80 public abstract class CommonMatchingTask extends MatchingTask 81 { 82 83 final String className; 84 final List fileSets = new LinkedList (); 85 86 private Java java = null; 87 88 public CommonMatchingTask(String className) 89 { 90 this.className = className; 91 } 92 93 private String getClassName() 94 { 95 return className; 96 } 97 98 protected Java getJava() 99 { 100 if (java == null) 101 { 102 java = (Java)getProject().createTask("java"); 103 java.setTaskName(getTaskName()); 104 java.setClassname(getClassName()); 105 java.setFork(true); 106 java.setDir(getProject().getBaseDir()); 107 108 115 if (getClass().getClassLoader() instanceof AntClassLoader) 116 { 117 String classpath = ((AntClassLoader)getClass() 118 .getClassLoader()).getClasspath(); 119 createClasspath().setPath( 120 StringUtil.replaceAll(classpath, "%20", " ")); 121 } 122 else if (getClass().getClassLoader() instanceof URLClassLoader ) 123 { 124 URL [] earls = ((URLClassLoader )getClass().getClassLoader()) 125 .getURLs(); 126 for (int i = 0; i < earls.length; i++) 127 { 128 String classpath = earls[i].getFile(); 129 createClasspath().setPath( 130 StringUtil.replaceAll(classpath, "%20", " ")); 131 } 132 } 133 } 134 135 return java; 136 } 137 138 protected void createArgumentsForFilesets( CommandLineBuilder builder) throws IOException { 139 Iterator iter = fileSets.iterator(); 140 while (iter.hasNext()) 141 { 142 FileSet fileSet = (FileSet)iter.next(); 143 144 builder.addArg("--basedir", baseDir(fileSet)); 145 createArgumentsForFilenames( builder, getFilenames(fileSet)); 146 } 147 } 148 149 private void createArgumentsForFilenames( CommandLineBuilder builder, String [] filenames) throws IOException 150 { 151 for (int i = 0; i < filenames.length; i++) 152 { 153 getProject().log("Adding " + filenames[i] + " to list", 154 Project.MSG_VERBOSE); 155 builder.addArg(filenames[i]); 156 } 157 } 158 159 public Path createClasspath() 160 { 161 return getJava().createClasspath().createPath(); 162 } 163 164 public void setClasspath(Path classpath) 165 { 166 createClasspath().append(classpath); 167 } 168 169 public void setClasspathRef(Reference r) 170 { 171 createClasspath().setRefid(r); 172 } 173 174 DirectoryScanner getDirectoryScanner(FileSet fileSet) 175 { 176 return fileSet.getDirectoryScanner(getProject()); 177 } 178 179 String [] getIncludedFiles(FileSet fileSet) 180 { 181 return getDirectoryScanner(fileSet).getIncludedFiles(); 182 } 183 184 String [] getExcludedFiles(FileSet fileSet) 185 { 186 return getDirectoryScanner(fileSet).getExcludedFiles(); 187 } 188 189 String [] getFilenames(FileSet fileSet) 190 { 191 String [] filesToReturn = getIncludedFiles(fileSet); 192 193 return filesToReturn; 194 } 195 196 String baseDir(FileSet fileSet) 197 { 198 return fileSet.getDirectoryScanner(getProject()).getBasedir() 199 .toString(); 200 } 201 202 public void addFileset(FileSet fileSet) 203 { 204 fileSets.add(fileSet); 205 } 206 } 207 | Popular Tags |