1 2 17 18 package org.apache.poi.util; 19 20 import java.io.UnsupportedEncodingException ; 21 import java.text.FieldPosition ; 22 import java.text.NumberFormat ; 23 33 public class StringUtil { 34 private final static String ENCODING = "ISO-8859-1"; 35 38 private StringUtil() { 39 } 40 41 60 public static String getFromUnicodeLE( 61 final byte[] string, 62 final int offset, 63 final int len) 64 throws ArrayIndexOutOfBoundsException , IllegalArgumentException { 65 if ((offset < 0) || (offset >= string.length)) { 66 throw new ArrayIndexOutOfBoundsException ("Illegal offset"); 67 } 68 if ((len < 0) || (((string.length - offset) / 2) < len)) { 69 throw new IllegalArgumentException ("Illegal length"); 70 } 71 72 try { 73 return new String (string, offset, len * 2, "UTF-16LE"); 74 } catch (UnsupportedEncodingException e) { 75 throw new InternalError (); 76 } 77 } 78 79 88 public static String getFromUnicodeLE(final byte[] string) { 89 return getFromUnicodeLE(string, 0, string.length / 2); 90 } 91 92 111 public static String getFromUnicodeBE( 112 final byte[] string, 113 final int offset, 114 final int len) 115 throws ArrayIndexOutOfBoundsException , IllegalArgumentException { 116 if ((offset < 0) || (offset >= string.length)) { 117 throw new ArrayIndexOutOfBoundsException ("Illegal offset"); 118 } 119 if ((len < 0) || (((string.length - offset) / 2) < len)) { 120 throw new IllegalArgumentException ("Illegal length"); 121 } 122 try { 123 return new String (string, offset, len * 2, "UTF-16BE"); 124 } catch (UnsupportedEncodingException e) { 125 throw new InternalError (); 126 } 127 } 128 129 138 public static String getFromUnicodeBE(final byte[] string) { 139 return getFromUnicodeBE(string, 0, string.length / 2); 140 } 141 142 150 public static String getFromCompressedUnicode( 151 final byte[] string, 152 final int offset, 153 final int len) { 154 try { 155 return new String (string, offset, len, "ISO-8859-1"); 156 } catch (UnsupportedEncodingException e) { 157 throw new InternalError (); 158 } 159 } 160 161 169 public static void putCompressedUnicode( 170 final String 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 e) { 177 throw new InternalError (); 178 } 179 } 180 181 188 public static void putUnicodeLE( 189 final String 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 e) { 196 throw new InternalError (); 197 } 198 } 199 200 207 public static void putUnicodeBE( 208 final String 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 e) { 215 throw new InternalError (); 216 } 217 } 218 219 226 public static String format(String message, Object [] params) { 227 int currentParamNumber = 0; 228 StringBuffer formattedMessage = new StringBuffer (); 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 ) 235 && (i + 1 < message.length())) { 236 i 237 += matchOptionalFormatting( 238 (Number ) 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 267 private static int matchOptionalFormatting( 268 Number number, 269 String formatting, 270 StringBuffer outputTo) { 271 NumberFormat 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 (0)); 282 return 3; 283 } 284 numberFormat.format(number, outputTo, new FieldPosition (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 (0)); 293 return 2; 294 } 295 } 296 numberFormat.format(number, outputTo, new FieldPosition (0)); 297 return 1; 298 } 299 300 303 public static String getPreferredEncoding() { 304 return ENCODING; 305 } 306 } 307 | Popular Tags |