Skip to content

Commit 6e2047f

Browse files
committed
PDFBOX-5660: refactor
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1931010 13f79535-47bb-0310-9956-ffa450edef68
1 parent 63ef266 commit 6e2047f

File tree

1 file changed

+46
-59
lines changed

1 file changed

+46
-59
lines changed

xmpbox/src/main/java/org/apache/xmpbox/xml/DomXmpParser.java

Lines changed: 46 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,81 +1136,68 @@ private AbstractStructuredType tryParseAttributesAsProperties(
11361136
for (int i = 0; i < attributes.getLength(); ++i)
11371137
{
11381138
Attr attr = (Attr) attributes.item(i);
1139-
if (XMLConstants.XMLNS_ATTRIBUTE.equals(attr.getPrefix()))
1140-
{
1141-
// do nothing
1142-
}
1143-
else if (XmpConstants.DEFAULT_RDF_PREFIX.equals(attr.getPrefix())
1144-
&& XmpConstants.ABOUT_NAME.equals(attr.getLocalName()))
1145-
{
1146-
// do nothing (maybe later?)
1147-
}
1148-
else if (XMLConstants.XML_NS_URI.equals(attr.getNamespaceURI()))
1139+
if (XMLConstants.XMLNS_ATTRIBUTE.equals(attr.getPrefix()) ||
1140+
XMLConstants.XML_NS_URI.equals(attr.getNamespaceURI()) ||
1141+
XmpConstants.DEFAULT_RDF_PREFIX.equals(attr.getPrefix()))
11491142
{
11501143
// do nothing
1144+
continue;
11511145
}
1152-
else if (XmpConstants.DEFAULT_RDF_PREFIX.equals(attr.getPrefix()))
1146+
if (ast == null && attr.getNamespaceURI() != null) // What to do if attr.getNamespaceURI() is null?
11531147
{
1154-
// other rdf stuff, e.g. rdf:parseType
1148+
// like in parseLiDescription():
1149+
// Instantiate abstract structured type with hint from first element
1150+
QName attrQName = new QName(attr.getNamespaceURI(), attr.getLocalName(), attr.getPrefix());
1151+
PropertyType ctype = checkPropertyDefinition(tm, attrQName, null);
1152+
// this is the type of the AbstractStructuredType, not of the element(s)
1153+
if (ctype == null)
1154+
{
1155+
throw new XmpParsingException(ErrorType.NoType,
1156+
"Property '" + attrQName.getLocalPart() + "' not defined in " + attrQName.getNamespaceURI());
1157+
}
1158+
Types tt = ctype.type();
1159+
ast = instanciateStructured(tm, tt, qName.getLocalPart(), attr.getNamespaceURI());
1160+
if (tt.isStructured())
1161+
{
1162+
pm = tm.getStructuredPropMapping(tt);
1163+
}
1164+
else
1165+
{
1166+
pm = tm.getDefinedDescriptionByNamespace(attr.getNamespaceURI(), attr.getLocalName());
1167+
}
11551168
}
1156-
else
1169+
if (ast != null && pm != null && attr.getNamespaceURI() != null)
11571170
{
1158-
if (ast == null && attr.getNamespaceURI() != null) // What to do if attr.getNamespaceURI() is null?
1171+
PropertyType type = pm.getPropertyType(attr.getLocalName());
1172+
if (type == null)
11591173
{
1160-
// like in parseLiDescription():
1161-
// Instantiate abstract structured type with hint from first element
1162-
QName attrQName = new QName(attr.getNamespaceURI(), attr.getLocalName(), attr.getPrefix());
1163-
PropertyType ctype = checkPropertyDefinition(tm, attrQName, null);
1164-
// this is the type of the AbstractStructuredType, not of the element(s)
1165-
if (ctype == null)
1166-
{
1167-
throw new XmpParsingException(ErrorType.NoType,
1168-
"Property '" + attrQName.getLocalPart() + "' not defined in " + attrQName.getNamespaceURI());
1169-
}
1170-
Types tt = ctype.type();
1171-
ast = instanciateStructured(tm, tt, qName.getLocalPart(), attr.getNamespaceURI());
1172-
if (tt.isStructured())
1173-
{
1174-
pm = tm.getStructuredPropMapping(tt);
1175-
}
1176-
else
1174+
if (strictParsing)
11771175
{
1178-
pm = tm.getDefinedDescriptionByNamespace(attr.getNamespaceURI(), attr.getLocalName());
1176+
throw new XmpParsingException(ErrorType.InvalidType, "No type defined for {" + attr.getNamespaceURI() + "}"
1177+
+ attr.getLocalName());
11791178
}
1179+
// PDFBOX-2318, PDFBOX-6106: Default to text if no type is found
1180+
type = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
11801181
}
1181-
if (ast != null && pm != null && attr.getNamespaceURI() != null)
1182+
else if (!type.type().isSimple() || type.card().isArray() || type.type() == Types.LangAlt)
11821183
{
1183-
PropertyType type = pm.getPropertyType(attr.getLocalName());
1184-
if (type == null)
1184+
if (strictParsing)
11851185
{
1186-
if (strictParsing)
1187-
{
1188-
throw new XmpParsingException(ErrorType.InvalidType, "No type defined for {" + attr.getNamespaceURI() + "}"
1189-
+ attr.getLocalName());
1190-
}
1191-
// PDFBOX-2318, PDFBOX-6106: Default to text if no type is found
1192-
type = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
1186+
throw new XmpParsingException(ErrorType.InvalidType, "The type '" +
1187+
type.type().name() + "' in '" + attr.getPrefix() + ":" + attr.getLocalName() + "=" + attr.getValue()
1188+
+ "' is a structured or array type, but attributes are simple types");
11931189
}
1194-
else if (!type.type().isSimple() || type.card().isArray() || type.type() == Types.LangAlt)
1190+
// PDFBOX-6125: Default to text or skip
1191+
if (attr.getValue() == null || attr.getValue().isEmpty())
11951192
{
1196-
if (strictParsing)
1197-
{
1198-
throw new XmpParsingException(ErrorType.InvalidType, "The type '" +
1199-
type.type().name() + "' in '" + attr.getPrefix() + ":" + attr.getLocalName() + "=" + attr.getValue()
1200-
+ "' is a structured or array type, but attributes are simple types");
1201-
}
1202-
// PDFBOX-6125: Default to text or skip
1203-
if (attr.getValue() == null || attr.getValue().isEmpty())
1204-
{
1205-
continue;
1206-
}
1207-
type = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
1193+
continue;
12081194
}
1209-
AbstractSimpleProperty asp = tm.instanciateSimpleProperty(
1210-
attr.getNamespaceURI(), attr.getPrefix(), attr.getLocalName(),
1211-
attr.getValue(), type.type());
1212-
ast.getContainer().addProperty(asp);
1195+
type = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
12131196
}
1197+
AbstractSimpleProperty asp = tm.instanciateSimpleProperty(
1198+
attr.getNamespaceURI(), attr.getPrefix(), attr.getLocalName(),
1199+
attr.getValue(), type.type());
1200+
ast.getContainer().addProperty(asp);
12141201
}
12151202
}
12161203
return ast;

0 commit comments

Comments
 (0)