FdfReader.java

1
/*
2
 * Copyright 2003 by Paulo Soares.
3
 *
4
 * The contents of this file are subject to the Mozilla Public License Version 1.1
5
 * (the "License"); you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7
 *
8
 * Software distributed under the License is distributed on an "AS IS" basis,
9
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10
 * for the specific language governing rights and limitations under the License.
11
 *
12
 * The Original Code is 'iText, a free JAVA-PDF library'.
13
 *
14
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
15
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
16
 * All Rights Reserved.
17
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
18
 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
19
 *
20
 * Contributor(s): all the names of the contributors are added in the source code
21
 * where applicable.
22
 *
23
 * Alternatively, the contents of this file may be used under the terms of the
24
 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
25
 * provisions of LGPL are applicable instead of those above.  If you wish to
26
 * allow use of your version of this file only under the terms of the LGPL
27
 * License and not to allow others to use your version of this file under
28
 * the MPL, indicate your decision by deleting the provisions above and
29
 * replace them with the notice and other provisions required by the LGPL.
30
 * If you do not delete the provisions above, a recipient may use your version
31
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
32
 *
33
 * This library is free software; you can redistribute it and/or modify it
34
 * under the terms of the MPL as stated above or under the terms of the GNU
35
 * Library General Public License as published by the Free Software Foundation;
36
 * either version 2 of the License, or any later version.
37
 *
38
 * This library is distributed in the hope that it will be useful, but WITHOUT
39
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
40
 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
41
 * details.
42
 *
43
 * If you didn't download this code from the following link, you should check if
44
 * you aren't using an obsolete version:
45
 * http://www.lowagie.com/iText/
46
 */
47
package com.lowagie.text.pdf;
48
import java.io.IOException;
49
import java.io.InputStream;
50
import java.net.URL;
51
import java.util.HashMap;
52
import java.util.Map;
53
54
/** Reads an FDF form and makes the fields available
55
 * @author Paulo Soares (psoares@consiste.pt)
56
 */
