001: package com.etymon.pjx.util;
002:
003: import com.etymon.pjx.*;
004:
005: /**
006: Modifies indirect references within a PDF object, adding a
007: specified offset to each object number in a reference. {@link
008: PdfObjectFilter PdfObjectFilter} is used to filter the indirect
009: references. This class is synchronized.
010: @author Nassib Nassar
011: */
012: public class PdfRenumberOffset implements PdfObjectFilter {
013:
014: /**
015: The offset value to add to each object number.
016: */
017: protected int _offset;
018:
019: /**
020: Controls whether generation numbers will be set to 0 during
021: the renumbering process. If so, the value is
022: <code>true</code>.
023: */
024: protected boolean _resetG;
025:
026: /**
027: Constructs a <code>PdfRenumberOffset</code> instance. By
028: default, generation numbers will not be modified (see
029: {@link #resetGeneration(boolean)
030: resetGeneration(boolean)}). The offset value defaults to 0
031: (see {@link #setOffset(int) setOffset(int)}).
032: */
033: public PdfRenumberOffset() {
034:
035: _offset = 0;
036: _resetG = false;
037:
038: }
039:
040: /**
041: Sets the offset value to add to each object number during
042: renumbering.
043: @param offset the offset to use.
044: */
045: public void setOffset(int offset) {
046: synchronized (this ) {
047:
048: _offset = offset;
049:
050: }
051: }
052:
053: /**
054: Controls whether generation numbers will be set to 0 during
055: the renumbering process.
056: @param reset if <code>true</code>, generation numbers will
057: be set to 0; otherwise they are not modified.
058: */
059: public void resetGeneration(boolean reset) {
060: synchronized (this ) {
061:
062: _resetG = reset;
063:
064: }
065: }
066:
067: /**
068: Adds an offset to the object number in each {@link
069: PdfReference PdfReference} within the specified object.
070: The generation number may optionally be reset to 0 (see
071: {@link #resetGeneration(boolean)
072: resetGeneration(boolean)}). The offset is specified with
073: {@link #setOffset(int) setOffset(int)}.
074: @param obj the object to renumber.
075: @return the renumbered object.
076: @throws PdfFormatException
077: */
078: public PdfObject renumber(PdfObject obj) throws PdfFormatException {
079: synchronized (this ) {
080:
081: return obj.filter(this );
082:
083: }
084: }
085:
086: /**
087: This method is used by {@link #renumber(PdfObject)
088: renumber(PdfObject)} to filter indirect references and
089: <b>should not be called externally</b>. (It is not
090: synchronized.)
091: @param obj the object to filter.
092: @return the filtered object.
093: @throws PdfFormatException
094: */
095: public PdfObject preFilter(PdfObject obj) throws PdfFormatException {
096: if (obj instanceof PdfReference) {
097: PdfReference r = (PdfReference) obj;
098: return new PdfReference(r.getObjectNumber() + _offset,
099: _resetG ? 0 : r.getGenerationNumber());
100: } else {
101: return obj;
102: }
103: }
104:
105: /**
106: This method is used by {@link #renumber(PdfObject)
107: renumber(PdfObject)} to filter indirect references and
108: <b>should not be called externally</b>. (It is not
109: synchronized.)
110: @param obj the object to filter.
111: @return the filtered object.
112: @throws PdfFormatException
113: */
114: public PdfObject postFilter(PdfObject obj)
115: throws PdfFormatException {
116: return obj;
117: }
118:
119: }
|