KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > diff > ListBasedValidator


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.diff;
34
35 import java.io.*;
36 import java.util.*;
37
38 import org.apache.log4j.*;
39
40 public class ListBasedValidator implements Validator {
41     private Collection allowedElements = new HashSet();
42
43     public ListBasedValidator() {
44         // Do nothing
45
}
46
47     public ListBasedValidator(String JavaDoc filename) throws IOException {
48         load(filename);
49     }
50
51     public ListBasedValidator(File file) throws IOException {
52         load(file);
53     }
54
55     public ListBasedValidator(BufferedReader in) throws IOException {
56         load(in);
57     }
58     
59     public void load(String JavaDoc filename) throws IOException {
60         BufferedReader in = null;
61         
62         try {
63             in = new BufferedReader(new FileReader(filename));
64             load(in);
65         } catch (FileNotFoundException ex) {
66             // Ignore
67
} finally {
68             if (in != null) {
69                 in.close();
70             }
71         }
72     }
73         
74     public void load(File file) throws IOException {
75         BufferedReader in = null;
76         
77         try {
78             in = new BufferedReader(new FileReader(file));
79             load(in);
80         } catch (FileNotFoundException ex) {
81             // Ignore
82
} finally {
83             if (in != null) {
84                 in.close();
85             }
86         }
87     }
88
89     public void load(BufferedReader in) throws IOException {
90         String JavaDoc line;
91         while ((line = in.readLine()) != null) {
92             if (line.length() > 0) {
93                 line = line.trim();
94                 int pos = line.lastIndexOf(" [");
95                 if (pos != -1) {
96                     allowedElements.add(line.substring(0, pos));
97                 } else {
98                     allowedElements.add(line);
99                 }
100             }
101         }
102     }
103     
104     public boolean isPackageAllowed(String JavaDoc name) {
105         return isAllowed(name);
106     }
107
108     public boolean isClassAllowed(String JavaDoc name) {
109         return isAllowed(name);
110     }
111
112     public boolean isFeatureAllowed(String JavaDoc name) {
113         return isAllowed(name);
114     }
115
116     public boolean isAllowed(String JavaDoc name) {
117         Logger.getLogger(getClass()).debug("IsAllowed(\"" + name + "\")");
118         Logger.getLogger(getClass()).debug("allowed_elements.size() = " + allowedElements.size());
119         Logger.getLogger(getClass()).debug("allowed_elements.contains(\"" + name + "\") = " + allowedElements.contains(name));
120         return allowedElements.size() == 0 || allowedElements.contains(name);
121     }
122 }
123
Popular Tags