~ubuntu-branches/ubuntu/raring/libitext-java/raring-proposed

« back to all changes in this revision

Viewing changes to core/com/lowagie/text/pdf/PRAcroForm.java

  • Committer: Bazaar Package Importer
  • Author(s): Adriaan Peeters
  • Date: 2008-11-23 12:26:51 UTC
  • mfrom: (5.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20081123122651-ab7juwjz41q1123k
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: PRAcroForm.java 3373 2008-05-12 16:21:24Z xlv $
 
3
 *
 
4
 * Copyright 2001, 2002 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
 * This class written by Mark Thompson, Copyright (C) 2002 by Mark Thompson.
 
23
 *
 
24
 * Contributor(s): all the names of the contributors are added in the source code
 
25
 * where applicable.
 
26
 *
 
27
 * Alternatively, the contents of this file may be used under the terms of the
 
28
 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
 
29
 * provisions of LGPL are applicable instead of those above.  If you wish to
 
30
 * allow use of your version of this file only under the terms of the LGPL
 
31
 * License and not to allow others to use your version of this file under
 
32
 * the MPL, indicate your decision by deleting the provisions above and
 
33
 * replace them with the notice and other provisions required by the LGPL.
 
34
 * If you do not delete the provisions above, a recipient may use your version
 
35
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
 
36
 *
 
37
 * This library is free software; you can redistribute it and/or modify it
 
38
 * under the terms of the MPL as stated above or under the terms of the GNU
 
39
 * Library General Public License as published by the Free Software Foundation;
 
40
 * either version 2 of the License, or any later version.
 
41
 *
 
42
 * This library is distributed in the hope that it will be useful, but WITHOUT
 
43
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
44
 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
 
45
 * details.
 
46
 *
 
47
 * If you didn't download this code from the following link, you should check if
 
48
 * you aren't using an obsolete version:
 
49
 * http://www.lowagie.com/iText/
 
50
 */
 
51
 
 
52
package com.lowagie.text.pdf;
 
53
 
 
54
import java.util.ArrayList;
 
55
import java.util.HashMap;
 
56
import java.util.Iterator;
 
57
 
 
58
/**
 
59
 * This class captures an AcroForm on input. Basically, it extends Dictionary
 
60
 * by indexing the fields of an AcroForm
 
61
 * @author Mark Thompson
 
62
 */
 
63
 
 
64
public class PRAcroForm extends PdfDictionary {
 
65
    
 
66
    /**
 
67
     * This class holds the information for a single field
 
68
     */
 
69
    public static class FieldInformation {
 
70
        String name;
 
71
        PdfDictionary info;
 
72
        PRIndirectReference ref;
 
73
        
 
74
        FieldInformation(String name, PdfDictionary info, PRIndirectReference ref) {
 
75
            this.name = name; this.info = info; this.ref = ref;
 
76
        }
 
77
        public String getName() { return name; }
 
78
        public PdfDictionary getInfo() { return info; }
 
79
        public PRIndirectReference getRef() { return ref; }
 
80
    };
 
81
    ArrayList fields;
 
82
    ArrayList stack;
 
83
    HashMap fieldByName;
 
84
    PdfReader reader;
 
85
    
 
86
    /**
 
87
     * Constructor
 
88
     * @param reader reader of the input file
 
89
     */
 
90
    public PRAcroForm(PdfReader reader) {
 
91
        this.reader = reader;
 
92
        fields = new ArrayList();
 
93
        fieldByName = new HashMap();
 
94
        stack = new ArrayList();
 
95
    }
 
96
    /**
 
97
     * Number of fields found
 
98
     * @return size
 
99
     */
 
100
    public int size() {
 
101
        return fields.size();
 
102
    }
 
103
    
 
104
    public ArrayList getFields() {
 
105
        return fields;
 
106
    }
 
107
    
 
108
    public FieldInformation getField(String name) {
 
109
        return (FieldInformation)fieldByName.get(name);
 
110
    }
 
111
    
 
112
    /**
 
113
     * Given the title (/T) of a reference, return the associated reference
 
114
     * @param name a string containing the path
 
115
     * @return a reference to the field, or null
 
116
     */
 
117
    public PRIndirectReference getRefByName(String name) {
 
118
        FieldInformation fi = (FieldInformation)fieldByName.get(name);
 
119
        if (fi == null) return null;
 
120
        return fi.getRef();
 
121
    }
 
122
    /**
 
123
     * Read, and comprehend the acroform
 
124
     * @param root the document root
 
125
     */
 
126
    public void readAcroForm(PdfDictionary root) {
 
127
        if (root == null)
 
128
            return;
 
129
        hashMap = root.hashMap;
 
130
        pushAttrib(root);
 
131
        PdfArray fieldlist = (PdfArray)PdfReader.getPdfObjectRelease(root.get(PdfName.FIELDS));
 
132
        iterateFields(fieldlist, null, null);
 
133
    }
 
134
    
 
135
    /**
 
136
     * After reading, we index all of the fields. Recursive.
 
137
     * @param fieldlist An array of fields
 
138
     * @param fieldDict the last field dictionary we encountered (recursively)
 
139
     * @param title the pathname of the field, up to this point or null
 
140
     */
 
141
    protected void iterateFields(PdfArray fieldlist, PRIndirectReference fieldDict, String title) {
 
142
        for (Iterator it = fieldlist.getArrayList().iterator(); it.hasNext();) {
 
143
            PRIndirectReference ref = (PRIndirectReference)it.next();
 
144
            PdfDictionary dict = (PdfDictionary) PdfReader.getPdfObjectRelease(ref);
 
145
            
 
146
            // if we are not a field dictionary, pass our parent's values
 
147
            PRIndirectReference myFieldDict = fieldDict;
 
148
            String myTitle = title;
 
149
            PdfString tField = (PdfString)dict.get(PdfName.T);
 
150
            boolean isFieldDict = tField != null;
 
151
            
 
152
            if (isFieldDict) {
 
153
                myFieldDict = ref;
 
154
                if (title == null) myTitle = tField.toString();
 
155
                else myTitle = title + '.' + tField.toString();
 
156
            }
 
157
            
 
158
            PdfArray kids = (PdfArray)dict.get(PdfName.KIDS);
 
159
            if (kids != null) {
 
160
                pushAttrib(dict);
 
161
                iterateFields(kids, myFieldDict, myTitle);
 
162
                stack.remove(stack.size() - 1);   // pop
 
163
            }
 
164
            else {          // leaf node
 
165
                if (myFieldDict != null) {
 
166
                    PdfDictionary mergedDict = (PdfDictionary)stack.get(stack.size() - 1);
 
167
                    if (isFieldDict)
 
168
                        mergedDict = mergeAttrib(mergedDict, dict);
 
169
                    
 
170
                    mergedDict.put(PdfName.T, new PdfString(myTitle));
 
171
                    FieldInformation fi = new FieldInformation(myTitle, mergedDict, myFieldDict);
 
172
                    fields.add(fi);
 
173
                    fieldByName.put(myTitle, fi);
 
174
                }
 
175
            }
 
176
        }
 
177
    }
 
178
    /**
 
179
     * merge field attributes from two dictionaries
 
180
     * @param parent one dictionary
 
181
     * @param child the other dictionary
 
182
     * @return a merged dictionary
 
183
     */
 
184
    protected PdfDictionary mergeAttrib(PdfDictionary parent, PdfDictionary child) {
 
185
        PdfDictionary targ = new PdfDictionary();
 
186
        if (parent != null) targ.putAll(parent);
 
187
        
 
188
        for (Iterator it = child.getKeys().iterator(); it.hasNext();) {
 
189
            PdfName key = (PdfName) it.next();
 
190
            if (key.equals(PdfName.DR) || key.equals(PdfName.DA) ||
 
191
            key.equals(PdfName.Q)  || key.equals(PdfName.FF) ||
 
192
            key.equals(PdfName.DV) || key.equals(PdfName.V)
 
193
            || key.equals(PdfName.FT)
 
194
            || key.equals(PdfName.F)) {
 
195
                targ.put(key,child.get(key));
 
196
            }
 
197
        }
 
198
        return targ;
 
199
    }
 
200
    /**
 
201
     * stack a level of dictionary. Merge in a dictionary from this level
 
202
     */
 
203
    protected void pushAttrib(PdfDictionary dict) {
 
204
        PdfDictionary dic = null;
 
205
        if (!stack.isEmpty()) {
 
206
            dic = (PdfDictionary)stack.get(stack.size() - 1);
 
207
        }
 
208
        dic = mergeAttrib(dic, dict);
 
209
        stack.add(dic);
 
210
    }
 
211
}