KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > util > strainer > FilterReader


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 /*
14  * Created on Jun 18, 2003
15  *
16  */

17 package com.openedit.util.strainer;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.naming.ConfigurationException JavaDoc;
23
24 import com.openedit.OpenEditException;
25 import com.openedit.config.Configuration;
26
27
28 /**
29  * DOCUMENT ME!
30  *
31  * @author cburkey
32  */

33 public class FilterReader
34 {
35     /**
36      * Read the given configuration as a parent of a collection of filters.
37      *
38      * @param inConfig The configuration
39      *
40      * @return A filter, or <code>null</code>, depending on the configuration passed in:
41      * <ul><li><code>null</code> if the configuration is null or has no children;
42      * <li>the corresponding filter, if the configuration has exactly one child; or
43      * <li>an <code>AndFilter</code> containing all the children, if there is more than
44      * one child. </ul>
45      *
46      * @throws ConfigurationException
47      */

48     public Filter readFilterCollection(Configuration inConfig) throws OpenEditException
49     {
50         Filter filter = null;
51
52         if (inConfig != null)
53         {
54             List JavaDoc elements = inConfig.getChildren();
55             switch (elements.size())
56             {
57                 case 0:
58                     //this means that this config is letting everything pass
59
filter = new BlankFilter();
60                     break;
61
62                 case 1:
63                     filter = readFilter((Configuration) elements.get(0));
64
65                     break;
66
67                 default:
68
69                     Filter[] filters = new Filter[elements.size()];
70
71                     for (int i = 0; i < filters.length; i++)
72                     {
73                         filters[i] = readFilter((Configuration) elements.get(i));
74                     }
75
76                     filter = new AndFilter(filters);
77
78                     break;
79             }
80         }
81
82         return filter;
83     }
84
85     /**
86      * Read the given configuration, and deserialize it into a filter.
87      *
88      * @param inConfig The configuration
89      *
90      * @return Filter The decoded filter
91      *
92      * @throws ConfigurationException If the configuration could not be decoded successfully
93      */

94     protected Filter readFilter(Configuration inConfig) throws OpenEditException
95     {
96         if (inConfig == null)
97         {
98             return null;
99         }
100
101         // FIXME: Should make these into XML factories.
102
Filter result = null;
103         String JavaDoc elemName = inConfig.getName();
104         List JavaDoc elements = inConfig.getChildren();
105         if (elemName.equals("and"))
106         {
107             result = new AndFilter(readSubFilters(inConfig));
108         }
109         else if (elemName.equals("or"))
110         {
111             result = new OrFilter(readSubFilters(inConfig));
112         }
113         else if (elemName.equals("not"))
114         {
115             if (elements.size() > 1)
116             {
117                 throw new OpenEditException("<not> element must have one or less children");
118             }
119             if ( elements.size() == 0)
120             {
121                 result = new NotFilter();
122             }
123             else
124             {
125                 result = new NotFilter(readFilter((Configuration) elements.get(0)));
126             }
127         }
128         else if (elemName.equals("user"))
129         {
130             result = new UserFilter(inConfig.getAttribute("name"));
131         }
132         else if (elemName.equals("group"))
133         {
134             result = new GroupFilter(inConfig.getAttribute("name"));
135         }
136         else if (elemName.equals("permission"))
137         {
138             result = new PermissionFilter(inConfig.getAttribute("name"));
139         }
140         else if (elemName.equals("path"))
141         {
142             result = new PathFilter(inConfig.getAttribute("name"));
143         }
144         else if (elemName.equals("page-property"))
145         {
146             result = new PagePropertyFilter(
147                     inConfig.getAttribute("name"), inConfig.getAttribute("equals"));
148         }
149         else if (elemName.equals("context-variable"))
150         {
151             result = new ContextVariableFilter(
152                     inConfig.getAttribute("name"), inConfig.getAttribute("equals"));
153         }
154         else if (elemName.equals("request-attribute"))
155         {
156             result = new RequestAttributeFilter(
157                     inConfig.getAttribute("name"), inConfig.getAttribute("equals"));
158         }
159         else if (elemName.equals("blank"))
160         {
161             result = new BlankFilter();
162         }
163         else
164         {
165             throw new OpenEditException("Unrecognized filter element <" + elemName + ">");
166         }
167
168         return result;
169     }
170
171     /**
172      * Read the children of the given configuration as an array of filters.
173      *
174      * @param inConfig The configuration whose children should be parsed
175      *
176      * @return Filter[] The decoded filters
177      *
178      * @throws ConfigurationException If the configuration could not be decoded successfully
179      */

180     protected Filter[] readSubFilters(Configuration inConfig) throws OpenEditException
181     {
182         List JavaDoc elements = inConfig.getChildren();
183         Filter[] subFilters = new Filter[elements.size()];
184         int index = 0;
185
186         for (Iterator JavaDoc iter = elements.iterator(); iter.hasNext();)
187         {
188             subFilters[index] = readFilter((Configuration) iter.next());
189             index++;
190         }
191
192         return subFilters;
193     }
194 }
195
Popular Tags