Author: tilman
Date: Sat Dec 13 07:38:19 2025
New Revision: 1930482

Log:
PDFBOX-6121: avoid ClassCastExeption, as suggested by ChatGPT

Modified:
   
pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/schema/DublinCoreSchema.java

Modified: 
pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/schema/DublinCoreSchema.java
==============================================================================
--- 
pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/schema/DublinCoreSchema.java
     Sat Dec 13 07:38:14 2025        (r1930481)
+++ 
pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/schema/DublinCoreSchema.java
     Sat Dec 13 07:38:19 2025        (r1930482)
@@ -25,6 +25,7 @@ import java.util.Calendar;
 import java.util.List;
 
 import org.apache.xmpbox.XMPMetadata;
+import org.apache.xmpbox.type.AbstractField;
 import org.apache.xmpbox.type.ArrayProperty;
 import org.apache.xmpbox.type.Cardinality;
 import org.apache.xmpbox.type.MIMEType;
@@ -402,7 +403,7 @@ public class DublinCoreSchema extends XM
      */
     public ArrayProperty getContributorsProperty()
     {
-        return (ArrayProperty) getProperty(CONTRIBUTOR);
+        return getPropertyAs(CONTRIBUTOR, ArrayProperty.class);
     }
 
     /**
@@ -423,7 +424,7 @@ public class DublinCoreSchema extends XM
      */
     public TextType getCoverageProperty()
     {
-        return (TextType) getProperty(COVERAGE);
+        return getPropertyAs(COVERAGE, TextType.class);
     }
 
     /**
@@ -433,7 +434,7 @@ public class DublinCoreSchema extends XM
      */
     public String getCoverage()
     {
-        TextType tt = (TextType) getProperty(COVERAGE);
+        TextType tt = getPropertyAs(COVERAGE, TextType.class);
         return tt == null ? null : tt.getStringValue();
     }
 
@@ -444,7 +445,7 @@ public class DublinCoreSchema extends XM
      */
     public ArrayProperty getCreatorsProperty()
     {
-        return (ArrayProperty) getProperty(CREATOR);
+        return getPropertyAs(CREATOR, ArrayProperty.class);
     }
 
     /**
@@ -464,7 +465,7 @@ public class DublinCoreSchema extends XM
      */
     public ArrayProperty getDatesProperty()
     {
-        return (ArrayProperty) getProperty(DATE);
+        return getPropertyAs(DATE, ArrayProperty.class);
     }
 
     /**
@@ -484,7 +485,7 @@ public class DublinCoreSchema extends XM
      */
     public ArrayProperty getDescriptionProperty()
     {
-        return (ArrayProperty) getProperty(DESCRIPTION);
+        return getPropertyAs(DESCRIPTION, ArrayProperty.class);
     }
 
     /**
@@ -526,7 +527,7 @@ public class DublinCoreSchema extends XM
      */
     public TextType getFormatProperty()
     {
-        return (TextType) getProperty(FORMAT);
+        return getPropertyAs(FORMAT, TextType.class);
     }
 
     /**
@@ -536,7 +537,7 @@ public class DublinCoreSchema extends XM
      */
     public String getFormat()
     {
-        TextType tt = (TextType) getProperty(FORMAT);
+        TextType tt = getPropertyAs(FORMAT, TextType.class);
         return tt == null ? null : tt.getStringValue();
     }
 
@@ -547,7 +548,7 @@ public class DublinCoreSchema extends XM
      */
     public TextType getIdentifierProperty()
     {
-        return (TextType) getProperty(IDENTIFIER);
+        return getPropertyAs(IDENTIFIER, TextType.class);
     }
 
     /**
@@ -557,7 +558,7 @@ public class DublinCoreSchema extends XM
      */
     public String getIdentifier()
     {
-        TextType tt = (TextType) getProperty(IDENTIFIER);
+        TextType tt = getPropertyAs(IDENTIFIER, TextType.class);
         return tt == null ? null : tt.getStringValue();
     }
 
@@ -568,7 +569,7 @@ public class DublinCoreSchema extends XM
      */
     public ArrayProperty getLanguagesProperty()
     {
-        return (ArrayProperty) getProperty(LANGUAGE);
+        return getPropertyAs(LANGUAGE, ArrayProperty.class);
     }
 
     /**
@@ -588,7 +589,7 @@ public class DublinCoreSchema extends XM
      */
     public ArrayProperty getPublishersProperty()
     {
-        return (ArrayProperty) getProperty(PUBLISHER);
+        return getPropertyAs(PUBLISHER, ArrayProperty.class);
     }
 
     /**
@@ -608,7 +609,7 @@ public class DublinCoreSchema extends XM
      */
     public ArrayProperty getRelationsProperty()
     {
-        return (ArrayProperty) getProperty(RELATION);
+        return getPropertyAs(RELATION, ArrayProperty.class);
     }
 
     /**
@@ -628,7 +629,7 @@ public class DublinCoreSchema extends XM
      */
     public ArrayProperty getRightsProperty()
     {
-        return (ArrayProperty) getProperty(RIGHTS);
+        return getPropertyAs(RIGHTS, ArrayProperty.class);
     }
 
     /**
@@ -671,7 +672,7 @@ public class DublinCoreSchema extends XM
      */
     public TextType getSourceProperty()
     {
-        return (TextType) getProperty(SOURCE);
+        return getPropertyAs(SOURCE, TextType.class);
     }
 
     /**
@@ -681,7 +682,7 @@ public class DublinCoreSchema extends XM
      */
     public String getSource()
     {
-        TextType tt = (TextType) getProperty(SOURCE);
+        TextType tt = getPropertyAs(SOURCE, TextType.class);
         return tt == null ? null : tt.getStringValue();
     }
 
@@ -692,7 +693,7 @@ public class DublinCoreSchema extends XM
      */
     public ArrayProperty getSubjectsProperty()
     {
-        return (ArrayProperty) getProperty(SUBJECT);
+        return getPropertyAs(SUBJECT, ArrayProperty.class);
     }
 
     /**
@@ -712,7 +713,7 @@ public class DublinCoreSchema extends XM
      */
     public ArrayProperty getTitleProperty()
     {
-        return (ArrayProperty) getProperty(TITLE);
+        return getPropertyAs(TITLE, ArrayProperty.class);
     }
 
     /**
@@ -754,7 +755,7 @@ public class DublinCoreSchema extends XM
      */
     public ArrayProperty getTypesProperty()
     {
-        return (ArrayProperty) getProperty(TYPE);
+        return getPropertyAs(TYPE, ArrayProperty.class);
     }
 
     /**
@@ -772,4 +773,9 @@ public class DublinCoreSchema extends XM
         removeUnqualifiedBagValue(TYPE, type);
     }
 
+    private <T> T getPropertyAs(String name, Class<T> type)
+    {
+        AbstractField property = getProperty(name);
+        return type.isInstance(property) ? type.cast(property) : null;
+    }
 }

Reply via email to