001: /**
002: * Copyright (c) 2003-2004, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. 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: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdfparser;
031:
032: import java.io.IOException;
033:
034: import java.util.ArrayList;
035: import java.util.List;
036:
037: import org.pdfbox.cos.COSBase;
038: import org.pdfbox.cos.COSDocument;
039: import org.pdfbox.cos.COSInteger;
040: import org.pdfbox.cos.COSObject;
041: import org.pdfbox.cos.COSStream;
042:
043: /**
044: * This will parse a PDF 1.5 object stream and extract all of the objects from the stream.
045: *
046: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
047: * @version $Revision: 1.6 $
048: */
049: public class PDFObjectStreamParser extends BaseParser {
050: private List streamObjects = null;
051: private List objectNumbers = null;
052: private COSStream stream;
053:
054: /**
055: * Constructor.
056: *
057: * @param strm The stream to parse.
058: * @param doc The document for the current parsing.
059: *
060: * @throws IOException If there is an error initializing the stream.
061: */
062: public PDFObjectStreamParser(COSStream strm, COSDocument doc)
063: throws IOException {
064: super (strm.getUnfilteredStream());
065: setDocument(doc);
066: stream = strm;
067: }
068:
069: /**
070: * This will parse the tokens in the stream. This will close the
071: * stream when it is finished parsing.
072: *
073: * @throws IOException If there is an error while parsing the stream.
074: */
075: public void parse() throws IOException {
076: try {
077: //need to first parse the header.
078: int numberOfObjects = stream.getInt("N");
079: objectNumbers = new ArrayList(numberOfObjects);
080: streamObjects = new ArrayList(numberOfObjects);
081: for (int i = 0; i < numberOfObjects; i++) {
082: int objectNumber = readInt();
083: int offset = readInt();
084: objectNumbers.add(new Integer(objectNumber));
085: }
086: COSObject object = null;
087: COSBase cosObject = null;
088: int objectCounter = 0;
089: while ((cosObject = parseDirObject()) != null) {
090: object = new COSObject(cosObject);
091: object.setGenerationNumber(COSInteger.ZERO);
092: COSInteger objNum = new COSInteger(
093: ((Integer) objectNumbers.get(objectCounter))
094: .intValue());
095: object.setObjectNumber(objNum);
096: streamObjects.add(object);
097: objectCounter++;
098: }
099: } finally {
100: pdfSource.close();
101: }
102: }
103:
104: /**
105: * This will get the objects that were parsed from the stream.
106: *
107: * @return All of the objects in the stream.
108: */
109: public List getObjects() {
110: return streamObjects;
111: }
112: }
|