57
public class FdfReader extends PdfReader {
58
59
    private Map<String, PdfDictionary> fields;
60
    private String fileSpec;
61
    PdfName encoding;
62
63
    /** Reads an FDF form.
64
     * @param filename the file name of the form
65
     * @throws IOException on error
66
     */
67
    public FdfReader(String filename) throws IOException {
68
        super(filename);
69
    }
70
71
    /** Reads an FDF form.
72
     * @param pdfIn the byte array with the form
73
     * @throws IOException on error
74
     */
75
    public FdfReader(byte[] pdfIn) throws IOException {
76
        super(pdfIn);
77
    }
78
79
    /** Reads an FDF form.
80
     * @param url the URL of the document
81
     * @throws IOException on error
82
     */
83
    public FdfReader(URL url) throws IOException {
84
        super(url);
85
    }
86
87
    /** Reads an FDF form.
88
     * @param is the <CODE>InputStream</CODE> containing the document. The stream is read to the
89
     * end but is not closed
90
     * @throws IOException on error
91
     */
92
    public FdfReader(InputStream is) throws IOException {
93
        super(is);
94
    }
95
96
    protected void readPdf() throws IOException {
97
        fields = new HashMap<>();
98
        try {
99 1 1. readPdf : removed call to com/lowagie/text/pdf/PRTokeniser::checkFdfHeader → NO_COVERAGE
            tokens.checkFdfHeader();
100 1 1. readPdf : removed call to com/lowagie/text/pdf/FdfReader::rebuildXref → NO_COVERAGE
            rebuildXref();
101 1 1. readPdf : removed call to com/lowagie/text/pdf/FdfReader::readDocObj → NO_COVERAGE
            readDocObj();
102
        }
103
        finally {
104
            try {
105 1 1. readPdf : removed call to com/lowagie/text/pdf/PRTokeniser::close → NO_COVERAGE
                tokens.close();
106
            }
107
            catch (Exception e) {
108
                // empty on purpose
109
            }
110
        }
111 1 1. readPdf : removed call to com/lowagie/text/pdf/FdfReader::readFields → NO_COVERAGE
        readFields();
112
    }
113
114
    protected void kidNode(PdfDictionary merged, String name) {
115
        PdfArray kids = merged.getAsArray(PdfName.KIDS);
116 2 1. kidNode : negated conditional → NO_COVERAGE
2. kidNode : negated conditional → NO_COVERAGE
        if (kids == null || kids.isEmpty()) {
117 2 1. kidNode : changed conditional boundary → NO_COVERAGE
2. kidNode : negated conditional → NO_COVERAGE
            if (name.length() > 0)
118
                name = name.substring(1);
119
            fields.put(name, merged);
120
        }
121
        else {
122 1 1. kidNode : removed call to com/lowagie/text/pdf/PdfDictionary::remove → NO_COVERAGE
            merged.remove(PdfName.KIDS);
123 2 1. kidNode : changed conditional boundary → NO_COVERAGE
2. kidNode : negated conditional → NO_COVERAGE
            for (int k = 0; k < kids.size(); ++k) {
124
                PdfDictionary dic = new PdfDictionary();
125 1 1. kidNode : removed call to com/lowagie/text/pdf/PdfDictionary::merge → NO_COVERAGE
                dic.merge(merged);
126
                PdfDictionary newDic = kids.getAsDict(k);
127
                PdfString t = newDic.getAsString(PdfName.T);
128
                String newName = name;
129 1 1. kidNode : negated conditional → NO_COVERAGE
                if (t != null)
130
                    newName += "." + t.toUnicodeString();
131 1 1. kidNode : removed call to com/lowagie/text/pdf/PdfDictionary::merge → NO_COVERAGE
                dic.merge(newDic);
132 1 1. kidNode : removed call to com/lowagie/text/pdf/PdfDictionary::remove → NO_COVERAGE
                dic.remove(PdfName.T);
133 1 1. kidNode : removed call to com/lowagie/text/pdf/FdfReader::kidNode → NO_COVERAGE
                kidNode(dic, newName);
134
            }
135
        }
136
    }
137
138
    protected void readFields() {
139
        catalog = trailer.getAsDict(PdfName.ROOT);
140
        PdfDictionary fdf = catalog.getAsDict(PdfName.FDF);
141 1 1. readFields : negated conditional → NO_COVERAGE
        if (fdf == null)
142
            return;
143
        PdfString fs = fdf.getAsString(PdfName.F);
144 1 1. readFields : negated conditional → NO_COVERAGE
        if (fs != null)
145
            fileSpec = fs.toUnicodeString();
146
        PdfArray fld = fdf.getAsArray(PdfName.FIELDS);
147 1 1. readFields : negated conditional → NO_COVERAGE
        if (fld == null)
148
            return;
149
        encoding = fdf.getAsName(PdfName.ENCODING);
150
        PdfDictionary merged = new PdfDictionary();
151 1 1. readFields : removed call to com/lowagie/text/pdf/PdfDictionary::put → NO_COVERAGE
        merged.put(PdfName.KIDS, fld);
152 1 1. readFields : removed call to com/lowagie/text/pdf/FdfReader::kidNode → NO_COVERAGE
        kidNode(merged, "");
153
    }
154
155
    /** Gets all the fields. The map is keyed by the fully qualified
156
     * field name and the value is a merged <CODE>PdfDictionary</CODE>
157
     * with the field content.
158
     * @return all the fields
159
     */
160
    public Map<String, PdfDictionary> getFields() {
161
        return fields;
162
    }
163
164
    /** Gets the field dictionary.
165
     * @param name the fully qualified field name
166
     * @return the field dictionary
167
     */
168
    public PdfDictionary getField(String name) {
169 1 1. getField : mutated return of Object value for com/lowagie/text/pdf/FdfReader::getField to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return fields.get(name);
170
    }
171
172
    /** Gets the field value or <CODE>null</CODE> if the field does not
173
     * exist or has no value defined.
174
     * @param name the fully qualified field name
175
     * @return the field value or <CODE>null</CODE>
176
     */
177
    public String getFieldValue(String name) {
178
        PdfDictionary field = fields.get(name);
179 1 1. getFieldValue : negated conditional → NO_COVERAGE
        if (field == null)
180 1 1. getFieldValue : mutated return of Object value for com/lowagie/text/pdf/FdfReader::getFieldValue to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
            return null;
181
        PdfObject v = getPdfObject(field.get(PdfName.V));
182 1 1. getFieldValue : negated conditional → NO_COVERAGE
        if (v == null)
183 1 1. getFieldValue : mutated return of Object value for com/lowagie/text/pdf/FdfReader::getFieldValue to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
            return null;
184 1 1. getFieldValue : negated conditional → NO_COVERAGE
        if (v.isName())
185 1 1. getFieldValue : mutated return of Object value for com/lowagie/text/pdf/FdfReader::getFieldValue to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
            return PdfName.decodeName(v.toString());
186 1 1. getFieldValue : negated conditional → NO_COVERAGE
        else if (v.isString()) {
187
            PdfString vs = (PdfString)v;
188 2 1. getFieldValue : negated conditional → NO_COVERAGE
2. getFieldValue : negated conditional → NO_COVERAGE
            if (encoding == null || vs.getEncoding() != null)
189 1 1. getFieldValue : mutated return of Object value for com/lowagie/text/pdf/FdfReader::getFieldValue to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return vs.toUnicodeString();
190
            byte[] b = vs.getBytes();
191 4 1. getFieldValue : changed conditional boundary → NO_COVERAGE
2. getFieldValue : negated conditional → NO_COVERAGE
3. getFieldValue : negated conditional → NO_COVERAGE
4. getFieldValue : negated conditional → NO_COVERAGE
            if (b.length >= 2 && b[0] == (byte)254 && b[1] == (byte)255)
192 1 1. getFieldValue : mutated return of Object value for com/lowagie/text/pdf/FdfReader::getFieldValue to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return vs.toUnicodeString();
193
            try {
194 1 1. getFieldValue : negated conditional → NO_COVERAGE
                if (encoding.equals(PdfName.SHIFT_JIS))
195 1 1. getFieldValue : mutated return of Object value for com/lowagie/text/pdf/FdfReader::getFieldValue to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                    return new String(b, "SJIS");
196 1 1. getFieldValue : negated conditional → NO_COVERAGE
                else if (encoding.equals(PdfName.UHC))
197 1 1. getFieldValue : mutated return of Object value for com/lowagie/text/pdf/FdfReader::getFieldValue to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                    return new String(b, "MS949");
198 1 1. getFieldValue : negated conditional → NO_COVERAGE
                else if (encoding.equals(PdfName.GBK))
199 1 1. getFieldValue : mutated return of Object value for com/lowagie/text/pdf/FdfReader::getFieldValue to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                    return new String(b, "GBK");
200 1 1. getFieldValue : negated conditional → NO_COVERAGE
                else if (encoding.equals(PdfName.BIGFIVE))
201 1 1. getFieldValue : mutated return of Object value for com/lowagie/text/pdf/FdfReader::getFieldValue to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                    return new String(b, "Big5");
202
            }
203
            catch (Exception e) {
204
            }
205 1 1. getFieldValue : mutated return of Object value for com/lowagie/text/pdf/FdfReader::getFieldValue to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
            return vs.toUnicodeString();
206
        }
207 1 1. getFieldValue : mutated return of Object value for com/lowagie/text/pdf/FdfReader::getFieldValue to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return null;
208
    }
209
210
    /** Gets the PDF file specification contained in the FDF.
211
     * @return the PDF file specification contained in the FDF
212
     */
213
    public String getFileSpec() {
214
        return fileSpec;
215
    }
216
}

