You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2020/03/31 19:40:58 UTC

svn commit: r1875963 - in /pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight: font/util/FontMetaDataValidation.java metadata/SynchronizedMetaDataValidation.java

Author: tilman
Date: Tue Mar 31 19:40:58 2020
New Revision: 1875963

URL: http://svn.apache.org/viewvc?rev=1875963&view=rev
Log:
PDFBOX-4803: catch BadFieldValueException

Modified:
    pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/font/util/FontMetaDataValidation.java
    pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/metadata/SynchronizedMetaDataValidation.java

Modified: pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/font/util/FontMetaDataValidation.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/font/util/FontMetaDataValidation.java?rev=1875963&r1=1875962&r2=1875963&view=diff
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/font/util/FontMetaDataValidation.java (original)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/font/util/FontMetaDataValidation.java Tue Mar 31 19:40:58 2020
@@ -35,6 +35,7 @@ import org.apache.xmpbox.schema.DublinCo
 import org.apache.xmpbox.schema.XMPRightsManagementSchema;
 import org.apache.xmpbox.type.AbstractField;
 import org.apache.xmpbox.type.ArrayProperty;
+import org.apache.xmpbox.type.BadFieldValueException;
 import org.apache.xmpbox.type.BooleanType;
 import org.apache.xmpbox.type.TextType;
 
