KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pjx > PdfName


1 /*
2   Copyright (C) Etymon Systems, Inc. <http://www.etymon.com/>
3 */

4
5 package com.etymon.pjx;
6
7 import java.io.*;
8
9 /**
10    Represents the PDF name object.
11    @author Nassib Nassar
12 */

13 public class PdfName
14     extends PdfObject {
15
16     /**
17        The string value of this name object.
18     */

19     protected String JavaDoc _s;
20     
21     /**
22        Constructs a name object representing a string value.
23        @param s the string value.
24      */

25     public PdfName(String JavaDoc s) {
26         _s = s;
27     }
28
29     public boolean equals(Object JavaDoc obj) {
30
31         if ( (obj == null) || ( !(obj instanceof PdfName) ) ) {
32             return false;
33         }
34
35         return _s.equals( ((PdfName)obj)._s );
36     }
37
38     /**
39        Returns the string value of this name object.
40        @return the string value.
41      */

42     public String JavaDoc getString() {
43         return _s;
44     }
45
46     public int hashCode() {
47         return _s.hashCode();
48     }
49
50     /**
51        Converts a name object in PDF format to a string value as
52        stored by this class.
53        @param pdf the name object in PDF format.
54      */

55     protected static String JavaDoc pdfToString(String JavaDoc 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 JavaDoc sb = new StringBuffer JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc s = _s;
110         int len = s.length();
111         String JavaDoc 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': // null is technically not allowed
119
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; // 1 for '#'
150
} else {
151                 dos.write(ch);
152                 count++;
153             }
154             
155         }
156
157         return count;
158         
159     }
160
161 }
162
Popular Tags