001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.st;
031:
032: import de.intarsys.pdf.cos.COSObject;
033: import de.intarsys.pdf.cos.COSObjectKey;
034: import de.intarsys.pdf.crypt.ISystemSecurityHandler;
035:
036: /**
037: * Represents a free object entry in a pdf xref table.
038: *
039: */
040: public class STXRefEntryFree extends STXRefEntry {
041: /** a link to the next free XRef entry */
042: private STXRefEntryFree next = this ;
043:
044: /** a link to the prev free XRef entry */
045: private STXRefEntryFree prev = this ;
046:
047: private int nextFreeObject;
048:
049: public STXRefEntryFree(COSObjectKey key, int nextFreeObject) {
050: super (key);
051: this .nextFreeObject = nextFreeObject;
052: }
053:
054: public long getColumn1() {
055: return getNextFreeObjectNumber();
056: }
057:
058: public int getColumn2() {
059: return getGenerationNumber();
060: }
061:
062: public boolean isFree() {
063: return true;
064: }
065:
066: public int getNextFreeObjectNumber() {
067: return nextFreeObject;
068: }
069:
070: /**
071: * Add a new free entry in the linked list of free entries. The linked list
072: * is formed by the head entry with index 0. This entry and all subsequent
073: * hold a reference to their successor and predecessor. Adding a new entry
074: * means that we look up the entry in the linked list, starting at the head,
075: * that has the highest index smaller than the index of the new entry. The
076: * new entry is inserted after that position in the linked list.
077: *
078: * @param entry
079: * The new entry to insert in the linked list.
080: */
081: public void enqueue(STXRefEntryFree entry) {
082: int prevIndex = getPrev().getObjectNumber();
083: if ((prevIndex == 0) || (prevIndex < entry.getObjectNumber())) {
084: entry.setNext(this );
085: entry.setPrev(getPrev());
086: getPrev().setNext(entry);
087: this .setPrev(entry);
088: } else {
089: getPrev().enqueue(entry);
090: }
091: }
092:
093: public STXRefEntryOccupied fill(int pos) {
094: unlink();
095:
096: /*
097: * STXRefEntry newEntry = STXRefEntryOccupied.create( getObjectNumber(),
098: * pos, getGenerationNumber() ); return newEntry.fill(pos);
099: */
100: return null;
101: }
102:
103: protected STXRefEntryFree getNext() {
104: return next;
105: }
106:
107: protected STXRefEntryFree getPrev() {
108: return prev;
109: }
110:
111: /*
112: * (non-Javadoc)
113: *
114: * @see de.intarsys.pdf.writer.XRefEntry#unlink()
115: */
116: protected void unlink() {
117: getPrev().setNext(getNext());
118: getNext().setPrev(getPrev());
119: setPrev(this );
120: setNext(this );
121: }
122:
123: private void setNext(STXRefEntryFree free) {
124: next = free;
125: }
126:
127: private void setPrev(STXRefEntryFree free) {
128: prev = free;
129: }
130:
131: /*
132: * (non-Javadoc)
133: *
134: * @see de.intarsys.pdf.storage.STXRefEntry#loadObject(de.intarsys.pdf.cos.COSIndirectObject)
135: */
136: public COSObject load(STDocument doc,
137: ISystemSecurityHandler securityHandler) {
138: return null;
139: }
140:
141: /*
142: * (non-Javadoc)
143: *
144: * @see de.intarsys.pdf.storage.STXRefEntry#accept(de.intarsys.pdf.storage.IXRefEntryVisitor)
145: */
146: public void accept(IXRefEntryVisitor visitor)
147: throws XRefEntryVisitorException {
148: visitor.visitFromFree(this );
149: }
150:
151: /*
152: * (non-Javadoc)
153: *
154: * @see de.intarsys.pdf.storage.STXRefEntry#copy()
155: */
156: public STXRefEntry copy() {
157: return new STXRefEntryFree(getKey(), getNextFreeObjectNumber());
158: }
159: }
|