KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > utils > LdapFilterUtils


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.utils;
22
23
24 import org.apache.directory.ldapstudio.browser.core.model.IValue;
25
26
27 /**
28  * Utilies for filter handling.
29  *
30  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
31  * @version $Rev$, $Date$
32  */

33 public class LdapFilterUtils
34 {
35
36     /**
37      * Creates a filter from the given value.
38      *
39      * @param value the value
40      *
41      * @return the filter
42      */

43     public static String JavaDoc getFilter( IValue value )
44     {
45         if ( value.isString() )
46         {
47             return "(" + value.getAttribute().getDescription() + "=" + getEncodedValue( value.getStringValue() ) + ")";
48         }
49         else
50         {
51             StringBuffer JavaDoc filter = new StringBuffer JavaDoc();
52             filter.append( "(" );
53             filter.append( value.getAttribute().getDescription() );
54             filter.append( "=" );
55
56             byte[] bytes = value.getBinaryValue();
57             for ( int i = 0; i < bytes.length; i++ )
58             {
59                 int b = ( int ) bytes[i];
60                 if ( b < 0 )
61                 {
62                     b = 256 + b;
63                 }
64                 String JavaDoc s = Integer.toHexString( b );
65                 filter.append( "\\" );
66                 if ( s.length() == 1 )
67                 {
68                     filter.append( "0" );
69                 }
70                 filter.append( s );
71             }
72
73             filter.append( ")" );
74             return filter.toString();
75         }
76     }
77
78
79     /**
80      * Encodes the given value according RFC2254.
81      *
82      * <pre>
83      * If a value should contain any of the following characters
84      * Character ASCII value
85      * ---------------------------
86      * * 0x2a
87      * ( 0x28
88      * ) 0x29
89      * \ 0x5c
90      * NUL 0x00
91      * the character must be encoded as the backslash '\' character (ASCII
92      * 0x5c) followed by the two hexadecimal digits representing the ASCII
93      * value of the encoded character. The case of the two hexadecimal
94      * digits is not significant.
95      * </pre>
96      *
97      * @param value the value
98      *
99      * @return the encoded value
100      */

101     public static String JavaDoc getEncodedValue( String JavaDoc value )
102     {
103         value = value.replaceAll( "\\\\", "\\\\5c" );
104         value = value.replaceAll( "" + '\u0000', "\\\\00" );
105         value = value.replaceAll( "\\*", "\\\\2a" );
106         value = value.replaceAll( "\\(", "\\\\28" );
107         value = value.replaceAll( "\\)", "\\\\29" );
108         return value;
109     }
110
111 }
112
Popular Tags