KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
36 import java.util.List JavaDoc;
37
38 import org.pdfbox.cos.COSArray;
39 import org.pdfbox.cos.COSBase;
40 import org.pdfbox.cos.COSDictionary;
41 import org.pdfbox.cos.COSStream;
42 import org.pdfbox.cos.COSString;
43
44 import org.pdfbox.pdmodel.common.COSObjectable;
45 import org.pdfbox.pdmodel.common.COSArrayList;
46 import org.pdfbox.pdmodel.common.filespecification.PDFileSpecification;
47 import org.pdfbox.pdmodel.common.filespecification.PDSimpleFileSpecification;
48 import org.w3c.dom.Element JavaDoc;
49 import org.w3c.dom.Node JavaDoc;
50 import org.w3c.dom.NodeList JavaDoc;
51
52 /**
53  * This represents an FDF dictionary that is part of the FDF document.
54  *
55  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
56  * @version $Revision: 1.8 $
57  */

58 public class FDFDictionary implements COSObjectable
59 {
60     private COSDictionary fdf;
61
62     /**
63      * Default constructor.
64      */

65     public FDFDictionary()
66     {
67         fdf = new COSDictionary();
68     }
69
70     /**
71      * Constructor.
72      *
73      * @param fdfDictionary The FDF documents catalog.
74      */

75     public FDFDictionary( COSDictionary fdfDictionary )
76     {
77         fdf = fdfDictionary;
78     }
79     
80     /**
81      * This will create an FDF dictionary from an XFDF XML document.
82      *
83      * @param fdfXML The XML document that contains the XFDF data.
84      * @throws IOException If there is an error reading from the dom.
85      */

86     public FDFDictionary( Element JavaDoc fdfXML ) throws IOException JavaDoc
87     {
88         this();
89         NodeList JavaDoc nodeList = fdfXML.getChildNodes();
90         for( int i=0; i<nodeList.getLength(); i++ )
91         {
92             Node JavaDoc node = nodeList.item( i );
93             if( node instanceof Element JavaDoc )
94             {
95                 Element JavaDoc child = (Element JavaDoc)node;
96                 if( child.getTagName().equals( "f" ) )
97                 {
98                     PDSimpleFileSpecification fs = new PDSimpleFileSpecification();
99                     fs.setFile( child.getAttribute( "href" ) );
100                 
101                 }
102                 else if( child.getTagName().equals( "ids" ) )
103                 {
104                     COSArray ids = new COSArray();
105                     String JavaDoc original = child.getAttribute( "original" );
106                     String JavaDoc modified = child.getAttribute( "modified" );
107                     ids.add( COSString.createFromHexString( original ) );
108                     ids.add( COSString.createFromHexString( modified ) );
109                     setID( ids );
110                 }
111                 else if( child.getTagName().equals( "fields" ) )
112                 {
113                     NodeList JavaDoc fields = child.getElementsByTagName( "field" );
114                     List JavaDoc fieldList = new ArrayList JavaDoc();
115                     for( int f=0; f<fields.getLength(); f++ )
116                     {
117                         fieldList.add( new FDFField( (Element JavaDoc)fields.item( f ) ) );
118                     }
119                     setFields( fieldList );
120                 }
121             }
122         }
123     }
124     
125     /**
126      * This will write this element as an XML document.
127      *
128      * @param output The stream to write the xml to.
129      *
130      * @throws IOException If there is an error writing the XML.
131      */

132     public void writeXML( Writer JavaDoc output ) throws IOException JavaDoc
133     {
134         PDFileSpecification fs = this.getFile();
135         if( fs != null )
136         {
137             output.write( "<f HREF=\"" + fs.getFile() + "\" />\n" );
138         }
139         COSArray ids = this.getID();
140         if( ids != null )
141         {
142             COSString original = (COSString)ids.getObject( 0 );
143             COSString modified = (COSString)ids.getObject( 1 );
144             output.write( "<ids original=\"" + original.getHexString() + "\" " );
145             output.write( "modified=\"" + modified.getHexString() + "\" />\n");
146         }
147         List JavaDoc fields = getFields();
148         if( fields != null && fields.size() > 0 )
149         {
150             output.write( "<fields>\n" );
151             for( int i=0; i<fields.size(); i++ )
152             {
153                 ((FDFField)fields.get( i )).writeXML( output );
154             }
155             output.write( "</fields>\n" );
156         }
157     }
158
159     /**
160      * Convert this standard java object to a COS object.
161      *
162      * @return The cos object that matches this Java object.
163      */

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

174     public COSDictionary getCOSDictionary()
175     {
176         return fdf;
177     }
178
179     /**
180      * The source file or target file: the PDF document file that
181      * this FDF file was exported from or is intended to be imported into.
182      *
183      * @return The F entry of the FDF dictionary.
184      *
185      * @throws IOException If there is an error creating the file spec.
186      */

187     public PDFileSpecification getFile() throws IOException JavaDoc
188     {
189         return PDFileSpecification.createFS( fdf.getDictionaryObject( "F" ) );
190     }
191
192     /**
193      * This will set the file specification.
194      *
195      * @param fs The file specification.
196      */

197     public void setFile( PDFileSpecification fs )
198     {
199         fdf.setItem( "F", fs );
200     }
201
202     /**
203      * This is the FDF id.
204      *
205      * @return The FDF ID.
206      */

207     public COSArray getID()
208     {
209         return (COSArray)fdf.getDictionaryObject( "ID" );
210     }
211
212     /**
213      * This will set the FDF id.
214      *
215      * @param id The new id for the FDF.
216      */

217     public void setID( COSArray id )
218     {
219         fdf.setItem( "ID", id );
220     }
221
222     /**
223      * This will get the list of FDF Fields. This will return a list of FDFField
224      * objects.
225      *
226      * @return A list of FDF fields.
227      */

228     public List JavaDoc getFields()
229     {
230         List JavaDoc retval = null;
231         COSArray fieldArray = (COSArray)fdf.getDictionaryObject( "Fields" );
232         if( fieldArray != null )
233         {
234             List JavaDoc fields = new ArrayList JavaDoc();
235             for( int i=0; i<fieldArray.size(); i++ )
236             {
237                 fields.add( new FDFField( (COSDictionary)fieldArray.getObject( i ) ) );
238             }
239             retval = new COSArrayList( fields, fieldArray );
240         }
241         return retval;
242     }
243
244     /**
245      * This will set the list of fields. This should be a list of FDFField objects.
246      *
247      * @param fields The list of fields.
248      */

249     public void setFields( List JavaDoc fields )
250     {
251         fdf.setItem( "Fields", COSArrayList.converterToCOSArray( fields ) );
252     }
253
254     /**
255      * This will get the status string to be displayed as the result of an
256      * action.
257      *
258      * @return The status.
259      */

260     public String JavaDoc getStatus()
261     {
262         return fdf.getString( "Status" );
263     }
264
265     /**
266      * This will set the status string.
267      *
268      * @param status The new status string.
269      */

270     public void setStatus( String JavaDoc status )
271     {
272         fdf.setString( "Status", status );
273     }
274
275     /**
276      * This will get the list of FDF Pages. This will return a list of FDFPage objects.
277      *
278      * @return A list of FDF pages.
279      */

280     public List JavaDoc getPages()
281     {
282         List JavaDoc retval = null;
283         COSArray pageArray = (COSArray)fdf.getDictionaryObject( "Pages" );
284         if( pageArray != null )
285         {
286             List JavaDoc pages = new ArrayList JavaDoc();
287             for( int i=0; i<pageArray.size(); i++ )
288             {
289                 pages.add( new FDFPage( (COSDictionary)pageArray.get( i ) ) );
290             }
291             retval = new COSArrayList( pages, pageArray );
292         }
293         return retval;
294     }
295
296     /**
297      * This will set the list of pages. This should be a list of FDFPage objects.
298      *
299      *
300      * @param pages The list of pages.
301      */

302     public void setPages( List JavaDoc pages )
303     {
304         fdf.setItem( "Pages", COSArrayList.converterToCOSArray( pages ) );
305     }
306
307     /**
308      * The encoding to be used for a FDF field. The default is PDFDocEncoding
309      * and this method will never return null.
310      *
311      * @return The encoding value.
312      */

313     public String JavaDoc getEncoding()
314     {
315         String JavaDoc encoding = fdf.getNameAsString( "Encoding" );
316         if( encoding == null )
317         {
318             encoding = "PDFDocEncoding";
319         }
320         return encoding;
321
322     }
323
324     /**
325      * This will set the encoding.
326      *
327      * @param encoding The new encoding.
328      */

329     public void setEncoding( String JavaDoc encoding )
330     {
331         fdf.setName( "Encoding", encoding );
332     }
333
334     /**
335      * This will get the list of FDF Annotations. This will return a list of FDFAnnotation objects
336      * or null if the entry is not set.
337      *
338      * @return A list of FDF annotations.
339      */

340     public List JavaDoc getAnnotations()
341     {
342         List JavaDoc retval = null;
343         COSArray annotArray = (COSArray)fdf.getDictionaryObject( "Annots" );
344         if( annotArray != null )
345         {
346             List JavaDoc annots = new ArrayList JavaDoc();
347             for( int i=0; i<annotArray.size(); i++ )
348             {
349                 annots.add( new FDFAnnotation( (COSDictionary)annotArray.getObject( i ) ) );
350             }
351             retval = new COSArrayList( annots, annotArray );
352         }
353         return retval;
354     }
355
356     /**
357      * This will set the list of annotations. This should be a list of FDFAnnotation objects.
358      *
359      *
360      * @param annots The list of annotations.
361      */

362     public void setAnnotations( List JavaDoc annots )
363     {
364         fdf.setItem( "Annots", COSArrayList.converterToCOSArray( annots ) );
365     }
366
367     /**
368      * This will get the incremental updates since the PDF was last opened.
369      *
370      * @return The differences entry of the FDF dictionary.
371      */

372     public COSStream getDifferences()
373     {
374         return (COSStream)fdf.getDictionaryObject( "Differences" );
375     }
376
377     /**
378      * This will set the differences stream.
379      *
380      * @param diff The new differences stream.
381      */

382     public void setDifferences( COSStream diff )
383     {
384         fdf.setItem( "Differences", diff );
385     }
386
387     /**
388      * This will get the target frame in the browser to open this document.
389      *
390      * @return The target frame.
391      */

392     public String JavaDoc getTarget()
393     {
394         return fdf.getString( "Target" );
395     }
396
397     /**
398      * This will set the target frame in the browser to open this document.
399      *
400      * @param target The new target frame.
401      */

402     public void setTarget( String JavaDoc target )
403     {
404         fdf.setString( "Target", target );
405     }
406
407     /**
408      * This will get the list of embedded FDF entries, or null if the entry is null.
409      * This will return a list of PDFileSpecification objects.
410      *
411      * @return A list of embedded FDF files.
412      *
413      * @throws IOException If there is an error creating the file spec.
414      */

415     public List JavaDoc getEmbeddedFDFs() throws IOException JavaDoc
416     {
417         List JavaDoc retval = null;
418         COSArray embeddedArray = (COSArray)fdf.getDictionaryObject( "EmbeddedFDFs" );
419         if( embeddedArray != null )
420         {
421             List JavaDoc embedded = new ArrayList JavaDoc();
422             for( int i=0; i<embeddedArray.size(); i++ )
423             {
424                 embedded.add( PDFileSpecification.createFS( embeddedArray.get( i ) ) );
425             }
426             retval = new COSArrayList( embedded, embeddedArray );
427         }
428         return retval;
429     }
430
431     /**
432      * This will set the list of embedded FDFs. This should be a list of
433      * PDFileSpecification objects.
434      *
435      *
436      * @param embedded The list of embedded FDFs.
437      */

438     public void setEmbeddedFDFs( List JavaDoc embedded )
439     {
440         fdf.setItem( "EmbeddedFDFs", COSArrayList.converterToCOSArray( embedded ) );
441     }
442
443     /**
444      * This will get the java script entry.
445      *
446      * @return The java script entry describing javascript commands.
447      */

448     public FDFJavaScript getJavaScript()
449     {
450         FDFJavaScript fs = null;
451         COSDictionary dic = (COSDictionary)fdf.getDictionaryObject( "JavaScript" );
452         if( dic != null )
453         {
454             fs = new FDFJavaScript( dic );
455         }
456         return fs;
457     }
458
459     /**
460      * This will set the JavaScript entry.
461      *
462      * @param js The javascript entries.
463      */

464     public void setJavaScript( FDFJavaScript js )
465     {
466         fdf.setItem( "JavaScript", js );
467     }
468
469 }
Popular Tags