KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
27
28 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages;
29 import org.apache.directory.ldapstudio.browser.core.model.filter.parser.LdapFilterToken;
30
31
32 public class LdapFilter
33 {
34
35     private LdapFilterToken startToken;
36
37     private LdapFilterComponent filterComponent;
38
39     private LdapFilterToken stopToken;
40
41     private List JavaDoc otherTokens;
42
43
44     public LdapFilter()
45     {
46         this.startToken = null;
47         this.filterComponent = null;
48         this.stopToken = null;
49         this.otherTokens = new ArrayList JavaDoc( 2 );
50     }
51
52
53     public boolean setStartToken( LdapFilterToken startToken )
54     {
55         if ( this.startToken == null && startToken != null && startToken.getType() == LdapFilterToken.LPAR )
56         {
57             this.startToken = startToken;
58             return true;
59         }
60         else
61         {
62             return false;
63         }
64     }
65
66
67     public boolean setFilterComponent( LdapFilterComponent filterComponent )
68     {
69         if ( this.startToken != null && this.filterComponent == null && filterComponent != null )
70         {
71             this.filterComponent = filterComponent;
72             return true;
73         }
74         else
75         {
76             return false;
77         }
78     }
79
80
81     public boolean setStopToken( LdapFilterToken stopToken )
82     {
83         if ( this.startToken != null && this.stopToken == null && stopToken != null
84             && stopToken.getType() == LdapFilterToken.RPAR )
85         {
86             this.stopToken = stopToken;
87             return true;
88         }
89         else
90         {
91             return false;
92         }
93     }
94
95
96     public void addOtherToken( LdapFilterToken otherToken )
97     {
98         this.otherTokens.add( otherToken );
99     }
100
101
102     public LdapFilterToken getStartToken()
103     {
104         return this.startToken;
105     }
106
107
108     public LdapFilterComponent getFilterComponent()
109     {
110         return this.filterComponent;
111     }
112
113
114     public LdapFilterToken getStopToken()
115     {
116         return this.stopToken;
117     }
118
119
120     public LdapFilterToken[] getTokens()
121     {
122
123         // collect tokens
124
List JavaDoc tokenList = new ArrayList JavaDoc();
125         if ( this.startToken != null )
126         {
127             tokenList.add( this.startToken );
128         }
129         if ( this.stopToken != null )
130         {
131             tokenList.add( this.stopToken );
132         }
133         if ( this.filterComponent != null )
134         {
135             tokenList.addAll( Arrays.asList( this.filterComponent.getTokens() ) );
136         }
137         tokenList.addAll( this.otherTokens );
138
139         // sort tokens
140
LdapFilterToken[] tokens = ( LdapFilterToken[] ) tokenList.toArray( new LdapFilterToken[tokenList.size()] );
141         Arrays.sort( tokens );
142
143         // return
144
return tokens;
145     }
146
147
148     public boolean isValid()
149     {
150         return this.startToken != null && this.filterComponent != null && this.filterComponent.isValid()
151             && this.stopToken != null;
152     }
153
154
155     public LdapFilter[] getInvalidFilters()
156     {
157         if ( this.startToken == null || this.filterComponent == null || this.stopToken == null )
158         {
159             return new LdapFilter[]
160                 { this };
161         }
162         else
163         {
164             return this.filterComponent.getInvalidFilters();
165         }
166     }
167
168
169     public LdapFilter getFilter( int offset )
170     {
171         if ( this.startToken != null && this.startToken.getOffset() == offset )
172         {
173             return this;
174         }
175         else if ( this.stopToken != null && this.stopToken.getOffset() == offset )
176         {
177             return this;
178         }
179
180         if ( this.otherTokens != null && this.otherTokens.size() > 0 )
181         {
182             for ( int i = 0; i < this.otherTokens.size(); i++ )
183             {
184                 LdapFilterToken otherToken = ( LdapFilterToken ) this.otherTokens.get( i );
185                 if ( otherToken != null && otherToken.getOffset() <= offset
186                     && offset < otherToken.getOffset() + otherToken.getLength() )
187                 {
188                     return this;
189                 }
190             }
191         }
192
193         if ( this.filterComponent != null )
194         {
195             return this.filterComponent.getFilter( offset );
196         }
197
198         return this;
199     }
200
201
202     public String JavaDoc getInvalidCause()
203     {
204         if ( this.stopToken == null )
205         {
206             return BrowserCoreMessages.model_filter_missing_closing_parenthesis;
207         }
208         else if ( this.filterComponent == null )
209         {
210             return BrowserCoreMessages.model_filter_missing_filter_expression;
211         }
212         else
213         {
214             return this.filterComponent.getInvalidCause();
215         }
216     }
217
218
219     public String JavaDoc toString()
220     {
221         return ( this.startToken != null ? "(" : "" ) + ( filterComponent != null ? filterComponent.toString() : "" ) + ( this.stopToken != null ? ")" : "" ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
222
}
223
224 }
225
Popular Tags