~ubuntu-branches/ubuntu/feisty/libitext-java/feisty

« back to all changes in this revision

Viewing changes to com/lowagie/text/pdf/FdfReader.java

  • Committer: Bazaar Package Importer
  • Author(s): Gerardo Curiel
  • Date: 2006-09-21 00:08:53 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060921000853-le9rrpcnw9fc57sm
Tags: 1.4.5-1
* New upstream release
* debian/rules modified due to a new build.xml file
* Patched Pdfgraphics2d.java to remove privative dependencie on com.sun.image.codec.jpeg.*
  (more information on
  http://developer.classpath.org/mediation/ClasspathMigration#head-d4ee9efe53a641e29ffdcd96e985bf38bbc671c1 )
* ant/.ant.properties paths fixed
* Build package with the packages provided by java-gcj-compat-dev
* Removed unused README.Debian
* Removed unused debian/patches/01libitext-java-addbuildscript.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2003 by Paulo Soares.
 
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, 2000, 2001, 2002 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, 2001, 2002 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;
 
48
import java.io.IOException;
 
49
import java.io.InputStream;
 
50
import java.util.HashMap;
 
51
import java.util.ArrayList;
 
52
import java.net.URL;
 
53
/** Reads an FDF form and makes the fields available
 
54
 * @author Paulo Soares (psoares@consiste.pt)
 
55
 */
 
56
public class FdfReader extends PdfReader {
 
57
    
 
58
    HashMap fields;
 
59
    String fileSpec;
 
60
    PdfName encoding;
 
61
    
 
62
    /** Reads an FDF form.
 
63
     * @param filename the file name of the form
 
64
     * @throws IOException on error
 
65
     */    
 
66
    public FdfReader(String filename) throws IOException {
 
67
        super(filename);
 
68
    }
 
69
    
 
70
    /** Reads an FDF form.
 
71
     * @param pdfIn the byte array with the form
 
72
     * @throws IOException on error
 
73
     */    
 
74
    public FdfReader(byte pdfIn[]) throws IOException {
 
75
        super(pdfIn);
 
76
    }
 
77
    
 
78
    /** Reads an FDF form.
 
79
     * @param url the URL of the document
 
80
     * @throws IOException on error
 
81
     */    
 
82
    public FdfReader(URL url) throws IOException {
 
83
        super(url);
 
84
    }
 
85
    
 
86
    /** Reads an FDF form.
 
87
     * @param is the <CODE>InputStream</CODE> containing the document. The stream is read to the
 
88
     * end but is not closed
 
89
     * @throws IOException on error
 
90
     */    
 
91
    public FdfReader(InputStream is) throws IOException {
 
92
        super(is);
 
93
    }
 
94
    
 
95
    protected void readPdf() throws IOException {
 
96
        fields = new HashMap();
 
97
        try {
 
98
            tokens.checkFdfHeader();
 
99
            rebuildXref();
 
100
            readDocObj();
 
101
        }
 
102
        finally {
 
103
            try {
 
104
                tokens.close();
 
105
            }
 
106
            catch (Exception e) {
 
107
                // empty on purpose
 
108
            }
 
109
        }
 
110
        readFields();
 
111
    }
 
112
    
 
113
    protected void kidNode(PdfDictionary merged, String name) {
 
114
        PdfArray kids = (PdfArray)getPdfObject(merged.get(PdfName.KIDS));
 
115
        if (kids == null || kids.getArrayList().size() == 0) {
 
116
            if (name.length() > 0)
 
117
                name = name.substring(1);
 
118
            fields.put(name, merged);
 
119
        }
 
120
        else {
 
121
            merged.remove(PdfName.KIDS);
 
122
            ArrayList ar = kids.getArrayList();
 
123
            for (int k = 0; k < ar.size(); ++k) {
 
124
                PdfDictionary dic = new PdfDictionary();
 
125
                dic.merge(merged);
 
126
                PdfDictionary newDic = (PdfDictionary)getPdfObject((PdfObject)ar.get(k));
 
127
                PdfString t = (PdfString)getPdfObject(newDic.get(PdfName.T));
 
128
                String newName = name;
 
129
                if (t != null)
 
130
                    newName += "." + t.toUnicodeString();
 
131
                dic.merge(newDic);
 
132
                dic.remove(PdfName.T);
 
133
                kidNode(dic, newName);
 
134
            }
 
135
        }
 
136
    }
 
137
    
 
138
    protected void readFields() throws IOException {
 
139
        catalog = (PdfDictionary)getPdfObject(trailer.get(PdfName.ROOT));
 
140
        PdfDictionary fdf = (PdfDictionary)getPdfObject(catalog.get(PdfName.FDF));
 
141
        PdfString fs = (PdfString)getPdfObject(fdf.get(PdfName.F));
 
142
        if (fs != null)
 
143
            fileSpec = fs.toUnicodeString();
 
144
        PdfArray fld = (PdfArray)getPdfObject(fdf.get(PdfName.FIELDS));
 
145
        if (fld == null)
 
146
            return;
 
147
        encoding = (PdfName)getPdfObject(fdf.get(PdfName.ENCODING));
 
148
        PdfDictionary merged = new PdfDictionary();
 
149
        merged.put(PdfName.KIDS, fld);
 
150
        kidNode(merged, "");
 
151
    }
 
152
 
 
153
    /** Gets all the fields. The map is keyed by the fully qualified
 
154
     * field name and the value is a merged <CODE>PdfDictionary</CODE>
 
155
     * with the field content.
 
156
     * @return all the fields
 
157
     */    
 
158
    public HashMap getFields() {
 
159
        return fields;
 
160
    }
 
161
    
 
162
    /** Gets the field dictionary.
 
163
     * @param name the fully qualified field name
 
164
     * @return the field dictionary
 
165
     */    
 
166
    public PdfDictionary getField(String name) {
 
167
        return (PdfDictionary)fields.get(name);
 
168
    }
 
169
    
 
170
    /** Gets the field value or <CODE>null</CODE> if the field does not
 
171
     * exist or has no value defined.
 
172
     * @param name the fully qualified field name
 
173
     * @return the field value or <CODE>null</CODE>
 
174
     */    
 
175
    public String getFieldValue(String name) {
 
176
        PdfDictionary field = (PdfDictionary)fields.get(name);
 
177
        if (field == null)
 
178
            return null;
 
179
        PdfObject v = getPdfObject(field.get(PdfName.V));
 
180
        if (v == null)
 
181
            return null;
 
182
        if (v.isName())
 
183
            return PdfName.decodeName(((PdfName)v).toString());
 
184
        else if (v.isString()) {
 
185
            PdfString vs = (PdfString)v;
 
186
            if (encoding == null || vs.getEncoding() != null)
 
187
                return vs.toUnicodeString();
 
188
            byte b[] = vs.getBytes();
 
189
            if (b.length >= 2 && b[0] == (byte)254 && b[1] == (byte)255)
 
190
                return vs.toUnicodeString();
 
191
            try {
 
192
                if (encoding.equals(PdfName.SHIFT_JIS))
 
193
                    return new String(b, "SJIS");
 
194
                else if (encoding.equals(PdfName.UHC))
 
195
                    return new String(b, "MS949");
 
196
                else if (encoding.equals(PdfName.GBK))
 
197
                    return new String(b, "GBK");
 
198
                else if (encoding.equals(PdfName.BIGFIVE))
 
199
                    return new String(b, "Big5");
 
200
            }
 
201
            catch (Exception e) {
 
202
            }
 
203
            return vs.toUnicodeString();
 
204
        }
 
205
        return null;
 
206
    }
 
207
    
 
208
    /** Gets the PDF file specification contained in the FDF.
 
209
     * @return the PDF file specification contained in the FDF
 
210
     */    
 
211
    public String getFileSpec() {
 
212
        return fileSpec;
 
213
    }
 
214
}
 
 
b'\\ No newline at end of file'