KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > files > File


1 /*
2   Copyright © 2006 Stefano Chizzolini. http://clown.stefanochizzolini.it
3
4   Contributors:
5     * Stefano Chizzolini (original code developer, info@stefanochizzolini.it):
6       contributed code is Copyright © 2006 by Stefano Chizzolini.
7
8   This file should be part of the source code distribution of "PDF Clown library"
9   (the Program): see the accompanying README files for more info.
10
11   This Program is free software; you can redistribute it and/or modify it under
12   the terms of the GNU General Public License as published by the Free Software
13   Foundation; either version 2 of the License, or (at your option) any later version.
14
15   This Program is distributed in the hope that it will be useful, but WITHOUT ANY
16   WARRANTY, either expressed or implied; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
18
19   You should have received a copy of the GNU General Public License along with this
20   Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
21
22   Redistribution and use, with or without modification, are permitted provided that such
23   redistributions retain the above copyright notice, license and disclaimer, along with
24   this list of conditions.
25 */

26
27 package it.stefanochizzolini.clown.files;
28
29 import it.stefanochizzolini.clown.bytes.IInputStream;
30 import it.stefanochizzolini.clown.bytes.IOutputStream;
31 import it.stefanochizzolini.clown.documents.Document;
32 import it.stefanochizzolini.clown.objects.IPdfIndirectObject;
33 import it.stefanochizzolini.clown.objects.PdfDataObject;
34 import it.stefanochizzolini.clown.objects.PdfDictionary;
35 import it.stefanochizzolini.clown.objects.PdfDirectObject;
36 import it.stefanochizzolini.clown.objects.PdfName;
37 import it.stefanochizzolini.clown.objects.PdfObject;
38 import it.stefanochizzolini.clown.objects.PdfReference;
39 import it.stefanochizzolini.clown.tokens.FileFormatException;
40 import it.stefanochizzolini.clown.tokens.Reader;
41 import it.stefanochizzolini.clown.tokens.Writer;
42 import it.stefanochizzolini.clown.tokens.XRefEntry;
43 import it.stefanochizzolini.clown.util.NotImplementedException;
44
45 import java.util.Random JavaDoc;
46
47 /**
48   PDF file representation.
49 */

50 public class File
51 {
52   // <class>
53
// <static>
54
// <fields>
55
private static Random JavaDoc hashCodeGenerator = new Random JavaDoc();
56   // </fields>
57

58   // <interface>
59
// <public>
60
/**
61     Forces a generic object to be expressed as its corresponding data object.
62   */

63   public static PdfDataObject resolve(
64     PdfObject object
65     )
66   {
67     if(object instanceof IPdfIndirectObject)
68       return ((IPdfIndirectObject)object).getDataObject();
69     else
70       return (PdfDataObject)object;
71   }
72
73   /**
74     Forces a direct object to be updated (whether possible).
75   */

76   public static boolean update(
77     PdfDirectObject object
78     )
79   {
80     /*
81       NOTE: Only PDF references are able to be updated. Other direct types
82       are dependent on their respective containers for update.
83     */

84     if(object instanceof PdfReference)
85     {
86       ((PdfReference)object).getIndirectObject().update();
87       return true;
88     }
89
90     return false;
91   }
92   // </public>
93
// </interface>
94
// </static>
95

96   // <dynamic>
97
// <fields>
98
private Document document;
99   private int hashCode = File.hashCodeGenerator.nextInt();
100   private IndirectObjects indirectObjects;
101   private Reader reader;
102   private PdfDictionary trailer;
103   private String JavaDoc version;
104   private XRefEntry[] xrefEntries;
105   // </fields>
106

107   // <constructors>
108
public File(
109     )
110   {
111     this.version = "1.6";
112     this.trailer = new PdfDictionary();
113     this.indirectObjects = new IndirectObjects(this,null);
114     this.document = new Document(this);
115   }
116
117   public File(
118     IInputStream stream
119     ) throws FileFormatException
120   {
121     this.reader = new Reader(stream,this);
122
123     this.version = reader.readVersion();
124     this.trailer = reader.readTrailer();
125
126     // Is this file encrypted?
127
if(trailer.containsKey(PdfName.Encrypt))
128       throw new NotImplementedException("Encrypted files are currently not supported.");
129
130     this.xrefEntries = reader.readXRefTable(trailer);
131     this.indirectObjects = new IndirectObjects(this,xrefEntries);
132     this.document = new Document(trailer.get(PdfName.Root));
133   }
134   // </constructors>
135

136   // <interface>
137
// <public>
138
public Document getDocument(
139     )
140   {return document;}
141
142   public IndirectObjects getIndirectObjects(
143     )
144   {return indirectObjects;}
145
146   public Reader getReader(
147     )
148   {return reader;}
149
150   public PdfDictionary getTrailer(
151     )
152   {return trailer;}
153
154   public String JavaDoc getVersion(
155     )
156   {return version;}
157
158   //TODO:IMPL avoid direct exposure of array (corruptible!)!!!
159
public XRefEntry[] getXRefEntries(
160     )
161   {return xrefEntries;}
162
163   public int hashCode(
164     )
165   {return hashCode;}
166
167   /**
168     <h3>Remarks</h3>
169     <p>It's caller responsibility to close the stream after this method ends.</p>
170   */

171   public void writeTo(
172     IOutputStream stream,
173     SerializationModeEnum mode
174     )
175   {
176     Writer writer = new Writer(stream,this);
177
178     switch(mode)
179     {
180       case Incremental:
181         if(reader != null)
182         {
183           writer.writeIncremental();
184           break;
185         }
186         // If reader IS null, fall through to Standard!
187
case Standard:
188         writer.writeStandard();
189         break;
190       case Linearized:
191         throw new NotImplementedException();
192     }
193   }
194   // </public>
195
// </interface>
196
// </dynamic>
197
// </class>
198
}
Popular Tags