Skip to content

Commit 014f358

Browse files
committed
PDFBOX-6121: avoid ClassCastExeption, as suggested by ChatGPT
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1930483 13f79535-47bb-0310-9956-ffa450edef68
1 parent 8d8d972 commit 014f358

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

xmpbox/src/main/java/org/apache/xmpbox/schema/DublinCoreSchema.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.List;
2626

2727
import org.apache.xmpbox.XMPMetadata;
28+
import org.apache.xmpbox.type.AbstractField;
2829
import org.apache.xmpbox.type.ArrayProperty;
2930
import org.apache.xmpbox.type.BadFieldValueException;
3031
import org.apache.xmpbox.type.Cardinality;
@@ -403,7 +404,7 @@ public void addType(String type)
403404
*/
404405
public ArrayProperty getContributorsProperty()
405406
{
406-
return (ArrayProperty) getProperty(CONTRIBUTOR);
407+
return getPropertyAs(CONTRIBUTOR, ArrayProperty.class);
407408
}
408409

409410
/**
@@ -424,7 +425,7 @@ public List<String> getContributors()
424425
*/
425426
public TextType getCoverageProperty()
426427
{
427-
return (TextType) getProperty(COVERAGE);
428+
return getPropertyAs(COVERAGE, TextType.class);
428429
}
429430

430431
/**
@@ -434,7 +435,7 @@ public TextType getCoverageProperty()
434435
*/
435436
public String getCoverage()
436437
{
437-
TextType tt = (TextType) getProperty(COVERAGE);
438+
TextType tt = getPropertyAs(COVERAGE, TextType.class);
438439
return tt == null ? null : tt.getStringValue();
439440
}
440441

@@ -445,7 +446,7 @@ public String getCoverage()
445446
*/
446447
public ArrayProperty getCreatorsProperty()
447448
{
448-
return (ArrayProperty) getProperty(CREATOR);
449+
return getPropertyAs(CREATOR, ArrayProperty.class);
449450
}
450451

451452
/**
@@ -465,7 +466,7 @@ public List<String> getCreators()
465466
*/
466467
public ArrayProperty getDatesProperty()
467468
{
468-
return (ArrayProperty) getProperty(DATE);
469+
return getPropertyAs(DATE, ArrayProperty.class);
469470
}
470471

471472
/**
@@ -485,7 +486,7 @@ public List<Calendar> getDates()
485486
*/
486487
public ArrayProperty getDescriptionProperty()
487488
{
488-
return (ArrayProperty) getProperty(DESCRIPTION);
489+
return getPropertyAs(DESCRIPTION, ArrayProperty.class);
489490
}
490491

491492
/**
@@ -531,7 +532,7 @@ public String getDescription() throws BadFieldValueException
531532
*/
532533
public TextType getFormatProperty()
533534
{
534-
return (TextType) getProperty(FORMAT);
535+
return getPropertyAs(FORMAT, TextType.class);
535536
}
536537

537538
/**
@@ -541,7 +542,7 @@ public TextType getFormatProperty()
541542
*/
542543
public String getFormat()
543544
{
544-
TextType tt = (TextType) getProperty(FORMAT);
545+
TextType tt = getPropertyAs(FORMAT, TextType.class);
545546
return tt == null ? null : tt.getStringValue();
546547
}
547548

@@ -552,7 +553,7 @@ public String getFormat()
552553
*/
553554
public TextType getIdentifierProperty()
554555
{
555-
return (TextType) getProperty(IDENTIFIER);
556+
return getPropertyAs(IDENTIFIER, TextType.class);
556557
}
557558

558559
/**
@@ -562,7 +563,7 @@ public TextType getIdentifierProperty()
562563
*/
563564
public String getIdentifier()
564565
{
565-
TextType tt = (TextType) getProperty(IDENTIFIER);
566+
TextType tt = getPropertyAs(IDENTIFIER, TextType.class);
566567
return tt == null ? null : tt.getStringValue();
567568
}
568569

@@ -573,7 +574,7 @@ public String getIdentifier()
573574
*/
574575
public ArrayProperty getLanguagesProperty()
575576
{
576-
return (ArrayProperty) getProperty(LANGUAGE);
577+
return getPropertyAs(LANGUAGE, ArrayProperty.class);
577578
}
578579

579580
/**
@@ -593,7 +594,7 @@ public List<String> getLanguages()
593594
*/
594595
public ArrayProperty getPublishersProperty()
595596
{
596-
return (ArrayProperty) getProperty(PUBLISHER);
597+
return getPropertyAs(PUBLISHER, ArrayProperty.class);
597598
}
598599

599600
/**
@@ -613,7 +614,7 @@ public List<String> getPublishers()
613614
*/
614615
public ArrayProperty getRelationsProperty()
615616
{
616-
return (ArrayProperty) getProperty(RELATION);
617+
return getPropertyAs(RELATION, ArrayProperty.class);
617618
}
618619

619620
/**
@@ -633,7 +634,7 @@ public List<String> getRelations()
633634
*/
634635
public ArrayProperty getRightsProperty()
635636
{
636-
return (ArrayProperty) getProperty(RIGHTS);
637+
return getPropertyAs(RIGHTS, ArrayProperty.class);
637638
}
638639

639640
/**
@@ -678,7 +679,7 @@ public String getRights() throws BadFieldValueException
678679
*/
679680
public TextType getSourceProperty()
680681
{
681-
return (TextType) getProperty(SOURCE);
682+
return getPropertyAs(SOURCE, TextType.class);
682683
}
683684

684685
/**
@@ -688,7 +689,7 @@ public TextType getSourceProperty()
688689
*/
689690
public String getSource()
690691
{
691-
TextType tt = (TextType) getProperty(SOURCE);
692+
TextType tt = getPropertyAs(SOURCE, TextType.class);
692693
return tt == null ? null : tt.getStringValue();
693694
}
694695

@@ -699,7 +700,7 @@ public String getSource()
699700
*/
700701
public ArrayProperty getSubjectsProperty()
701702
{
702-
return (ArrayProperty) getProperty(SUBJECT);
703+
return getPropertyAs(SUBJECT, ArrayProperty.class);
703704
}
704705

705706
/**
@@ -719,7 +720,7 @@ public List<String> getSubjects()
719720
*/
720721
public ArrayProperty getTitleProperty()
721722
{
722-
return (ArrayProperty) getProperty(TITLE);
723+
return getPropertyAs(TITLE, ArrayProperty.class);
723724
}
724725

725726
/**
@@ -763,7 +764,7 @@ public String getTitle() throws BadFieldValueException
763764
*/
764765
public ArrayProperty getTypesProperty()
765766
{
766-
return (ArrayProperty) getProperty(TYPE);
767+
return getPropertyAs(TYPE, ArrayProperty.class);
767768
}
768769

769770
/**
@@ -781,4 +782,9 @@ public void removeType(String type)
781782
removeUnqualifiedBagValue(TYPE, type);
782783
}
783784

785+
private <T> T getPropertyAs(String name, Class<T> type)
786+
{
787+
AbstractField property = getProperty(name);
788+
return type.isInstance(property) ? type.cast(property) : null;
789+
}
784790
}

0 commit comments

Comments
 (0)