PdfContentParser.java

1
/*
2
 * $Id: PdfContentParser.java 4066 2009-09-19 12:44:47Z psoares33 $
3
 *
4
 * Copyright 2005 by 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
52
import java.io.IOException;
53
import java.util.ArrayList;
54
import java.util.List;
55
56
import com.lowagie.text.error_messages.MessageLocalization;
57
/**
58
 * Parses the page or template content.
59
 * @author Paulo Soares (psoares@consiste.pt)
60
 */
61
public class PdfContentParser {
62
    
63
    /**
64
     * Commands have this type.
65
     */    
66
    public static final int COMMAND_TYPE = 200;
67
    /**
68
     * Holds value of property tokeniser.
69
     */
70
    private PRTokeniser tokeniser;    
71
    
72
    /**
73
     * Creates a new instance of PdfContentParser
74
     * @param tokeniser the tokeniser with the content
75
     */
76
    public PdfContentParser(PRTokeniser tokeniser) {
77
        this.tokeniser = tokeniser;
78
    }
79
    
80
    /**
81
     * Parses a single command from the content. Each command is output as an array of arguments
82
     * having the command itself as the last element. The returned array will be empty if the
83
     * end of content was reached.
84
     * @param ls an <CODE>ArrayList</CODE> to use. It will be cleared before using. If it's
85
     * <CODE>null</CODE> will create a new <CODE>ArrayList</CODE>
86
     * @return the same <CODE>ArrayList</CODE> given as argument or a new one
87
     * @throws IOException on error
88
     */    
89
    public List<PdfObject> parse(List<PdfObject> ls) throws IOException {
90 1 1. parse : negated conditional → NO_COVERAGE
        if (ls == null) {
91
            ls = new ArrayList<>();
92
        } else {
93 1 1. parse : removed call to java/util/List::clear → NO_COVERAGE
            ls.clear();
94
        }
95
        PdfObject ob;
96 1 1. parse : negated conditional → NO_COVERAGE
        while ((ob = readPRObject()) != null) {
97
            ls.add(ob);
98 1 1. parse : negated conditional → NO_COVERAGE
            if (ob.type() == COMMAND_TYPE) {
99
                break;
100
            }
101
        }
102 1 1. parse : mutated return of Object value for com/lowagie/text/pdf/PdfContentParser::parse to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return ls;
103
    }
104
    
105
    /**
106
     * Gets the tokeniser.
107
     * @return the tokeniser.
108
     */
109
    public PRTokeniser getTokeniser() {
110
        return this.tokeniser;
111
    }
112
    
113
    /**
114
     * Sets the tokeniser.
115
     * @param tokeniser the tokeniser
116
     */
117
    public void setTokeniser(PRTokeniser tokeniser) {
118
        this.tokeniser = tokeniser;
119
    }
120
    
121
    /**
122
     * Reads a dictionary. The tokeniser must be positioned past the "&lt;&lt;" token.
123
     * @return the dictionary
124
     * @throws IOException on error
125
     */    
126
    public PdfDictionary readDictionary() throws IOException {
127
        PdfDictionary dic = new PdfDictionary();
128
        while (true) {
129 1 1. readDictionary : negated conditional → NO_COVERAGE
            if (!nextValidToken())
130
                throw new IOException(MessageLocalization.getComposedMessage("unexpected.end.of.file"));
131 1 1. readDictionary : negated conditional → NO_COVERAGE
                if (tokeniser.getTokenType() == PRTokeniser.TK_END_DIC)
132
                    break;
133 1 1. readDictionary : negated conditional → NO_COVERAGE
                if (tokeniser.getTokenType() != PRTokeniser.TK_NAME)
134
                    throw new IOException(MessageLocalization.getComposedMessage("dictionary.key.is.not.a.name"));
135
                PdfName name = new PdfName(tokeniser.getStringValue(), false);
136
                PdfObject obj = readPRObject();
137
                int type = obj.type();
138 2 1. readDictionary : removed negation → NO_COVERAGE
2. readDictionary : negated conditional → NO_COVERAGE
                if (-type == PRTokeniser.TK_END_DIC)
139
                    throw new IOException(MessageLocalization.getComposedMessage("unexpected.gt.gt"));
140 2 1. readDictionary : removed negation → NO_COVERAGE
2. readDictionary : negated conditional → NO_COVERAGE
                if (-type == PRTokeniser.TK_END_ARRAY)
141
                    throw new IOException(MessageLocalization.getComposedMessage("unexpected.close.bracket"));
142 1 1. readDictionary : removed call to com/lowagie/text/pdf/PdfDictionary::put → NO_COVERAGE
                dic.put(name, obj);
143
        }
144 1 1. readDictionary : mutated return of Object value for com/lowagie/text/pdf/PdfContentParser::readDictionary to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return dic;
145
    }
146
    
147
    /**
148
     * Reads an array. The tokeniser must be positioned past the "[" token.
149
     * @return an array
150
     * @throws IOException on error
151
     */    
152
    public PdfArray readArray() throws IOException {
153
        PdfArray array = new PdfArray();
154
        while (true) {
155
            PdfObject obj = readPRObject();
156
            int type = obj.type();
157 2 1. readArray : removed negation → NO_COVERAGE
2. readArray : negated conditional → NO_COVERAGE
            if (-type == PRTokeniser.TK_END_ARRAY)
158
                break;
159 2 1. readArray : removed negation → NO_COVERAGE
2. readArray : negated conditional → NO_COVERAGE
            if (-type == PRTokeniser.TK_END_DIC)
160
                throw new IOException(MessageLocalization.getComposedMessage("unexpected.gt.gt"));
161
            array.add(obj);
162
        }
163 1 1. readArray : mutated return of Object value for com/lowagie/text/pdf/PdfContentParser::readArray to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return array;
164
    }
165
    
166
    /**
167
     * Reads a pdf object.
168
     * @return the pdf object
169
     * @throws IOException on error
170
     */    
171
    public PdfObject readPRObject() throws IOException {
172 1 1. readPRObject : negated conditional → NO_COVERAGE
        if (!nextValidToken())
173 1 1. readPRObject : mutated return of Object value for com/lowagie/text/pdf/PdfContentParser::readPRObject to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
            return null;
174
        int type = tokeniser.getTokenType();
175
        switch (type) {
176
            case PRTokeniser.TK_START_DIC: {
177
                PdfDictionary dic = readDictionary();
178 1 1. readPRObject : mutated return of Object value for com/lowagie/text/pdf/PdfContentParser::readPRObject to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return dic;
179
            }
180
            case PRTokeniser.TK_START_ARRAY:
181 1 1. readPRObject : mutated return of Object value for com/lowagie/text/pdf/PdfContentParser::readPRObject to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return readArray();
182
            case PRTokeniser.TK_STRING:
183
                PdfString str = new PdfString(tokeniser.getStringValue(), null).setHexWriting(tokeniser.isHexString());
184 1 1. readPRObject : mutated return of Object value for com/lowagie/text/pdf/PdfContentParser::readPRObject to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return str;
185
            case PRTokeniser.TK_NAME:
186 1 1. readPRObject : mutated return of Object value for com/lowagie/text/pdf/PdfContentParser::readPRObject to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return new PdfName(tokeniser.getStringValue(), false);
187
            case PRTokeniser.TK_NUMBER:
188 1 1. readPRObject : mutated return of Object value for com/lowagie/text/pdf/PdfContentParser::readPRObject to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return new PdfNumber(tokeniser.getStringValue());
189
            case PRTokeniser.TK_OTHER:
190 1 1. readPRObject : mutated return of Object value for com/lowagie/text/pdf/PdfContentParser::readPRObject to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return new PdfLiteral(COMMAND_TYPE, tokeniser.getStringValue());
191
            default:
192 2 1. readPRObject : removed negation → NO_COVERAGE
2. readPRObject : mutated return of Object value for com/lowagie/text/pdf/PdfContentParser::readPRObject to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return new PdfLiteral(-type, tokeniser.getStringValue());
193
        }
194
    }
195
    
196
    /**
197
     * Reads the next token skipping over the comments.
198
     * @return <CODE>true</CODE> if a token was read, <CODE>false</CODE> if the end of content was reached
199
     * @throws IOException on error
200
     */    
201
    public boolean nextValidToken() throws IOException {
202 1 1. nextValidToken : negated conditional → NO_COVERAGE
        while (tokeniser.nextToken()) {
203 1 1. nextValidToken : negated conditional → NO_COVERAGE
            if (tokeniser.getTokenType() == PRTokeniser.TK_COMMENT)
204
                continue;
205 1 1. nextValidToken : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
            return true;
206
        }
207 1 1. nextValidToken : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
        return false;
208
    }
209
}

Mutations

90

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

93

1.1
Location : parse
Killed by : none
removed call to java/util/List::clear → NO_COVERAGE

96

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

98

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

102

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

129

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

131

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

133

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

138

1.1
Location : readDictionary
Killed by : none
removed negation → NO_COVERAGE

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

140

1.1
Location : readDictionary
Killed by : none
removed negation → NO_COVERAGE

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

142

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

144

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

157

1.1
Location : readArray
Killed by : none
removed negation → NO_COVERAGE

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

159

1.1
Location : readArray
Killed by : none
removed negation → NO_COVERAGE

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

163

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

172

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

173

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

178

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

181

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

184

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

186

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

188

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

190

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

192

1.1
Location : readPRObject
Killed by : none
removed negation → NO_COVERAGE

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

202

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

203

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

205

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

207

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

Active mutators

Tests examined


Report generated by PIT 1.4.2