aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/render/pdf/extensions/PDFDictionaryExtension.java
blob: d4b11cdb4dca34252c24c94a0bc12fde0cb0d0df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.render.pdf.extensions;

import java.util.List;
import java.util.Map;

// CSOFF: LineLengthCheck

public class PDFDictionaryExtension extends PDFDictionaryEntryExtension {

    public static final String PROPERTY_PAGE_NUMBERS = "page-numbers";

    private static final long serialVersionUID = -1L;

    private PDFDictionaryType dictionaryType;
    private Map<String, String> properties;
    private List<PDFDictionaryEntryExtension> entries;

    PDFDictionaryExtension() {
    }

    PDFDictionaryExtension(PDFDictionaryType dictionaryType) {
        super(PDFDictionaryEntryType.Dictionary);
        this.dictionaryType = dictionaryType;
        this.properties = new java.util.HashMap<String, String>();
        this.entries = new java.util.ArrayList<PDFDictionaryEntryExtension>();
    }

    public PDFDictionaryType getDictionaryType() {
        return dictionaryType;
    }

    public void setProperty(String name, String value) {
        properties.put(name, value);
    }

    public String getProperty(String name) {
        return properties.get(name);
    }

    public void addEntry(PDFDictionaryEntryExtension entry) {
        entries.add(entry);
    }

    public List<PDFDictionaryEntryExtension> getEntries() {
        return entries;
    }

    public PDFDictionaryEntryExtension getLastEntry() {
        if (entries.size() > 0) {
            return entries.get(entries.size() - 1);
        } else {
            return null;
        }
    }

    /**
     * Determine if page dictionary and page number matches.
     * @param pageNumber page number, where first page number is 1
     * @return true if this dictionary is a page dictionary and specified page number matches specified page-number property
     */
    public boolean matchesPageNumber(int pageNumber) {
        if (dictionaryType != PDFDictionaryType.Page) {
            return false;
        }
        String pageNumbers = getProperty(PROPERTY_PAGE_NUMBERS);
        if ((pageNumbers == null) || (pageNumbers.length() == 0)) {
            return false;
        } else if (pageNumbers.equals("*")) {
            return true;
        } else {
            for (String interval : pageNumbers.split("\\s*,\\s*")) {
                String[] components = interval.split("\\s*-\\s*");
                if (components.length < 1) {
                    continue;
                } else {
                    try {
                        int start = Integer.parseInt(components[0]);
                        int end = 0;
                        if (components.length > 1) {
                            if (!components[1].equals("LAST")) {
                                end = Integer.parseInt(components[1]);
                            }
                        }
                        if ((end == 0) && (pageNumber == start)) {
                            return true;
                        } else if ((end > start) && (pageNumber >= start) && (pageNumber < end)) {
                            return true;
                        } else {
                            continue;
                        }
                    } catch (NumberFormatException e) {
                        continue;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public String getElementName() {
        return dictionaryType.elementName();
    }

}