KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfString


1 /*
2  * $Id: PdfString.java 2739 2007-05-04 11:24:51Z blowagie $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.pdf;
52
53 import java.io.IOException JavaDoc;
54 import java.io.OutputStream JavaDoc;
55
56 /**
57  * A <CODE>PdfString</CODE>-class is the PDF-equivalent of a JAVA-<CODE>String</CODE>-object.
58  * <P>
59  * A string is a sequence of characters delimited by parenthesis. If a string is too long
60  * to be conveniently placed on a single line, it may be split across multiple lines by using
61  * the backslash character (\) at the end of a line to indicate that the string continues
62  * on the following line. Within a string, the backslash character is used as an escape to
63  * specify unbalanced parenthesis, non-printing ASCII characters, and the backslash character
64  * itself. Use of the \<I>ddd</I> escape sequence is the preferred way to represent characters
65  * outside the printable ASCII character set.<BR>
66  * This object is described in the 'Portable Document Format Reference Manual version 1.7'
67  * section 3.2.3 (page 53-56).
68  *
69  * @see PdfObject
70  * @see BadPdfFormatException
71  */

72
73 public class PdfString extends PdfObject {
74     
75     // membervariables
76

77     /** The value of this object. */
78     protected String JavaDoc value = NOTHING;
79     protected String JavaDoc originalValue = null;
80     
81     /** The encoding. */
82     protected String JavaDoc encoding = TEXT_PDFDOCENCODING;
83     protected int objNum = 0;
84     protected int objGen = 0;
85     protected boolean hexWriting = false;
86
87     // constructors
88

89     /**
90      * Constructs an empty <CODE>PdfString</CODE>-object.
91      */

92     
93     public PdfString() {
94         super(STRING);
95     }
96     
97     /**
98      * Constructs a <CODE>PdfString</CODE>-object.
99      *
100      * @param value the content of the string
101      */

102     
103     public PdfString(String JavaDoc value) {
104         super(STRING);
105         this.value = value;
106     }
107     
108     /**
109      * Constructs a <CODE>PdfString</CODE>-object.
110      *
111      * @param value the content of the string
112      * @param encoding an encoding
113      */

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

126     
127     public PdfString(byte[] bytes) {
128         super(STRING);
129         value = PdfEncodings.convertToString(bytes, null);
130         encoding = NOTHING;
131     }
132     
133     // methods overriding some methods in PdfObject
134

135     /**
136      * Returns the PDF representation of this <CODE>PdfString</CODE>.
137      */

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

165     
166     public String JavaDoc toString() {
167         return value;
168     }
169     
170     // other methods
171

172     /**
173      * Gets the encoding of this string.
174      *
175      * @return a <CODE>String</CODE>
176      */

177     
178     public String JavaDoc getEncoding() {
179         return encoding;
180     }
181     
182     public String JavaDoc toUnicodeString() {
183         if (encoding != null && encoding.length() != 0)
184             return value;
185         getBytes();
186         if (bytes.length >= 2 && bytes[0] == (byte)254 && bytes[1] == (byte)255)
187             return PdfEncodings.convertToString(bytes, PdfObject.TEXT_UNICODE);
188         else
189             return PdfEncodings.convertToString(bytes, PdfObject.TEXT_PDFDOCENCODING);
190     }
191     
192     void setObjNum(int objNum, int objGen) {
193         this.objNum = objNum;
194         this.objGen = objGen;
195     }
196     
197     void decrypt(PdfReader reader) {
198         PdfEncryption decrypt = reader.getDecrypt();
199         if (decrypt != null) {
200             originalValue = value;
201             decrypt.setHashKey(objNum, objGen);
202             bytes = PdfEncodings.convertToBytes(value, null);
203             bytes = decrypt.decryptByteArray(bytes);
204             value = PdfEncodings.convertToString(bytes, null);
205         }
206     }
207    
208     public byte[] getBytes() {
209         if (bytes == null) {
210             if (encoding != null && encoding.equals(TEXT_UNICODE) && PdfEncodings.isPdfDocEncoding(value))
211                 bytes = PdfEncodings.convertToBytes(value, TEXT_PDFDOCENCODING);
212             else
213                 bytes = PdfEncodings.convertToBytes(value, encoding);
214         }
215         return bytes;
216     }
217     
218     public byte[] getOriginalBytes() {
219         if (originalValue == null)
220             return getBytes();
221         return PdfEncodings.convertToBytes(originalValue, null);
222     }
223     
224     public PdfString setHexWriting(boolean hexWriting) {
225         this.hexWriting = hexWriting;
226         return this;
227     }
228     
229     public boolean isHexWriting() {
230         return hexWriting;
231     }
232 }
Popular Tags