Author: leleueri
Date: Sun Sep 18 17:04:33 2011
New Revision: 1172303

URL: http://svn.apache.org/viewvc?rev=1172303&view=rev
Log:
[PDFBOX-1117] [Preflight] Continue the PDF validation after syntax error

Modified:
    
pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/PdfA1bValidator.java
    
pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/StreamValidationHelper.java

Modified: 
pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/PdfA1bValidator.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/PdfA1bValidator.java?rev=1172303&r1=1172302&r2=1172303&view=diff
==============================================================================
--- 
pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/PdfA1bValidator.java
 (original)
+++ 
pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/PdfA1bValidator.java
 Sun Sep 18 17:04:33 2011
@@ -52,16 +52,18 @@ public class PdfA1bValidator extends Abs
        throws ValidationException {
                DocumentHandler handler = createDocumentHandler(source);
                try {
+                       ArrayList<ValidationError> allErrors = new 
ArrayList<ValidationError>();
+                       
                        // syntax (javacc) validation
                        try {
                                PDFParser parser = new 
PDFParser(source.getInputStream());
-                               parser.PDF();
                                handler.setParser(parser);
+                               parser.PDF();
                        } catch (IOException e) {
                                throw new ValidationException("Failed to parse 
datasource due to : "
                                                + e.getMessage(), e);
                        } catch (ParseException e) {
-                               return createErrorResult(e);
+                                
allErrors.addAll(createErrorResult(e).getErrorsList());
                        }
 
                        // if here is reached, validate with helpers
@@ -85,8 +87,10 @@ public class PdfA1bValidator extends Abs
                                                "PDF ExtractorTokenMng failed 
to parse datasource", e);
                        }
 
-                       // call all helpers
-                       ArrayList<ValidationError> allErrors = new 
ArrayList<ValidationError>();
+                       /* 
+                        * call all helpers
+                        */
+
                
                        // Execute priority helpers.
                        for ( AbstractValidationHelper helper : priorHelpers ) {

Modified: 
pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/StreamValidationHelper.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/StreamValidationHelper.java?rev=1172303&r1=1172302&r2=1172303&view=diff
==============================================================================
--- 
pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/StreamValidationHelper.java
 (original)
+++ 
pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/StreamValidationHelper.java
 Sun Sep 18 17:04:33 2011
@@ -52,8 +52,8 @@ import org.apache.pdfbox.persistence.uti
 public class StreamValidationHelper extends AbstractValidationHelper {
 
        public StreamValidationHelper(ValidatorConfig cfg)
-       throws ValidationException {
-         super(cfg);
+                       throws ValidationException {
+               super(cfg);
        }
 
        /*
@@ -65,7 +65,7 @@ public class StreamValidationHelper exte
         */
        @Override
        public List<ValidationError> innerValidate(DocumentHandler handler)
-       throws ValidationException {
+                       throws ValidationException {
                List<ValidationError> result = new 
ArrayList<ValidationError>(0);
                PDDocument pdfDoc = handler.getDocument();
                COSDocument cDoc = pdfDoc.getDocument();
@@ -131,7 +131,7 @@ public class StreamValidationHelper exte
                        } else {
                                // ---- The filter type is invalid
                                result.add(new 
ValidationError(ERROR_SYNTAX_STREAM_INVALID_FILTER,
-                               "Filter should be a Name or an Array"));
+                                               "Filter should be a Name or an 
Array"));
                        }
                } 
                //  else Filter entry is optional
@@ -201,7 +201,7 @@ public class StreamValidationHelper exte
                                break;
                        default:
                                maybe = false;
-                       break;
+                               break;
                        }
                } while (search);
                return false;
@@ -216,86 +216,94 @@ public class StreamValidationHelper exte
                try {
                        ra = handler.getSource().getInputStream();
                        Integer offset = (Integer) 
handler.getDocument().getDocument()
-                       .getXrefTable().get(new COSObjectKey(cObj));
+                                       .getXrefTable().get(new 
COSObjectKey(cObj));
 
                        // ---- go to the beginning of the object
                        long skipped = 0;
-                       while (skipped != offset) {
-                               long curSkip = ra.skip(offset - skipped);
-                               if (curSkip < 0) {
-                                       throw new ValidationException(
-                                                       "Unable to skip bytes 
in the PDFFile to check stream length");
-                               }
-                               skipped += curSkip;
-                       }
-
-                       // ---- go to the stream key word
-                       if (readUntilStream(ra)) {
-                               int c = ra.read();
-                               if (c == '\r') {
-                                       ra.read();
-                               } // else c is '\n' no more character to read
-
-
-                               // ---- Here is the true beginning of the 
Stream Content.
-                               // ---- Read the given length of bytes and 
check the 10 next bytes
-                               // ---- to see if there are endstream.
-                               byte[] buffer = new byte[1024];
-                               int nbBytesToRead = length;
-
-                               do {
-                                       int cr = 0;
-                                       if (nbBytesToRead > 1024) {
-                                               cr = ra.read(buffer, 0, 1024);
-                                       } else {
-                                               cr = ra.read(buffer, 0, 
nbBytesToRead);
-                                       }
-                                       if (cr == -1) {
-                                               result.add(new 
ValidationResult.ValidationError(
-                                                               
ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
-                                                               "Stream length 
is invalide"));
-                                               return;
-                                       } else {
-                                               nbBytesToRead = nbBytesToRead - 
cr;
+                       if (offset != null) {
+                               while (skipped != offset) {
+                                       long curSkip = ra.skip(offset - 
skipped);
+                                       if (curSkip < 0) {
+                                               throw new ValidationException(
+                                                               "Unable to skip 
bytes in the PDFFile to check stream length");
                                        }
-                               } while (nbBytesToRead > 0);
-
-                               int len = "endstream".length() + 2;
-                               byte[] buffer2 = new byte[len];
-                               for (int i = 0; i < len; ++i) {
-                                       buffer2[i] = (byte) ra.read();
+                                       skipped += curSkip;
                                }
 
-                               // ---- check the content of 10 last characters
-                               String endStream = new String(buffer2);
-                               if (buffer2[0] == '\r' && buffer2[1] == '\n') {
-                                       if (!endStream.contains("endstream")) {
-                                               result.add(new 
ValidationResult.ValidationError(
-                                                               
ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
-                                                               "Stream length 
is invalide"));
-                                       }
-                               } else if (buffer2[0] == '\r' && buffer2[1] == 
'e') {
-                                       if (!endStream.contains("endstream")) {
-                                               result.add(new 
ValidationResult.ValidationError(
-                                                               
ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
-                                                               "Stream length 
is invalide"));
+                               // ---- go to the stream key word
+                               if (readUntilStream(ra)) {
+                                       int c = ra.read();
+                                       if (c == '\r') {
+                                               ra.read();
+                                       } // else c is '\n' no more character 
to read
+
+
+                                       // ---- Here is the true beginning of 
the Stream Content.
+                                       // ---- Read the given length of bytes 
and check the 10 next bytes
+                                       // ---- to see if there are endstream.
+                                       byte[] buffer = new byte[1024];
+                                       int nbBytesToRead = length;
+
+                                       do {
+                                               int cr = 0;
+                                               if (nbBytesToRead > 1024) {
+                                                       cr = ra.read(buffer, 0, 
1024);
+                                               } else {
+                                                       cr = ra.read(buffer, 0, 
nbBytesToRead);
+                                               }
+                                               if (cr == -1) {
+                                                       result.add(new 
ValidationResult.ValidationError(
+                                                                       
ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
+                                                                       "Stream 
length is invalide"));
+                                                       return;
+                                               } else {
+                                                       nbBytesToRead = 
nbBytesToRead - cr;
+                                               }
+                                       } while (nbBytesToRead > 0);
+
+                                       int len = "endstream".length() + 2;
+                                       byte[] buffer2 = new byte[len];
+                                       for (int i = 0; i < len; ++i) {
+                                               buffer2[i] = (byte) ra.read();
                                        }
-                               } else if (buffer2[0] == '\n' && buffer2[1] == 
'e') {
-                                       if (!endStream.contains("endstream")) {
+
+                                       // ---- check the content of 10 last 
characters
+                                       String endStream = new String(buffer2);
+                                       if (buffer2[0] == '\r' && buffer2[1] == 
'\n') {
+                                               if 
(!endStream.contains("endstream")) {
+                                                       result.add(new 
ValidationResult.ValidationError(
+                                                                       
ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
+                                                                       "Stream 
length is invalide"));
+                                               }
+                                       } else if (buffer2[0] == '\r' && 
buffer2[1] == 'e') {
+                                               if 
(!endStream.contains("endstream")) {
+                                                       result.add(new 
ValidationResult.ValidationError(
+                                                                       
ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
+                                                                       "Stream 
length is invalide"));
+                                               }
+                                       } else if (buffer2[0] == '\n' && 
buffer2[1] == 'e') {
+                                               if 
(!endStream.contains("endstream")) {
+                                                       result.add(new 
ValidationResult.ValidationError(
+                                                                       
ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
+                                                                       "Stream 
length is invalide"));
+                                               }
+                                       } else {
                                                result.add(new 
ValidationResult.ValidationError(
                                                                
ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
                                                                "Stream length 
is invalide"));
                                        }
+
                                } else {
                                        result.add(new 
ValidationResult.ValidationError(
                                                        
ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
                                                        "Stream length is 
invalide"));
                                }
-
                        } else {
-                               result.add(new ValidationResult.ValidationError(
-                                               
ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
-                                               "Stream length is invalide"));
+                               /*
+                                * 
+                                * Offset is null. The stream isn't used, check 
is useless.
+                                * 
+                                */
                        }
                } catch (IOException e) {
                        throw new ValidationException(


Reply via email to