KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > upgrade > IncludeExclude


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.upgrade;
21
22 import java.io.*;
23 import java.util.*;
24 import java.util.jar.*;
25 import java.util.regex.*;
26 import org.openide.util.Union2;
27
28
29
30 /** A test that is initialized based on includes and excludes.
31  *
32  * @author Jaroslav Tulach
33  */

34 final class IncludeExclude extends AbstractSet {
35     /** List<Boolean and Pattern>
36      */

37     private List<Union2<Boolean JavaDoc, Pattern>> patterns = new ArrayList<Union2<Boolean JavaDoc, Pattern>> ();
38
39     private IncludeExclude () {
40     }
41
42     /** Reads the include/exclude set from a given reader.
43      * @param r reader
44      * @return set that accepts names based on include exclude from the file
45      */

46     public static Set create (Reader r) throws IOException {
47         IncludeExclude set = new IncludeExclude ();
48         
49         BufferedReader buf = new BufferedReader (r);
50         for (;;) {
51             String JavaDoc line = buf.readLine ();
52             if (line == null) break;
53             
54             line = line.trim ();
55             if (line.length () == 0 || line.startsWith ("#")) {
56                 continue;
57             }
58             
59             Boolean JavaDoc plus;
60             if (line.startsWith ("include ")) {
61                 line = line.substring (8);
62                 plus = Boolean.TRUE;
63             } else {
64                 if (line.startsWith ("exclude ")) {
65                     line = line.substring (8);
66                     plus = Boolean.FALSE;
67                 } else {
68                     throw new java.io.IOException JavaDoc ("Wrong line: " + line);
69                 }
70             }
71             
72             Pattern p = Pattern.compile (line);
73             
74             set.patterns.add (Union2.<Boolean JavaDoc,Pattern>createFirst(plus));
75             set.patterns.add (Union2.<Boolean JavaDoc,Pattern>createSecond(p));
76         }
77         
78         return set;
79     }
80     
81     
82     public Iterator iterator () {
83         return null;
84     }
85     
86     public int size () {
87         return 0;
88     }
89     
90     public boolean contains (Object JavaDoc o) {
91         String JavaDoc s = (String JavaDoc)o;
92         
93         boolean yes = false;
94         
95         Iterator<Union2<Boolean JavaDoc,Pattern>> it = patterns.iterator ();
96         while (it.hasNext ()) {
97             Boolean JavaDoc include = it.next ().first();
98             Pattern p = it.next ().second();
99             
100             Matcher m = p.matcher (s);
101             if (m.matches ()) {
102                 yes = include.booleanValue ();
103             }
104         }
105         
106         return yes;
107     }
108     
109 }
110
Popular Tags