Author: tilman
Date: Sat Dec 13 07:38:14 2025
New Revision: 1930481
Log:
PDFBOX-6121: avoid ClassCastExeption, as suggested by ChatGPT
Modified:
pdfbox/branches/3.0/xmpbox/src/main/java/org/apache/xmpbox/schema/DublinCoreSchema.java
Modified:
pdfbox/branches/3.0/xmpbox/src/main/java/org/apache/xmpbox/schema/DublinCoreSchema.java
==============================================================================
---
pdfbox/branches/3.0/xmpbox/src/main/java/org/apache/xmpbox/schema/DublinCoreSchema.java
Sat Dec 13 04:20:02 2025 (r1930480)
+++
pdfbox/branches/3.0/xmpbox/src/main/java/org/apache/xmpbox/schema/DublinCoreSchema.java
Sat Dec 13 07:38:14 2025 (r1930481)
@@ -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.BadFieldValueException;
import org.apache.xmpbox.type.Cardinality;
@@ -403,7 +404,7 @@ public class DublinCoreSchema extends XM
*/
public ArrayProperty getContributorsProperty()
{
- return (ArrayProperty) getProperty(CONTRIBUTOR);
+ return getPropertyAs(CONTRIBUTOR, ArrayProperty.class);
}
/**
@@ -424,7 +425,7 @@ public class DublinCoreSchema extends XM
*/
public TextType getCoverageProperty()
{
- return (TextType) getProperty(COVERAGE);
+ return getPropertyAs(COVERAGE, TextType.class);
}
/**
@@ -434,7 +435,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();
}
@@ -445,7 +446,7 @@ public class DublinCoreSchema extends XM
*/
public ArrayProperty getCreatorsProperty()
{
- return (ArrayProperty) getProperty(CREATOR);
+ return getPropertyAs(CREATOR, ArrayProperty.class);
}
/**
@@ -465,7 +466,7 @@ public class DublinCoreSchema extends XM
*/
public ArrayProperty getDatesProperty()
{
- return (ArrayProperty) getProperty(DATE);
+ return getPropertyAs(DATE, ArrayProperty.class);
}
/**
@@ -485,7 +486,7 @@ public class DublinCoreSchema extends XM
*/
public ArrayProperty getDescriptionProperty()
{
- return (ArrayProperty) getProperty(DESCRIPTION);
+ return getPropertyAs(DESCRIPTION, ArrayProperty.class);
}
/**
@@ -531,7 +532,7 @@ public class DublinCoreSchema extends XM
*/
public TextType getFormatProperty()
{
- return (TextType) getProperty(FORMAT);
+ return getPropertyAs(FORMAT, TextType.class);
}
/**
@@ -541,7 +542,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();
}
@@ -552,7 +553,7 @@ public class DublinCoreSchema extends XM
*/
public TextType getIdentifierProperty()
{
- return (TextType) getProperty(IDENTIFIER);
+ return getPropertyAs(IDENTIFIER, TextType.class);
}
/**
@@ -562,7 +563,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();
}
@@ -573,7 +574,7 @@ public class DublinCoreSchema extends XM
*/
public ArrayProperty getLanguagesProperty()
{
- return (ArrayProperty) getProperty(LANGUAGE);
+ return getPropertyAs(LANGUAGE, ArrayProperty.class);
}
/**
@@ -593,7 +594,7 @@ public class DublinCoreSchema extends XM
*/
public ArrayProperty getPublishersProperty()
{
- return (ArrayProperty) getProperty(PUBLISHER);
+ return getPropertyAs(PUBLISHER, ArrayProperty.class);
}
/**
@@ -613,7 +614,7 @@ public class DublinCoreSchema extends XM
*/
public ArrayProperty getRelationsProperty()
{
- return (ArrayProperty) getProperty(RELATION);
+ return getPropertyAs(RELATION, ArrayProperty.class);
}
/**
@@ -633,7 +634,7 @@ public class DublinCoreSchema extends XM
*/
public ArrayProperty getRightsProperty()
{
- return (ArrayProperty) getProperty(RIGHTS);
+ return getPropertyAs(RIGHTS, ArrayProperty.class);
}
/**
@@ -678,7 +679,7 @@ public class DublinCoreSchema extends XM
*/
public TextType getSourceProperty()
{
- return (TextType) getProperty(SOURCE);
+ return getPropertyAs(SOURCE, TextType.class);
}
/**
@@ -688,7 +689,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();
}
@@ -699,7 +700,7 @@ public class DublinCoreSchema extends XM
*/
public ArrayProperty getSubjectsProperty()
{
- return (ArrayProperty) getProperty(SUBJECT);
+ return getPropertyAs(SUBJECT, ArrayProperty.class);
}
/**
@@ -719,7 +720,7 @@ public class DublinCoreSchema extends XM
*/
public ArrayProperty getTitleProperty()
{
- return (ArrayProperty) getProperty(TITLE);
+ return getPropertyAs(TITLE, ArrayProperty.class);
}
/**
@@ -763,7 +764,7 @@ public class DublinCoreSchema extends XM
*/
public ArrayProperty getTypesProperty()
{
- return (ArrayProperty) getProperty(TYPE);
+ return getPropertyAs(TYPE, ArrayProperty.class);
}
/**
@@ -781,4 +782,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;
+ }
}