PdfReaderInstance.java

1
/*
2
 * $Id: PdfReaderInstance.java 4065 2009-09-16 23:09:11Z psoares33 $
3
 *
4
 * Copyright 2001, 2002 Paulo Soares
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
import java.io.IOException;
52
import java.util.ArrayList;
53
import java.util.HashMap;
54
55
import com.lowagie.text.error_messages.MessageLocalization;
56
/**
57
 * Instance of PdfReader in each output document.
58
 *
59
 * @author Paulo Soares (psoares@consiste.pt)
60
 */
61
class PdfReaderInstance {
62
    static final PdfLiteral IDENTITYMATRIX = new PdfLiteral("[1 0 0 1 0 0]");
63
    static final PdfNumber ONE = new PdfNumber(1);
64
    int[] myXref;
65
    PdfReader reader;
66
    RandomAccessFileOrArray file;
67
    HashMap importedPages = new HashMap();
68
    PdfWriter writer;
69
    HashMap visited = new HashMap();
70
    ArrayList nextRound = new ArrayList();
71
    
72
    PdfReaderInstance(PdfReader reader, PdfWriter writer) {
73
        this.reader = reader;
74
        this.writer = writer;
75
        file = reader.getSafeFile();
76
        myXref = new int[reader.getXrefSize()];
77
    }
78
    
79
    PdfReader getReader() {
80
        return reader;
81
    }
82
    
83
    PdfImportedPage getImportedPage(int pageNumber) {
84 1 1. getImportedPage : negated conditional → NO_COVERAGE
        if (!reader.isOpenedWithFullPermissions())
85
            throw new IllegalArgumentException(MessageLocalization.getComposedMessage("pdfreader.not.opened.with.owner.password"));
86 4 1. getImportedPage : changed conditional boundary → NO_COVERAGE
2. getImportedPage : changed conditional boundary → NO_COVERAGE
3. getImportedPage : negated conditional → NO_COVERAGE
4. getImportedPage : negated conditional → NO_COVERAGE
        if (pageNumber < 1 || pageNumber > reader.getNumberOfPages())
87
            throw new IllegalArgumentException(MessageLocalization.getComposedMessage("invalid.page.number.1", pageNumber));
88
        Integer i = pageNumber;
89
        PdfImportedPage pageT = (PdfImportedPage)importedPages.get(i);
90 1 1. getImportedPage : negated conditional → NO_COVERAGE
        if (pageT == null) {
91
            pageT = new PdfImportedPage(this, writer, pageNumber);
92
            importedPages.put(i, pageT);
93
        }
94 1 1. getImportedPage : mutated return of Object value for com/lowagie/text/pdf/PdfReaderInstance::getImportedPage to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return pageT;
95
    }
96
    
97
    int getNewObjectNumber(int number, int generation) {
98 1 1. getNewObjectNumber : negated conditional → NO_COVERAGE
        if (myXref[number] == 0) {
99
            myXref[number] = writer.getIndirectReferenceNumber();
100
            nextRound.add(number);
101
        }
102 1 1. getNewObjectNumber : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
        return myXref[number];
103
    }
104
    
105
    RandomAccessFileOrArray getReaderFile() {
106
        return file;
107
    }
108
    
109
    PdfObject getResources(int pageNumber) {
110
        PdfObject obj = PdfReader.getPdfObjectRelease(reader.getPageNRelease(pageNumber).get(PdfName.RESOURCES));
111 1 1. getResources : mutated return of Object value for com/lowagie/text/pdf/PdfReaderInstance::getResources to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return obj;
112
    }
113
    
114
    /**
115
     * Gets the content stream of a page as a PdfStream object.
116
     * @param    pageNumber            the page of which you want the stream
117
     * @param    compressionLevel    the compression level you want to apply to the stream
118
     * @return    a PdfStream object
119
     * @since    2.1.3 (the method already existed without param compressionLevel)
120
     */
121
    PdfStream getFormXObject(int pageNumber, int compressionLevel) throws IOException {
122
        PdfDictionary page = reader.getPageNRelease(pageNumber);
123
        PdfObject contents = PdfReader.getPdfObjectRelease(page.get(PdfName.CONTENTS));
124
        PdfDictionary dic = new PdfDictionary();
125
        byte[] bout = null;
126 1 1. getFormXObject : negated conditional → NO_COVERAGE
        if (contents != null) {
127 1 1. getFormXObject : negated conditional → NO_COVERAGE
            if (contents.isStream())
128 1 1. getFormXObject : removed call to com/lowagie/text/pdf/PdfDictionary::putAll → NO_COVERAGE
                dic.putAll((PRStream)contents);
129
            else
130
                bout = reader.getPageContent(pageNumber, file);
131
        }
132
        else
133
            bout = new byte[0];
134 1 1. getFormXObject : removed call to com/lowagie/text/pdf/PdfDictionary::put → NO_COVERAGE
        dic.put(PdfName.RESOURCES, PdfReader.getPdfObjectRelease(page.get(PdfName.RESOURCES)));
135 1 1. getFormXObject : removed call to com/lowagie/text/pdf/PdfDictionary::put → NO_COVERAGE
        dic.put(PdfName.TYPE, PdfName.XOBJECT);
136 1 1. getFormXObject : removed call to com/lowagie/text/pdf/PdfDictionary::put → NO_COVERAGE
        dic.put(PdfName.SUBTYPE, PdfName.FORM);
137
        PdfImportedPage impPage = (PdfImportedPage)importedPages.get(pageNumber);
138 1 1. getFormXObject : removed call to com/lowagie/text/pdf/PdfDictionary::put → NO_COVERAGE
        dic.put(PdfName.BBOX, new PdfRectangle(impPage.getBoundingBox()));
139
        PdfArray matrix = impPage.getMatrix();
140 1 1. getFormXObject : negated conditional → NO_COVERAGE
        if (matrix == null)
141 1 1. getFormXObject : removed call to com/lowagie/text/pdf/PdfDictionary::put → NO_COVERAGE
            dic.put(PdfName.MATRIX, IDENTITYMATRIX);
142
        else
143 1 1. getFormXObject : removed call to com/lowagie/text/pdf/PdfDictionary::put → NO_COVERAGE
            dic.put(PdfName.MATRIX, matrix);
144 1 1. getFormXObject : removed call to com/lowagie/text/pdf/PdfDictionary::put → NO_COVERAGE
        dic.put(PdfName.FORMTYPE, ONE);
145
        PRStream stream;
146 1 1. getFormXObject : negated conditional → NO_COVERAGE
        if (bout == null) {
147
            stream = new PRStream((PRStream)contents, dic);
148
        }
149
        else {
150
            stream = new PRStream(reader, bout, compressionLevel);
151 1 1. getFormXObject : removed call to com/lowagie/text/pdf/PRStream::putAll → NO_COVERAGE
            stream.putAll(dic);
152
        }
153 1 1. getFormXObject : mutated return of Object value for com/lowagie/text/pdf/PdfReaderInstance::getFormXObject to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return stream;
154
    }
155
    
156
    void writeAllVisited() throws IOException {
157 1 1. writeAllVisited : negated conditional → NO_COVERAGE
        while (!nextRound.isEmpty()) {
158
            ArrayList vec = nextRound;
159
            nextRound = new ArrayList();
160
            for (Object o : vec) {
161
                Integer i = (Integer) o;
162 1 1. writeAllVisited : negated conditional → NO_COVERAGE
                if (!visited.containsKey(i)) {
163
                    visited.put(i, null);
164
                    int n = i;
165
                    writer.addToBody(reader.getPdfObjectRelease(n), myXref[n]);
166
                }
167
            }
168
        }
169
    }
170
    
171
    void writeAllPages() throws IOException {
172
        try {
173 1 1. writeAllPages : removed call to com/lowagie/text/pdf/RandomAccessFileOrArray::reOpen → NO_COVERAGE
            file.reOpen();
174
            for (Object o : importedPages.values()) {
175
                PdfImportedPage ip = (PdfImportedPage) o;
176
                writer.addToBody(ip.getFormXObject(writer.getCompressionLevel()), ip.getIndirectReference());
177
            }
178 1 1. writeAllPages : removed call to com/lowagie/text/pdf/PdfReaderInstance::writeAllVisited → NO_COVERAGE
            writeAllVisited();
179
        }
180
        finally {
181
            try {
182 1 1. writeAllPages : removed call to com/lowagie/text/pdf/PdfReader::close → NO_COVERAGE
                reader.close();
183 1 1. writeAllPages : removed call to com/lowagie/text/pdf/RandomAccessFileOrArray::close → NO_COVERAGE
                file.close();
184
            }
185
            catch (Exception e) {
186
                //Empty on purpose
187
            }
188
        }
189
    }
190
}

Mutations

84

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

86

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

2.2
Location : getImportedPage
Killed by : none
changed conditional boundary → NO_COVERAGE

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

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

90

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

94

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

98

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

102

1.1
Location : getNewObjectNumber
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

111

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

126

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

127

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

128

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

134

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

135

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

136

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

138

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

140

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

141

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

143

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

144

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

146

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

151

1.1
Location : getFormXObject
Killed by : none
removed call to com/lowagie/text/pdf/PRStream::putAll → NO_COVERAGE

153

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

157

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

162

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

173

1.1
Location : writeAllPages
Killed by : none
removed call to com/lowagie/text/pdf/RandomAccessFileOrArray::reOpen → NO_COVERAGE

178

1.1
Location : writeAllPages
Killed by : none
removed call to com/lowagie/text/pdf/PdfReaderInstance::writeAllVisited → NO_COVERAGE

182

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

183

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

Active mutators

Tests examined


Report generated by PIT 1.4.2