| 1 4 5 package com.etymon.pjx; 6 7 import java.io.*; 8 import java.nio.*; 9 10 14 public class PdfString 15 extends PdfObject { 16 17 20 protected String _s; 21 22 26 public PdfString(String s) { 27 _s = s; 28 } 29 30 public boolean equals(Object obj) { 31 32 if ( (obj == null) || ( !(obj instanceof PdfString) ) ) { 33 return false; 34 } 35 36 return _s.equals( ((PdfString)obj)._s ); 37 } 38 39 43 public String getString() { 44 return _s; 45 } 46 47 public int hashCode() { 48 return _s.hashCode(); 49 } 50 51 57 protected static boolean isWhiteSpace(char ch) { 58 switch(ch) { 59 case 0: 60 case '\t': 61 case '\n': 62 case '\f': 63 case '\r': 64 case ' ': 65 return true; 66 default: 67 return false; 68 } 69 } 70 71 76 protected static String pdfToString(CharBuffer buf) throws PdfFormatException { 77 78 try { 79 80 char ch; 82 do { 83 ch = buf.get(); 84 } while (isWhiteSpace(ch)); 85 87 if (ch == '(') { 88 return decodeLiteralString(buf); 89 } 90 else if (ch == '<') { 91 return decodeHexString(buf); 92 } 93 else { 94 throw new PdfFormatException("'(' or '<' expected at beginning of string."); 95 } 96 97 } 98 catch (BufferUnderflowException e) { 99 throw new PdfFormatException("End of buffer reached while parsing string."); 100 } 101 102 } 103 104 protected static String decodeLiteralString(CharBuffer buf) 105 throws BufferUnderflowException, PdfFormatException { 106 107 char ch, chc, che; 108 boolean append; 109 boolean escaping = false; 110 int paren = 0; StringBuffer sb = new StringBuffer (); 112 char[] code = new char[3]; 113 int codeLen; 114 boolean done = false; 115 116 do { 117 118 ch = buf.get(); 119 120 append = true; 121 122 che = ch; 123 124 if (escaping) { 125 126 escaping = false; 127 128 switch (ch) { 129 case 'n': 130 che = '\n'; 131 break; 132 case 'r': 133 che = '\r'; 134 break; 135 case 't': 136 che = '\t'; 137 break; 138 case 'b': 139 che = '\b'; 140 break; 141 case 'f': 142 che = '\f'; 143 break; 144 case '(': 145 che = '('; 146 break; 147 case ')': 148 che = ')'; 149 break; 150 case '\\': 151 che = '\\'; 152 break; 153 case '0': 154 case '1': 155 case '2': 156 case '3': 157 case '4': 158 case '5': 159 case '6': 160 case '7': 161 case '8': 162 case '9': 163 append = false; 164 code[0] = ch; 165 codeLen = 1; 166 boolean code_done = false; 167 do { 168 chc = buf.get(); 169 switch (chc) { 170 case '0': 171 case '1': 172 case '2': 173 case '3': 174 case '4': 175 case '5': 176 case '6': 177 case '7': 178 case '8': 179 if (codeLen < 3) { 180 code[codeLen++] = chc; 181 break; 182 } 183 default: 184 code_done = true; 185 buf.position( buf.position() - 1 ); 186 } 187 } while (!code_done); 188 sb.append( (char)Integer.parseInt(new String (code, 0, codeLen), 8) ); 189 break; 190 case '\r': 191 case '\n': 192 append = false; 193 do { 194 chc = buf.get(); 195 } while ( (chc == '\r') || (chc == '\n') ); 196 buf.position ( buf.position() - 1 ); 197 break; 198 default: 199 } 200 201 } else { 202 203 switch (ch) { 204 case '(': 205 paren++; 206 break; 207 case ')': 208 if (paren > 0) { 209 paren--; 210 } else { 211 append = false; 212 done = true; 213 } 214 break; 215 case '\\': 216 escaping = true; 217 append = false; 218 break; 219 case '\r': 220 if (buf.get() != '\n') { 221 buf.position( buf.position() - 1 ); 222 } 223 che = '\n'; 224 break; 225 default: 226 } 227 228 } 230 if (append) { 231 sb.append(che); 232 } 233 234 } while (!done); 235 236 return sb.toString(); 237 238 } 239 240 protected static String decodeHexString(CharBuffer buf) 241 throws BufferUnderflowException, PdfFormatException { 242 243 char ch; 244 boolean done = false; 245 StringBuffer sb = new StringBuffer (); 246 char[] hex = new char[2]; 247 int hexLen = 0; 248 249 do { 250 251 ch = buf.get(); 252 253 switch (ch) { 254 case '0': 255 case '1': 256 case '2': 257 case '3': 258 case '4': 259 case '5': 260 case '6': 261 case '7': 262 case '8': 263 case '9': 264 case 'A': 265 case 'B': 266 case 'C': 267 case 'D': 268 case 'E': 269 case 'F': 270 case 'a': 271 case 'b': 272 case 'c': 273 case 'd': 274 case 'e': 275 case 'f': 276 hex[hexLen] = ch; 277 hexLen++; 278 break; 279 case '>': 280 done = true; 281 case 0: 282 case '\t': 283 case '\n': 284 case '\f': 285 case '\r': 286 case ' ': 287 break; 288 default: 289 throw new PdfFormatException("Unrecognized character in hexadecimal string."); 290 } 291 292 if ( (done) && (hexLen == 1) ) { 293 hex[1] = '0'; 294 hexLen = 2; 295 } 296 297 if (hexLen >= 2) { 298 sb.append( (char)Integer.parseInt(new String (hex), 16) ); 299 hexLen = 0; 300 } 301 302 } while (!done); 303 304 return sb.toString(); 305 306 } 307 308 protected int writePdf(PdfWriter w, boolean spacing) throws IOException { 309 310 DataOutputStream dos = w.getDataOutputStream(); 311 312 int count = 1; 313 dos.write('('); 314 315 String s = _s; 316 int len = s.length(); 317 318 for (int x = 0; x < len; x++) { 319 320 char ch = s.charAt(x); 321 322 switch (ch) { 323 case '(': 324 dos.writeBytes("\\("); 325 count += 2; 326 break; 327 case ')': 328 dos.writeBytes("\\)"); 329 count += 2; 330 break; 331 case '\\': 332 dos.writeBytes("\\\\"); 333 count += 2; 334 break; 335 case '\r': 336 dos.writeBytes("\\r"); 337 count += 2; 338 break; 339 case '\n': 340 dos.writeBytes("\\n"); 341 count += 2; 342 break; 343 default: 344 dos.write(ch); 345 count++; 346 } 347 348 } 350 dos.write(')'); 351 return count + 1; 352 353 } 354 355 } 356 | Popular Tags |