KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > fdf > FDFField


1 /**
2  * Copyright (c) 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.fdf;
32
33 import java.io.IOException JavaDoc;
34 import java.io.Writer JavaDoc;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.List JavaDoc;
38
39 import org.pdfbox.cos.COSArray;
40 import org.pdfbox.cos.COSBase;
41 import org.pdfbox.cos.COSDictionary;
42 import org.pdfbox.cos.COSInteger;
43 import org.pdfbox.cos.COSName;
44 import org.pdfbox.cos.COSNumber;
45 import org.pdfbox.cos.COSStream;
46 import org.pdfbox.cos.COSString;
47
48 import org.pdfbox.pdmodel.common.COSObjectable;
49 import org.pdfbox.pdmodel.common.COSArrayList;
50 import org.pdfbox.pdmodel.common.PDTextStream;
51
52 import org.pdfbox.pdmodel.interactive.action.PDActionFactory;
53 import org.pdfbox.pdmodel.interactive.action.PDAdditionalActions;
54 import org.pdfbox.pdmodel.interactive.action.type.PDAction;
55
56 import org.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
57
58 import org.pdfbox.util.XMLUtil;
59
60 import org.w3c.dom.Element JavaDoc;
61 import org.w3c.dom.Node JavaDoc;
62 import org.w3c.dom.NodeList JavaDoc;
63
64 /**
65  * This represents an FDF field that is part of the FDF document.
66  *
67  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
68  * @version $Revision: 1.5 $
69  */

