Line 1
Link Here
|
1 |
/* * ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and |
|
|
2 |
wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache POI" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * "Apache POI", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS |
1 |
wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache POI" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * "Apache POI", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS |
3 |
*@author Sergei Kozello (sergeikozello at mail.ru) *@created May 10, 2002 *@version 1.0 */public class StringUtil { private final static String ENCODING="ISO-8859-1"; /** * Constructor for the StringUtil object */ private StringUtil() { } /** * given a byte array of 16-bit unicode characters, compress to 8-bit and * return a string * * { 0x16, 0x00 } -> 0x16 * *@param string the byte array to be converted *@param offset the initial offset into the * byte array. it is assumed that string[ offset ] and string[ offset + * 1 ] contain the first 16-bit unicode character *@param len *@return the converted string *@exception ArrayIndexOutOfBoundsException if offset is out of bounds for * the byte array (i.e., is negative or is greater than or equal to * string.length) *@exception IllegalArgumentEx |
2 |
*@author Sergei Kozello (sergeikozello at mail.ru) *@created May 10, 2002 *@version 1.0 */public class StringUtil { private final static String ENCODING="ISO-8859-1"; /** * Constructor for the StringUtil object */ private StringUtil() { } /** * given a byte array of 16-bit unicode characters, compress to 8-bit and * return a string * * { 0x16, 0x00 } -> 0x16 * *@param string the byte array to be converted *@param offset the initial offset into the * byte array. it is assumed that string[ offset ] and string[ offset + * 1 ] contain the first 16-bit unicode character *@param len *@return the converted string *@exception ArrayIndexOutOfBoundsException if offset is out of bounds for * the byte array (i.e., is negative or is greater than or equal to * string.length) *@exception IllegalArgumentEx |
4 |
/** * given a byte array of 16-bit unicode characters, compress to 8-bit and * return a string * * { 0x00, 0x16 } -> 0x16 * *@param string the byte array to be converted *@return the converted string */ public static String getFromUnicode(final byte[] string) { return getFromUnicode(string, 0, string.length / 2); } /** * read compressed unicode(8bit) * * @author Toshiaki Kamoshida(kamoshida.toshiaki at future dot co dot jp) * * @param string byte array to read * @param offset offset to read byte array * @param len length to read byte array * @return String generated String instance by reading byte array */ public static String getFromCompressedUnicode(final byte[] string, final int offset, final int len){ try{ return new String(string,offset,len,"ISO-8859-1"); } catch(UnsupportedEncodingException e){ throw new InternalError();/* unr |
3 |
/** * given a byte array of 16-bit unicode characters, compress to 8-bit and * return a string * * { 0x00, 0x16 } -> 0x16 * *@param string the byte array to be converted *@return the converted string */ public static String getFromUnicode(final byte[] string) { return getFromUnicode(string, 0, string.length / 2); } /** * read compressed unicode(8bit) * * @author Toshiaki Kamoshida(kamoshida.toshiaki at future dot co dot jp) * * @param string byte array to read * @param offset offset to read byte array * @param len length to read byte array * @return String generated String instance by reading byte array */ public static String getFromCompressedUnicode(final byte[] string, final int offset, final int len){ try{ return new String(string,offset,len,"ISO-8859-1"); } catch(UnsupportedEncodingException e){ throw new InternalError();/* unr |
5 |
for (int k = 0; k < strlen; k++) { char c = input.charAt(k); output[offset + (2 * k)] = (byte) c; output[offset + (2 * k) + 1] = (byte) (c >> 8); } } /** * Write uncompressed unicode * *@param input the String containing the unicode data to be written *@param output the byte array to hold the uncompressed unicode *@param offset the offset to start writing into the byte array */ public static void putUncompressedUnicodeHigh(final String input, final byte[] output, final int offset) { int strlen = input.length(); for (int k = 0; k < strlen; k++) { char c = input.charAt(k); output[offset + (2 * k)] = (byte) (c >> 8); output[offset + (2 * k)] = (byte) c; } } /** * Description of the Method * *@param message Description of the Parameter *@param params Description of the Parameter *@return Description o |
4 |
for (int k = 0; k < strlen; k++) { char c = input.charAt(k); output[offset + (2 * k)] = (byte) c; output[offset + (2 * k) + 1] = (byte) (c >> 8); } } /** * Write uncompressed unicode * *@param input the String containing the unicode data to be written *@param output the byte array to hold the uncompressed unicode *@param offset the offset to start writing into the byte array */ public static void putUncompressedUnicodeHigh(final String input, final byte[] output, final int offset) { int strlen = input.length(); for (int k = 0; k < strlen; k++) { char c = input.charAt(k); output[offset + (2 * k)] = (byte) (c >> 8); output[offset + (2 * k)] = (byte) c; } } /** * Description of the Method * *@param message Description of the Parameter *@param params Description of the Parameter *@return Description o |
6 |
numberFormat.setMaximumFractionDigits(Integer.parseInt(formatting.charAt(2) + "")); numberFormat.format(number, outputTo, new FieldPosition(0)); return 3; } numberFormat.format(number, outputTo, new FieldPosition(0)); return 1; } else if ((0 < formatting.length()) && (formatting.charAt(0) == '.')) { if ((1 < formatting.length()) && Character.isDigit(formatting.charAt(1))) { numberFormat.setMaximumFractionDigits(Integer.parseInt(formatting.charAt(1) + "")); numberFormat.format(number, outputTo, new FieldPosition(0)); return 2; } } numberFormat.format(number, outputTo, new FieldPosition(0)); return 1; } /** * @return the encoding we want to use (ISO-8859-1) */ public static String getPreferredEncoding() { return ENCODING; }} |
5 |
numberFormat.setMaximumFractionDigits(Integer.parseInt(formatting.charAt(2) + "")); numberFormat.format(number, outputTo, new FieldPosition(0)); return 3; } numberFormat.format(number, outputTo, new FieldPosition(0)); return 1; } else if ((0 < formatting.length()) && (formatting.charAt(0) == '.')) { if ((1 < formatting.length()) && Character.isDigit(formatting.charAt(1))) { numberFormat.setMaximumFractionDigits(Integer.parseInt(formatting.charAt(1) + "")); numberFormat.format(number, outputTo, new FieldPosition(0)); return 2; } } numberFormat.format(number, outputTo, new FieldPosition(0)); return 1; } /** * @return the encoding we want to use (ISO-8859-1) */ public static String getPreferredEncoding() { return ENCODING; }} |
7 |
-- |
6 |
|
|
|
7 |
/* |
8 |
* ==================================================================== |
9 |
* The Apache Software License, Version 1.1 |
10 |
* |
11 |
* Copyright (c) 2003 The Apache Software Foundation. All rights |
12 |
* reserved. |
13 |
* |
14 |
* Redistribution and use in source and binary forms, with or without |
15 |
* modification, are permitted provided that the following conditions |
16 |
* are met: |
17 |
* |
18 |
* 1. Redistributions of source code must retain the above copyright |
19 |
* notice, this list of conditions and the following disclaimer. |
20 |
* |
21 |
* 2. Redistributions in binary form must reproduce the above copyright |
22 |
* notice, this list of conditions and the following disclaimer in |
23 |
* the documentation and/or other materials provided with the |
24 |
* distribution. |
25 |
* |
26 |
* 3. The end-user documentation included with the redistribution, |
27 |
* if any, must include the following acknowledgment: |
28 |
* "This product includes software developed by the |
29 |
* Apache Software Foundation (http://www.apache.org/)." |
30 |
* Alternately, this acknowledgment may appear in the software itself, |
31 |
* if and wherever such third-party acknowledgments normally appear. |
32 |
* |
33 |
* 4. The names "Apache" and "Apache Software Foundation" and |
34 |
* "Apache POI" must not be used to endorse or promote products |
35 |
* derived from this software without prior written permission. For |
36 |
* written permission, please contact apache@apache.org. |
37 |
* |
38 |
* 5. Products derived from this software may not be called "Apache", |
39 |
* "Apache POI", nor may "Apache" appear in their name, without |
40 |
* prior written permission of the Apache Software Foundation. |
41 |
* |
42 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
43 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
44 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
45 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
46 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
47 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
48 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
49 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
50 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
51 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
52 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
53 |
* SUCH DAMAGE. |
54 |
* ==================================================================== |
55 |
* |
56 |
* This software consists of voluntary contributions made by many |
57 |
* individuals on behalf of the Apache Software Foundation. For more |
58 |
* information on the Apache Software Foundation, please see |
59 |
* <http://www.apache.org/>. |
60 |
*/ |
61 |
package org.apache.poi.util; |
62 |
|
63 |
import java.io.UnsupportedEncodingException; |
64 |
import java.text.NumberFormat; |
65 |
import java.text.FieldPosition; |
66 |
import java.util.Arrays; |
67 |
/** |
68 |
* Title: String Utility Description: Collection of string handling utilities |
69 |
* |
70 |
* |
71 |
*@author Andrew C. Oliver |
72 |
*@author Sergei Kozello (sergeikozello at mail.ru) |
73 |
*@author Toshiaki Kamoshida (kamoshida.toshiaki at future dot co dot jp) |
74 |
*@created May 10, 2002 |
75 |
*@version 1.0 |
76 |
*/ |
77 |
public class StringUtil { |
78 |
private final static String ENCODING = "ISO-8859-1"; |
79 |
/** |
80 |
* Constructor for the StringUtil object |
81 |
*/ |
82 |
private StringUtil() { |
83 |
} |
84 |
|
85 |
/** |
86 |
* given a byte array of 16-bit unicode characters, compress to 8-bit and |
87 |
* return a string |
88 |
* |
89 |
* { 0x16, 0x00 } -> 0x16 |
90 |
* |
91 |
*@param string the byte array to be converted |
92 |
*@param offset the initial offset into the |
93 |
* byte array. it is assumed that string[ offset ] and string[ offset + |
94 |
* 1 ] contain the first 16-bit unicode character |
95 |
*@param len |
96 |
*@return the converted string |
97 |
*@exception ArrayIndexOutOfBoundsException if offset is out of bounds for |
98 |
* the byte array (i.e., is negative or is greater than or equal to |
99 |
* string.length) |
100 |
*@exception IllegalArgumentException if len is too large (i.e., |
101 |
* there is not enough data in string to create a String of that |
102 |
* length) |
103 |
*@len the length of the final string |
104 |
*/ |
105 |
public static String getFromUnicodeLE( |
106 |
final byte[] string, |
107 |
final int offset, |
108 |
final int len) |
109 |
throws ArrayIndexOutOfBoundsException, IllegalArgumentException { |
110 |
if ((offset < 0) || (offset >= string.length)) { |
111 |
throw new ArrayIndexOutOfBoundsException("Illegal offset"); |
112 |
} |
113 |
if ((len < 0) || (((string.length - offset) / 2) < len)) { |
114 |
throw new IllegalArgumentException("Illegal length"); |
115 |
} |
116 |
|
117 |
try { |
118 |
return new String(string, offset, len * 2, "UTF-16LE"); |
119 |
} catch (UnsupportedEncodingException e) { |
120 |
throw new InternalError(); /*unreachable*/ |
121 |
} |
122 |
} |
123 |
|
124 |
/** |
125 |
* given a byte array of 16-bit unicode characters, compress to 8-bit and |
126 |
* return a string |
127 |
* |
128 |
* { 0x16, 0x00 } -> 0x16 |
129 |
* |
130 |
*@param string the byte array to be converted |
131 |
*@return the converted string |
132 |
*/ |
133 |
public static String getFromUnicodeLE(final byte[] string) { |
134 |
return getFromUnicodeLE(string, 0, string.length / 2); |
135 |
} |
136 |
|
137 |
/** |
138 |
* given a byte array of 16-bit unicode characters, compress to 8-bit and |
139 |
* return a string |
140 |
* |
141 |
* { 0x00, 0x16 } -> 0x16 |
142 |
* |
143 |
*@param string the byte array to be converted |
144 |
**@param offset the initial offset into the |
145 |
* byte array. it is assumed that string[ offset ] and string[ offset + |
146 |
* 1 ] contain the first 16-bit unicode character |
147 |
*@param len |
148 |
*@return the converted string |
149 |
*@exception ArrayIndexOutOfBoundsException if offset is out of bounds for |
150 |
* the byte array (i.e., is negative or is greater than or equal to |
151 |
* string.length) |
152 |
*@exception IllegalArgumentException if len is too large (i.e., |
153 |
* there is not enough data in string to create a String of that |
154 |
* length) |
155 |
*@len the length of the final string |
156 |
*/ |
157 |
public static String getFromUnicodeBE( |
158 |
final byte[] string, |
159 |
final int offset, |
160 |
final int len) |
161 |
throws ArrayIndexOutOfBoundsException, IllegalArgumentException { |
162 |
if ((offset < 0) || (offset >= string.length)) { |
163 |
throw new ArrayIndexOutOfBoundsException("Illegal offset"); |
164 |
} |
165 |
if ((len < 0) || (((string.length - offset) / 2) < len)) { |
166 |
throw new IllegalArgumentException("Illegal length"); |
167 |
} |
168 |
try { |
169 |
return new String(string, offset, len * 2, "UTF-16BE"); |
170 |
} catch (UnsupportedEncodingException e) { |
171 |
throw new InternalError(); /*unreachable*/ |
172 |
} |
173 |
} |
174 |
|
175 |
/** |
176 |
* given a byte array of 16-bit unicode characters, compress to 8-bit and |
177 |
* return a string |
178 |
* |
179 |
* { 0x00, 0x16 } -> 0x16 |
180 |
* |
181 |
*@param string the byte array to be converted |
182 |
*@return the converted string |
183 |
*/ |
184 |
public static String getFromUnicodeBE(final byte[] string) { |
185 |
return getFromUnicodeBE(string, 0, string.length / 2); |
186 |
} |
187 |
|
188 |
/** |
189 |
* read compressed unicode(8bit) |
190 |
* |
191 |
* @param string byte array to read |
192 |
* @param offset offset to read byte array |
193 |
* @param len length to read byte array |
194 |
* @return String generated String instance by reading byte array |
195 |
*/ |
196 |
public static String getFromCompressedUnicode( |
197 |
final byte[] string, |
198 |
final int offset, |
199 |
final int len) { |
200 |
try { |
201 |
return new String(string, offset, len, "ISO-8859-1"); |
202 |
} catch (UnsupportedEncodingException e) { |
203 |
throw new InternalError(); /* unreachable */ |
204 |
} |
205 |
} |
206 |
|
207 |
/** |
208 |
* write compressed unicode |
209 |
* |
210 |
*@param input the String containing the data to be written |
211 |
*@param output the byte array to which the data is to be written |
212 |
*@param offset an offset into the byte arrat at which the data is start |
213 |
* when written |
214 |
*/ |
215 |
public static void putCompressedUnicode( |
216 |
final String input, |
217 |
final byte[] output, |
218 |
final int offset) { |
219 |
try { |
220 |
byte[] bytes = input.getBytes("ISO-8859-1"); |
221 |
System.arraycopy(bytes, 0, output, offset, bytes.length); |
222 |
} catch (UnsupportedEncodingException e) { |
223 |
throw new InternalError(); /*unreachable*/ |
224 |
} |
225 |
} |
226 |
|
227 |
/** |
228 |
* Write uncompressed unicode |
229 |
* |
230 |
*@param input the String containing the unicode data to be written |
231 |
*@param output the byte array to hold the uncompressed unicode |
232 |
*@param offset the offset to start writing into the byte array |
233 |
*/ |
234 |
public static void putUnicodeLE( |
235 |
final String input, |
236 |
final byte[] output, |
237 |
final int offset) { |
238 |
try { |
239 |
byte[] bytes = input.getBytes("UTF-16LE"); |
240 |
System.arraycopy(bytes, 0, output, offset, bytes.length); |
241 |
} catch (UnsupportedEncodingException e) { |
242 |
throw new InternalError(); /*unreachable*/ |
243 |
} |
244 |
} |
245 |
|
246 |
/** |
247 |
* Write uncompressed unicode |
248 |
* |
249 |
*@param input the String containing the unicode data to be written |
250 |
*@param output the byte array to hold the uncompressed unicode |
251 |
*@param offset the offset to start writing into the byte array |
252 |
*/ |
253 |
public static void putUnicodeBE( |
254 |
final String input, |
255 |
final byte[] output, |
256 |
final int offset) { |
257 |
try { |
258 |
byte[] bytes = input.getBytes("UTF-16BE"); |
259 |
System.arraycopy(bytes, 0, output, offset, bytes.length); |
260 |
} catch (UnsupportedEncodingException e) { |
261 |
throw new InternalError(); /*unreachable*/ |
262 |
} |
263 |
} |
264 |
|
265 |
/** |
266 |
* Description of the Method |
267 |
* |
268 |
*@param message Description of the Parameter |
269 |
*@param params Description of the Parameter |
270 |
*@return Description of the Return Value |
271 |
*/ |
272 |
public static String format(String message, Object[] params) { |
273 |
int currentParamNumber = 0; |
274 |
StringBuffer formattedMessage = new StringBuffer(); |
275 |
for (int i = 0; i < message.length(); i++) { |
276 |
if (message.charAt(i) == '%') { |
277 |
if (currentParamNumber >= params.length) { |
278 |
formattedMessage.append("?missing data?"); |
279 |
} else if ( |
280 |
(params[currentParamNumber] instanceof Number) |
281 |
&& (i + 1 < message.length())) { |
282 |
i |
283 |
+= matchOptionalFormatting( |
284 |
(Number) params[currentParamNumber++], |
285 |
message.substring(i + 1), |
286 |
formattedMessage); |
287 |
} else { |
288 |
formattedMessage.append( |
289 |
params[currentParamNumber++].toString()); |
290 |
} |
291 |
} else { |
292 |
if ((message.charAt(i) == '\\') |
293 |
&& (i + 1 < message.length()) |
294 |
&& (message.charAt(i + 1) == '%')) { |
295 |
formattedMessage.append('%'); |
296 |
i++; |
297 |
} else { |
298 |
formattedMessage.append(message.charAt(i)); |
299 |
} |
300 |
} |
301 |
} |
302 |
return formattedMessage.toString(); |
303 |
} |
304 |
|
305 |
/** |
306 |
* Description of the Method |
307 |
* |
308 |
*@param number Description of the Parameter |
309 |
*@param formatting Description of the Parameter |
310 |
*@param outputTo Description of the Parameter |
311 |
*@return Description of the Return Value |
312 |
*/ |
313 |
private static int matchOptionalFormatting( |
314 |
Number number, |
315 |
String formatting, |
316 |
StringBuffer outputTo) { |
317 |
NumberFormat numberFormat = NumberFormat.getInstance(); |
318 |
if ((0 < formatting.length()) |
319 |
&& Character.isDigit(formatting.charAt(0))) { |
320 |
numberFormat.setMinimumIntegerDigits( |
321 |
Integer.parseInt(formatting.charAt(0) + "")); |
322 |
if ((2 < formatting.length()) |
323 |
&& (formatting.charAt(1) == '.') |
324 |
&& Character.isDigit(formatting.charAt(2))) { |
325 |
numberFormat.setMaximumFractionDigits( |
326 |
Integer.parseInt(formatting.charAt(2) + "")); |
327 |
numberFormat.format(number, outputTo, new FieldPosition(0)); |
328 |
return 3; |
329 |
} |
330 |
numberFormat.format(number, outputTo, new FieldPosition(0)); |
331 |
return 1; |
332 |
} else if ( |
333 |
(0 < formatting.length()) && (formatting.charAt(0) == '.')) { |
334 |
if ((1 < formatting.length()) |
335 |
&& Character.isDigit(formatting.charAt(1))) { |
336 |
numberFormat.setMaximumFractionDigits( |
337 |
Integer.parseInt(formatting.charAt(1) + "")); |
338 |
numberFormat.format(number, outputTo, new FieldPosition(0)); |
339 |
return 2; |
340 |
} |
341 |
} |
342 |
numberFormat.format(number, outputTo, new FieldPosition(0)); |
343 |
return 1; |
344 |
} |
345 |
|
346 |
/** |
347 |
* @return the encoding we want to use (ISO-8859-1) |
348 |
*/ |
349 |
public static String getPreferredEncoding() { |
350 |
return ENCODING; |
351 |
} |
352 |
} |