KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > model > filter > LdapFilterComponent


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.core.model.filter;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.directory.ldapstudio.browser.core.model.filter.parser.LdapFilterToken;
30
31
32 public abstract class LdapFilterComponent
33 {
34
35     protected final LdapFilter parent;
36
37     protected LdapFilterToken startToken;
38
39     protected List JavaDoc filterList;
40
41
42     /**
43      *
44      *
45      * @param parent
46      * the parent filter of this filter component, not null.
47      */

48     public LdapFilterComponent( LdapFilter parent )
49     {
50         if ( parent == null )
51             throw new IllegalArgumentException JavaDoc( "parent is null" );
52
53         this.parent = parent;
54         this.startToken = null;
55         this.filterList = new ArrayList JavaDoc( 2 );
56     }
57
58
59     /**
60      * Returns the parent filter of this filter component.
61      *
62      * @return the parent filter, never null.
63      */

64     public final LdapFilter getParent()
65     {
66         return this.parent;
67     }
68
69
70     /**
71      * Sets the start token of the filter component. Checks if start token
72      * isn't set yet and if the given start token isn't null.
73      *
74      * @param startToken
75      * @return true if setting the start token was successful, false
76      * otherwise.
77      */

78     public boolean setStartToken( LdapFilterToken startToken )
79     {
80         if ( this.startToken == null && startToken != null )
81         {
82             this.startToken = startToken;
83             return true;
84         }
85         else
86         {
87             return false;
88         }
89     }
90
91
92     /**
93      * Returns the start token of this filter component.
94      *
95      * @return the start token or null if not set.
96      */

97     public final LdapFilterToken getStartToken()
98     {
99         return this.startToken;
100     }
101
102
103     /**
104      * Adds a filter to the list of subfilters. Checks if the start token
105      * was set before and if the filter isn't null.
106      *
107      * @param filter
108      * @return true if adding the filter was successful, false otherwise.
109      */

110     public boolean addFilter( LdapFilter filter )
111     {
112         if ( this.startToken != null && filter != null )
113         {
114             this.filterList.add( filter );
115             return true;
116         }
117         else
118         {
119             return false;
120         }
121     }
122
123
124     /**
125      * Returns the subfilters of this filter component.
126      *
127      * @return an array of subfilters or an empty array.
128      */

129     public LdapFilter[] getFilters()
130     {
131         LdapFilter[] filters = new LdapFilter[this.filterList.size()];
132         this.filterList.toArray( filters );
133         return filters;
134     }
135
136
137     /**
138      * Checks if this filter component including all subfilters is valid.
139      *
140      * @return true if this filter component is valid.
141      */

142     public boolean isValid()
143     {
144         if ( this.startToken == null )
145         {
146             return false;
147         }
148
149         if ( this.filterList.isEmpty() )
150         {
151             return false;
152         }
153
154         for ( Iterator JavaDoc it = filterList.iterator(); it.hasNext(); )
155         {
156             LdapFilter filter = ( LdapFilter ) it.next();
157             if ( filter == null || !filter.isValid() )
158             {
159                 return false;
160             }
161         }
162
163         return true;
164     }
165
166
167     /**
168      * Returns the invalid cause.
169      *
170      * @return the invalid cause.
171      */

172     public abstract String JavaDoc getInvalidCause();
173
174
175     /**
176      * Returns the invalid filters. This may be the whole parent filter or
177      * any of the subfilters.
178      *
179      * @return an array of invalid filters or an empty array if all filters
180      * are valid.
181      */

182     public LdapFilter[] getInvalidFilters()
183     {
184         if ( this.startToken == null || this.filterList.isEmpty() )
185         {
186             return new LdapFilter[]
187                 { this.parent };
188         }
189         else
190         {
191             List JavaDoc invalidFilterList = new ArrayList JavaDoc();
192             for ( Iterator JavaDoc it = this.filterList.iterator(); it.hasNext(); )
193             {
194                 LdapFilter filter = ( LdapFilter ) it.next();
195                 if ( filter != null )
196                 {
197                     invalidFilterList.addAll( Arrays.asList( filter.getInvalidFilters() ) );
198                 }
199             }
200             return ( LdapFilter[] ) invalidFilterList.toArray( new LdapFilter[invalidFilterList.size()] );
201         }
202     }
203
204
205     /**
206      * Returns all tokens of the filter component including all subfilters.
207      *
208      * @return an array of tokens of an empty array.
209      */

210     public LdapFilterToken[] getTokens()
211     {
212         // collect tokens
213
List JavaDoc tokenList = new ArrayList JavaDoc();
214         if ( this.startToken != null )
215         {
216             tokenList.add( this.startToken );
217         }
218         for ( Iterator JavaDoc it = this.filterList.iterator(); it.hasNext(); )
219         {
220             LdapFilter filter = ( LdapFilter ) it.next();
221             if ( filter != null )
222             {
223                 tokenList.addAll( Arrays.asList( filter.getTokens() ) );
224             }
225         }
226
227         // sort tokens
228
LdapFilterToken[] tokens = ( LdapFilterToken[] ) tokenList.toArray( new LdapFilterToken[tokenList.size()] );
229         Arrays.sort( tokens );
230
231         // return
232
return tokens;
233     }
234
235
236     /**
237      * Returns the filter at the given offset. This may be the whole parent
238      * filter or one of the subfilters.
239      *
240      * @param offset
241      * @return the filter at the given offset or null is offset is out of
242      * range.
243      */

244     public LdapFilter getFilter( int offset )
245     {
246         if ( this.startToken != null && this.startToken.getOffset() == offset )
247         {
248             return this.parent;
249         }
250         else if ( this.filterList != null || !this.filterList.isEmpty() )
251         {
252             for ( Iterator JavaDoc it = this.filterList.iterator(); it.hasNext(); )
253             {
254                 LdapFilter filter = ( LdapFilter ) it.next();
255                 if ( filter != null && filter.getFilter( offset ) != null )
256                 {
257                     return filter.getFilter( offset );
258                 }
259             }
260             return null;
261         }
262         else
263         {
264             return null;
265         }
266     }
267
268 }
269
Popular Tags