KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.XMLDecoder JavaDoc;
25 import java.beans.XMLEncoder JavaDoc;
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33
34 public class Utils
35 {
36
37     public static String JavaDoc[] stringToArray( String JavaDoc s )
38     {
39         if ( s == null )
40         {
41             return null;
42         }
43         else
44         {
45             List JavaDoc attributeList = new ArrayList JavaDoc();
46
47             StringBuffer JavaDoc temp = new StringBuffer JavaDoc();
48             for ( int i = 0; i < s.length(); i++ )
49             {
50                 char c = s.charAt( i );
51
52                 if ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) || ( c >= '0' && c <= '9' ) || c == '-'
53                     || c == '.' || c == ';' )
54                 {
55                     temp.append( c );
56                 }
57                 else
58                 {
59                     if ( temp.length() > 0 )
60                     {
61                         attributeList.add( temp.toString() );
62                         temp = new StringBuffer JavaDoc();
63                     }
64                 }
65             }
66             if ( temp.length() > 0 )
67             {
68                 attributeList.add( temp.toString() );
69             }
70
71             return ( String JavaDoc[] ) attributeList.toArray( new String JavaDoc[attributeList.size()] );
72         }
73
74         // else {
75
// s = s.trim();
76
// s = s.replaceAll(" ", "");
77
// s = s.replaceAll(",,", ",");
78
// String[] a;
79
// if(s.length() > 0) {
80
// a = s.split(",", 0);
81
// }
82
// else {
83
// a = new String[0];
84
// }
85
// return a;
86
// }
87
}
88
89
90     public static String JavaDoc arrayToString( String JavaDoc[] array )
91     {
92         if ( array == null || array.length == 0 )
93         {
94             return "";
95         }
96         else
97         {
98             StringBuffer JavaDoc sb = new StringBuffer JavaDoc( array[0] );
99             for ( int i = 1; i < array.length; i++ )
100             {
101                 sb.append( ", " );
102                 sb.append( array[i] );
103             }
104             return sb.toString();
105         }
106     }
107
108
109     public static boolean equals( byte[] data1, byte[] data2 )
110     {
111         if ( data1 == data2 )
112             return true;
113         if ( data1 == null || data2 == null )
114             return false;
115         if ( data1.length != data2.length )
116             return false;
117         for ( int i = 0; i < data1.length; i++ )
118         {
119             if ( data1[i] != data2[i] )
120                 return false;
121         }
122         return true;
123     }
124
125
126     public static String JavaDoc getShortenedString( String JavaDoc value, int length )
127     {
128
129         if ( value == null )
130             return "";
131
132         if ( value.length() > length )
133         {
134             value = value.substring( 0, length ) + "...";
135         }
136
137         return value;
138     }
139
140
141     public static String JavaDoc serialize( Object JavaDoc o )
142     {
143         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
144         XMLEncoder JavaDoc encoder = new XMLEncoder JavaDoc( baos );
145         encoder.writeObject( o );
146         encoder.close();
147         String JavaDoc s = LdifUtils.utf8decode( baos.toByteArray() );
148         return s;
149     }
150
151
152     public static Object JavaDoc deserialize( String JavaDoc s )
153     {
154         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc( LdifUtils.utf8encode( s ) );
155         XMLDecoder JavaDoc decoder = new XMLDecoder JavaDoc( bais );
156         Object JavaDoc o = decoder.readObject();
157         decoder.close();
158         return o;
159     }
160
161
162     public static String JavaDoc getNonNullString( Object JavaDoc o )
163     {
164         return o == null ? "-" : o.toString();
165     }
166
167
168     public static String JavaDoc formatBytes( long bytes )
169     {
170         String JavaDoc size = "";
171         if ( bytes > 1024 * 1024 )
172             size += ( bytes / 1024 / 1024 ) + " MB (" + bytes + " Bytes)";
173         else if ( bytes > 1024 )
174             size += ( bytes / 1024 ) + " KB (" + bytes + " Bytes)";
175         else if ( bytes > 1 )
176             size += bytes + " Bytes";
177         else
178             size += bytes + " Byte";
179         return size;
180     }
181
182
183     public static boolean containsIgnoreCase( Collection JavaDoc c, String JavaDoc s )
184     {
185         if ( c == null || s == null )
186         {
187             return false;
188         }
189
190         Iterator JavaDoc it = c.iterator();
191         while ( it.hasNext() )
192         {
193             Object JavaDoc o = it.next();
194             if ( o instanceof String JavaDoc && ( ( String JavaDoc ) o ).equalsIgnoreCase( s ) )
195             {
196                 return true;
197             }
198         }
199
200         return false;
201     }
202
203
204     public static String JavaDoc shorten( String JavaDoc label, int maxLength )
205     {
206         if ( label == null )
207         {
208             return null;
209         }
210         if ( maxLength < 3 )
211         {
212             return "...";
213         }
214         if ( label.length() > maxLength )
215         {
216             label = label.substring( 0, maxLength / 2 ) + "..."
217                 + label.substring( label.length() - maxLength / 2, label.length() );
218
219         }
220         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( maxLength + 3 );
221         for ( int i = 0; i < label.length(); i++ )
222         {
223             char c = label.charAt( i );
224             if ( c > 31 && c < 127 )
225                 sb.append( c );
226             else
227                 sb.append( '.' );
228         }
229         return sb.toString();
230     }
231
232 }
233
Popular Tags