KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > interactive > form > PDFieldFactory


1 /**
2  * Copyright (c) 2003-2004, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.interactive.form;
32
33 import org.pdfbox.cos.COSArray;
34 import org.pdfbox.cos.COSDictionary;
35 import org.pdfbox.cos.COSName;
36 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
37
38 import java.io.IOException JavaDoc;
39
40 import java.util.List JavaDoc;
41
42 /**
43  * This is the Factory for creating and returning the correct
44  * field elements.
45  *
46  * @author sug
47  * @version $Revision: 1.8 $
48  */

49 public class PDFieldFactory
50 {
51     private static final int RADIO_BITMASK = 32768;
52     private static final int PUSHBUTTON_BITMASK = 65536;
53     private static final int RADIOS_IN_UNISON_BITMASK = 33554432;
54
55     private static final String JavaDoc FIELD_TYPE_BTN = "Btn";
56     private static final String JavaDoc FIELD_TYPE_TX = "Tx";
57     private static final String JavaDoc FIELD_TYPE_CH = "Ch";
58     private static final String JavaDoc FIELD_TYPE_SIG = "Sig";
59
60     /**
61      * Utility class so no constructor.
62      */

63     private PDFieldFactory()
64     {
65         //do nothing.
66
}
67
68     /**
69      * This method creates a COSField subclass from the given field.
70      * The field is a PDF Dictionary object that must represent a
71      * field element. - othewise null is returned
72      *
73      * @param acroForm The form that the field will be part of.
74      * @param field The dictionary representing a field element
75      *
76      * @return a subclass to COSField according to the kind of field passed to createField
77      * @throws IOException If there is an error determining the field type.
78      */

79     public static PDField createField( PDAcroForm acroForm, COSDictionary field) throws IOException JavaDoc
80     {
81         PDField pdField = new PDUnknownField( acroForm, field );
82         if( isButton(pdField) )
83         {
84             int flags = pdField.getFieldFlags();
85             //BJL, I have found that the radio flag bit is not always set
86
//and that sometimes there is just a kids dictionary.
87
//so, if there is a kids dictionary then it must be a radio button
88
//group.
89
COSArray kids = (COSArray)field.getDictionaryObject( COSName.getPDFName( "Kids" ) );
90             if( kids != null || isRadio(flags) )
91             {
92                 pdField = new PDRadioCollection( acroForm, field );
93             }
94             else if( isPushButton( flags ) )
95             {
96                 pdField = new PDPushButton( acroForm, field );
97             }
98             else
99             {
100                 pdField = new PDCheckbox( acroForm, field );
101             }
102
103         }
104         else if (isChoiceField(pdField))
105         {
106             pdField = new PDChoiceField( acroForm, field );
107         }
108         else if (isTextbox(pdField))
109         {
110             pdField = new PDTextbox( acroForm, field );
111         }
112         else if( isSignature( pdField ) )
113         {
114             pdField = new PDSignature( acroForm, field );
115         }
116         else
117         {
118             //do nothing and return an unknown field type.
119
}
120         return pdField;
121     }
122
123     /**
124      * This method determines if the given
125      * field is a radiobutton collection.
126      *
127      * @param flags The field flags.
128      *
129      * @return the result of the determination
130      */

131     private static boolean isRadio( int flags )
132     {
133         return (flags & RADIO_BITMASK) > 0;
134     }
135
136     /**
137      * This method determines if the given
138      * field is a pushbutton.
139      *
140      * @param flags The field flags.
141      *
142      * @return the result of the determination
143      */

144     private static boolean isPushButton( int flags )
145     {
146         return (flags & PUSHBUTTON_BITMASK) > 0;
147     }
148
149     /**
150      * This method determines if the given field is a choicefield
151      * Choicefields are either listboxes or comboboxes.
152      *
153      * @param field the field to determine
154      * @return the result of the determination
155      */

156     private static boolean isChoiceField(PDField field) throws IOException JavaDoc
157     {
158         return FIELD_TYPE_CH.equals(field.findFieldType());
159     }
160
161     /**
162      * This method determines if the given field is a button.
163      *
164      * @param field the field to determine
165      * @return the result of the determination
166      *
167      * @throws IOException If there is an error determining the field type.
168      */

169     private static boolean isButton(PDField field) throws IOException JavaDoc
170     {
171         String JavaDoc ft = field.findFieldType();
172         boolean retval = FIELD_TYPE_BTN.equals( ft );
173         List JavaDoc kids = field.getKids();
174         if( ft == null && kids != null && kids.size() > 0)
175         {
176             //sometimes if it is a button the type is only defined by one
177
//of the kids entries
178
Object JavaDoc obj = kids.get( 0 );
179             COSDictionary kidDict = null;
180             if( obj instanceof PDField )
181             {
182                 kidDict = ((PDField)obj).getDictionary();
183             }
184             else if( obj instanceof PDAnnotationWidget )
185             {
186                 kidDict = ((PDAnnotationWidget)obj).getDictionary();
187             }
188             else
189             {
190                 throw new IOException JavaDoc( "Error:Unexpected type of kids field:" + obj );
191             }
192             retval = isButton( new PDUnknownField( field.getAcroForm(), kidDict ) );
193         }
194         return retval;
195     }
196
197    /**
198      * This method determines if the given field is a signature.
199      *
200      * @param field the field to determine
201      * @return the result of the determination
202      */

203     private static boolean isSignature(PDField field) throws IOException JavaDoc
204     {
205         return FIELD_TYPE_SIG.equals(field.findFieldType());
206     }
207
208     /**
209      * This method determines if the given field is a Textbox.
210      *
211      * @param field the field to determine
212      * @return the result of the determination
213      */

214     private static boolean isTextbox(PDField field) throws IOException JavaDoc
215     {
216         return FIELD_TYPE_TX.equals(field.findFieldType());
217     }
218 }
Popular Tags