KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.UnsupportedEncodingException JavaDoc;
25 import java.net.URLEncoder JavaDoc;
26
27 import org.apache.commons.codec.binary.Base64;
28 import org.apache.commons.codec.binary.Hex;
29 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants;
30 import org.apache.directory.ldapstudio.browser.core.model.IValue;
31
32
33 /**
34  * Utilities for LDAP related encoding and decoding.
35  *
36  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
37  * @version $Rev$, $Date$
38  */

39 public class LdifUtils
40 {
41
42
43     /**
44      * Encodes the given string to UTF-8
45      *
46      * @param s the string to encode
47      *
48      * @return the byte[] the encoded value
49      */

50     public static byte[] utf8encode( String JavaDoc s )
51     {
52         try
53         {
54             return s.getBytes( "UTF-8" );
55         }
56         catch ( UnsupportedEncodingException JavaDoc e )
57         {
58             return s.getBytes();
59         }
60     }
61
62
63     /**
64      * Encodes the given string into URL format.
65      *
66      * @param s the string to encode
67      *
68      * @return the string the URL encoded string
69      */

70     public static String JavaDoc urlEncode( String JavaDoc s )
71     {
72         try
73         {
74             return URLEncoder.encode( s, "UTF-8" );
75         }
76         catch ( UnsupportedEncodingException JavaDoc e )
77         {
78             return s;
79         }
80     }
81
82
83     /**
84      * Encodes the given byte array using BASE-64 encoding.
85      *
86      * @param b the b the byte array to encode
87      *
88      * @return the BASE-64 encoded string
89      */

90     public static String JavaDoc base64encode( byte[] b )
91     {
92         return utf8decode( Base64.encodeBase64( b ) );
93     }
94
95
96     /**
97      * Encodes the given byte array to a sequence of
98      * its hex values.
99      *
100      * @param data the data to encode
101      * @return the HEX encoded string
102      */

103     public static String JavaDoc hexEncode( byte[] data )
104     {
105         if ( data == null )
106             return null;
107
108         char[] c = Hex.encodeHex( data );
109         String JavaDoc s = new String JavaDoc( c );
110         return s;
111
112         // StringBuffer sb = new StringBuffer(data.length*3);
113
// for(int i=0; i<data.length; i++) {
114
// int b = (int)data[i];
115
// if(b<0) b=256+b;
116
// String s = Integer.toHexString(b)/*.toUpperCase()*/;
117
// if(s.length() ==1) s = "0" + s; //$NON-NLS-1$
118
// sb.append(s);
119
// if(i+1 < data.length)
120
// sb.append(' ');
121
// }
122
// return(sb.toString());
123
}
124
125
126     /**
127      * Decodes the given UTF-8 byte array to an string.
128      *
129      * @param b the b the byte array to decode
130      *
131      * @return the decoded string
132      */

133     public static String JavaDoc utf8decode( byte[] b )
134     {
135         try
136         {
137             return new String JavaDoc( b, "UTF-8" );
138         }
139         catch ( UnsupportedEncodingException JavaDoc e )
140         {
141             return new String JavaDoc( b );
142         }
143     }
144
145
146     /**
147      * Decodes the given BASE-64 encoded string to its
148      * bytes presentation.
149      *
150      * @param s the s the BASE-64 encoded string
151      *
152      * @return the byte[] the decoded byte array
153      */

154     public static byte[] base64decodeToByteArray( String JavaDoc s )
155     {
156         return Base64.decodeBase64( utf8encode( s ) );
157     }
158
159
160     /**
161      * Checks if the given distinguished name must be encoded.
162      *
163      * @param dn the dn to check
164      *
165      * @return true, if must encode DN
166      */

167     public static boolean mustEncodeDN( String JavaDoc dn )
168     {
169         return mustEncode( dn );
170     }
171
172
173     /**
174      * Checks if the given string must be encoded to be
175      * used in an LDIF.
176      *
177      * @param value the value to check
178      *
179      * @return true, if must encode
180      */

181     public static boolean mustEncode( String JavaDoc value )
182     {
183         if ( value == null || value.length() < 1 )
184         {
185             return false;
186         }
187
188         if ( value.startsWith( " " ) || value.startsWith( ":" ) || value.startsWith( "<" ) )
189         {
190             return true;
191         }
192         if ( value.endsWith( " " ) )
193         {
194             return true;
195         }
196
197         for ( int i = 0; i < value.length(); i++ )
198         {
199             if ( value.charAt( i ) == '\r' || value.charAt( i ) == '\n' || value.charAt( i ) == '\u0000'
200                 || value.charAt( i ) > '\u007F' )
201             {
202                 return true;
203             }
204         }
205
206         return false;
207     }
208
209
210     /**
211      * Gets the string value from the given {@link IValue}. If the given
212      * {@link IValue} is binary is is encoded according to the regquested
213      * encoding type.
214      *
215      * @param value the value
216      * @param binaryEncoding the binary encoding type
217      *
218      * @return the string value
219      */

220     public static String JavaDoc getStringValue( IValue value, int binaryEncoding )
221     {
222         String JavaDoc s = value.getStringValue();
223         if ( value.isBinary() && LdifUtils.mustEncode( s ) )
224         {
225             byte[] binary = value.getBinaryValue();
226             if ( binaryEncoding == BrowserCoreConstants.BINARYENCODING_BASE64 )
227             {
228                 s = LdifUtils.base64encode( binary );
229             }
230             else if ( binaryEncoding == BrowserCoreConstants.BINARYENCODING_HEX )
231             {
232                 s = LdifUtils.hexEncode( binary );
233             }
234             else
235             {
236                 s = BrowserCoreConstants.BINARY;
237             }
238         }
239         return s;
240     }
241
242 }
243
Popular Tags