001: /*
002: * $Id: PDFXref.java,v 1.2 2007/12/20 18:17:41 rbair Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021:
022: package com.sun.pdfview;
023:
024: import java.lang.ref.SoftReference;
025:
026: /**
027: * a cross reference representing a line in the PDF cross referencing
028: * table.
029: * <p>
030: * There are two forms of the PDFXref, destinguished by absolutely nothing.
031: * The first type of PDFXref is used as indirect references in a PDFObject.
032: * In this type, the id is an index number into the object cross reference
033: * table. The id will range from 0 to the size of the cross reference
034: * table.
035: * <p>
036: * The second form is used in the Java representation of the cross reference
037: * table. In this form, the id is the file position of the start of the
038: * object in the PDF file. See the use of both of these in the
039: * PDFFile.dereference() method, which takes a PDFXref of the first form,
040: * and uses (internally) a PDFXref of the second form.
041: * <p>
042: * This is an unhappy state of affairs, and should be fixed. Fortunatly,
043: * the two uses have already been factored out as two different methods.
044: *
045: * @author Mike Wessler
046: */
047: public class PDFXref {
048: private int id;
049: private int generation;
050:
051: // this field is only used in PDFFile.objIdx
052: private SoftReference reference = null;
053:
054: /**
055: * create a new PDFXref, given a parsed id and generation.
056: */
057: public PDFXref(int id, int gen) {
058: this .id = id;
059: this .generation = gen;
060: }
061:
062: /**
063: * create a new PDFXref, given a sequence of bytes representing the
064: * fixed-width cross reference table line
065: */
066: public PDFXref(byte[] line) {
067: if (line == null) {
068: id = -1;
069: generation = -1;
070: } else {
071: id = Integer.parseInt(new String(line, 0, 10));
072: generation = Integer.parseInt(new String(line, 11, 5));
073: }
074: }
075:
076: /**
077: * get the character index into the file of the start of this object
078: */
079: public int getFilePos() {
080: return id;
081: }
082:
083: /**
084: * get the generation of this object
085: */
086: public int getGeneration() {
087: return generation;
088: }
089:
090: /**
091: * get the object number of this object
092: */
093: public int getID() {
094: return id;
095: }
096:
097: /**
098: * Get the object this reference refers to, or null if it hasn't been
099: * set.
100: * @return the object if it exists, or null if not
101: */
102: public PDFObject getObject() {
103: if (reference != null) {
104: return (PDFObject) reference.get();
105: }
106:
107: return null;
108: }
109:
110: /**
111: * Set the object this reference refers to.
112: */
113: public void setObject(PDFObject obj) {
114: this .reference = new SoftReference(obj);
115: }
116: }
|