KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ecs > filter > StringFilter


1 /*
2  * ====================================================================
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Jakarta Element Construction Set",
29  * "Jakarta ECS" , and "Apache Software Foundation" must not be used
30  * to endorse or promote products derived
31  * from this software without prior written permission. For written
32  * permission, please contact apache@apache.org.
33  *
34  * 5. Products derived from this software may not be called "Apache",
35  * "Jakarta Element Construction Set" nor "Jakarta ECS" nor may "Apache"
36  * appear in their names without prior written permission of the Apache Group.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals on behalf of the Apache Software Foundation. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  *
57  */

58 package org.apache.ecs.filter;
59
60 import org.apache.ecs.Filter;
61 import java.text.StringCharacterIterator JavaDoc;
62 import java.text.CharacterIterator JavaDoc;
63
64 /**
65     Stupid implementation of Filter interface to demonstrate how easy <br>
66     it is to create your own filters. <b>This should NOT be used</b> in/for<br>
67     anything real. Anyone want to implement a regex filter?
68
69     @version $Id: StringFilter.java,v 1.5 2003/04/27 09:28:56 rdonkin Exp $
70     @author <a HREF="mailto:snagy@servletapi.com">Stephan Nagy</a>
71     @author <a HREF="mailto:jon@clearink.com">Jon S. Stevens</a>
72 */

73 public class StringFilter extends java.util.Hashtable JavaDoc implements Filter
74 {
75
76     public StringFilter()
77     {
78         super(4);
79     }
80
81     /** Returns the name of the filter */
82     public String JavaDoc getInfo()
83     {
84         return "StringFilter";
85     }
86
87     /**
88         this method actually performs the filtering.
89     */

90     public String JavaDoc process(String JavaDoc to_process)
91     { System.out.println("\nString to Process in StringFilter = "+to_process);
92         String JavaDoc[] value = split(to_process);
93         StringBuffer JavaDoc new_value = new StringBuffer JavaDoc();
94         for(int x = 0; x < value.length; x++)
95         {
96             if(hasAttribute(value[x]))
97                 new_value.append((String JavaDoc)get(value[x]));
98             else
99                 new_value.append(value[x]);
100             if(x != value.length - 1)
101                 new_value.append(" ");
102         }
103         return(new_value.toString());
104     }
105
106     /**
107         Put a filter somewhere we can get to it.
108     */

109     public Filter addAttribute(String JavaDoc attribute,Object JavaDoc entity)
110     {
111         put(attribute,entity);
112         return(this);
113     }
114
115     /**
116         Get rid of a current filter.
117     */

118     public Filter removeAttribute(String JavaDoc attribute)
119     {
120         try
121         {
122             remove(attribute);
123         }
124         catch(NullPointerException JavaDoc exc)
125         { // don't really care if this throws a null pointer exception
126
}
127         return(this);
128     }
129
130     /**
131         Does the filter filter this?
132     */

133     public boolean hasAttribute(String JavaDoc attribute)
134     {
135         return(containsKey(attribute));
136     }
137
138     /**
139         Need a way to parse the stream so we can do string comparisons instead
140         of character comparisons.
141     */

142     private String JavaDoc[] split(String JavaDoc to_split)
143     {
144
145         if ( to_split == null || to_split.length() == 0 )
146         {
147             String JavaDoc[] array = new String JavaDoc[0];
148             return array;
149         }
150
151         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(to_split.length()+50);
152         StringCharacterIterator JavaDoc sci = new StringCharacterIterator JavaDoc(to_split);
153         int length = 0;
154
155         for (char c = sci.first(); c != CharacterIterator.DONE; c = sci.next())
156         {
157             if(String.valueOf(c).equals(" "))
158                 length++;
159             else if(sci.getEndIndex()-1 == sci.getIndex())
160                 length++;
161         }
162
163         String JavaDoc[] array = new String JavaDoc[length];
164         length = 0;
165         String JavaDoc tmp = new String JavaDoc();
166         for (char c = sci.first(); c!= CharacterIterator.DONE; c = sci.next())
167         {
168             if(String.valueOf(c).equals(" "))
169             {
170                 array[length] = tmp;
171                 tmp = new String JavaDoc();
172                 length++;
173             }
174             else if(sci.getEndIndex()-1 == sci.getIndex())
175             {
176                 tmp = tmp+String.valueOf(sci.last());
177                 array[length] = tmp;
178                 tmp = new String JavaDoc();
179                 length++;
180             }
181             else
182                 tmp += String.valueOf(c);
183         }
184         return(array);
185     }
186 }
187
Popular Tags