IndexEvents.java

1
/*
2
 * Copyright 2005 by Michael Niedermair.
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-2005 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-2005 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.events;
48
49
import java.util.ArrayList;
50
import java.util.Comparator;
51
import java.util.HashMap;
52
import java.util.List;
53
import java.util.Map;
54
import java.util.TreeMap;
55
56
import com.lowagie.text.Chunk;
57
import com.lowagie.text.Document;
58
import com.lowagie.text.Rectangle;
59
import com.lowagie.text.pdf.PdfPageEventHelper;
60
import com.lowagie.text.pdf.PdfWriter;
61
62
/**
63
 * Class for an index.
64
 * 
65
 * @author Michael Niedermair
66
 */
67
public class IndexEvents extends PdfPageEventHelper {
68
69
    /**
70
     * keeps the indextag with the pagenumber
71
     */
72
    private Map indextag = new TreeMap();
73
74
    /**
75
     * All the text that is passed to this event, gets registered in the indexentry.
76
     * 
77
     * @see com.lowagie.text.pdf.PdfPageEventHelper#onGenericTag(
78
     *      com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document,
79
     *      com.lowagie.text.Rectangle, java.lang.String)
80
     */
81
    public void onGenericTag(PdfWriter writer, Document document,
82
            Rectangle rect, String text) {
83
        indextag.put(text, writer.getPageNumber());
84
    }
85
86
    // --------------------------------------------------------------------
87
    /**
88
     * indexcounter
89
     */
90
    private long indexcounter = 0;
91
92
    /**
93
     * the list for the index entry
94
     */
95
    private List indexentry = new ArrayList();
96
97
    /**
98
     * Create an index entry.
99
     *
100
     * @param text  The text for the Chunk.
101
     * @param in1   The first level.
102
     * @param in2   The second level.
103
     * @param in3   The third level.
104
     * @return Returns the Chunk.
105
     */
106
    public Chunk create(final String text, final String in1, final String in2,
107
            final String in3) {
108
109
        Chunk chunk = new Chunk(text);
110 1 1. create : Replaced long addition with subtraction → NO_COVERAGE
        String tag = "idx_" + (indexcounter++);
111
        chunk.setGenericTag(tag);
112
        chunk.setLocalDestination(tag);
113
        Entry entry = new Entry(in1, in2, in3, tag);
114
        indexentry.add(entry);
115 1 1. create : mutated return of Object value for com/lowagie/text/pdf/events/IndexEvents::create to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return chunk;
116
    }
117
118
    /**
119
     * Create an index entry.
120
     *
121
     * @param text  The text for the Chunk.
122
     * @param in1   The first level.
123
     * @return Returns the Chunk.
124
     */
125
    public Chunk create(final String text, final String in1) {
126 1 1. create : mutated return of Object value for com/lowagie/text/pdf/events/IndexEvents::create to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return create(text, in1, "", "");
127
    }
128
129
    /**
130
     * Create an index entry.
131
     *
132
     * @param text  The text for the Chunk.
133
     * @param in1   The first level.
134
     * @param in2   The second level.
135
     * @return Returns the Chunk.
136
     */
137
    public Chunk create(final String text, final String in1, final String in2) {
138 1 1. create : mutated return of Object value for com/lowagie/text/pdf/events/IndexEvents::create to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return create(text, in1, in2, "");
139
    }
140
141
    /**
142
     * Create an index entry.
143
     *
144
     * @param text  The text.
145
     * @param in1   The first level.
146
     * @param in2   The second level.
147
     * @param in3   The third level.
148
     */
149
    public void create(final Chunk text, final String in1, final String in2,
150
            final String in3) {
151
152 1 1. create : Replaced long addition with subtraction → NO_COVERAGE
        String tag = "idx_" + (indexcounter++);
153
        text.setGenericTag(tag);
154
        text.setLocalDestination(tag);
155
        Entry entry = new Entry(in1, in2, in3, tag);
156
        indexentry.add(entry);
157
    }
158
159
    /**
160
     * Create an index entry.
161
     *
162
     * @param text  The text.
163
     * @param in1   The first level.
164
     */
165
    public void create(final Chunk text, final String in1) {
166 1 1. create : removed call to com/lowagie/text/pdf/events/IndexEvents::create → NO_COVERAGE
        create(text, in1, "", "");
167
    }
168
169
    /**
170
     * Create an index entry.
171
     *
172
     * @param text  The text.
173
     * @param in1   The first level.
174
     * @param in2   The second level.
175
     */
176
    public void create(final Chunk text, final String in1, final String in2) {
177 1 1. create : removed call to com/lowagie/text/pdf/events/IndexEvents::create → NO_COVERAGE
        create(text, in1, in2, "");
178
    }
179
180
    /**
181
     * Comparator for sorting the index
182
     */
183
    private Comparator comparator = (arg0, arg1) -> {
184
        Entry en1 = (Entry) arg0;
185
        Entry en2 = (Entry) arg1;
186
187
        int rt = 0;
188
        if (en1.getIn1() != null && en2.getIn1() != null) {
189
            if ((rt = en1.getIn1().compareToIgnoreCase(en2.getIn1())) == 0) {
190
                // in1 equals
191
                if (en1.getIn2() != null && en2.getIn2() != null) {
192
                    if ((rt = en1.getIn2()
193
                            .compareToIgnoreCase(en2.getIn2())) == 0) {
194
                        // in2 equals
195
                        if (en1.getIn3() != null && en2.getIn3() != null) {
196
                            rt = en1.getIn3().compareToIgnoreCase(
197
                                    en2.getIn3());
198
                        }
199
                    }
200
                }
201
            }
202
        }
203
        return rt;
204
    };
205
206
    /**
207
     * Set the comparator.
208
     * @param aComparator The comparator to set.
209
     */
210
    public void setComparator(Comparator aComparator) {
211
        comparator = aComparator;
212
    }
213
214
    /**
215
     * Returns the sorted list with the entries and the collected page numbers.
216
     * @return Returns the sorted list with the entries and the collected page numbers.
217
     */
218
    public List getSortedEntries() {
219
220
        Map grouped = new HashMap();
221
222
        for (Object o : indexentry) {
223
            Entry e = (Entry) o;
224
            String key = e.getKey();
225
226
            Entry master = (Entry) grouped.get(key);
227 1 1. getSortedEntries : negated conditional → NO_COVERAGE
            if (master != null) {
228 1 1. getSortedEntries : removed call to com/lowagie/text/pdf/events/IndexEvents$Entry::addPageNumberAndTag → NO_COVERAGE
                master.addPageNumberAndTag(e.getPageNumber(), e.getTag());
229
            } else {
230 1 1. getSortedEntries : removed call to com/lowagie/text/pdf/events/IndexEvents$Entry::addPageNumberAndTag → NO_COVERAGE
                e.addPageNumberAndTag(e.getPageNumber(), e.getTag());
231
                grouped.put(key, e);
232
            }
233
        }
234
235
        // copy to a list and sort it
236
        List sorted = new ArrayList(grouped.values());
237 1 1. getSortedEntries : removed call to java/util/List::sort → NO_COVERAGE
        sorted.sort(comparator);
238 1 1. getSortedEntries : mutated return of Object value for com/lowagie/text/pdf/events/IndexEvents::getSortedEntries to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return sorted;
239
    }
240
241
    // --------------------------------------------------------------------
242
    /**
243
     * Class for an index entry.
244
     * <p>
245
     * In the first step, only in1, in2,in3 and tag are used.
246
     * After the collections of the index entries, pagenumbers are used.
247
     * </p>
248
     */
249
    public class Entry {
250
251
        /**
252
         * first level
253
         */
254
        private String in1;
255
256
        /**
257
         * second level
258
         */
259
        private String in2;
260
261
        /**
262
         * third level
263
         */
264
        private String in3;
265
266
        /**
267
         * the tag
268
         */
269
        private String tag;
270
271
        /**
272
         * the list of all page numbers.
273
         */
274
        private List pagenumbers = new ArrayList();
275
276
        /**
277
         * the list of all tags.
278
         */
279
        private List tags = new ArrayList();
280
281
        /**
282
         * Create a new object.
283
         * @param aIn1   The first level.
284
         * @param aIn2   The second level.
285
         * @param aIn3   The third level.
286
         * @param aTag   The tag.
287
         */
288
        public Entry(final String aIn1, final String aIn2, final String aIn3,
289
                final String aTag) {
290
            in1 = aIn1;
291
            in2 = aIn2;
292
            in3 = aIn3;
293
            tag = aTag;
294
        }
295
296
        /**
297
         * Returns the in1.
298
         * @return Returns the in1.
299
         */
300
        public String getIn1() {
301
            return in1;
302
        }
303
304
        /**
305
         * Returns the in2.
306
         * @return Returns the in2.
307
         */
308
        public String getIn2() {
309
            return in2;
310
        }
311
312
        /**
313
         * Returns the in3.
314
         * @return Returns the in3.
315
         */
316
        public String getIn3() {
317
            return in3;
318
        }
319
320
        /**
321
         * Returns the tag.
322
         * @return Returns the tag.
323
         */
324
        public String getTag() {
325
            return tag;
326
        }
327
328
        /**
329
         * Returns the pagenumber for this entry.
330
         * @return Returns the pagenumber for this entry.
331
         */
332
        public int getPageNumber() {
333
            int rt = -1;
334
            Integer i = (Integer) indextag.get(tag);
335 1 1. getPageNumber : negated conditional → NO_COVERAGE
            if (i != null) {
336
                rt = i;
337
            }
338 1 1. getPageNumber : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
            return rt;
339
        }
340
341
        /**
342
         * Add a pagenumber.
343
         * @param number    The page number.
344
         * @param tag
345
         */
346
        public void addPageNumberAndTag(final int number, final String tag) {
347
            pagenumbers.add(number);
348
            tags.add(tag);
349
        }
350
351
        /**
352
         * Returns the key for the map-entry.
353
         * @return Returns the key for the map-entry.
354
         */
355
        public String getKey() {
356 1 1. getKey : mutated return of Object value for com/lowagie/text/pdf/events/IndexEvents$Entry::getKey to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
            return in1 + "!" + in2 + "!" + in3;
357
        }
358
359
        /**
360
         * Returns the pagenumbers.
361
         * @return Returns the pagenumbers.
362
         */
363
        public List getPagenumbers() {
364
            return pagenumbers;
365
        }
366
367
        /**
368
         * Returns the tags.
369
         * @return Returns the tags.
370
         */
371
        public List getTags() {
372
            return tags;
373
        }
374
375
        /**
376
         * print the entry (only for test)
377
         * @return the toString implementation of the entry
378
         */
379
        public String toString() {
380
            StringBuilder buf = new StringBuilder();
381
            buf.append(in1).append(' ');
382
            buf.append(in2).append(' ');
383
            buf.append(in3).append(' ');
384
            for (Object pagenumber : pagenumbers) {
385
                buf.append(pagenumber).append(' ');
386
            }
387
            return buf.toString();
388
        }
389
    }
390
}

Mutations

110

1.1
Location : create
Killed by : none
Replaced long addition with subtraction → NO_COVERAGE

115

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

126

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

138

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

152

1.1
Location : create
Killed by : none
Replaced long addition with subtraction → NO_COVERAGE

166

1.1
Location : create
Killed by : none
removed call to com/lowagie/text/pdf/events/IndexEvents::create → NO_COVERAGE

177

1.1
Location : create
Killed by : none
removed call to com/lowagie/text/pdf/events/IndexEvents::create → NO_COVERAGE

227

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

228

1.1
Location : getSortedEntries
Killed by : none
removed call to com/lowagie/text/pdf/events/IndexEvents$Entry::addPageNumberAndTag → NO_COVERAGE

230

1.1
Location : getSortedEntries
Killed by : none
removed call to com/lowagie/text/pdf/events/IndexEvents$Entry::addPageNumberAndTag → NO_COVERAGE

237

1.1
Location : getSortedEntries
Killed by : none
removed call to java/util/List::sort → NO_COVERAGE

238

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

335

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

338

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

356

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

Active mutators

Tests examined


Report generated by PIT 1.4.2