001: /*
002: Copyright © 2006 Stefano Chizzolini. http://clown.stefanochizzolini.it
003:
004: Contributors:
005: * Stefano Chizzolini (original code developer, http://www.stefanochizzolini.it):
006: contributed code is Copyright © 2006 by Stefano Chizzolini.
007:
008: This file should be part of the source code distribution of "PDF Clown library"
009: (the Program): see the accompanying README files for more info.
010:
011: This Program is free software; you can redistribute it and/or modify it under
012: the terms of the GNU General Public License as published by the Free Software
013: Foundation; either version 2 of the License, or (at your option) any later version.
014:
015: This Program is distributed in the hope that it will be useful, but WITHOUT ANY
016: WARRANTY, either expressed or implied; without even the implied warranty of
017: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
018:
019: You should have received a copy of the GNU General Public License along with this
020: Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
021:
022: Redistribution and use, with or without modification, are permitted provided that such
023: redistributions retain the above copyright notice, license and disclaimer, along with
024: this list of conditions.
025: */
026:
027: package it.stefanochizzolini.clown.objects;
028:
029: import it.stefanochizzolini.clown.bytes.IOutputStream;
030: import it.stefanochizzolini.clown.files.File;
031: import it.stefanochizzolini.clown.tokens.Parser;
032:
033: import java.text.DecimalFormat;
034:
035: /**
036: PDF reference object.
037: */
038: public class PdfReference extends PdfDirectObject implements
039: IPdfIndirectObject {
040: // <class>
041: // <static>
042: // <fields>
043: private static final DecimalFormat xRefGenerationFormatter;
044: private static final DecimalFormat xRefOffsetFormatter;
045: // </fields>
046:
047: // <constructors>
048: static {
049: xRefGenerationFormatter = new DecimalFormat("00000");
050: xRefOffsetFormatter = new DecimalFormat("0000000000");
051: }
052: // </constructors>
053: // </static>
054:
055: // <dynamic>
056: // <fields>
057: private PdfIndirectObject indirectObject;
058:
059: private int generationNumber;
060: private int objectNumber;
061:
062: private File file;
063:
064: // </fields>
065:
066: // <constructors>
067: PdfReference(PdfIndirectObject indirectObject, int objectNumber,
068: int generationNumber) {
069: this .indirectObject = indirectObject;
070:
071: this .objectNumber = objectNumber;
072: this .generationNumber = generationNumber;
073: }
074:
075: /**
076: <h3>Remarks</h3>
077: <p>For internal use only.</p>
078: <p>This is a necessary hack because indirect objects are unreachable on parsing bootstrap
079: (see File(IInputStream) constructor).</p>
080: */
081: public PdfReference(Parser.Reference reference, File file) {
082: this .objectNumber = reference.getObjectNumber();
083: this .generationNumber = reference.getGenerationNumber();
084:
085: this .file = file;
086: }
087:
088: // </constructors>
089:
090: // <interface>
091: // <public>
092: public String getCrossReference(long offset) {
093: return (xRefOffsetFormatter.format(offset) + " "
094: + xRefGenerationFormatter.format(generationNumber)
095: + " " + getIndirectObject().getUsage() + "\r\n");
096: }
097:
098: @Override
099: public boolean equals(Object object) {
100: if (!(object instanceof PdfReference))
101: return false;
102:
103: return (getID().equals(((PdfReference) object).getID()));
104: }
105:
106: public int getGenerationNumber() {
107: return generationNumber;
108: }
109:
110: @Override
111: public int hashCode() {
112: return getIndirectObject().hashCode();
113: }
114:
115: public String getID() {
116: return (objectNumber + " " + generationNumber);
117: }
118:
119: public String getIndirectReference() {
120: return (getID() + " R");
121: }
122:
123: public int getObjectNumber() {
124: return objectNumber;
125: }
126:
127: @Override
128: public String toPdf() {
129: return getIndirectReference();
130: }
131:
132: @Override
133: public String toString() {
134: return getIndirectReference();
135: }
136:
137: // <IPdfIndirectObject>
138: @Override
139: public Object clone(File context) {
140: return ((PdfIndirectObject) getIndirectObject().clone(context))
141: .getReference();
142: }
143:
144: public void delete() {
145: getIndirectObject().delete();
146: }
147:
148: public PdfDataObject getDataObject() {
149: return getIndirectObject().getDataObject();
150: }
151:
152: public PdfIndirectObject getIndirectObject() {
153: if (indirectObject == null)
154: indirectObject = file.getIndirectObjects()
155: .get(objectNumber);
156:
157: return indirectObject;
158: }
159:
160: public PdfReference getReference() {
161: return this ;
162: }
163:
164: // </IPdfIndirectObject>
165: // </public>
166:
167: // <internal>
168: @Override
169: int writeTo(IOutputStream stream) {
170: return stream.write(getIndirectReference());
171: }
172: // </internal>
173: // </interface>
174: // </dynamic>
175: // </class>
176: }
|