PdfString.java

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 1 1. toPdf : negated conditional → NO_COVERAGE
        if (writer != null)
148
            crypto = writer.getEncryption();
149 2 1. toPdf : negated conditional → NO_COVERAGE
2. toPdf : negated conditional → NO_COVERAGE
        if (crypto != null && !crypto.isEmbeddedFilesOnly())
150
            b = crypto.encryptByteArray(b);
151 1 1. toPdf : negated conditional → NO_COVERAGE
        if (hexWriting) {
152
            ByteBuffer buf = new ByteBuffer();
153
            buf.append('<');
154
            int len = b.length;
155
            for (byte b1 : b) buf.appendHex(b1);
156
            buf.append('>');
157 1 1. toPdf : removed call to java/io/OutputStream::write → NO_COVERAGE
            os.write(buf.toByteArray());
158
        }
159
        else
160 1 1. toPdf : removed call to java/io/OutputStream::write → NO_COVERAGE
            os.write(PdfContentByte.escapeString(b));
161
    }
162
    
163
    /**
164
     * Returns the <CODE>String</CODE> value of this <CODE>PdfString</CODE>-object.
165
     *
166
     * @return A <CODE>String</CODE>
167
     */
168
    public String toString() {
169
        return value;
170
    }
171
    
172
    public byte[] getBytes() {
173 1 1. getBytes : negated conditional → NO_COVERAGE
        if (bytes == null) {
174 3 1. getBytes : negated conditional → NO_COVERAGE
2. getBytes : negated conditional → NO_COVERAGE
3. getBytes : negated conditional → NO_COVERAGE
            if (encoding != null && encoding.equals(TEXT_UNICODE) && PdfEncodings.isPdfDocEncoding(value))
175
                bytes = PdfEncodings.convertToBytes(value, TEXT_PDFDOCENCODING);
176
            else
177
                bytes = PdfEncodings.convertToBytes(value, encoding);
178
        }
179 1 1. getBytes : mutated return of Object value for com/lowagie/text/pdf/PdfString::getBytes to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return bytes;
180
    }
181
    
182
    // other methods
183
    
184
    /**
185
     * Returns the Unicode <CODE>String</CODE> value of this
186
     * <CODE>PdfString</CODE>-object.
187
     *
188
     * @return A <CODE>String</CODE>
189
     */
190
    public String toUnicodeString() {
191 2 1. toUnicodeString : negated conditional → NO_COVERAGE
2. toUnicodeString : negated conditional → NO_COVERAGE
        if (encoding != null && encoding.length() != 0)
192 1 1. toUnicodeString : mutated return of Object value for com/lowagie/text/pdf/PdfString::toUnicodeString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
            return value;
193
        getBytes();
194 4 1. toUnicodeString : changed conditional boundary → NO_COVERAGE
2. toUnicodeString : negated conditional → NO_COVERAGE
3. toUnicodeString : negated conditional → NO_COVERAGE
4. toUnicodeString : negated conditional → NO_COVERAGE
        if (bytes.length >= 2 && bytes[0] == (byte)254 && bytes[1] == (byte)255)
195 1 1. toUnicodeString : mutated return of Object value for com/lowagie/text/pdf/PdfString::toUnicodeString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
            return PdfEncodings.convertToString(bytes, PdfObject.TEXT_UNICODE);
196
        else
197 1 1. toUnicodeString : mutated return of Object value for com/lowagie/text/pdf/PdfString::toUnicodeString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
            return PdfEncodings.convertToString(bytes, PdfObject.TEXT_PDFDOCENCODING);
198
    }
199
    
200
    /**
201
     * Gets the encoding of this string.
202
     *
203
     * @return a <CODE>String</CODE>
204
     */
205
    public String getEncoding() {
206
        return encoding;
207
    }
208
    
209
    void setObjNum(int objNum, int objGen) {
210
        this.objNum = objNum;
211
        this.objGen = objGen;
212
    }
213
    
214
    /**
215
     * Decrypt an encrypted <CODE>PdfString</CODE>
216
     */
217
    void decrypt(PdfReader reader) {
218
        PdfEncryption decrypt = reader.getDecrypt();
219 1 1. decrypt : negated conditional → NO_COVERAGE
        if (decrypt != null) {
220
            originalValue = value;
221 1 1. decrypt : removed call to com/lowagie/text/pdf/PdfEncryption::setHashKey → NO_COVERAGE
            decrypt.setHashKey(objNum, objGen);
222
            bytes = PdfEncodings.convertToBytes(value, null);
223
            bytes = decrypt.decryptByteArray(bytes);
224
            value = PdfEncodings.convertToString(bytes, null);
225
        }
226
    }
227
    
228
    /**
229
     * @return The original bytes used to create this PDF string, or the bytes of our current value
230
     *         if the original bytes are missing.
231
     */
