KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > objects > PdfReference


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.objects;
28
29 import it.stefanochizzolini.clown.bytes.IOutputStream;
30 import it.stefanochizzolini.clown.files.File;
31 import it.stefanochizzolini.clown.tokens.Parser;
32
33 import java.text.DecimalFormat JavaDoc;
34
35 /**
36   PDF reference object.
37 */

38 public class PdfReference
39   extends PdfDirectObject
40   implements IPdfIndirectObject
41 {
42   // <class>
43
// <static>
44
// <fields>
45
private static final DecimalFormat JavaDoc xRefGenerationFormatter;
46   private static final DecimalFormat JavaDoc xRefOffsetFormatter;
47   // </fields>
48

49   // <constructors>
50
static
51   {
52     xRefGenerationFormatter = new DecimalFormat JavaDoc("00000");
53     xRefOffsetFormatter = new DecimalFormat JavaDoc("0000000000");
54   }
55   // </constructors>
56
// </static>
57

58   // <dynamic>
59
// <fields>
60
private PdfIndirectObject indirectObject;
61
62   private int generationNumber;
63   private int objectNumber;
64
65   private File file;
66   // </fields>
67

68   // <constructors>
69
PdfReference(
70     PdfIndirectObject indirectObject,
71     int objectNumber,
72     int generationNumber
73     )
74   {
75     this.indirectObject = indirectObject;
76
77     this.objectNumber = objectNumber;
78     this.generationNumber = generationNumber;
79   }
80
81   /**
82     <h3>Remarks</h3>
83     <p>For internal use only.</p>
84     <p>This is a necessary hack because indirect objects are unreachable on parsing bootstrap
85     (see File(IInputStream) constructor).</p>
86   */

87   public PdfReference(
88     Parser.Reference reference,
89     File file
90     )
91   {
92     this.objectNumber = reference.getObjectNumber();
93     this.generationNumber = reference.getGenerationNumber();
94
95     this.file = file;
96   }
97   // </constructors>
98

99   // <interface>
100
// <public>
101
public String JavaDoc getCrossReference(
102     long offset
103     )
104   {
105     return (xRefOffsetFormatter.format(offset) + " "
106         + xRefGenerationFormatter.format(generationNumber) + " "
107         + getIndirectObject().getUsage()
108         + "\r\n");
109   }
110
111   @Override JavaDoc
112   public boolean equals(
113     Object JavaDoc object
114     )
115   {
116     if(!(object instanceof PdfReference))
117       return false;
118
119     return (getID().equals(((PdfReference)object).getID()));
120   }
121
122   public int getGenerationNumber(
123     )
124   {return generationNumber;}
125
126   @Override JavaDoc
127   public int hashCode(
128     )
129   {return getIndirectObject().hashCode();}
130
131   public String JavaDoc getID(
132     )
133   {return (objectNumber + " " + generationNumber);}
134
135   public String JavaDoc getIndirectReference(
136     )
137   {return (getID() + " R");}
138
139   public int getObjectNumber(
140     )
141   {return objectNumber;}
142
143   @Override JavaDoc
144   public String JavaDoc toPdf(
145     )
146   {return getIndirectReference();}
147
148   @Override JavaDoc
149   public String JavaDoc toString(
150     )
151   {return getIndirectReference();}
152
153   // <IPdfIndirectObject>
154
@Override JavaDoc
155   public Object JavaDoc clone(
156     File context
157     )
158   {return ((PdfIndirectObject)getIndirectObject().clone(context)).getReference();}
159
160   public void delete(
161     )
162   {getIndirectObject().delete();}
163
164   public PdfDataObject getDataObject(
165     )
166   {return getIndirectObject().getDataObject();}
167
168   public PdfIndirectObject getIndirectObject(
169     )
170   {
171     if(indirectObject == null)
172       indirectObject = file.getIndirectObjects().get(objectNumber);
173
174     return indirectObject;
175   }
176
177   public PdfReference getReference(
178     )
179   {return this;}
180   // </IPdfIndirectObject>
181
// </public>
182

183   // <internal>
184
@Override JavaDoc
185   int writeTo(
186     IOutputStream stream
187     )
188   {return stream.write(getIndirectReference());}
189   // </internal>
190
// </interface>
191
// </dynamic>
192
// </class>
193
}
Popular Tags