@@ -77,7 +78,18 @@ public class FontMetaDataValidation
         DublinCoreSchema dc = metadata.getDublinCoreSchema();
         if (dc != null && dc.getTitleProperty() != null)
         {
-            String defaultTitle = dc.getTitle("x-default");
+            String defaultTitle;
+            try
+            {
+                defaultTitle = dc.getTitle("x-default");
+            }
+            catch (BadFieldValueException badFieldValueException)
+            {
+                StringBuilder sb = new StringBuilder(80);
+                sb.append("Title property of XMP information is not a multi-lingual property");
+                ve.add(new ValidationError(PreflightConstants.ERROR_METADATA_PROPERTY_FORMAT, sb.toString()));
+                return false;
+            }
             if (defaultTitle != null)
             {
                 if (!defaultTitle.equals(fontName) && (noSubSetName != null && !defaultTitle.equals(noSubSetName)))

Modified: pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/metadata/SynchronizedMetaDataValidation.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/metadata/SynchronizedMetaDataValidation.java?rev=1875963&r1=1875962&r2=1875963&view=diff
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/metadata/SynchronizedMetaDataValidation.java (original)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/metadata/SynchronizedMetaDataValidation.java Tue Mar 31 19:40:58 2020
@@ -40,6 +40,7 @@ import org.apache.xmpbox.schema.AdobePDF
 import org.apache.xmpbox.schema.DublinCoreSchema;
 import org.apache.xmpbox.schema.XMPBasicSchema;
 import org.apache.xmpbox.type.AbstractField;
+import org.apache.xmpbox.type.BadFieldValueException;
 import org.apache.xmpbox.type.TextType;
 
 /**
@@ -67,47 +68,54 @@ public class SynchronizedMetaDataValidat
             title = removeTrailingNul(title);
             if (dc != null)
             {
-                // Check the x-default value, if not found, check with the first value found
-                if (dc.getTitle() != null)
+                try
                 {
-                    if (dc.getTitle("x-default") != null)
+                    // Check the x-default value, if not found, check with the first value found
+                    if (dc.getTitle() != null)
                     {
-                        if (!dc.getTitle("x-default").equals(title))
+                        if (dc.getTitle("x-default") != null)
                         {
-                            ve.add(unsynchronizedMetaDataError("Title"));
+                            if (!dc.getTitle("x-default").equals(title))
+                            {
+                                ve.add(unsynchronizedMetaDataError("Title"));
+                            }
                         }
-                    }
-                    else
-                    {
-                        // This search of first value is made just to keep compatibility
-                        // with lot of PDF documents
-                        // which use title without lang definition
-                        // REM : MAY we have to delete this option in the future
-                        Iterator<AbstractField> it = dc.getTitleProperty().getContainer().getAllProperties().iterator();
-                        if (it.hasNext())
+                        else
                         {
-                            AbstractField tmp = it.next();
-                            if (tmp instanceof TextType)
+                            // This search of first value is made just to keep compatibility
+                            // with lot of PDF documents
+                            // which use title without lang definition
+                            // REM : MAY we have to delete this option in the future
+                            Iterator<AbstractField> it = dc.getTitleProperty().getContainer().getAllProperties().iterator();
+                            if (it.hasNext())
                             {
-                                if (!((TextType) tmp).getStringValue().equals(title))
+                                AbstractField tmp = it.next();
+                                if (tmp instanceof TextType)
+                                {
+                                    if (!((TextType) tmp).getStringValue().equals(title))
+                                    {
+                                        ve.add(unsynchronizedMetaDataError("Title"));
+                                    }
+                                }
+                                else
                                 {
-                                    ve.add(unsynchronizedMetaDataError("Title"));
+                                    ve.add(absentXMPPropertyError("Title", "Property is badly defined"));
                                 }
                             }
                             else
                             {
-                                ve.add(absentXMPPropertyError("Title", "Property is badly defined"));
+                                ve.add(absentXMPPropertyError("Title", "Property is not defined"));
                             }
                         }
-                        else
-                        {
-                            ve.add(absentXMPPropertyError("Title", "Property is not defined"));
-                        }
+                    }
+                    else
+                    {
+                        ve.add(absentXMPPropertyError("Title", "Property is not defined"));
                     }
                 }
-                else
+                catch (BadFieldValueException ex)
                 {
-                    ve.add(absentXMPPropertyError("Title", "Property is not defined"));
+                    ve.add(badFieldXMPPropertyError("Title", ex.getMessage()));
                 }
             }
             else
@@ -189,19 +197,26 @@ public class SynchronizedMetaDataValidat
                 // as a Text type embedded in the dc:description["x-default"].
                 if (dc.getDescriptionProperty() != null)
                 {
-                    if (dc.getDescription("x-default") == null)
+                    try
                     {
-                        ve.add(absentXMPPropertyError("Subject",
-                                "Subject not found in XMP (dc:description[\"x-default\"] not found)"));
-                    }
-                    else
-                    {
-                        if (!dc.getDescription("x-default").equals(subject))
+                        if (dc.getDescription("x-default") == null)
                         {
-                            ve.add(unsynchronizedMetaDataError("Subject"));
-
+                            ve.add(absentXMPPropertyError("Subject",
+                                    "Subject not found in XMP (dc:description[\"x-default\"] not found)"));
+                        }
+                        else
+                        {
+                            if (!dc.getDescription("x-default").equals(subject))
+                            {
+                                ve.add(unsynchronizedMetaDataError("Subject"));
+                                
+                            }
                         }
                     }
+                    catch (BadFieldValueException ex)
+                    {
+                        ve.add(badFieldXMPPropertyError("Subject", ex.getMessage()));
+                    }
                 }
                 else
                 {
@@ -541,6 +556,21 @@ public class SynchronizedMetaDataValidat
     {
         StringBuilder sb = new StringBuilder(80);
         sb.append(target).append(" present in the document catalog dictionary can't be found in XMP information (")
+                .append(details).append(")");
+        return new ValidationError(PreflightConstants.ERROR_METADATA_MISMATCH, sb.toString());
+    }
+    
+    /**
+     * Return a formatted validation error when a specific XMP property has the wrong type.
+     *
+     * @param target the concerned property
+     * @param details comments about the XMP property
+     * @return the generated validation error
+     */
+    protected ValidationError badFieldXMPPropertyError(String target, String details)
+    {
+        StringBuilder sb = new StringBuilder(80);
+        sb.append(target).append(" present in the document catalog dictionary can't be found in XMP information (")
                 .append(details).append(")");
         return new ValidationError(PreflightConstants.ERROR_METADATA_MISMATCH, sb.toString());
     }