Mutations

99

1.1
Location : readPdf
Killed by : none
removed call to com/lowagie/text/pdf/PRTokeniser::checkFdfHeader → NO_COVERAGE

100

1.1
Location : readPdf
Killed by : none
removed call to com/lowagie/text/pdf/FdfReader::rebuildXref → NO_COVERAGE

101

1.1
Location : readPdf
Killed by : none
removed call to com/lowagie/text/pdf/FdfReader::readDocObj → NO_COVERAGE

105

1.1
Location : readPdf
Killed by : none
removed call to com/lowagie/text/pdf/PRTokeniser::close → NO_COVERAGE

111

1.1
Location : readPdf
Killed by : none
removed call to com/lowagie/text/pdf/FdfReader::readFields → NO_COVERAGE

116

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

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

117

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

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

122

1.1
Location : kidNode
Killed by : none
removed call to com/lowagie/text/pdf/PdfDictionary::remove → NO_COVERAGE

123

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

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

125

1.1
Location : kidNode
Killed by : none
removed call to com/lowagie/text/pdf/PdfDictionary::merge → NO_COVERAGE

129

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

131

1.1
Location : kidNode
Killed by : none
removed call to com/lowagie/text/pdf/PdfDictionary::merge → NO_COVERAGE

132

1.1
Location : kidNode
Killed by : none
removed call to com/lowagie/text/pdf/PdfDictionary::remove → NO_COVERAGE

133

1.1
Location : kidNode
Killed by : none
removed call to com/lowagie/text/pdf/FdfReader::kidNode → NO_COVERAGE

141

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

144

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

147

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

151

1.1
Location : readFields
Killed by : none
removed call to com/lowagie/text/pdf/PdfDictionary::put → NO_COVERAGE

152

1.1
Location : readFields
Killed by : none
removed call to com/lowagie/text/pdf/FdfReader::kidNode → NO_COVERAGE

169

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

179

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

180

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

182

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

183

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

184

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

185

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

186

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

188

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

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

189

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

191

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

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

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

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

192

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

194

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

195

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

196

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

197

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

198

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

199

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

200

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

201

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

205

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

207

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

Active mutators

Tests examined


Report generated by PIT 1.4.2