1 4 5 package com.etymon.pjx; 6 7 import java.io.*; 8 9 13 public class PdfName 14 extends PdfObject { 15 16 19 protected String _s; 20 21 25 public PdfName(String s) { 26 _s = s; 27 } 28 29 public boolean equals(Object obj) { 30 31 if ( (obj == null) || ( !(obj instanceof PdfName) ) ) { 32 return false; 33 } 34 35 return _s.equals( ((PdfName)obj)._s ); 36 } 37 38 42 public String getString() { 43 return _s; 44 } 45 46 public int hashCode() { 47 return _s.hashCode(); 48 } 49 50 55 protected static String pdfToString(String pdf) throws PdfFormatException { 56 57 int len = pdf.length(); 58 if (len < 1) { 59 throw new PdfFormatException("Invalid PDF name (length < 1).", 0); 60 } 61 if (pdf.charAt(0) != '/') { 62 throw new PdfFormatException("Invalid PDF name (missing initial '/').", 0); 63 } 64 StringBuffer sb = new StringBuffer (len); 65 int x = 1; 66 do { 67 68 char ch = pdf.charAt(x); 69 70 if (ch == '#') { 71 72 if ( (x + 2) >= len ) { 73 throw new PdfFormatException("Invalid PDF name (incorrect use of '#').", x); 74 } 75 76 try { 77 78 Integer code = Integer.valueOf(pdf.substring(x + 1, x + 3), 16); 79 sb.append( (char)code.byteValue() ); 80 81 x += 3; 82 83 } 84 catch (NumberFormatException e) { 85 throw new PdfFormatException("Invalid PDF name (incorrect use of '#').", x); 86 } 87 88 } else { 89 90 sb.append(ch); 91 92 x++; 93 94 } 95 96 } while (x < len); 97 98 return sb.toString(); 99 100 } 101 102 protected int writePdf(PdfWriter w, boolean spacing) throws IOException { 103 104 DataOutputStream dos = w.getDataOutputStream(); 105 106 int count = 1; 107 dos.write('/'); 108 109 String s = _s; 110 int len = s.length(); 111 String hex; 112 boolean special; 113 for (int x = 0; x < len; x++) { 114 115 char ch = s.charAt(x); 116 117 switch (ch) { 118 case '\u0000': case '\t': 120 case '\n': 121 case '\f': 122 case '\r': 123 case ' ': 124 case '(': 125 case ')': 126 case '<': 127 case '>': 128 case '[': 129 case ']': 130 case '{': 131 case '}': 132 case '/': 133 case '%': 134 case '#': 135 special = true; 136 break; 137 default: 138 special = ( (ch < 33) || (ch > 126) ); 139 } 140 141 if (special) { 142 dos.write('#'); 143 if (ch < 16) { 144 dos.write('0'); 145 count++; 146 } 147 hex = Integer.toHexString(ch); 148 dos.writeBytes(hex); 149 count += hex.length() + 1; } else { 151 dos.write(ch); 152 count++; 153 } 154 155 } 156 157 return count; 158 159 } 160 161 } 162 | Popular Tags |