1 /*
2 * $Id: PdfString.java 3538 2008-07-08 13:08:04Z blowagie $
3 *
4 * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * (the "License"); you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the License.
13 *
14 * The Original Code is 'iText, a free JAVA-PDF library'.
15 *
16 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18 * All Rights Reserved.
19 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21 *
22 * Contributor(s): all the names of the contributors are added in the source code
23 * where applicable.
24 *
25 * Alternatively, the contents of this file may be used under the terms of the
26 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27 * provisions of LGPL are applicable instead of those above. If you wish to
28 * allow use of your version of this file only under the terms of the LGPL
29 * License and not to allow others to use your version of this file under
30 * the MPL, indicate your decision by deleting the provisions above and
31 * replace them with the notice and other provisions required by the LGPL.
32 * If you do not delete the provisions above, a recipient may use your version
33 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34 *
35 * This library is free software; you can redistribute it and/or modify it
36 * under the terms of the MPL as stated above or under the terms of the GNU
37 * Library General Public License as published by the Free Software Foundation;
38 * either version 2 of the License, or any later version.
39 *
40 * This library is distributed in the hope that it will be useful, but WITHOUT
41 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43 * details.
44 *
45 * If you didn't download this code from the following link, you should check if
46 * you aren't using an obsolete version:
47 * http://www.lowagie.com/iText/
48 */
49
50 package com.lowagie.text.pdf;
51
52 import java.io.IOException;
53 import java.io.OutputStream;
54
55 /**
56 * A <CODE>PdfString</CODE>-class is the PDF-equivalent of a JAVA-<CODE>String</CODE>-object.
57 * <P>
58 * A string is a sequence of characters delimited by parenthesis. If a string is too long
59 * to be conveniently placed on a single line, it may be split across multiple lines by using
60 * the backslash character (\) at the end of a line to indicate that the string continues
61 * on the following line. Within a string, the backslash character is used as an escape to
62 * specify unbalanced parenthesis, non-printing ASCII characters, and the backslash character
63 * itself. Use of the \<I>ddd</I> escape sequence is the preferred way to represent characters
64 * outside the printable ASCII character set.<BR>
65 * This object is described in the 'Portable Document Format Reference Manual version 1.7'
66 * section 3.2.3 (page 53-56).
67 *
68 * @see PdfObject
69 * @see BadPdfFormatException
70 */
71
72 public class PdfString extends PdfObject {
73
74 // membervariables
75
76 /** The value of this object. */
77 protected String value = NOTHING;
78 protected String originalValue = null;
79
80 /** The encoding. */
81 protected String encoding = TEXT_PDFDOCENCODING;
82 protected int objNum = 0;
83 protected int objGen = 0;
84 protected boolean hexWriting = false;
85
86 // constructors
87
88 /**
89 * Constructs an empty <CODE>PdfString</CODE>-object.
90 */
91
92 public PdfString() {
93 super(STRING);
94 }
95
96 /**
97 * Constructs a <CODE>PdfString</CODE>-object.
98 *
99 * @param value the content of the string
100 */
101
102 public PdfString(String value) {
103 super(STRING);
104 this.value = value;
105 }
106
107 /**
108 * Constructs a <CODE>PdfString</CODE>-object.
109 *
110 * @param value the content of the string
111 * @param encoding an encoding
112 */
113
114 public PdfString(String value, String encoding) {
115 super(STRING);
116 this.value = value;
117 this.encoding = encoding;
118 }
119
120 /**
121 * Constructs a <CODE>PdfString</CODE>-object.
122 *
123 * @param bytes an array of <CODE>byte</CODE>
124 */
125
126 public PdfString(byte[] bytes) {
127 super(STRING);
128 value = PdfEncodings.convertToString(bytes, null);
129 encoding = NOTHING;
130 }
131
132 // methods overriding some methods in PdfObject
133
134 /**
135 * Returns the PDF representation of this <CODE>PdfString</CODE>.
136 */
137
138 public void toPdf(PdfWriter writer, OutputStream os) throws IOException {
139 byte b[] = getBytes();
140 PdfEncryption crypto = null;
141 if (writer != null)
142 crypto = writer.getEncryption();
143 if (crypto != null && !crypto.isEmbeddedFilesOnly()) {
144 b = crypto.encryptByteArray(b);
145 }
146 if (hexWriting) {
147 ByteBuffer buf = new ByteBuffer();
148 buf.append('<');
149 int len = b.length;
150 for (int k = 0; k < len; ++k)
151 buf.appendHex(b[k]);
152 buf.append('>');
153 os.write(buf.toByteArray());
154 }
155 else
156 os.write(PdfContentByte.escapeString(b));
157 }
158
159 /**
160 * Returns the <CODE>String</CODE> value of the <CODE>PdfString</CODE>-object.
161 *
162 * @return a <CODE>String</CODE>
163 */
164
165 public String toString() {
166 return value;
167 }
168
169 // other methods
170
171 /**
172 * Gets the encoding of this string.
173 *
174 * @return a <CODE>String</CODE>
175 */
176
177 public String getEncoding() {
178 return encoding;
179 }
180
181 public String toUnicodeString() {
182 if (encoding != null && encoding.length() != 0)
183 return value;
184 getBytes();
185 if (bytes.length >= 2 && bytes[0] == (byte)254 && bytes[1] == (byte)255)
186 return PdfEncodings.convertToString(bytes, PdfObject.TEXT_UNICODE);
187 else
188 return PdfEncodings.convertToString(bytes, PdfObject.TEXT_PDFDOCENCODING);
189 }
190
191 void setObjNum(int objNum, int objGen) {
192 this.objNum = objNum;
193 this.objGen = objGen;
194 }
195
196 void decrypt(PdfReader reader) {
197 PdfEncryption decrypt = reader.getDecrypt();
198 if (decrypt != null) {
199 originalValue = value;
200 decrypt.setHashKey(objNum, objGen);
201 bytes = PdfEncodings.convertToBytes(value, null);
202 bytes = decrypt.decryptByteArray(bytes);
203 value = PdfEncodings.convertToString(bytes, null);
204 }
205 }
206
207 public byte[] getBytes() {
208 if (bytes == null) {
209 if (encoding != null && encoding.equals(TEXT_UNICODE) && PdfEncodings.isPdfDocEncoding(value))
210 bytes = PdfEncodings.convertToBytes(value, TEXT_PDFDOCENCODING);
211 else
212 bytes = PdfEncodings.convertToBytes(value, encoding);
213 }
214 return bytes;
215 }
216
217 public byte[] getOriginalBytes() {
218 if (originalValue == null)
219 return getBytes();
220 return PdfEncodings.convertToBytes(originalValue, null);
221 }
222
223 public PdfString setHexWriting(boolean hexWriting) {
224 this.hexWriting = hexWriting;
225 return this;
226 }
227
228 public boolean isHexWriting() {
229 return hexWriting;
230 }
231 }