70 public class FDFField implements COSObjectable
71 {
72     private COSDictionary field;
73
74     /**
75      * Default constructor.
76      */

77     public FDFField()
78     {
79         field = new COSDictionary();
80     }
81
82     /**
83      * Constructor.
84      *
85      * @param f The FDF field.
86      */

87     public FDFField( COSDictionary f )
88     {
89         field = f;
90     }
91     
92     /**
93      * This will create an FDF field from an XFDF XML document.
94      *
95      * @param fieldXML The XML document that contains the XFDF data.
96      * @throws IOException If there is an error reading from the dom.
97      */

98     public FDFField( Element JavaDoc fieldXML ) throws IOException JavaDoc
99     {
100         this();
101         this.setPartialFieldName( fieldXML.getAttribute( "name" ) );
102         NodeList JavaDoc nodeList = fieldXML.getChildNodes();
103         List JavaDoc kids = new ArrayList JavaDoc();
104         for( int i=0; i<nodeList.getLength(); i++ )
105         {
106             Node JavaDoc node = nodeList.item( i );
107             if( node instanceof Element JavaDoc )
108             {
109                 Element JavaDoc child = (Element JavaDoc)node;
110                 if( child.getTagName().equals( "value" ) )
111                 {
112                     setValue( XMLUtil.getNodeValue( child ) );
113                 }
114                 else if( child.getTagName().equals( "value-richtext" ) )
115                 {
116                     setRichText( new PDTextStream( XMLUtil.getNodeValue( child ) ) );
117                 }
118                 else if( child.getTagName().equals( "field" ) )
119                 {
120                     kids.add( new FDFField( child ) );
121                 }
122             }
123         }
124         if( kids.size() > 0 )
125         {
126             setKids( kids );
127         }
128     
129     }
130     
131     /**
132      * This will write this element as an XML document.
133      *
134      * @param output The stream to write the xml to.
135      *
136      * @throws IOException If there is an error writing the XML.
137      */

138     public void writeXML( Writer JavaDoc output ) throws IOException JavaDoc
139     {
140         output.write( "<field name=\"" + getPartialFieldName() + "\">\n");
141         Object JavaDoc value = getValue();
142         if( value != null )
143         {
144             output.write( "<value>" + value + "</value>\n" );
145         }
146         PDTextStream rt = getRichText();
147         if( rt != null )
148         {
149             output.write( "<value-richtext>" + rt.getAsString() + "</value-richtext>\n" );
150         }
151         List JavaDoc kids = getKids();
152         if( kids != null )
153         {
154             for( int i=0; i<kids.size(); i++ )
155             {
156                 ((FDFField)kids.get( i ) ).writeXML( output );
157             }
158         }
159         output.write( "</field>\n");
160     }
161
162     /**
163      * Convert this standard java object to a COS object.
164      *
165      * @return The cos object that matches this Java object.
166      */

167     public COSBase getCOSObject()
168     {
169         return field;
170     }
171
172     /**
173      * Convert this standard java object to a COS object.
174      *
175      * @return The cos object that matches this Java object.
176      */

177     public COSDictionary getCOSDictionary()
178     {
179         return field;
180     }
181
182     /**
183      * This will get the list of kids. This will return a list of FDFField objects.
184      * This will return null if the underlying list is null.
185      *
186      * @return The list of kids.
187      */

188     public List JavaDoc getKids()
189     {
190         COSArray kids = (COSArray)field.getDictionaryObject( "Kids" );
191         List JavaDoc retval = null;
192         if( kids != null )
193         {
194             List JavaDoc actuals = new ArrayList JavaDoc();
195             for( int i=0; i<kids.size(); i++ )
196             {
197                 actuals.add( new FDFField( (COSDictionary)kids.getObject( i ) ) );
198             }
199             retval = new COSArrayList( actuals, kids );
200         }
201         return retval;
202     }
203
204     /**
205      * This will set the list of kids.
206      *
207      * @param kids A list of FDFField objects.
208      */

209     public void setKids( List JavaDoc kids )
210     {
211         field.setItem( "Kids", COSArrayList.converterToCOSArray( kids ) );
212     }
213
214     /**
215      * This will get the "T" entry in the field dictionary. A partial field
216      * name. Where the fully qualified field name is a concatenation of
217      * the parent's fully qualified field name and "." as a separator. For example<br/>
218      * Address.State<br />
219      * Address.City<br />
220      *
221      * @return The partial field name.
222      */

223     public String JavaDoc getPartialFieldName()
224     {
225         return field.getString( "T" );
226     }
227
228     /**
229      * This will set the partial field name.
230      *
231      * @param partial The partial field name.
232      */

233     public void setPartialFieldName( String JavaDoc partial )
234     {
235         field.setString( "T", partial );
236     }
237
238     /**
239      * This will set the value for the field. This will return type will either be <br />
240      * String : Checkboxes, Radio Button <br />
241      * java.util.List of strings: Choice Field
242      * PDTextStream: Textfields
243      *
244      * @return The value of the field.
245      *
246      * @throws IOException If there is an error getting the value.
247      */

248     public Object JavaDoc getValue() throws IOException JavaDoc
249     {
250         Object JavaDoc retval = null;
251         COSBase value = field.getDictionaryObject( "V" );
252         if( value instanceof COSName )
253         {
254             retval = ((COSName)value).getName();
255         }
256         else if( value instanceof COSArray )
257         {
258             retval = COSArrayList.convertCOSStringCOSArrayToList( (COSArray)value );
259         }
260         else if( value instanceof COSString || value instanceof COSStream )
261         {
262             retval = PDTextStream.createTextStream( value );
263         }
264         else if( value == null )
265         {
266             //Ok, value is null so do nothing
267
}
268         else
269         {
270             throw new IOException JavaDoc( "Error:Unknown type for field import" + value );
271         }
272         return retval;
273     }
274
275     /**
276      * You should pass in a string, or a java.util.List of strings to set the
277      * value.
278      *
279      * @param value The value that should populate when imported.
280      *
281      * @throws IOException If there is an error setting the value.
282      */

283     public void setValue( Object JavaDoc value ) throws IOException JavaDoc
284     {
285         COSBase cos = null;
286         if( value instanceof List JavaDoc )
287         {
288             cos = COSArrayList.convertStringListToCOSStringCOSArray( (List JavaDoc)value );
289         }
290         else if( value instanceof String JavaDoc )
291         {
292             cos = COSName.getPDFName( (String JavaDoc)value );
293         }
294         else if( value instanceof COSObjectable )
295         {
296             cos = ((COSObjectable)value).getCOSObject();
297         }
298         else if( value == null )
299         {
300             //do nothing and let cos remain null as well.
301
}
302         else
303         {
304             throw new IOException JavaDoc( "Error:Unknown type for field import" + value );
305         }
306         field.setItem( "V", cos );
307     }
308
309     /**
310      * This will get the Ff entry of the cos dictionary. If it it not present then
311      * this method will return null.
312      *
313      * @return The field flags.
314      */

315     public Integer JavaDoc getFieldFlags()
316     {
317         Integer JavaDoc retval = null;
318         COSNumber ff = (COSNumber)field.getDictionaryObject( "Ff" );
319         if( ff != null )
320         {
321             retval = new Integer JavaDoc( ff.intValue() );
322         }
323         return retval;
324     }
325
326     /**
327      * This will get the field flags that are associated with this field. The Ff entry
328      * in the FDF field dictionary.
329      *
330      * @param ff The new value for the field flags.
331      */

332     public void setFieldFlags( Integer JavaDoc ff )
333     {
334         COSInteger value = null;
335         if( ff != null )
336         {
337             value = new COSInteger( ff.intValue() );
338         }
339         field.setItem( "Ff", value );
340     }
341
342     /**
343      * This will get the field flags that are associated with this field. The Ff entry
344      * in the FDF field dictionary.
345      *
346      * @param ff The new value for the field flags.
347      */

348     public void setFieldFlags( int ff )
349     {
350         field.setItem( "Ff", new COSInteger( ff ) );
351     }
352
353     /**
354      * This will get the SetFf entry of the cos dictionary. If it it not present then
355      * this method will return null.
356      *
357      * @return The field flags.
358      */

359     public Integer JavaDoc getSetFieldFlags()
360     {
361         Integer JavaDoc retval = null;
362         COSNumber ff = (COSNumber)field.getDictionaryObject( "SetFf" );
363         if( ff != null )
364         {
365             retval = new Integer JavaDoc( ff.intValue() );
366         }
367         return retval;
368     }
369
370     /**
371      * This will get the field flags that are associated with this field. The SetFf entry
372      * in the FDF field dictionary.
373      *
374      * @param ff The new value for the "set field flags".
375      */

376     public void setSetFieldFlags( Integer JavaDoc ff )
377     {
378         COSInteger value = null;
379         if( ff != null )
380         {
381             value = new COSInteger( ff.intValue() );
382         }
383         field.setItem( "SetFf", value );
384     }
385
386     /**
387      * This will get the field flags that are associated with this field. The SetFf entry
388      * in the FDF field dictionary.
389      *
390      * @param ff The new value for the "set field flags".
391      */

392     public void setSetFieldFlags( int ff )
393     {
394         field.setItem( "SetFf", new COSInteger( ff ) );
395     }
396
397     /**
398      * This will get the ClrFf entry of the cos dictionary. If it it not present then
399      * this method will return null.
400      *
401      * @return The field flags.
402      */

403     public Integer JavaDoc getClearFieldFlags()
404     {
405         Integer JavaDoc retval = null;
406         COSNumber ff = (COSNumber)field.getDictionaryObject( "ClrFf" );
407         if( ff != null )
408         {
409             retval = new Integer JavaDoc( ff.intValue() );
410         }
411         return retval;
412     }
413
414     /**
415      * This will get the field flags that are associated with this field. The ClrFf entry
416      * in the FDF field dictionary.
417      *
418      * @param ff The new value for the "clear field flags".
419      */

420     public void setClearFieldFlags( Integer JavaDoc ff )
421     {
422         COSInteger value = null;
423         if( ff != null )
424         {
425             value = new COSInteger( ff.intValue() );
426         }
427         field.setItem( "ClrFf", value );
428     }
429
430     /**
431      * This will get the field flags that are associated with this field. The ClrFf entry
432      * in the FDF field dictionary.
433      *
434      * @param ff The new value for the "clear field flags".
435      */

436     public void setClearFieldFlags( int ff )
437     {
438         field.setItem( "ClrFf", new COSInteger( ff ) );
439     }
440
441     /**
442      * This will get the F entry of the cos dictionary. If it it not present then
443      * this method will return null.
444      *
445      * @return The widget field flags.
446      */

447     public Integer JavaDoc getWidgetFieldFlags()
448     {
449         Integer JavaDoc retval = null;
450         COSNumber f = (COSNumber)field.getDictionaryObject( "F" );
451         if( f != null )
452         {
453             retval = new Integer JavaDoc( f.intValue() );
454         }
455         return retval;
456     }
457
458     /**
459      * This will get the widget field flags that are associated with this field. The F entry
460      * in the FDF field dictionary.
461      *
462      * @param f The new value for the field flags.
463      */

464     public void setWidgetFieldFlags( Integer JavaDoc f )
465     {
466         COSInteger value = null;
467         if( f != null )
468         {
469             value = new COSInteger( f.intValue() );
470         }
471         field.setItem( "F", value );
472     }
473
474     /**
475      * This will get the field flags that are associated with this field. The F entry
476      * in the FDF field dictionary.
477      *
478      * @param f The new value for the field flags.
479      */

480     public void setWidgetFieldFlags( int f )
481     {
482         field.setItem( "F", new COSInteger( f ) );
483     }
484
485     /**
486      * This will get the SetF entry of the cos dictionary. If it it not present then
487      * this method will return null.
488      *
489      * @return The field flags.
490      */

491     public Integer JavaDoc getSetWidgetFieldFlags()
492     {
493         Integer JavaDoc retval = null;
494         COSNumber ff = (COSNumber)field.getDictionaryObject( "SetF" );
495         if( ff != null )
496         {
497             retval = new Integer JavaDoc( ff.intValue() );
498         }
499         return retval;
500     }
501
502     /**
503      * This will get the widget field flags that are associated with this field. The SetF entry
504      * in the FDF field dictionary.
505      *
506      * @param ff The new value for the "set widget field flags".
507      */

508     public void setSetWidgetFieldFlags( Integer JavaDoc ff )
509     {
510         COSInteger value = null;
511         if( ff != null )
512         {
513             value = new COSInteger( ff.intValue() );
514         }
515         field.setItem( "SetF", value );
516     }
517
518     /**
519      * This will get the widget field flags that are associated with this field. The SetF entry
520      * in the FDF field dictionary.
521      *
522      * @param ff The new value for the "set widget field flags".
523      */

524     public void setSetWidgetFieldFlags( int ff )
525     {
526         field.setItem( "SetF", new COSInteger( ff ) );
527     }
528
529     /**
530      * This will get the ClrF entry of the cos dictionary. If it it not present then
531      * this method will return null.
532      *
533      * @return The widget field flags.
534      */

535     public Integer JavaDoc getClearWidgetFieldFlags()
536     {
537         Integer JavaDoc retval = null;
538         COSNumber ff = (COSNumber)field.getDictionaryObject( "ClrF" );
539         if( ff != null )
540         {
541             retval = new Integer JavaDoc( ff.intValue() );
542         }
543         return retval;
544     }
545
546     /**
547      * This will get the field flags that are associated with this field. The ClrF entry
548      * in the FDF field dictionary.
549      *
550      * @param ff The new value for the "clear widget field flags".
551      */

552     public void setClearWidgetFieldFlags( Integer JavaDoc ff )
553     {
554         COSInteger value = null;
555         if( ff != null )
556         {
557             value = new COSInteger( ff.intValue() );
558         }
559         field.setItem( "ClrF", value );
560     }
561
562     /**
563      * This will get the field flags that are associated with this field. The ClrF entry
564      * in the FDF field dictionary.
565      *
566      * @param ff The new value for the "clear field flags".
567      */

568     public void setClearWidgetFieldFlags( int ff )
569     {
570         field.setItem( "ClrF", new COSInteger( ff ) );
571     }
572
573     /**
574      * This will get the appearance dictionary that specifies the appearance of
575      * a pushbutton field.
576      *
577      * @return The AP entry of this dictionary.
578      */

579     public PDAppearanceDictionary getAppearanceDictionary()
580     {
581         PDAppearanceDictionary retval = null;
582         COSDictionary dict = (COSDictionary)field.getDictionaryObject( "AP" );
583         if( dict != null )
584         {
585             retval = new PDAppearanceDictionary( dict );
586         }
587         return retval;
588     }
589
590     /**
591      * This will set the appearance dictionary.
592      *
593      * @param ap The apperance dictionary.
594      */

595     public void setAppearanceDictionary( PDAppearanceDictionary ap )
596     {
597         field.setItem( "AP", ap );
598     }
599
600     /**
601      * This will get named page references..
602      *
603      * @return The named page references.
604      */

605     public FDFNamedPageReference getAppearanceStreamReference()
606     {
607         FDFNamedPageReference retval = null;
608         COSDictionary ref = (COSDictionary)field.getDictionaryObject( "APRef" );
609         if( ref != null )
610         {
611             retval = new FDFNamedPageReference( ref );
612         }
613         return retval;
614     }
615
616     /**
617      * This will set the named page references.
618      *
619      * @param ref The named page references.
620      */

621     public void setAppearanceStreamReference( FDFNamedPageReference ref )
622     {
623         field.setItem( "APRef", ref );
624     }
625
626     /**
627      * This will get the icon fit that is associated with this field.
628      *
629      * @return The IF entry.
630      */

631     public FDFIconFit getIconFit()
632     {
633         FDFIconFit retval = null;
634         COSDictionary dic = (COSDictionary)field.getDictionaryObject( "IF" );
635         if( dic != null )
636         {
637             retval = new FDFIconFit( dic );
638         }
639         return retval;
640     }
641
642     /**
643      * This will set the icon fit entry.
644      *
645      * @param fit The icon fit object.
646      */

647     public void setIconFit( FDFIconFit fit )
648     {
649         field.setItem( "IF", fit );
650     }
651
652     /**
653      * This will return a list of options for a choice field. The value in the
654      * list will be 1 of 2 types. java.lang.String or FDFOptionElement.
655      *
656      * @return A list of all options.
657      */

658     public List JavaDoc getOptions()
659     {
660         List JavaDoc retval = null;
661         COSArray array = (COSArray)field.getDictionaryObject( "Opt" );
662         if( array != null )
663         {
664             List JavaDoc objects = new ArrayList JavaDoc();
665             for( int i=0; i<array.size(); i++ )
666             {
667                 COSBase next = array.getObject( i );
668                 if( next instanceof COSString )
669                 {
670                     objects.add( ((COSString)next).getString() );
671                 }
672                 else
673                 {
674                     COSArray value = (COSArray)next;
675                     objects.add( new FDFOptionElement( value ) );
676                 }
677             }
678             retval = new COSArrayList( objects, array );
679         }
680         return retval;
681     }
682
683     /**
684      * This will set the options for the choice field. The objects in the list
685      * should either be java.lang.String or FDFOptionElement.
686      *
687      * @param options The options to set.
688      */

689     public void setOptions( List JavaDoc options )
690     {
691         COSArray value = COSArrayList.converterToCOSArray( options );
692         field.setItem( "Opt", value );
693     }
694
695     /**
696      * This will get the action that is associated with this field.
697      *
698      * @return The A entry in the field dictionary.
699      */

700     public PDAction getAction()
701     {
702         return PDActionFactory.createAction( (COSDictionary)field.getDictionaryObject( "A" ) );
703     }
704
705     /**
706      * This will set the action that is associated with this field.
707      *
708      * @param a The new action.
709      */

710     public void setAction( PDAction a )
711     {
712         field.setItem( "A", a );
713     }
714
715     /**
716      * This will get a list of additional actions that will get executed based
717      * on events.
718      *
719      * @return The AA entry in this field dictionary.
720      */

721     public PDAdditionalActions getAdditionalActions()
722     {
723         PDAdditionalActions retval = null;
724         COSDictionary dict = (COSDictionary)field.getDictionaryObject( "AA" );
725         if( dict != null )
726         {
727             retval = new PDAdditionalActions( dict );
728         }
729
730         return retval;
731     }
732
733     /**
734      * This will set the additional actions that are associated with this field.
735      *
736      * @param aa The additional actions.
737      */

738     public void setAdditionalActions( PDAdditionalActions aa )
739     {
740         field.setItem( "AA", aa );
741     }
742
743     /**
744      * This will set the rich text that is associated with this field.
745      *
746      * @return The rich text XHTML stream.
747      */

748     public PDTextStream getRichText()
749     {
750         COSBase rv = field.getDictionaryObject( "RV" );
751         return PDTextStream.createTextStream( rv );
752     }
753
754     /**
755      * This will set the rich text value.
756      *
757      * @param rv The rich text value for the stream.
758      */

759     public void setRichText( PDTextStream rv )
760     {
761         field.setItem( "RV", rv );
762     }
763 }
Popular Tags