001: /*
002: * $Id: PdfString.java 2906 2007-08-31 07:12:24Z psoares33 $
003: * $Name$
004: *
005: * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.pdf;
052:
053: import java.io.IOException;
054: import java.io.OutputStream;
055:
056: /**
057: * A <CODE>PdfString</CODE>-class is the PDF-equivalent of a JAVA-<CODE>String</CODE>-object.
058: * <P>
059: * A string is a sequence of characters delimited by parenthesis. If a string is too long
060: * to be conveniently placed on a single line, it may be split across multiple lines by using
061: * the backslash character (\) at the end of a line to indicate that the string continues
062: * on the following line. Within a string, the backslash character is used as an escape to
063: * specify unbalanced parenthesis, non-printing ASCII characters, and the backslash character
064: * itself. Use of the \<I>ddd</I> escape sequence is the preferred way to represent characters
065: * outside the printable ASCII character set.<BR>
066: * This object is described in the 'Portable Document Format Reference Manual version 1.7'
067: * section 3.2.3 (page 53-56).
068: *
069: * @see PdfObject
070: * @see BadPdfFormatException
071: */
072:
073: public class PdfString extends PdfObject {
074:
075: // membervariables
076:
077: /** The value of this object. */
078: protected String value = NOTHING;
079: protected String originalValue = null;
080:
081: /** The encoding. */
082: protected String encoding = TEXT_PDFDOCENCODING;
083: protected int objNum = 0;
084: protected int objGen = 0;
085: protected boolean hexWriting = false;
086:
087: // constructors
088:
089: /**
090: * Constructs an empty <CODE>PdfString</CODE>-object.
091: */
092:
093: public PdfString() {
094: super (STRING);
095: }
096:
097: /**
098: * Constructs a <CODE>PdfString</CODE>-object.
099: *
100: * @param value the content of the string
101: */
102:
103: public PdfString(String 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 value, String 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 os)
140: throws IOException {
141: byte b[] = getBytes();
142: PdfEncryption crypto = null;
143: if (writer != null)
144: crypto = writer.getEncryption();
145: if (crypto != null) {
146: b = crypto.encryptByteArray(b);
147: }
148: if (hexWriting) {
149: ByteBuffer buf = new ByteBuffer();
150: buf.append('<');
151: int len = b.length;
152: for (int k = 0; k < len; ++k)
153: buf.appendHex(b[k]);
154: buf.append('>');
155: os.write(buf.toByteArray());
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 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 getEncoding() {
179: return encoding;
180: }
181:
182: public String toUnicodeString() {
183: if (encoding != null && encoding.length() != 0)
184: return value;
185: getBytes();
186: if (bytes.length >= 2 && bytes[0] == (byte) 254
187: && bytes[1] == (byte) 255)
188: return PdfEncodings.convertToString(bytes,
189: PdfObject.TEXT_UNICODE);
190: else
191: return PdfEncodings.convertToString(bytes,
192: PdfObject.TEXT_PDFDOCENCODING);
193: }
194:
195: void setObjNum(int objNum, int objGen) {
196: this .objNum = objNum;
197: this .objGen = objGen;
198: }
199:
200: void decrypt(PdfReader reader) {
201: PdfEncryption decrypt = reader.getDecrypt();
202: if (decrypt != null) {
203: originalValue = value;
204: decrypt.setHashKey(objNum, objGen);
205: bytes = PdfEncodings.convertToBytes(value, null);
206: bytes = decrypt.decryptByteArray(bytes);
207: value = PdfEncodings.convertToString(bytes, null);
208: }
209: }
210:
211: public byte[] getBytes() {
212: if (bytes == null) {
213: if (encoding != null && encoding.equals(TEXT_UNICODE)
214: && PdfEncodings.isPdfDocEncoding(value))
215: bytes = PdfEncodings.convertToBytes(value,
216: TEXT_PDFDOCENCODING);
217: else
218: bytes = PdfEncodings.convertToBytes(value, encoding);
219: }
220: return bytes;
221: }
222:
223: public byte[] getOriginalBytes() {
224: if (originalValue == null)
225: return getBytes();
226: return PdfEncodings.convertToBytes(originalValue, null);
227: }
228:
229: public PdfString setHexWriting(boolean hexWriting) {
230: this .hexWriting = hexWriting;
231: return this ;
232: }
233:
234: public boolean isHexWriting() {
235: return hexWriting;
236: }
237: }
|