KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > output > antutils > PatternSet


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.junit.output.antutils;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32 import org.apache.tools.ant.module.spi.TaskStructure;
33
34 /**
35  *
36  * @author Marian Petras
37  */

38 final class PatternSet {
39     
40     /** */
41     private final AntProject project;
42
43     /** */
44     private Collection JavaDoc<String JavaDoc> includePatterns;
45     /** */
46     private Collection JavaDoc<String JavaDoc> excludePatterns;
47     
48     /**
49      */

50     PatternSet(AntProject project) {
51         this.project = project;
52     }
53
54     /**
55      *
56      */

57     void handleChildrenAndAttrs(TaskStructure struct) {
58         setAttrs(struct);
59         for (TaskStructure child : struct.getChildren()) {
60             String JavaDoc childName = child.getName();
61             if (childName.equals("include")) { //NOI18N
62
Entry entry = new Entry();
63                 entry.handleChildrenAndAttrs(child);
64                 if (entry.isApplicable()) {
65                     addIncludePatterns(entry.getPattern());
66                 }
67                 continue;
68             }
69             if (childName.equals("exclude")) { //NOI18N
70
Entry entry = new Entry();
71                 entry.handleChildrenAndAttrs(child);
72                 if (entry.isApplicable()) {
73                     addExcludePatterns(entry.getPattern());
74                 }
75                 continue;
76             }
77             if (childName.equals("includesfile")) { //NOI18N
78
Entry entry = new Entry();
79                 entry.handleChildrenAndAttrs(child);
80                 if (entry.isApplicable()) {
81                     addIncludePatterns(project.resolveFile(entry.getPattern()));
82                 }
83                 continue;
84             }
85             if (childName.equals("excludesfile")) { //NOI18N
86
Entry entry = new Entry();
87                 entry.handleChildrenAndAttrs(child);
88                 if (entry.isApplicable()) {
89                     addExcludePatterns(project.resolveFile(entry.getPattern()));
90                 }
91                 continue;
92             }
93             if (childName.equals("patternset")) { //NOI18N
94
PatternSet patternSet = new PatternSet(project);
95                 patternSet.handleChildrenAndAttrs(child);
96                 addPatternSet(patternSet);
97                 continue;
98             }
99         }
100     }
101     
102     /**
103      */

104     Collection JavaDoc<String JavaDoc> getIncludePatterns() {
105         if (includePatterns != null) {
106             return includePatterns;
107         } else {
108             return Collections.emptyList();
109         }
110     }
111     
112     /**
113      */

114     Collection JavaDoc<String JavaDoc> getExcludePatterns() {
115         if (excludePatterns != null) {
116             return excludePatterns;
117         } else {
118             return Collections.emptyList();
119         }
120     }
121     
122     /**
123      * Handles this {@code PatternSet}'s attributes.
124      *
125      * @param struct XML element corresponding to this {@code PatternSet}
126      */

127     private void setAttrs(TaskStructure struct) {
128         String JavaDoc includes = struct.getAttribute("includes"); //NOI18N
129
String JavaDoc includesFile = struct.getAttribute("includesFile"); //NOI18N
130
String JavaDoc excludes = struct.getAttribute("excludes"); //NOI18N
131
String JavaDoc excludesFile = struct.getAttribute("excludesFile"); //NOI18N
132

133         if (includes != null) {
134             addIncludePatterns(project.replaceProperties(includes));
135         }
136         if (excludes != null) {
137             addExcludePatterns(project.replaceProperties(excludes));
138         }
139         if (includesFile != null) {
140             addIncludePatterns(project.resolveFile(
141                     project.replaceProperties(includesFile)));
142         }
143         if (excludesFile != null) {
144             addExcludePatterns(project.resolveFile(
145                     project.replaceProperties(excludesFile)));
146         }
147     }
148     
149     /**
150      */

151     private void addIncludePatterns(String JavaDoc patternsString) {
152         if (includePatterns == null) {
153             includePatterns = new ArrayList JavaDoc<String JavaDoc>();
154         }
155         addPatterns(patternsString, includePatterns);
156     }
157     
158     /**
159      */

160     private void addExcludePatterns(String JavaDoc patternsString) {
161         if (excludePatterns == null) {
162             excludePatterns = new ArrayList JavaDoc<String JavaDoc>();
163         }
164         addPatterns(patternsString, excludePatterns);
165     }
166     
167     /**
168      */

169     private void addIncludePatterns(File JavaDoc includesFile) {
170         if (includePatterns == null) {
171             includePatterns = new ArrayList JavaDoc<String JavaDoc>();
172         }
173         readPatterns(includesFile, includePatterns);
174     }
175     
176     /**
177      */

178     private void addExcludePatterns(File JavaDoc excludesFile) {
179         if (excludePatterns == null) {
180             excludePatterns = new ArrayList JavaDoc<String JavaDoc>();
181         }
182         readPatterns(excludesFile, excludePatterns);
183     }
184     
185     /**
186      */

187     private void addPatterns(String JavaDoc patternsString,
188                              Collection JavaDoc<String JavaDoc> patterns) {
189         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(patternsString,
190                                                         ", "); //NOI18N
191
while (tokenizer.hasMoreTokens()) {
192             patterns.add(tokenizer.nextToken());
193         }
194     }
195     
196     /**
197      */

198     private void readPatterns(File JavaDoc patternsFile,
199                               Collection JavaDoc<String JavaDoc> patterns) {
200         BufferedReader JavaDoc fileReader = null;
201         try {
202             fileReader = new BufferedReader JavaDoc(new FileReader JavaDoc(patternsFile));
203             for (String JavaDoc line = fileReader.readLine(); line != null; ) {
204                 if (line.length() != 0) {
205                     addPatterns(project.replaceProperties(line), patterns);
206                 }
207             }
208         } catch (IOException JavaDoc ex) {
209             Logger.getLogger(getClass().getName())
210                   .log(Level.INFO,
211                        "failed to read Ant patterns file " //NOI18N
212
+ patternsFile.getAbsolutePath(),
213                        ex);
214         } finally {
215             if (fileReader != null) {
216                 try {
217                     fileReader.close();
218                 } catch (IOException JavaDoc ex2) {
219                     //ignore the exception
220
}
221             }
222         }
223     }
224     
225     /**
226      */

227     private void addPatternSet(PatternSet p) {
228         if (p.includePatterns != null) {
229             if (includePatterns != null) {
230                 includePatterns.addAll(p.includePatterns);
231             } else {
232                 includePatterns = p.includePatterns;
233             }
234         }
235         
236         if (p.excludePatterns != null) {
237             if (excludePatterns != null) {
238                 excludePatterns.addAll(p.excludePatterns);
239             } else {
240                 excludePatterns = p.excludePatterns;
241             }
242         }
243     }
244
245     
246     /**
247      *
248      */

249     class Entry {
250         
251         /** */
252         private String JavaDoc pattern;
253         /** */
254         private String JavaDoc ifCondition;
255         /** */
256         private String JavaDoc unlessCondition;
257         
258         /**
259          */

260         void handleChildrenAndAttrs(TaskStructure struct) {
261             setAttrs(struct);
262         }
263         
264         /**
265          * Handles attributes of this {@code Entry}.
266          *
267          * @param struct XML element corresponding to this {@code Entry}
268          */

269         private void setAttrs(TaskStructure struct) {
270             pattern = struct.getAttribute("name"); //NOI18N
271
ifCondition = struct.getAttribute("if"); //NOI18N
272
unlessCondition = struct.getAttribute("unless"); //NOI18N
273
}
274         
275         /**
276          * Checks whether this entry is valid according to the
277          * <em>if</em> and <em>unless</em> conditions.
278          *
279          * @return {@code true} if call conditions imposed by the optional
280          * <em>if</em> and <em>unless</em> attributes are met;
281          * {@code false} otherwise
282          */

283         boolean isApplicable() {
284             return ((ifCondition == null)
285                         || project.toBoolean(
286                                 project.replaceProperties(ifCondition)))
287                    &&
288                    ((unlessCondition == null)
289                         || !project.toBoolean(
290                                 project.replaceProperties(unlessCondition)));
291         }
292         
293         /**
294          */

295         String JavaDoc getPattern() {
296             return project.replaceProperties(pattern);
297         }
298
299     }
300     
301 }
302
Popular Tags