KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > security > filters > FilterTreeNode


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.security.filters;
66
67 import com.jcorporate.expresso.core.misc.ReusableChar;
68 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
69 import org.apache.log4j.Category;
70
71 import java.util.HashMap JavaDoc;
72 import java.util.Iterator JavaDoc;
73
74
75 /**
76  * This class provides a &quot;filter parse tree&quot; interface to the system.
77  * It is a dual action object, it's a union of a node, and a leaf. Thus allowing
78  * us to play with TreeMap/HashMap quickly.
79  * <p/>
80  * <i>Please Note:</i> This class is unsynchronized for performance reasons, please
81  * be careful when using in a multi-threaded environment outisde of the Filter
82  * class.
83  * </p>
84  *
85  * @author Michael Rimov
86  */

87 public class FilterTreeNode
88         implements java.io.Serializable JavaDoc {
89     private String JavaDoc replacementString = null;
90     private HashMap JavaDoc subnodes = null;
91
92     public FilterTreeNode() {
93     }
94
95     /**
96      * @return A replacement string if it exists at this node. otherwise null
97      */

98     public String JavaDoc getReplacementString() {
99         return replacementString;
100     }
101
102     /**
103      * Should be only called be the parse tree builder
104      * Set the replacement string that corresponds to this node
105      *
106      * @param newReplacementString The replacement string to put at this node
107      * @throws Exception if a replacement string already exists for this
108      */

109     public void setReplacementString(String JavaDoc newReplacementString)
110             throws Exception JavaDoc {
111
112         // if (replacementString != null) {
113
// throw new Exception("Replacement String already exists for this node:" +
114
// " Value = " + replacementString);
115
// }
116
replacementString = (newReplacementString);
117     }
118
119     /**
120      * Set a subnode based upon the character key. Should be only called
121      * by the parse tree builder.
122      *
123      * @param key The character key to look up the subnode.
124      * @param newSubNode The FilterTreeNode to exist on that subnode
125      * @throw Exception if subnode already exists for this key.
126      */

127     public void setSubnode(ReusableChar key, FilterTreeNode newSubnode)
128             throws Exception JavaDoc {
129         if (subnodes == null) {
130             subnodes = new HashMap JavaDoc();
131         }
132
133         // if (subnodes.containsKey(key)) {
134
// throw new Exception(key + " already exists as a subnode");
135
// }
136
subnodes.put(key, newSubnode);
137     }
138
139     /**
140      * @return true if a subnode exists for this key.
141      */

142     public boolean subnodeExists(ReusableChar key) {
143         if (subnodes == null) {
144             return false;
145         }
146
147         return subnodes.containsKey(key);
148     }
149
150     /**
151      * @param key The key to base this subnode after.
152      * @return the subnode that exists at this key, or null if it doesn't exist
153      */

154     public FilterTreeNode getSubnode(ReusableChar key) {
155         if (subnodes == null) {
156             return null;
157         }
158         if (subnodes.containsKey(key)) {
159             return (FilterTreeNode) subnodes.get(key);
160         } else {
161             return null;
162         }
163     }
164
165     /**
166      * Dumps the contents of the tree to the log. (ie dumps this node and all
167      * subnodes.
168      *
169      * @param Category - The log4j Category to dump to.
170      */

171     public void dumpNode(Category log) {
172         if (log.isDebugEnabled()) {
173             FastStringBuffer fsb = FastStringBuffer.getInstance();
174             try {
175                 dumpNode(fsb, 0);
176                 log.debug(fsb.toString());
177             } finally {
178                 fsb.release();
179                 fsb = null;
180             }
181         }
182     }
183
184     /**
185      * Dumps a printout of the node and subnodes to System.out
186      */

187     public void dumpNode() {
188         FastStringBuffer fsb = FastStringBuffer.getInstance();
189         try {
190             dumpNode(fsb, 0);
191             System.out.println(fsb.toString());
192         } finally {
193             fsb.release();
194             fsb = null;
195         }
196     }
197
198     /**
199      * Dumps the contents of the tree to the log. uses depth to format
200      * how many tabs in
201      */

202     public void dumpNode(FastStringBuffer stringBuffer, int depth) {
203         if (replacementString != null) {
204             this.padWithTabs(stringBuffer, depth);
205             stringBuffer.append("Node's ReplacementString:" +
206                     replacementString + "\n");
207         }
208         if (subnodes != null) {
209             for (Iterator JavaDoc i = subnodes.keySet().iterator(); i.hasNext();) {
210                 Character JavaDoc c = (Character JavaDoc) i.next();
211                 FilterTreeNode ftn = (FilterTreeNode) subnodes.get(c);
212
213                 if (ftn != null) {
214                     this.padWithTabs(stringBuffer, depth);
215                     stringBuffer.append("dump for subnode: " + c + "\n");
216                     ftn.dumpNode(stringBuffer, depth + 1);
217                 }
218             }
219         }
220     }
221
222     /**
223      * Formatter helping function to pad in for each node.
224      */

225     private void padWithTabs(FastStringBuffer stringBuffer, int numTabs) {
226         for (int i = 0; i < numTabs; i++) {
227             stringBuffer.append('\t');
228         }
229     }
230 }
Popular Tags