1 package com.etymon.pj.object; 2 3 import java.io.*; 4 import com.etymon.pj.exception.*; 5 6 10 public class PjString 11 extends PjObject { 12 13 17 public PjString(String s) { 18 _s = s; 19 } 20 21 25 public String getString() { 26 return _s; 27 } 28 29 35 public long writePdf(OutputStream os) throws IOException { 36 long z = 0; 37 int length = _s.length(); 38 char c; 39 z = z + write(os, "("); 40 for (int x = 0; x < length; x++) { 41 c = _s.charAt(x); 42 switch (c) { 43 case '\n': 44 z = z + write(os, "\\n"); 45 break; 46 case '\r': 47 z = z + write(os, "\\r"); 48 break; 49 case '\t': 50 z = z + write(os, "\\t"); 51 break; 52 case '\b': 53 z = z + write(os, "\\b"); 54 break; 55 case '\f': 56 z = z + write(os, "\\f"); 57 break; 58 case '\\': 59 z = z + write(os, "\\\\"); 60 break; 61 case '(': 62 z = z + write(os, "\\("); 63 break; 64 case ')': 65 z = z + write(os, "\\)"); 66 break; 67 default: 68 z = z + write(os, c); 69 } 70 } 71 z = z + write(os, ")"); 72 return z; 73 } 74 75 79 public Object clone() { 80 return this; 81 } 82 83 130 131 139 public static String decodePdf(String pdfString) throws PdfFormatException { 140 int length = pdfString.length(); 141 if (length == 0) { 142 throw new PdfFormatException("'(' or '<' expected.", 0); 143 } 144 switch (pdfString.charAt(0)) { 145 case '(': 146 if (pdfString.charAt(length - 1) != ')') { 147 throw new PdfFormatException("')' expected.", length); 148 } 149 return decodeEscapedString(pdfString); 150 case '<': 151 if (pdfString.charAt(length - 1) != '>') { 152 throw new PdfFormatException("'>' expected.", length); 153 } 154 return decodeHexString(pdfString); 155 default: 156 throw new PdfFormatException("'(' or '<' expected.", 0); 157 } 158 } 159 160 170 private static String decodeEscapedString(String pdfString) throws PdfFormatException { 171 int length = pdfString.length(); 172 StringBuffer decodedString = new StringBuffer (length); 173 StringBuffer escapeString = new StringBuffer (4); 174 boolean escape = false; 175 char ch; 176 int x = 1; 177 while (x < (length - 1)) { 178 ch = pdfString.charAt(x); 179 if (ch == '\\') { 180 if (escape == false) { 181 escape = true; 183 escapeString.setLength(0); 184 } else { 185 decodedString.append('\\'); 187 escape = false; 188 } 189 } else { 190 if (escape == false) { 191 decodedString.append(ch); 193 } else { 194 if (escapeString.length() == 0) { 196 switch (ch) { 198 case 'n': 199 decodedString.append('\n'); 200 escape = false; 201 break; 202 case 'r': 203 decodedString.append('\r'); 204 escape = false; 205 break; 206 case 't': 207 decodedString.append('\t'); 208 escape = false; 209 break; 210 case 'b': 211 decodedString.append('\b'); 212 escape = false; 213 break; 214 case 'f': 215 decodedString.append('\f'); 216 escape = false; 217 break; 218 case '\\': 219 decodedString.append('\\'); 220 escape = false; 221 break; 222 case '(': 223 decodedString.append('('); 224 escape = false; 225 break; 226 case ')': 227 decodedString.append(')'); 228 escape = false; 229 break; 230 case '0': 231 case '1': 232 case '2': 233 case '3': 234 case '4': 235 case '5': 236 case '6': 237 case '7': 238 case '8': 239 case '9': 240 escapeString.append(ch); 241 break; 242 default: 243 decodedString.append('\\'); 254 escape = false; 255 x--; 257 } 258 } else { 259 switch (ch) { 263 case '0': 264 case '1': 265 case '2': 266 case '3': 267 case '4': 268 case '5': 269 case '6': 270 case '7': 271 case '8': 272 case '9': 273 280 int len = escapeString.length(); 281 if (len < 3) { 282 escapeString.append(ch); 283 } 284 if (len < 2) { 285 break; 286 } 287 x++; 291 default: 292 decodedString.append((char) 294 Integer.parseInt(escapeString.toString(), 8)); 295 escape = false; 296 x--; 298 } 299 } 300 } 301 } 302 x++; 303 } 304 return decodedString.toString(); 305 } 306 307 318 private static String decodeHexString(String pdfString) throws PdfFormatException { 319 int length = pdfString.length(); 320 StringBuffer decodedString = new StringBuffer (length); 321 StringBuffer hexString = new StringBuffer (4); 322 char ch; 323 int x = 1; 324 while (x < (length - 1)) { 325 ch = pdfString.charAt(x); 326 switch (Character.toUpperCase(ch)) { 328 case '0': 329 case '1': 330 case '2': 331 case '3': 332 case '4': 333 case '5': 334 case '6': 335 case '7': 336 case '8': 337 case '9': 338 case 'A': 339 case 'B': 340 case 'C': 341 case 'D': 342 case 'E': 343 case 'F': 344 hexString.append(ch); 347 if ( (x == (length - 2)) && (hexString.length() == 1) ) { 349 hexString.append('0'); 350 } 351 if (hexString.length() == 2) { 353 decodedString.append((char)Integer.parseInt(hexString.toString(), 16)); 354 hexString.setLength(0); 355 } 356 break; 357 case ' ': 358 case '\t': 359 case '\r': 360 case '\n': 361 case '\f': 362 break; 364 default: 365 throw new PdfFormatException("Hexadecimal digit expected.", x); 366 } 367 x++; 368 } 369 return decodedString.toString(); 370 } 371 372 377 public boolean equals(Object obj) { 378 if (obj == null) { 379 return false; 380 } 381 if (obj instanceof PjString) { 382 return _s.equals(((PjString)obj)._s); 383 } else { 384 return false; 385 } 386 } 387 388 private String _s; 389 390 } 391 | Popular Tags |