KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > dsmlv2 > request > SearchRequestDsml


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 package org.apache.directory.ldapstudio.dsmlv2.request;
21
22
23 import java.util.List JavaDoc;
24
25 import javax.naming.NamingEnumeration JavaDoc;
26 import javax.naming.directory.Attributes JavaDoc;
27
28 import org.apache.directory.ldapstudio.dsmlv2.ParserUtils;
29 import org.apache.directory.shared.ldap.codec.AttributeValueAssertion;
30 import org.apache.directory.shared.ldap.codec.LdapConstants;
31 import org.apache.directory.shared.ldap.codec.LdapMessage;
32 import org.apache.directory.shared.ldap.codec.search.AndFilter;
33 import org.apache.directory.shared.ldap.codec.search.AttributeValueAssertionFilter;
34 import org.apache.directory.shared.ldap.codec.search.ExtensibleMatchFilter;
35 import org.apache.directory.shared.ldap.codec.search.Filter;
36 import org.apache.directory.shared.ldap.codec.search.NotFilter;
37 import org.apache.directory.shared.ldap.codec.search.OrFilter;
38 import org.apache.directory.shared.ldap.codec.search.PresentFilter;
39 import org.apache.directory.shared.ldap.codec.search.SearchRequest;
40 import org.apache.directory.shared.ldap.codec.search.SubstringFilter;
41 import org.apache.directory.shared.ldap.message.ScopeEnum;
42 import org.dom4j.Element;
43 import org.dom4j.Namespace;
44 import org.dom4j.QName;
45
46
47 /**
48  * DSML Decorator for SearchRequest
49  *
50  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
51  * @version $Rev$, $Date$
52  */

