KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (c) 2004-2005, 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.BufferedInputStream JavaDoc;
34 import java.io.BufferedWriter JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.FileInputStream JavaDoc;
37 import java.io.FileOutputStream JavaDoc;
38 import java.io.FileWriter JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.OutputStream JavaDoc;
42 import java.io.Writer JavaDoc;
43
44 import org.pdfbox.cos.COSDictionary;
45 import org.pdfbox.cos.COSDocument;
46 import org.pdfbox.cos.COSName;
47
48 import org.pdfbox.exceptions.COSVisitorException;
49
50 import org.pdfbox.pdfparser.PDFParser;
51
52 import org.pdfbox.pdfwriter.COSWriter;
53
54 import org.pdfbox.util.XMLUtil;
55
56 import org.w3c.dom.Document JavaDoc;
57 import org.w3c.dom.Element JavaDoc;
58
59 /**
60  * This is the in-memory representation of the FDF document. You need to call
61  * close() on this object when you are done using it!!
62  *
63  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
64  * @version $Revision: 1.6 $
65  */

66 public class FDFDocument
67 {
68     private COSDocument document;
69
70     /**
71      * Constructor, creates a new FDF document.
72      *
73      * @throws IOException If there is an error creating this document.
74      */

75     public FDFDocument() throws IOException JavaDoc
76     {
77         document = new COSDocument();
78         document.setHeaderString( "%FDF-1.2" );
79
80         //First we need a trailer
81
document.setTrailer( new COSDictionary() );
82
83         //Next we need the root dictionary.
84
FDFCatalog catalog = new FDFCatalog();
85         setCatalog( catalog );
86     }
87
88     /**
89      * Constructor that uses an existing document. The COSDocument that
90      * is passed in must be valid.
91      *
92      * @param doc The COSDocument that this document wraps.
93      */

94     public FDFDocument( COSDocument doc )
95     {
96         document = doc;
97     }
98     
99     /**
100      * This will create an FDF document from an XFDF XML document.
101      *
102      * @param doc The XML document that contains the XFDF data.
103      * @throws IOException If there is an error reading from the dom.
104      */

105     public FDFDocument( Document doc ) throws IOException JavaDoc
106     {
107         this();
108         Element JavaDoc xfdf = doc.getDocumentElement();
109         if( !xfdf.getNodeName().equals( "xfdf" ) )
110         {
111             throw new IOException JavaDoc( "Error while importing xfdf document, " +
112                 "root should be 'xfdf' and not '" + xfdf.getNodeName() + "'" );
113         }
114         FDFCatalog cat = new FDFCatalog( xfdf );
115         setCatalog( cat );
116     }
117     
118     /**
119      * This will write this element as an XML document.
120      *
121      * @param output The stream to write the xml to.
122      *
123      * @throws IOException If there is an error writing the XML.
124      */

125     public void writeXML( Writer JavaDoc output ) throws IOException JavaDoc
126     {
127         output.write( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
128         output.write( "<xfdf xmlns=\"http://ns.adobe.com/xfdf/\" xml:space=\"preserve\">\n" );
129             
130         getCatalog().writeXML( output );
131     
132         output.write( "</xfdf>\n" );
133     }
134     
135     
136
137     /**
138      * This will get the low level document.
139      *
140      * @return The document that this layer sits on top of.
141      */

142     public COSDocument getDocument()
143     {
144         return document;
145     }
146
147     /**
148      * This will get the FDF Catalog. This is guaranteed to not return null.
149      *
150      * @return The documents /Root dictionary
151      */

152     public FDFCatalog getCatalog()
153     {
154         FDFCatalog retval = null;
155         COSDictionary trailer = document.getTrailer();
156         COSDictionary root = (COSDictionary)trailer.getDictionaryObject( COSName.ROOT );
157         if( root == null )
158         {
159             retval = new FDFCatalog();
160             setCatalog( retval );
161         }
162         else
163         {
164             retval = new FDFCatalog( root );
165         }
166         return retval;
167     }
168
169     /**
170      * This will set the FDF catalog for this FDF document.
171      *
172      * @param cat The FDF catalog.
173      */

174     public void setCatalog( FDFCatalog cat )
175     {
176         COSDictionary trailer = document.getTrailer();
177         trailer.setItem( COSName.ROOT, cat );
178     }
179
180     /**
181      * This will load a document from a file.
182      *
183      * @param filename The name of the file to load.
184      *
185      * @return The document that was loaded.
186      *
187      * @throws IOException If there is an error reading from the stream.
188      */

189     public static FDFDocument load( String JavaDoc filename ) throws IOException JavaDoc
190     {
191         return load( new BufferedInputStream JavaDoc( new FileInputStream JavaDoc( filename ) ) );
192     }
193
194     /**
195      * This will load a document from a file.
196      *
197      * @param file The name of the file to load.
198      *
199      * @return The document that was loaded.
200      *
201      * @throws IOException If there is an error reading from the stream.
202      */

203     public static FDFDocument load( File JavaDoc file ) throws IOException JavaDoc
204     {
205         return load( new BufferedInputStream JavaDoc( new FileInputStream JavaDoc( file ) ) );
206     }
207
208     /**
209      * This will load a document from an input stream.
210      *
211      * @param input The stream that contains the document.
212      *
213      * @return The document that was loaded.
214      *
215      * @throws IOException If there is an error reading from the stream.
216      */

217     public static FDFDocument load( InputStream JavaDoc input ) throws IOException JavaDoc
218     {
219         PDFParser parser = new PDFParser( input );
220         parser.parse();
221         return parser.getFDFDocument();
222     }
223     
224     /**
225      * This will load a document from a file.
226      *
227      * @param filename The name of the file to load.
228      *
229      * @return The document that was loaded.
230      *
231      * @throws IOException If there is an error reading from the stream.
232      */

233     public static FDFDocument loadXFDF( String JavaDoc filename ) throws IOException JavaDoc
234     {
235         return loadXFDF( new BufferedInputStream JavaDoc( new FileInputStream JavaDoc( filename ) ) );
236     }
237
238     /**
239      * This will load a document from a file.
240      *
241      * @param file The name of the file to load.
242      *
243      * @return The document that was loaded.
244      *
245      * @throws IOException If there is an error reading from the stream.
246      */

247     public static FDFDocument loadXFDF( File JavaDoc file ) throws IOException JavaDoc
248     {
249         return loadXFDF( new BufferedInputStream JavaDoc( new FileInputStream JavaDoc( file ) ) );
250     }
251
252     /**
253      * This will load a document from an input stream.
254      *
255      * @param input The stream that contains the document.
256      *
257      * @return The document that was loaded.
258      *
259      * @throws IOException If there is an error reading from the stream.
260      */

261     public static FDFDocument loadXFDF( InputStream JavaDoc input ) throws IOException JavaDoc
262     {
263         Document doc = XMLUtil.parse( input );
264         return new FDFDocument( doc );
265     }
266     
267     /**
268      * This will save this document to the filesystem.
269      *
270      * @param fileName The file to save as.
271      *
272      * @throws IOException If there is an error saving the document.
273      * @throws COSVisitorException If an error occurs while generating the data.
274      */

275     public void save( File JavaDoc fileName ) throws IOException JavaDoc, COSVisitorException
276     {
277         save( new FileOutputStream JavaDoc( fileName ) );
278     }
279
280     /**
281      * This will save this document to the filesystem.
282      *
283      * @param fileName The file to save as.
284      *
285      * @throws IOException If there is an error saving the document.
286      * @throws COSVisitorException If an error occurs while generating the data.
287      */

288     public void save( String JavaDoc fileName ) throws IOException JavaDoc, COSVisitorException
289     {
290         save( new FileOutputStream JavaDoc( fileName ) );
291     }
292
293     /**
294      * This will save the document to an output stream.
295      *
296      * @param output The stream to write to.
297      *
298      * @throws IOException If there is an error writing the document.
299      * @throws COSVisitorException If an error occurs while generating the data.
300      */

301     public void save( OutputStream JavaDoc output ) throws IOException JavaDoc, COSVisitorException
302     {
303         COSWriter writer = null;
304         try
305         {
306             writer = new COSWriter( output );
307             writer.write( document );
308             writer.close();
309         }
310         finally
311         {
312             if( writer != null )
313             {
314                 writer.close();
315             }
316         }
317     }
318     
319     /**
320      * This will save this document to the filesystem.
321      *
322      * @param fileName The file to save as.
323      *
324      * @throws IOException If there is an error saving the document.
325      * @throws COSVisitorException If an error occurs while generating the data.
326      */

327     public void saveXFDF( File JavaDoc fileName ) throws IOException JavaDoc, COSVisitorException
328     {
329         saveXFDF( new BufferedWriter JavaDoc( new FileWriter JavaDoc( fileName ) ) );
330     }
331
332     /**
333      * This will save this document to the filesystem.
334      *
335      * @param fileName The file to save as.
336      *
337      * @throws IOException If there is an error saving the document.
338      * @throws COSVisitorException If an error occurs while generating the data.
339      */

340     public void saveXFDF( String JavaDoc fileName ) throws IOException JavaDoc, COSVisitorException
341     {
342         saveXFDF( new BufferedWriter JavaDoc( new FileWriter JavaDoc( fileName ) ) );
343     }
344
345     /**
346      * This will save the document to an output stream and close the stream.
347      *
348      * @param output The stream to write to.
349      *
350      * @throws IOException If there is an error writing the document.
351      * @throws COSVisitorException If an error occurs while generating the data.
352      */

353     public void saveXFDF( Writer JavaDoc output ) throws IOException JavaDoc, COSVisitorException
354     {
355         try
356         {
357             writeXML( output );
358         }
359         finally
360         {
361             if( output != null )
362             {
363                 output.close();
364             }
365         }
366     }
367
368     /**
369      * This will close the underlying COSDocument object.
370      *
371      * @throws IOException If there is an error releasing resources.
372      */

373     public void close() throws IOException JavaDoc
374     {
375         document.close();
376     }
377 }
Popular Tags