KickJava   Java API By Example, From Geeks To Geeks.

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

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

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

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