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.io.IOException;
033: import java.util.Iterator;
034:
035: import de.intarsys.pdf.writer.COSWriter;
036: import de.intarsys.tools.randomaccess.IRandomAccess;
037: import de.intarsys.tools.string.StringTools;
038:
039: /**
040: * An abstract superclass for implementing a XRef serializer.
041: */
042: public abstract class AbstractXRefWriter implements IXRefEntryVisitor {
043: private COSWriter cosWriter;
044:
045: private IRandomAccess randomAccess;
046:
047: public static final byte[] STARTXREF = "startxref".getBytes(); //$NON-NLS-1$
048:
049: protected IRandomAccess getRandomAccess() {
050: return randomAccess;
051: }
052:
053: protected void setRandomAccess(IRandomAccess randomAccess) {
054: this .randomAccess = randomAccess;
055: }
056:
057: public AbstractXRefWriter(COSWriter cosWriter) {
058: this .cosWriter = cosWriter;
059: }
060:
061: protected void finish(STXRefSection xRefSection) throws IOException {
062: writeStartXRef(xRefSection.getOffset());
063: }
064:
065: protected void writeStartXRef(long offset) throws IOException {
066: getCosWriter().write(AbstractXRefWriter.STARTXREF);
067: getCosWriter().writeEOL();
068: getCosWriter().write(
069: StringTools.toByteArray(Long.toString(offset)));
070: getCosWriter().writeEOL();
071: }
072:
073: public COSWriter getCosWriter() {
074: return cosWriter;
075: }
076:
077: protected abstract byte[] getTypeCompressed();
078:
079: protected abstract byte[] getTypeFree();
080:
081: protected abstract byte[] getTypeOccupied();
082:
083: protected void initialize(STXRefSection xRefSection)
084: throws IOException {
085: xRefSection.setOffset(getCosWriter().getRandomAccess()
086: .getOffset());
087: int size = xRefSection.getMaxObjectNumber();
088: if (xRefSection.getPrevious() != null) {
089: size = Math.max(xRefSection.getPrevious().getSize(), size);
090: }
091: xRefSection.setSize(size);
092: }
093:
094: protected void visitFromSection(STXRefSection xRefSection)
095: throws IOException {
096: Iterator i = xRefSection.subsectionIterator();
097: while (i.hasNext()) {
098: STXRefSubsection section = (STXRefSubsection) i.next();
099: visitFromSubsection(section);
100: for (Iterator ie = section.getEntries().iterator(); ie
101: .hasNext();) {
102: try {
103: ((STXRefEntry) ie.next()).accept(this );
104: } catch (XRefEntryVisitorException e) {
105: // in this context the exception type is always an
106: // IOException
107: throw (IOException) e.getCause();
108: }
109: }
110: }
111: }
112:
113: public void visitFromCompressed(STXRefEntryCompressed entry)
114: throws XRefEntryVisitorException {
115: try {
116: write(entry.getStreamObjectNumber(), entry.getIndex(),
117: getTypeCompressed());
118: } catch (IOException e) {
119: throw new XRefEntryVisitorException(e);
120: }
121: }
122:
123: public void visitFromFree(STXRefEntryFree entry)
124: throws XRefEntryVisitorException {
125: try {
126: write(entry.getNextFreeObjectNumber(), entry
127: .getGenerationNumber(), getTypeFree());
128: } catch (IOException e) {
129: throw new XRefEntryVisitorException(e);
130: }
131: }
132:
133: public void visitFromOccupied(STXRefEntryOccupied entry)
134: throws XRefEntryVisitorException {
135: try {
136: write((int) entry.getOffset(), entry.getGenerationNumber(),
137: getTypeOccupied());
138: } catch (IOException e) {
139: throw new XRefEntryVisitorException(e);
140: }
141: }
142:
143: protected abstract void visitFromSubsection(STXRefSubsection section)
144: throws IOException;
145:
146: protected abstract void write(int col1, int col2, byte[] type)
147: throws IOException;
148:
149: public void writeXRef(STXRefSection xRefSection) throws IOException {
150: initialize(xRefSection);
151: visitFromSection(xRefSection);
152: finish(xRefSection);
153: }
154: }
|