KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > ant > CommonMatchingTask


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (C) 2000-2002 The Apache Software Foundation. All rights
5  * reserved.
6  * Copyright (C) 2003 jcoverage ltd.
7  * Copyright (C) 2005 Mark Doliner
8  * Copyright (C) 2005 Joakim Erdfelt
9  * Copyright (C) 2005 Grzegorz Lukasik
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution, if
24  * any, must include the following acknowlegement:
25  * "This product includes software developed by the
26  * Apache Software Foundation (http://www.apache.org/)."
27  * Alternately, this acknowlegement may appear in the software itself,
28  * if and wherever such third-party acknowlegements normally appear.
29  *
30  * 4. The names "Ant" and "Apache Software
31  * Foundation" must not be used to endorse or promote products derived
32  * from this software without prior written permission. For written
33  * permission, please contact apache@apache.org.
34  *
35  * 5. Products derived from this software may not be called "Apache"
36  * nor may "Apache" appear in their names without prior written
37  * permission of the Apache Group.
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This software consists of voluntary contributions made by many
54  * individuals on behalf of the Apache Software Foundation. For more
55  * information on the Apache Software Foundation, please see
56  * <http://www.apache.org/>.
57  */

58
59 package net.sourceforge.cobertura.ant;
60
61 import java.io.IOException JavaDoc;
62 import java.net.URL JavaDoc;
63 import java.net.URLClassLoader JavaDoc;
64 import java.util.Iterator JavaDoc;
65 import java.util.LinkedList JavaDoc;
66 import java.util.List JavaDoc;
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 JavaDoc className;
84     final List JavaDoc fileSets = new LinkedList JavaDoc();
85
86     private Java java = null;
87
88     public CommonMatchingTask(String JavaDoc className)
89     {
90         this.className = className;
91     }
92
93     private String JavaDoc 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             /**
109              * We replace %20 with a space character because, for some
110              * reason, when we call Cobertura from within CruiseControl,
111              * the classpath here contains %20's instead of spaces. I
112              * don't know if this is our problem, or CruiseControl, or
113              * ant, but this seems to fix it. --Mark
114              */

115             if (getClass().getClassLoader() instanceof AntClassLoader)
116             {
117                 String JavaDoc classpath = ((AntClassLoader)getClass()
118                         .getClassLoader()).getClasspath();
119                 createClasspath().setPath(
120                         StringUtil.replaceAll(classpath, "%20", " "));
121             }
122             else if (getClass().getClassLoader() instanceof URLClassLoader JavaDoc)
123             {
124                 URL JavaDoc[] earls = ((URLClassLoader JavaDoc)getClass().getClassLoader())
125                         .getURLs();
126                 for (int i = 0; i < earls.length; i++)
127                 {
128                     String JavaDoc 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 JavaDoc {
139         Iterator JavaDoc 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 JavaDoc[] filenames) throws IOException JavaDoc
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 JavaDoc[] getIncludedFiles(FileSet fileSet)
180     {
181         return getDirectoryScanner(fileSet).getIncludedFiles();
182     }
183
184     String JavaDoc[] getExcludedFiles(FileSet fileSet)
185     {
186         return getDirectoryScanner(fileSet).getExcludedFiles();
187     }
188
189     String JavaDoc[] getFilenames(FileSet fileSet)
190     {
191         String JavaDoc[] filesToReturn = getIncludedFiles(fileSet);
192
193         return filesToReturn;
194     }
195
196     String JavaDoc 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