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 java.util.ArrayList;
033: import java.util.List;
034:
035: /**
036: * A XRef subsection in a pdf document.
037: * <p>
038: * A XRef section consists of at least one subsection. The subsections describe
039: * the objects that have changed with regard to the previous document version.
040: * <p>
041: * Each subsection contains entries for a contiguous range of objects. The
042: * serialized form starts with 2 number, the object number of the first entry
043: * and the number of entries. Following this ther is one line for each enty in
044: * the form
045: *
046: * In Use entry: <code>
047: * offset[10] " " generation[5] " " n eol[2]
048: * </code>
049: *
050: * Free entry: <code>
051: * next free[10] " " generation[5] " " f eol[2]
052: * </code>
053: */
054: public class STXRefSubsection {
055: private STXRefSubsection next;
056:
057: private List entries;
058:
059: private int start;
060:
061: private STXRefSection xRefSection;
062:
063: protected STXRefSubsection(int start) {
064: this (null, start);
065: }
066:
067: public STXRefSubsection(STXRefSection xRefSection, int start) {
068: this .xRefSection = xRefSection;
069: this .start = start;
070: entries = new ArrayList();
071: }
072:
073: protected void addEntry(STXRefEntry entry) {
074: int number = entry.getObjectNumber();
075: if (number < getStart()) {
076: throw new IllegalArgumentException(
077: "can't add object with number " + number);
078: }
079: if (start <= number && number < start + getSize()) {
080: int index = number - start;
081: getEntries().set(index, entry);
082: return;
083: }
084: if (number == (start + getSize())) {
085: // fits to end of list
086: getEntries().add(entry);
087: checkNext();
088: return;
089: }
090: throw new IllegalArgumentException(
091: "can't add object with number " + number);
092: }
093:
094: protected void checkNext() {
095: if (getNext() == null) {
096: return;
097: }
098: if (getSize() == 0) {
099: start = getNext().getStart();
100: mergeWithNext();
101: return;
102: }
103: if ((start + getSize()) == getNext().getStart()) {
104: mergeWithNext();
105: }
106: }
107:
108: protected void mergeWithNext() {
109: getEntries().addAll(getNext().getEntries());
110: setNext(getNext().getNext());
111: }
112:
113: protected List getEntries() {
114: return entries;
115: }
116:
117: protected STXRefSubsection getNext() {
118: return next;
119: }
120:
121: public int getStart() {
122: return start;
123: }
124:
125: public int getStop() {
126: return start + getSize();
127: }
128:
129: protected void setNext(STXRefSubsection next) {
130: this .next = next;
131: }
132:
133: protected STXRefEntry getEntry(int objectNumber) {
134: return (STXRefEntry) getEntries().get(objectNumber - start);
135: }
136:
137: protected boolean isInSection(int objectNumber) {
138: return ((objectNumber >= start) && (objectNumber < (start + getSize())));
139: }
140:
141: public int getSize() {
142: return getEntries().size();
143: }
144:
145: protected STXRefSection getXRefSection() {
146: return xRefSection;
147: }
148:
149: protected void setXRefSection(STXRefSection xRefSection) {
150: this.xRefSection = xRefSection;
151: }
152:
153: }
|