KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > util > StringUtil


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18 package org.apache.poi.util;
19
20 import java.io.UnsupportedEncodingException JavaDoc;
21 import java.text.FieldPosition JavaDoc;
22 import java.text.NumberFormat JavaDoc;
23 /**
24  * Title: String Utility Description: Collection of string handling utilities
25  *
26  *
27  *@author Andrew C. Oliver
28  *@author Sergei Kozello (sergeikozello at mail.ru)
29  *@author Toshiaki Kamoshida (kamoshida.toshiaki at future dot co dot jp)
30  *@since May 10, 2002
31  *@version 1.0
32  */

33 public class StringUtil {
34     private final static String JavaDoc ENCODING = "ISO-8859-1";
35     /**
36      * Constructor for the StringUtil object
37      */

38     private StringUtil() {
39     }
40
41     /**
42      * given a byte array of 16-bit unicode characters, compress to 8-bit and
43      * return a string
44      *
45      * { 0x16, 0x00 } -0x16
46      *
47      * @param string the byte array to be converted
48      * @param offset the initial offset into the
49      * byte array. it is assumed that string[ offset ] and string[ offset +
50      * 1 ] contain the first 16-bit unicode character
51      * @param len the length of the final string
52      * @return the converted string
53      * @exception ArrayIndexOutOfBoundsException if offset is out of bounds for
54      * the byte array (i.e., is negative or is greater than or equal to
55      * string.length)
56      * @exception IllegalArgumentException if len is too large (i.e.,
57      * there is not enough data in string to create a String of that
58      * length)
59      */

60     public static String JavaDoc getFromUnicodeLE(
61         final byte[] string,
62         final int offset,
63         final int len)
64         throws ArrayIndexOutOfBoundsException JavaDoc, IllegalArgumentException JavaDoc {
65         if ((offset < 0) || (offset >= string.length)) {
66             throw new ArrayIndexOutOfBoundsException JavaDoc("Illegal offset");
67         }
68         if ((len < 0) || (((string.length - offset) / 2) < len)) {
69             throw new IllegalArgumentException JavaDoc("Illegal length");
70         }
71
72         try {
73             return new String JavaDoc(string, offset, len * 2, "UTF-16LE");
74         } catch (UnsupportedEncodingException JavaDoc e) {
75             throw new InternalError JavaDoc(); /*unreachable*/
76         }
77     }
78
79     /**
80      * given a byte array of 16-bit unicode characters, compress to 8-bit and
81      * return a string
82      *
83      * { 0x16, 0x00 } -0x16
84      *
85      *@param string the byte array to be converted
86      *@return the converted string
87      */

88     public static String JavaDoc getFromUnicodeLE(final byte[] string) {
89         return getFromUnicodeLE(string, 0, string.length / 2);
90     }
91
92     /**
93      * given a byte array of 16-bit unicode characters, compress to 8-bit and
94      * return a string
95      *
96      * { 0x00, 0x16 } -0x16
97      *
98      *@param string the byte array to be converted
99      **@param offset the initial offset into the
100      * byte array. it is assumed that string[ offset ] and string[ offset +
101      * 1 ] contain the first 16-bit unicode character
102          *@param len the length of the final string
103      *@return the converted string
104      *@exception ArrayIndexOutOfBoundsException if offset is out of bounds for
105      * the byte array (i.e., is negative or is greater than or equal to
106      * string.length)
107      *@exception IllegalArgumentException if len is too large (i.e.,
108      * there is not enough data in string to create a String of that
109      * length)
110      */

111     public static String JavaDoc getFromUnicodeBE(
112         final byte[] string,
113         final int offset,
114         final int len)
115         throws ArrayIndexOutOfBoundsException JavaDoc, IllegalArgumentException JavaDoc {
116         if ((offset < 0) || (offset >= string.length)) {
117             throw new ArrayIndexOutOfBoundsException JavaDoc("Illegal offset");
118         }
119         if ((len < 0) || (((string.length - offset) / 2) < len)) {
120             throw new IllegalArgumentException JavaDoc("Illegal length");
121         }
122         try {
123             return new String JavaDoc(string, offset, len * 2, "UTF-16BE");
124         } catch (UnsupportedEncodingException JavaDoc e) {
125             throw new InternalError JavaDoc(); /*unreachable*/
126         }
127     }
128
129     /**
130      * given a byte array of 16-bit unicode characters, compress to 8-bit and
131      * return a string
132      *
133      * { 0x00, 0x16 } -0x16
134      *
135      *@param string the byte array to be converted
136      *@return the converted string
137      */

138     public static String JavaDoc getFromUnicodeBE(final byte[] string) {
139         return getFromUnicodeBE(string, 0, string.length / 2);
140     }
141
142     /**
143      * read compressed unicode(8bit)
144      *
145      * @param string byte array to read
146      * @param offset offset to read byte array
147      * @param len length to read byte array
148      * @return String generated String instance by reading byte array
149      */

150     public static String JavaDoc getFromCompressedUnicode(
151         final byte[] string,
152         final int offset,
153         final int len) {
154         try {
155             return new String JavaDoc(string, offset, len, "ISO-8859-1");
156         } catch (UnsupportedEncodingException JavaDoc e) {
157             throw new InternalError JavaDoc(); /* unreachable */
158         }
159     }
160
161     /**
162      * write compressed unicode
163      *
164      *@param input the String containing the data to be written
165      *@param output the byte array to which the data is to be written
166      *@param offset an offset into the byte arrat at which the data is start
167      * when written
168      */

169     public static void putCompressedUnicode(
170         final String JavaDoc input,
171         final byte[] output,
172         final int offset) {
173         try {
174             byte[] bytes = input.getBytes("ISO-8859-1");
175             System.arraycopy(bytes, 0, output, offset, bytes.length);
176         } catch (UnsupportedEncodingException JavaDoc e) {
177             throw new InternalError JavaDoc(); /*unreachable*/
178         }
179     }
180
181     /**
182      * Write uncompressed unicode
183      *
184      *@param input the String containing the unicode data to be written
185      *@param output the byte array to hold the uncompressed unicode
186      *@param offset the offset to start writing into the byte array
187      */

188     public static void putUnicodeLE(
189         final String JavaDoc input,
190         final byte[] output,
191         final int offset) {
192         try {
193             byte[] bytes = input.getBytes("UTF-16LE");
194             System.arraycopy(bytes, 0, output, offset, bytes.length);
195         } catch (UnsupportedEncodingException JavaDoc e) {
196             throw new InternalError JavaDoc(); /*unreachable*/
197         }
198     }
199
200     /**
201      * Write uncompressed unicode
202      *
203      *@param input the String containing the unicode data to be written
204      *@param output the byte array to hold the uncompressed unicode
205      *@param offset the offset to start writing into the byte array
206      */

207     public static void putUnicodeBE(
208         final String JavaDoc input,
209         final byte[] output,
210         final int offset) {
211         try {
212             byte[] bytes = input.getBytes("UTF-16BE");
213             System.arraycopy(bytes, 0, output, offset, bytes.length);
214         } catch (UnsupportedEncodingException JavaDoc e) {
215             throw new InternalError JavaDoc(); /*unreachable*/
216         }
217     }
218
219     /**
220      * Description of the Method
221      *
222      *@param message Description of the Parameter
223      *@param params Description of the Parameter
224      *@return Description of the Return Value
225      */

226     public static String JavaDoc format(String JavaDoc message, Object JavaDoc[] params) {
227         int currentParamNumber = 0;
228         StringBuffer JavaDoc formattedMessage = new StringBuffer JavaDoc();
229         for (int i = 0; i < message.length(); i++) {
230             if (message.charAt(i) == '%') {
231                 if (currentParamNumber >= params.length) {
232                     formattedMessage.append("?missing data?");
233                 } else if (
234                     (params[currentParamNumber] instanceof Number JavaDoc)
235                         && (i + 1 < message.length())) {
236                     i
237                         += matchOptionalFormatting(
238                             (Number JavaDoc) params[currentParamNumber++],
239                             message.substring(i + 1),
240                             formattedMessage);
241                 } else {
242                     formattedMessage.append(
243                         params[currentParamNumber++].toString());
244                 }
245             } else {
246                 if ((message.charAt(i) == '\\')
247                     && (i + 1 < message.length())
248                     && (message.charAt(i + 1) == '%')) {
249                     formattedMessage.append('%');
250                     i++;
251                 } else {
252                     formattedMessage.append(message.charAt(i));
253                 }
254             }
255         }
256         return formattedMessage.toString();
257     }
258
259     /**
260      * Description of the Method
261      *
262      *@param number Description of the Parameter
263      *@param formatting Description of the Parameter
264      *@param outputTo Description of the Parameter
265      *@return Description of the Return Value
266      */

267     private static int matchOptionalFormatting(
268         Number JavaDoc number,
269         String JavaDoc formatting,
270         StringBuffer JavaDoc outputTo) {
271         NumberFormat JavaDoc numberFormat = NumberFormat.getInstance();
272         if ((0 < formatting.length())
273             && Character.isDigit(formatting.charAt(0))) {
274             numberFormat.setMinimumIntegerDigits(
275                 Integer.parseInt(formatting.charAt(0) + ""));
276             if ((2 < formatting.length())
277                 && (formatting.charAt(1) == '.')
278                 && Character.isDigit(formatting.charAt(2))) {
279                 numberFormat.setMaximumFractionDigits(
280                     Integer.parseInt(formatting.charAt(2) + ""));
281                 numberFormat.format(number, outputTo, new FieldPosition JavaDoc(0));
282                 return 3;
283             }
284             numberFormat.format(number, outputTo, new FieldPosition JavaDoc(0));
285             return 1;
286         } else if (
287             (0 < formatting.length()) && (formatting.charAt(0) == '.')) {
288             if ((1 < formatting.length())
289                 && Character.isDigit(formatting.charAt(1))) {
290                 numberFormat.setMaximumFractionDigits(
291                     Integer.parseInt(formatting.charAt(1) + ""));
292                 numberFormat.format(number, outputTo, new FieldPosition JavaDoc(0));
293                 return 2;
294             }
295         }
296         numberFormat.format(number, outputTo, new FieldPosition JavaDoc(0));
297         return 1;
298     }
299
300     /**
301      * @return the encoding we want to use (ISO-8859-1)
302      */

303     public static String JavaDoc getPreferredEncoding() {
304         return ENCODING;
305     }
306 }
307
Popular Tags