You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2012/05/15 21:30:09 UTC

svn commit: r1338849 [3/3] - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFObjectStreamParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFObjectStreamParser.java?rev=1338849&r1=1338848&r2=1338849&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFObjectStreamParser.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFObjectStreamParser.java Tue May 15 19:30:09 2012
@@ -40,7 +40,7 @@ public class PDFObjectStreamParser exten
     /**
      * Log instance.
      */
-    private static final Log log =
+    private static final Log LOG =
         LogFactory.getLog(PDFObjectStreamParser.class);
 
     private List<COSObject> streamObjects = null;
@@ -53,13 +53,14 @@ public class PDFObjectStreamParser exten
      * @since Apache PDFBox 1.3.0
      * @param strm The stream to parse.
      * @param doc The document for the current parsing.
-     * @param forceParcing flag to skip malformed or otherwise unparseable
+     * @param forceParsing flag to skip malformed or otherwise unparseable
      *                     input where possible
      * @throws IOException If there is an error initializing the stream.
      */
     public PDFObjectStreamParser(
             COSStream strm, COSDocument doc, boolean forceParsing)
-            throws IOException {
+            throws IOException 
+    {
        super(strm.getUnfilteredStream(), forceParsing);
        setDocument( doc );
        stream = strm;
@@ -74,7 +75,8 @@ public class PDFObjectStreamParser exten
      * @throws IOException If there is an error initializing the stream.
      */
     public PDFObjectStreamParser(COSStream strm, COSDocument doc)
-            throws IOException {
+            throws IOException 
+    {
         this(strm, doc, FORCE_PARSING);
     }
 
@@ -109,9 +111,9 @@ public class PDFObjectStreamParser exten
                     COSInteger.get( objectNumbers.get( objectCounter).intValue() );
                 object.setObjectNumber( objNum );
                 streamObjects.add( object );
-                if(log.isDebugEnabled())
+                if(LOG.isDebugEnabled())
                 {
-                    log.debug( "parsed=" + object );
+                    LOG.debug( "parsed=" + object );
                 }
                 objectCounter++;
             }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFStreamParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFStreamParser.java?rev=1338849&r1=1338848&r2=1338849&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFStreamParser.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFStreamParser.java Tue May 15 19:30:09 2012
@@ -55,13 +55,14 @@ public class PDFStreamParser extends Bas
      * @since Apache PDFBox 1.3.0
      * @param stream The stream to read data from.
      * @param raf The random access file.
-     * @param forceParcing flag to skip malformed or otherwise unparseable
+     * @param forceParsing flag to skip malformed or otherwise unparseable
      *                     input where possible
      * @throws IOException If there is an error reading from the stream.
      */
     public PDFStreamParser(
             InputStream stream, RandomAccess raf, boolean forceParsing)
-            throws IOException {
+            throws IOException 
+    {
         super(stream, forceParsing);
         file = raf;
     }
@@ -75,7 +76,8 @@ public class PDFStreamParser extends Bas
      * @throws IOException If there is an error reading from the stream.
      */
     public PDFStreamParser(InputStream stream, RandomAccess raf)
-            throws IOException {
+            throws IOException 
+    {
         this(stream, raf, FORCE_PARSING);
     }
 
@@ -96,12 +98,13 @@ public class PDFStreamParser extends Bas
      *
      * @since Apache PDFBox 1.3.0
      * @param stream The stream to parse.
-     * @param forceParcing flag to skip malformed or otherwise unparseable
+     * @param forceParsing flag to skip malformed or otherwise unparseable
      *                     input where possible
      * @throws IOException If there is an error initializing the stream.
      */
     public PDFStreamParser(COSStream stream, boolean forceParsing)
-            throws IOException {
+            throws IOException 
+    {
        this(stream.getUnfilteredStream(), stream.getScratchFile(), forceParsing);
     }
 
@@ -150,6 +153,9 @@ public class PDFStreamParser extends Bas
         return streamObjects;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     public void close() throws IOException
     {
         pdfSource.close();
@@ -190,7 +196,8 @@ public class PDFStreamParser extends Bas
             }
 
             /** {@inheritDoc} */
-            public Object next() {
+            public Object next() 
+            {
                 tryNext();
                 Object tmp = token;
                 if (tmp == null)
@@ -474,7 +481,8 @@ public class PDFStreamParser extends Bas
             nextChar = pdfSource.peek();
             buffer.append( currentChar );
             // Type3 Glyph description has operators with a number in the name
-            if (currentChar == 'd' && (nextChar == '0' || nextChar == '1') ) {
+            if (currentChar == 'd' && (nextChar == '0' || nextChar == '1') ) 
+            {
                 buffer.append( (char)pdfSource.read() );
                 nextChar = pdfSource.peek();
             }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFXrefStreamParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFXrefStreamParser.java?rev=1338849&r1=1338848&r2=1338849&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFXrefStreamParser.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFXrefStreamParser.java Tue May 15 19:30:09 2012
@@ -49,19 +49,19 @@ public class PDFXrefStreamParser extends
      * @param doc The document for the current parsing.
      * @param forceParsing flag to skip malformed or otherwise unparseable
      *                     input where possible
-     * @param xrefTrailerResolver resolver to read the xref/trailer information
+     * @param resolver resolver to read the xref/trailer information
      *
      * @throws IOException If there is an error initializing the stream.
      */
     public PDFXrefStreamParser(
             COSStream strm, COSDocument doc, boolean forceParsing,
-            XrefTrailerResolver xrefTrailerResolver )
+            XrefTrailerResolver resolver )
             throws IOException
     {
         super(strm.getUnfilteredStream(), forceParsing);
         setDocument(doc);
         stream = strm;
-        this.xrefTrailerResolver = xrefTrailerResolver;
+        this.xrefTrailerResolver = resolver;
     }
 
     /**
@@ -163,11 +163,11 @@ public class PDFXrefStreamParser extends
                          * table but add object stream number with minus sign in order to
                          * distinguish from file offsets
                          */
-	                      int objstmObjNr = 0;
-	                      for(int i = 0; i < w1; i++)
-	                      {
-	                      		objstmObjNr += (currLine[i + w0] & 0x00ff) << ((w1 - i - 1) * 8);
-	                      }
+                        int objstmObjNr = 0;
+                        for(int i = 0; i < w1; i++)
+                        {
+                            objstmObjNr += (currLine[i + w0] & 0x00ff) << ((w1 - i - 1) * 8);
+                        }    
                         objKey = new COSObjectKey( objID.intValue(), 0 );
                         xrefTrailerResolver.setXRef( objKey, -objstmObjNr );
                         break;

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/VisualSignatureParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/VisualSignatureParser.java?rev=1338849&r1=1338848&r2=1338849&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/VisualSignatureParser.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/VisualSignatureParser.java Tue May 15 19:30:09 2012
@@ -29,51 +29,74 @@ import org.apache.pdfbox.cos.COSObject;
 import org.apache.pdfbox.pdfwriter.COSWriter;
 import org.apache.pdfbox.persistence.util.COSObjectKey;
 
-public class VisualSignatureParser extends BaseParser {
+public class VisualSignatureParser extends BaseParser 
+{
 
     /**
      * Log instance.
      */
-    private static final Log log = LogFactory.getLog(PDFParser.class);
+    private static final Log LOG = LogFactory.getLog(PDFParser.class);
 
-    public VisualSignatureParser(InputStream input) throws IOException {
+    /**
+     * Constructor.
+     * 
+     * @param input the inputstream to be read.
+     * 
+     * @throws IOException If something went wrong
+     */
+    public VisualSignatureParser(InputStream input) throws IOException 
+    {
         super(input);
     }
 
-    public void parse() throws IOException {
+    /**
+     * {@inheritDoc}
+     */
+    public void parse() throws IOException 
+    {
         document = new COSDocument();
         skipToNextObj();
 
         boolean wasLastParsedObjectEOF = false;
-        try {
-            while(!wasLastParsedObjectEOF) {
-                if(pdfSource.isEOF()) {
+        try 
+        {
+            while(!wasLastParsedObjectEOF) 
+            {
+                if(pdfSource.isEOF()) 
+                {
                     break;
                 }
-                try {
+                try 
+                {
                     wasLastParsedObjectEOF = parseObject();
-                } catch(IOException e) {
+                } 
+                catch(IOException e) 
+                {
                     /*
                      * Warning is sent to the PDFBox.log and to the Console that
                      * we skipped over an object
                      */
-                    log.warn("Parsing Error, Skipping Object", e);
+                    LOG.warn("Parsing Error, Skipping Object", e);
                     skipToNextObj();
                 }
                 skipSpaces();
             }
-        } catch(IOException e) {
+        } 
+        catch(IOException e) 
+        {
             /*
              * PDF files may have random data after the EOF marker. Ignore errors if
              * last object processed is EOF.
              */
-            if(!wasLastParsedObjectEOF) {
+            if(!wasLastParsedObjectEOF) 
+            {
                 throw e;
             }
         }
     }
 
-    private void skipToNextObj() throws IOException {
+    private void skipToNextObj() throws IOException 
+    {
         byte[] b = new byte[16];
         Pattern p = Pattern.compile("\\d+\\s+\\d+\\s+obj.*", Pattern.DOTALL);
         /* Read a buffer of data each time to see if it starts with a
@@ -81,9 +104,11 @@ public class VisualSignatureParser exten
          * rarely be needing this function. We could update this to use the
          * circular buffer, like in readUntilEndStream().
          */
-        while(!pdfSource.isEOF()) {
+        while(!pdfSource.isEOF()) 
+        {
             int l = pdfSource.read(b);
-            if(l < 1) {
+            if(l < 1) 
+            {
                 break;
             }
             String s = new String(b, "US-ASCII");
@@ -91,80 +116,105 @@ public class VisualSignatureParser exten
                     || s.startsWith("xref")
                     || s.startsWith("startxref")
                     || s.startsWith("stream")
-                    || p.matcher(s).matches()) {
+                    || p.matcher(s).matches()) 
+            {
                 pdfSource.unread(b);
                 break;
-            } else {
+            } 
+            else 
+            {
                 pdfSource.unread(b, 1, l - 1);
             }
         }
     }
 
-    private boolean parseObject() throws IOException {
+    private boolean parseObject() throws IOException 
+    {
         boolean isEndOfFile = false;
         skipSpaces();
         //peek at the next character to determine the type of object we are parsing
         char peekedChar = (char) pdfSource.peek();
 
         //ignore endobj and endstream sections.
-        while(peekedChar == 'e') {
+        while(peekedChar == 'e') 
+        {
             //there are times when there are multiple endobj, so lets
             //just read them and move on.
             readString();
             skipSpaces();
             peekedChar = (char) pdfSource.peek();
         }
-        if(pdfSource.isEOF()) {
+        if(pdfSource.isEOF()) 
+        {
             // end of file we will return a false and call it a day.
-        } else if(peekedChar == 'x') {
+        } 
+        else if(peekedChar == 'x') 
+        {
             //xref table. Note: The contents of the Xref table are currently ignored
             return true;
-        } else if(peekedChar == 't' || peekedChar == 's') {
+        } 
+        else if(peekedChar == 't' || peekedChar == 's') 
+        {
             // Note: startxref can occur in either a trailer section or by itself
-            if(peekedChar == 't') {
+            if(peekedChar == 't') 
+            {
                 return true;
             }
-            if(peekedChar == 's') {
+            if(peekedChar == 's') 
+            {
                 skipToNextObj();
                 //verify that EOF exists
                 String eof = readExpectedString("%%EOF");
-                if(eof.indexOf("%%EOF") == -1 && !pdfSource.isEOF()) {
+                if(eof.indexOf("%%EOF") == -1 && !pdfSource.isEOF()) 
+                {
                     throw new IOException("expected='%%EOF' actual='" + eof + "' next=" + readString()
                             + " next=" + readString());
                 }
                 isEndOfFile = true;
             }
-        } else {
+        } 
+        else 
+        {
             //we are going to parse an normal object
             int number = -1;
             int genNum = -1;
             String objectKey = null;
             boolean missingObjectNumber = false;
-            try {
+            try 
+            {
                 char peeked = (char) pdfSource.peek();
-                if(peeked == '<') {
+                if(peeked == '<') 
+                {
                     missingObjectNumber = true;
-                } else {
+                } 
+                else 
+                {
                     number = readInt();
                 }
-            } catch(IOException e) {
+            } 
+            catch(IOException e) 
+            {
                 //ok for some reason "GNU Ghostscript 5.10" puts two endobj
                 //statements after an object, of course this is nonsense
                 //but because we want to support as many PDFs as possible
                 //we will simply try again
                 number = readInt();
             }
-            if(!missingObjectNumber) {
+            if(!missingObjectNumber)
+            {
                 skipSpaces();
                 genNum = readInt();
 
                 objectKey = readString(3);
                 //System.out.println( "parseObject() num=" + number +
                 //" genNumber=" + genNum + " key='" + objectKey + "'" );
-                if(!objectKey.equals("obj")) {
+                if(!objectKey.equals("obj")) 
+                {
                     throw new IOException("expected='obj' actual='" + objectKey + "' " + pdfSource);
                 }
-            } else {
+            } 
+            else 
+            {
                 number = -1;
                 genNum = -1;
             }
@@ -173,13 +223,17 @@ public class VisualSignatureParser exten
             COSBase pb = parseDirObject();
             String endObjectKey = readString();
 
-            if(endObjectKey.equals("stream")) {
+            if(endObjectKey.equals("stream")) 
+            {
                 pdfSource.unread(endObjectKey.getBytes());
                 pdfSource.unread(' ');
-                if(pb instanceof COSDictionary) {
+                if(pb instanceof COSDictionary) 
+                {
                     pb = parseCOSStream((COSDictionary) pb, getDocument().getScratchFile());
 
-                } else {
+                } 
+                else 
+                {
                     // this is not legal
                     // the combination of a dict and the stream/endstream forms a complete stream object
                     throw new IOException("stream not preceded by dictionary");
@@ -192,8 +246,10 @@ public class VisualSignatureParser exten
             pb.setNeedToBeUpdate(true);
             pdfObject.setObject(pb);
 
-            if(!endObjectKey.equals("endobj")) {
-                if(endObjectKey.startsWith("endobj")) {
+            if(!endObjectKey.equals("endobj")) 
+            {
+                if(endObjectKey.startsWith("endobj")) 
+                {
                     /*
                      * Some PDF files don't contain a new line after endobj so we
                      * need to make sure that the next object number is getting read separately
@@ -201,19 +257,26 @@ public class VisualSignatureParser exten
                      * instead of "endobj"
                      */
                     pdfSource.unread(endObjectKey.substring(6).getBytes());
-                } else if(!pdfSource.isEOF()) {
-                    try {
+                } 
+                else if(!pdfSource.isEOF()) 
+                {
+                    try 
+                    {
                         //It is possible that the endobj  is missing, there
                         //are several PDFs out there that do that so skip it and move on.
                         Float.parseFloat(endObjectKey);
                         pdfSource.unread(COSWriter.SPACE);
                         pdfSource.unread(endObjectKey.getBytes());
-                    } catch(NumberFormatException e) {
+                    } 
+                    catch(NumberFormatException e) 
+                    {
                         //we will try again incase there was some garbage which
                         //some writers will leave behind.
                         String secondEndObjectKey = readString();
-                        if(!secondEndObjectKey.equals("endobj")) {
-                            if(isClosing()) {
+                        if(!secondEndObjectKey.equals("endobj")) 
+                        {
+                            if(isClosing()) 
+                            {
                                 //found a case with 17506.pdf object 41 that was like this
                                 //41 0 obj [/Pattern /DeviceGray] ] endobj
                                 //notice the second array close, here we are reading it
@@ -222,7 +285,8 @@ public class VisualSignatureParser exten
                             }
                             skipSpaces();
                             String thirdPossibleEndObj = readString();
-                            if(!thirdPossibleEndObj.equals("endobj")) {
+                            if(!thirdPossibleEndObj.equals("endobj")) 
+                            {
                                 throw new IOException("expected='endobj' firstReadAttempt='" + endObjectKey + "' "
                                         + "secondReadAttempt='" + secondEndObjectKey + "' " + pdfSource);
                             }
@@ -235,8 +299,17 @@ public class VisualSignatureParser exten
         return isEndOfFile;
     }
 
-    public COSDocument getDocument() throws IOException {
-        if(document == null) {
+    /**
+     * Returns the underlying COSDocument.
+     * 
+     * @return the COSDocument
+     * 
+     * @throws IOException If something went wrong
+     */
+    public COSDocument getDocument() throws IOException 
+    {
+        if(document == null) 
+        {
             throw new IOException("You must call parse() before calling getDocument()");
         }
         return document;

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/XrefTrailerResolver.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/XrefTrailerResolver.java?rev=1338849&r1=1338848&r2=1338849&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/XrefTrailerResolver.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/XrefTrailerResolver.java Tue May 15 19:30:09 2012
@@ -122,9 +122,13 @@ public class XrefTrailerResolver
 
     /**
      * Returns the trailer last set by {@link #setTrailer(COSDictionary)}.
+     * 
+     * @return the current trailer.
+     * 
      */
-    public COSDictionary getCurrentTrailer() {
-    		return curXrefTrailerObj.trailer;
+    public COSDictionary getCurrentTrailer() 
+    {
+        return curXrefTrailerObj.trailer;
     }
 
     /**
@@ -137,6 +141,9 @@ public class XrefTrailerResolver
      * in byte position order.
      * Thus for incomplete PDF documents with missing
      * startxref one could call this method with parameter value -1.
+     * 
+     * @param startxrefBytePosValue starting position of the first XRef
+     * 
      */
     public void setStartxref( long startxrefBytePosValue )
     {
@@ -242,17 +249,20 @@ public class XrefTrailerResolver
      */
     public Set<Long> getContainedObjectNumbers( final int objstmObjNr ) 
     {
-    		if ( resolvedXrefTrailer == null )
-    			return null;
-    		
-    		final Set<Long> refObjNrs = new HashSet<Long>();
-    		final int       cmpVal    = - objstmObjNr;
-    		
-    		for ( Entry<COSObjectKey,Long> xrefEntry : resolvedXrefTrailer.xrefTable.entrySet() ) {
-						if ( xrefEntry.getValue() == cmpVal )
-							refObjNrs.add( xrefEntry.getKey().getNumber() );
-				}
-    		
-    		return refObjNrs;
+        if ( resolvedXrefTrailer == null )
+        {
+            return null;
+        }
+        final Set<Long> refObjNrs = new HashSet<Long>();
+        final int       cmpVal    = - objstmObjNr;
+        
+        for ( Entry<COSObjectKey,Long> xrefEntry : resolvedXrefTrailer.xrefTable.entrySet() ) 
+        {
+            if ( xrefEntry.getValue() == cmpVal )
+            {
+                refObjNrs.add( xrefEntry.getKey().getNumber() );
+            }
+        }
+        return refObjNrs;
     }
 }