53 public class SearchRequestDsml extends AbstractRequestDsml
54 {
55     /**
56      * Creates a new instance of SearchRequestDsml.
57      *
58      * @param ldapMessage
59      * the message to decorate
60      */

61     public SearchRequestDsml( LdapMessage ldapMessage )
62     {
63         super( ldapMessage );
64     }
65
66
67     /**
68      * {@inheritDoc}
69      */

70     public int getMessageType()
71     {
72         return instance.getMessageType();
73     }
74
75
76     /**
77      * {@inheritDoc}
78      */

79     public Element toDsml( Element root )
80     {
81         Element element = super.toDsml( root );
82
83         SearchRequest request = ( SearchRequest ) instance;
84
85         // DN
86
if ( request.getBaseObject() != null )
87         {
88             element.addAttribute( "dn", request.getBaseObject().toString() );
89         }
90
91         // Scope
92
ScopeEnum scope = request.getScope();
93         if ( scope != null )
94         {
95             if ( scope == ScopeEnum.BASE_OBJECT )
96             {
97                 element.addAttribute( "scope", "baseObject" );
98             }
99             else if ( scope == ScopeEnum.SINGLE_LEVEL )
100             {
101                 element.addAttribute( "scope", "singleLevel" );
102             }
103             else if ( scope == ScopeEnum.WHOLE_SUBTREE )
104             {
105                 element.addAttribute( "scope", "wholeSubtree" );
106             }
107         }
108
109         // DerefAliases
110
int derefAliases = request.getDerefAliases();
111         if ( derefAliases == LdapConstants.NEVER_DEREF_ALIASES )
112         {
113             element.addAttribute( "derefAliases", "neverDerefAliases" );
114         }
115         else if ( derefAliases == LdapConstants.DEREF_IN_SEARCHING )
116         {
117             element.addAttribute( "derefAliases", "derefInSearching" );
118         }
119         else if ( derefAliases == LdapConstants.DEREF_FINDING_BASE_OBJ )
120         {
121             element.addAttribute( "derefAliases", "derefFindingBaseObj" );
122         }
123         else if ( derefAliases == LdapConstants.DEREF_ALWAYS )
124         {
125             element.addAttribute( "derefAliases", "derefAlways" );
126         }
127
128         // SizeLimit
129
if ( request.getSizeLimit() != 0 )
130         {
131             element.addAttribute( "sizeLimit", "" + request.getSizeLimit() );
132         }
133
134         // TimeLimit
135
if ( request.getTimeLimit() != 0 )
136         {
137             element.addAttribute( "timeLimit", "" + request.getTimeLimit() );
138         }
139
140         // TypesOnly
141
if ( request.isTypesOnly() )
142         {
143             element.addAttribute( "typesOnly", "true" );
144         }
145
146         // Filter
147
Element filterElement = element.addElement( "filter" );
148         toDsml( filterElement, request.getFilter() );
149
150         // Attributes
151
Attributes JavaDoc attributes = request.getAttributes();
152         if ( attributes.size() > 0 )
153         {
154             Element attributesElement = element.addElement( "attributes" );
155
156             NamingEnumeration JavaDoc<String JavaDoc> ids = attributes.getIDs();
157             while ( ids.hasMoreElements() )
158             {
159                 attributesElement.addElement( "attribute" ).addAttribute( "name", ids.nextElement() );
160             }
161         }
162
163         return element;
164     }
165
166
167     /**
168      * Recursively converts the filter of the Search Request into a DSML representation and adds
169      * it to the XML Element corresponding to the Search Request
170      *
171      * @param element
172      * the parent Element
173      * @param filter
174      * the filter to convert
175      */

176     private void toDsml( Element element, Filter filter )
177     {
178         // AND FILTER
179
if ( filter instanceof AndFilter )
180         {
181             Element newElement = element.addElement( "and" );
182
183             List JavaDoc<Filter> filterList = ( ( AndFilter ) filter ).getAndFilter();
184             for ( int i = 0; i < filterList.size(); i++ )
185             {
186                 toDsml( newElement, filterList.get( i ) );
187             }
188         }
189         
190         // OR FILTER
191
else if ( filter instanceof OrFilter )
192         {
193             Element newElement = element.addElement( "or" );
194
195             List JavaDoc<Filter> filterList = ( ( OrFilter ) filter ).getOrFilter();
196             for ( int i = 0; i < filterList.size(); i++ )
197             {
198                 toDsml( newElement, filterList.get( i ) );
199             }
200         }
201         
202         // NOT FILTER
203
else if ( filter instanceof NotFilter )
204         {
205             Element newElement = element.addElement( "not" );
206
207             toDsml( newElement, ( ( NotFilter ) filter ).getNotFilter() );
208         }
209         
210         // SUBSTRING FILTER
211
else if ( filter instanceof SubstringFilter )
212         {
213             Element newElement = element.addElement( "substrings" );
214             
215             SubstringFilter substringFilter = ( SubstringFilter ) filter;
216             
217             newElement.addAttribute("name", substringFilter.getType() );
218             
219             String JavaDoc initial = substringFilter.getInitialSubstrings();
220             if ( ( initial != null ) && ( !"".equals( initial ) ) )
221             {
222                 newElement.addElement( "initial" ).setText( initial );
223             }
224             
225             List JavaDoc<String JavaDoc> anyList = substringFilter.getAnySubstrings();
226             for ( int i = 0; i < anyList.size(); i++ )
227             {
228                 newElement.addElement( "any" ).setText( anyList.get( i ) );
229             }
230             
231             String JavaDoc finalString = substringFilter.getFinalSubstrings();
232             if ( ( finalString != null ) && ( !"".equals( finalString ) ) )
233             {
234                 newElement.addElement( "final" ).setText( finalString );
235             }
236         }
237         
238         // APPROXMATCH, EQUALITYMATCH, GREATEROREQUALS & LESSOREQUAL FILTERS
239
else if ( filter instanceof AttributeValueAssertionFilter )
240         {
241             AttributeValueAssertionFilter avaFilter = ( AttributeValueAssertionFilter ) filter;
242             
243             Element newElement = null;
244             int filterType = avaFilter.getFilterType();
245             if ( filterType == LdapConstants.APPROX_MATCH_FILTER )
246             {
247                 newElement = element.addElement( "approxMatch" );
248             }
249             else if ( filterType == LdapConstants.EQUALITY_MATCH_FILTER )
250             {
251                 newElement = element.addElement( "equalityMatch" );
252             }
253             else if ( filterType == LdapConstants.GREATER_OR_EQUAL_FILTER )
254             {
255                 newElement = element.addElement( "greaterOrEqual" );
256             }
257             else if ( filterType == LdapConstants.LESS_OR_EQUAL_FILTER )
258             {
259                 newElement = element.addElement( "lessOrEqual" );
260             }
261             
262             AttributeValueAssertion assertion = avaFilter.getAssertion();
263             if ( assertion != null )
264             {
265                 newElement.addAttribute( "name", assertion.getAttributeDesc() );
266                 
267                 Object JavaDoc value = assertion.getAssertionValue();
268                 if ( value != null )
269                 {
270                     if ( ParserUtils.needsBase64Encoding( value ) )
271                     {
272                         Namespace xsdNamespace = new Namespace( "xsd", ParserUtils.XML_SCHEMA_URI );
273                         Namespace xsiNamespace = new Namespace( "xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI );
274                         element.getDocument().getRootElement().add( xsdNamespace );
275                         element.getDocument().getRootElement().add( xsiNamespace );
276
277                         Element valueElement =
278                             newElement.addElement( "value" ).addText( ParserUtils.base64Encode( value ) );
279                         valueElement
280                             .addAttribute( new QName( "type", xsiNamespace ), "xsd:" + ParserUtils.BASE64BINARY );
281                     }
282                     else
283                     {
284                         newElement.addElement( "value" ).setText( (String JavaDoc) value );
285                     }
286                 }
287             }
288         }
289         
290         // PRESENT FILTER
291
else if ( filter instanceof PresentFilter )
292         {
293             Element newElement = element.addElement( "present" );
294             
295             newElement.addAttribute( "name", ( ( PresentFilter ) filter ).getAttributeDescription() );
296         }
297         
298         // EXTENSIBLEMATCH
299
else if ( filter instanceof ExtensibleMatchFilter )
300         {
301             Element newElement = element.addElement( "extensibleMatch" );
302             
303             ExtensibleMatchFilter extensibleMatchFilter = ( ExtensibleMatchFilter ) filter;
304             
305             Object JavaDoc value = extensibleMatchFilter.getMatchValue();
306             if ( value != null )
307             {
308                 if ( ParserUtils.needsBase64Encoding( value ) )
309                 {
310                     Namespace xsdNamespace = new Namespace( "xsd", ParserUtils.XML_SCHEMA_URI );
311                     Namespace xsiNamespace = new Namespace( "xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI );
312                     element.getDocument().getRootElement().add( xsdNamespace );
313                     element.getDocument().getRootElement().add( xsiNamespace );
314
315                     Element valueElement =
316                         newElement.addElement( "value" ).addText( ParserUtils.base64Encode( value ) );
317                     valueElement
318                         .addAttribute( new QName( "type", xsiNamespace ), "xsd:" + ParserUtils.BASE64BINARY );
319                 }
320                 else
321                 {
322                     newElement.addElement( "value" ).setText( (String JavaDoc) value );
323                 }
324             }
325             
326             if ( extensibleMatchFilter.isDnAttributes() )
327             {
328                 newElement.addAttribute( "dnAttributes", "true" );
329             }
330             
331             String JavaDoc matchingRule = extensibleMatchFilter.getMatchingRule();
332             if ( ( matchingRule != null ) && ( "".equals( matchingRule ) ) )
333             {
334                 newElement.addAttribute( "matchingRule", matchingRule );
335             }
336         }
337     }
338 }
339
Popular Tags