1 /*
2  * $Id: PdfString.java 3759 2009-03-06 16:05:00Z 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
57  * JAVA-<CODE>String</CODE>-object.
58  * <P>
59  * A string is a sequence of characters delimited by parenthesis.
60  * If a string is too long to be conveniently placed on a single line, it may
61  * be split across multiple lines by using the backslash character (\) at the
62  * end of a line to indicate that the string continues on the following line.
63  * Within a string, the backslash character is used as an escape to specify
64  * unbalanced parenthesis, non-printing ASCII characters, and the backslash
65  * character itself. Use of the \<I>ddd</I> escape sequence is the preferred
66  * way to represent characters outside the printable ASCII character set.<BR>
67  * This object is described in the 'Portable Document Format Reference Manual
68  * version 1.7' section 3.2.3 (page 53-56).
69  *
70  * @see PdfObject
71  * @see BadPdfFormatException
72  */

73 public class PdfString extends PdfObject {
74     
75     // CLASS VARIABLES
76     
77     /** The value of this object. */
78     protected String value = NOTHING;
79     
80     protected String originalValue = null;
81     
82     /** The encoding. */
83     protected String encoding = TEXT_PDFDOCENCODING;
84     
85     protected int objNum = 0;
86     
87     protected int objGen = 0;
88     
89     protected boolean hexWriting = false;
90
91     // CONSTRUCTORS
92     
93     /**
94      * Constructs an empty <CODE>PdfString</CODE>-object.
95      */

96     public PdfString() {
97         super(STRING);
98     }
99     
100     /**
101      * Constructs a <CODE>PdfString</CODE>-object containing a string in the
102      * standard encoding <CODE>TEXT_PDFDOCENCODING</CODE>.
103      *
104      * @param value    the content of the string
105      */

106     public PdfString(String value) {
107         super(STRING);
108         this.value = value;
109     }
110     
111     /**
112      * Constructs a <CODE>PdfString</CODE>-object containing a string in the
113      * specified encoding.
114      *
115      * @param value    the content of the string
116      * @param encoding an encoding
117      */

118     public PdfString(String value, String encoding) {
119         super(STRING);
120         this.value = value;
121         this.encoding = encoding;
122     }
123     
124     /**
125      * Constructs a <CODE>PdfString</CODE>-object.
126      *
127      * @param bytes    an array of <CODE>byte</CODE>
128      */

129     public PdfString(byte[] bytes) {
130         super(STRING);
131         value = PdfEncodings.convertToString(bytes, null);
132         encoding = NOTHING;
133     }
134     
135     // methods overriding some methods in PdfObject
136     
137     /**
138      * Writes the PDF representation of this <CODE>PdfString</CODE> as an array
139      * of <CODE>byte</CODE> to the specified <CODE>OutputStream</CODE>.
140      * 
141      * @param writer for backwards compatibility
142      * @param os The <CODE>OutputStream</CODE> to write the bytes to.
143      */

144     public void toPdf(PdfWriter writer, OutputStream os) throws IOException {
145         byte b[] = getBytes();
146         PdfEncryption crypto = null;
147         if (writer != null)
148             crypto = writer.getEncryption();
149         if (crypto != null && !crypto.isEmbeddedFilesOnly())
150             b = crypto.encryptByteArray(b);
151         if (hexWriting) {
152             ByteBuffer buf = new ByteBuffer();
153             buf.append('<');
154             int len = b.length;
155             for (int k = 0; k < len; ++k)
156                 buf.appendHex(b[k]);
157             buf.append('>');
158             os.write(buf.toByteArray());
159         }
160         else
161             os.write(PdfContentByte.escapeString(b));
162     }
163     
164     /**
165      * Returns the <CODE>String</CODE> value of this <CODE>PdfString</CODE>-object.
166      *
167      * @return A <CODE>String</CODE>
168      */

169     public String toString() {
170         return value;
171     }
172     
173     public byte[] getBytes() {
174         if (bytes == null) {
175             if (encoding != null && encoding.equals(TEXT_UNICODE) && PdfEncodings.isPdfDocEncoding(value))
176                 bytes = PdfEncodings.convertToBytes(value, TEXT_PDFDOCENCODING);
177             else
178                 bytes = PdfEncodings.convertToBytes(value, encoding);
179         }
180         return bytes;
181     }
182     
183     // other methods
184     
185     /**
186      * Returns the Unicode <CODE>String</CODE> value of this
187      * <CODE>PdfString</CODE>-object.
188      *
189      * @return A <CODE>String</CODE>
190      */

191     public String toUnicodeString() {
192         if (encoding != null && encoding.length() != 0)
193             return value;
194         getBytes();
195         if (bytes.length >= 2 && bytes[0] == (byte)254 && bytes[1] == (byte)255)
196             return PdfEncodings.convertToString(bytes, PdfObject.TEXT_UNICODE);
197         else
198             return PdfEncodings.convertToString(bytes, PdfObject.TEXT_PDFDOCENCODING);
199     }
200     
201     /**
202      * Gets the encoding of this string.
203      *
204      * @return a <CODE>String</CODE>
205      */

206     public String getEncoding() {
207         return encoding;
208     }
209     
210     void setObjNum(int objNum, int objGen) {
211         this.objNum = objNum;
212         this.objGen = objGen;
213     }
214     
215     /**
216      * Decrypt an encrypted <CODE>PdfString</CODE>
217      */

218     void decrypt(PdfReader reader) {
219         PdfEncryption decrypt = reader.getDecrypt();
220         if (decrypt != null) {
221             originalValue = value;
222             decrypt.setHashKey(objNum, objGen);
223             bytes = PdfEncodings.convertToBytes(value, null);
224             bytes = decrypt.decryptByteArray(bytes);
225             value = PdfEncodings.convertToString(bytes, null);
226         }
227     }
228    
229     public byte[] getOriginalBytes() {
230         if (originalValue == null)
231             return getBytes();
232         return PdfEncodings.convertToBytes(originalValue, null);
233     }
234     
235     public PdfString setHexWriting(boolean hexWriting) {
236         this.hexWriting = hexWriting;
237         return this;
238     }
239     
240     public boolean isHexWriting() {
241         return hexWriting;
242     }
243 }