KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > server > Option


1 /******************************************************************************
2  * Option.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.server;
11
12 import java.io.*;
13 import java.util.*;
14
15 import org.apache.log4j.Logger;
16 import org.jdom.*;
17 import org.apache.regexp.RE;
18 import org.apache.regexp.RESyntaxException;
19
20 /**
21  * An Option contains a set of deny and allow patterns.
22  *
23  * @author Eric Bloch
24  * @version 1.0
25  */

26 public class Option implements Serializable {
27
28     private static Logger mLogger = Logger.getLogger(Option.class);
29     
30     // No need to sync; access is read only after construction. These lists hold
31
// regexp objects.
32
private ArrayList allowList = null;
33     private ArrayList deniesList = null;
34
35     // These lists hold the pattern for the regexp objects.
36
private ArrayList allowSerializableList = null;
37     private ArrayList deniesSerializableList = null;
38
39     void init() {
40         if (allowList == null)
41             allowList = new ArrayList();
42         if (deniesList == null)
43             deniesList = new ArrayList();
44         if (allowSerializableList == null)
45             allowSerializableList = new ArrayList();
46         if (deniesSerializableList == null)
47             deniesSerializableList = new ArrayList();
48     }
49
50     Option() {
51         init();
52     }
53
54     /**
55      * Constructs a new option
56      * @param elt
57      */

58     Option(Element el) {
59         this();
60         addElement(el);
61     }
62     
63     /**
64      * Add new patterns to option
65      */

66     public void addElement(Element el) {
67         List list = el.getChildren();
68         ListIterator iter = list.listIterator();
69         
70         Element a = el.getChild("allow", el.getNamespace());
71         if (a != null)
72             addPatterns(allowList, allowSerializableList, a);
73         Element d = el.getChild("deny", el.getNamespace());
74         if (d != null)
75             addPatterns(deniesList, deniesSerializableList, d);
76     }
77
78     /**
79      * @param l list to add REs to
80      * @param element that has pattern element children
81      */

82     private void addPatterns(ArrayList list, ArrayList serializableList, Element elt) {
83         ListIterator iter =
84             elt.getChildren("pattern", elt.getNamespace()).listIterator();
85
86         while(iter.hasNext()) {
87             String JavaDoc p = ((Element)iter.next()).getTextNormalize();
88             mLogger.debug(elt.getName() + ": " + p);
89             try {
90                 RE re = new RE(p);
91                 list.add(re);
92                 serializableList.add(p);
93             } catch (RESyntaxException e) {
94                 mLogger.error("ignoring bad regexp syntax: " + p);
95                 continue;
96             }
97         }
98     }
99
100     /**
101      * @param val value to check against
102      * @param allow if true, an undefined option means that it is allowed, else
103      * it is denied.
104      * @return true if this option allows the given value
105      */

106     boolean allows(String JavaDoc val, boolean allow) {
107
108         mLogger.debug("checking: " + val);
109
110         // If we don't specify what's allowed, allow all.
111
if (!allowList.isEmpty()) {
112             allow = false;
113             ListIterator iter = allowList.listIterator();
114             while (iter.hasNext()) {
115                 RE re = (RE)iter.next();
116                 if (re.match(val)) {
117                     allow = true;
118                     break;
119                 }
120             }
121         }
122
123         if (allow) {
124             ListIterator iter = deniesList.listIterator();
125             while (iter.hasNext()) {
126                 RE re = (RE)iter.next();
127                 if (re.match(val)) {
128                     allow = false;
129                     break;
130                 }
131             }
132         }
133         return allow;
134     }
135
136
137     /**
138      * Handle object serialization.
139      */

140     private void writeObject(ObjectOutputStream out)
141         throws IOException {
142
143         ListIterator iter;
144
145         // write out allow
146
out.writeInt(allowSerializableList.size());
147         iter = allowSerializableList.listIterator();
148         while (iter.hasNext()) {
149             out.writeObject(iter.next());
150         }
151
152         // write out denies
153
out.writeInt(deniesSerializableList.size());
154         iter = deniesSerializableList.listIterator();
155         while (iter.hasNext()) {
156             out.writeObject(iter.next());
157         }
158     }
159
160     /**
161      * Handle object deserialization.
162      */

163     private void readObject(ObjectInputStream in)
164         throws IOException, ClassNotFoundException JavaDoc {
165         int size, i;
166
167         init();
168
169         size = in.readInt();
170         for (i=0; i < size; i++) {
171             String JavaDoc p = (String JavaDoc)in.readObject();
172             try {
173                 allowList.add(new RE(p));
174                 allowSerializableList.add(p);
175             } catch (RESyntaxException e) {
176                 mLogger.error("ignoring bad regexp syntax: " + p);
177                 continue;
178             }
179         }
180
181         size = in.readInt();
182         for (i=0; i < size; i++) {
183             String JavaDoc p = (String JavaDoc)in.readObject();
184             try {
185                 deniesList.add(new RE(p));
186                 deniesSerializableList.add(p);
187             } catch (RESyntaxException e) {
188                 mLogger.error("ignoring bad regexp syntax: " + p);
189                 continue;
190             }
191         }
192     }
193 }
194
Popular Tags