232
    public byte[] getOriginalBytes() {
233 1 1. getOriginalBytes : negated conditional → NO_COVERAGE
        if (originalValue == null)
234 1 1. getOriginalBytes : mutated return of Object value for com/lowagie/text/pdf/PdfString::getOriginalBytes to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
            return getBytes();
235 1 1. getOriginalBytes : mutated return of Object value for com/lowagie/text/pdf/PdfString::getOriginalBytes to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return PdfEncodings.convertToBytes(originalValue, null);
236
    }
237
    
238
    /**
239
     * return the characters in our value without any translation. This allows
240
     * a string to be built that holds 2-byte or one-byte character codes, as needed
241
     * for processing by fonts when extracting text.
242
     * 
243
     * Intended for use when no encoding transformations are desired.
244
     * @return The code points in this font as chars.
245
     */
246
    public char[] getOriginalChars() {
247
        char[] chars;
248 2 1. getOriginalChars : negated conditional → NO_COVERAGE
2. getOriginalChars : negated conditional → NO_COVERAGE
        if (encoding == null || encoding.length() == 0) {
249
            byte [] bytes = getOriginalBytes();
250
            chars = new char[bytes.length];
251 2 1. getOriginalChars : changed conditional boundary → NO_COVERAGE
2. getOriginalChars : negated conditional → NO_COVERAGE
            for (int i = 0; i<bytes.length; i++)
252 1 1. getOriginalChars : Replaced bitwise AND with OR → NO_COVERAGE
                chars[i] = (char) (bytes[i]&0xff);
253
        } else {
254
            chars = new char[0];
255
        }
256 1 1. getOriginalChars : mutated return of Object value for com/lowagie/text/pdf/PdfString::getOriginalChars to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return chars;
257
    }
258
    
259
    public PdfString setHexWriting(boolean hexWriting) {
260
        this.hexWriting = hexWriting;
261 1 1. setHexWriting : mutated return of Object value for com/lowagie/text/pdf/PdfString::setHexWriting to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return this;
262
    }
263
    
264
    public boolean isHexWriting() {
265
        return hexWriting;
266
    }
267
}

Mutations

147

1.1
Location : toPdf
Killed by : none
negated conditional → NO_COVERAGE

149

1.1
Location : toPdf
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : toPdf
Killed by : none
negated conditional → NO_COVERAGE

151

1.1
Location : toPdf
Killed by : none
negated conditional → NO_COVERAGE

157

1.1
Location : toPdf
Killed by : none
removed call to java/io/OutputStream::write → NO_COVERAGE

160

1.1
Location : toPdf
Killed by : none
removed call to java/io/OutputStream::write → NO_COVERAGE

173

1.1
Location : getBytes
Killed by : none
negated conditional → NO_COVERAGE

174

1.1
Location : getBytes
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : getBytes
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : getBytes
Killed by : none
negated conditional → NO_COVERAGE

179

1.1
Location : getBytes
Killed by : none
mutated return of Object value for com/lowagie/text/pdf/PdfString::getBytes to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

191

1.1
Location : toUnicodeString
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : toUnicodeString
Killed by : none
negated conditional → NO_COVERAGE

192

1.1
Location : toUnicodeString
Killed by : none
mutated return of Object value for com/lowagie/text/pdf/PdfString::toUnicodeString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

194

1.1
Location : toUnicodeString
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : toUnicodeString
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : toUnicodeString
Killed by : none
negated conditional → NO_COVERAGE

4.4
Location : toUnicodeString
Killed by : none
negated conditional → NO_COVERAGE

195

1.1
Location : toUnicodeString
Killed by : none
mutated return of Object value for com/lowagie/text/pdf/PdfString::toUnicodeString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

197

1.1
Location : toUnicodeString
Killed by : none
mutated return of Object value for com/lowagie/text/pdf/PdfString::toUnicodeString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

219

1.1
Location : decrypt
Killed by : none
negated conditional → NO_COVERAGE

221

1.1
Location : decrypt
Killed by : none
removed call to com/lowagie/text/pdf/PdfEncryption::setHashKey → NO_COVERAGE

233

1.1
Location : getOriginalBytes
Killed by : none
negated conditional → NO_COVERAGE

234

1.1
Location : getOriginalBytes
Killed by : none
mutated return of Object value for com/lowagie/text/pdf/PdfString::getOriginalBytes to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

235

1.1
Location : getOriginalBytes
Killed by : none
mutated return of Object value for com/lowagie/text/pdf/PdfString::getOriginalBytes to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

248

1.1
Location : getOriginalChars
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : getOriginalChars
Killed by : none
negated conditional → NO_COVERAGE

251

1.1
Location : getOriginalChars
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : getOriginalChars
Killed by : none
negated conditional → NO_COVERAGE

252

1.1
Location : getOriginalChars
Killed by : none
Replaced bitwise AND with OR → NO_COVERAGE

256

1.1
Location : getOriginalChars
Killed by : none
mutated return of Object value for com/lowagie/text/pdf/PdfString::getOriginalChars to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

261

1.1
Location : setHexWriting
Killed by : none
mutated return of Object value for com/lowagie/text/pdf/PdfString::setHexWriting to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.4.2