use of org.apache.xmpbox.type.ComplexPropertyContainer in project pdfbox by apache.
the class DomXmpParser method parseLiDescription.
private AbstractStructuredType parseLiDescription(XMPMetadata xmp, QName descriptor, Element liElement) throws XmpParsingException {
TypeMapping tm = xmp.getTypeMapping();
List<Element> elements = DomHelper.getElementChildren(liElement);
if (elements.isEmpty()) {
// The list is empty
return null;
}
// Instantiate abstract structured type with hint from first element
Element first = elements.get(0);
PropertyType ctype = checkPropertyDefinition(xmp, DomHelper.getQName(first));
Types tt = ctype.type();
AbstractStructuredType ast = instanciateStructured(tm, tt, descriptor.getLocalPart(), first.getNamespaceURI());
ast.setNamespace(descriptor.getNamespaceURI());
ast.setPrefix(descriptor.getPrefix());
PropertiesDescription pm;
if (tt.isStructured()) {
pm = tm.getStructuredPropMapping(tt);
} else {
pm = tm.getDefinedDescriptionByNamespace(first.getNamespaceURI());
}
for (Element element : elements) {
String prefix = element.getPrefix();
String name = element.getLocalName();
String namespace = element.getNamespaceURI();
PropertyType type = pm.getPropertyType(name);
if (type == null) {
// not defined
throw new XmpParsingException(ErrorType.NoType, "Type '" + name + "' not defined in " + element.getNamespaceURI());
} else if (type.card().isArray()) {
ArrayProperty array = tm.createArrayProperty(namespace, prefix, name, type.card());
ast.getContainer().addProperty(array);
Element bagOrSeq = DomHelper.getUniqueElementChild(element);
List<Element> lis = DomHelper.getElementChildren(bagOrSeq);
for (Element element2 : lis) {
AbstractField ast2 = parseLiElement(xmp, descriptor, element2, type.type());
if (ast2 != null) {
array.addProperty(ast2);
}
}
} else if (type.type().isSimple()) {
AbstractSimpleProperty sp = tm.instanciateSimpleProperty(namespace, prefix, name, element.getTextContent(), type.type());
loadAttributes(sp, element);
ast.getContainer().addProperty(sp);
} else if (type.type().isStructured()) {
// create a new structured type
AbstractStructuredType inner = instanciateStructured(tm, type.type(), name, null);
inner.setNamespace(namespace);
inner.setPrefix(prefix);
ast.getContainer().addProperty(inner);
ComplexPropertyContainer cpc = inner.getContainer();
if (DomHelper.isParseTypeResource(element)) {
parseDescriptionInner(xmp, element, cpc);
} else {
Element descElement = DomHelper.getFirstChildElement(element);
if (descElement != null) {
parseDescriptionInner(xmp, descElement, cpc);
}
}
} else {
throw new XmpParsingException(ErrorType.NoType, "Unidentified element to parse " + element + " (type=" + type + ")");
}
}
return ast;
}
use of org.apache.xmpbox.type.ComplexPropertyContainer in project pdfbox by apache.
the class DomXmpParser method parseDescriptionRootAttr.
private void parseDescriptionRootAttr(XMPMetadata xmp, Element description, Attr attr, TypeMapping tm) throws XmpSchemaException, XmpParsingException {
String namespace = attr.getNamespaceURI();
XMPSchema schema = xmp.getSchema(namespace);
if (schema == null && tm.getSchemaFactory(namespace) != null) {
schema = tm.getSchemaFactory(namespace).createXMPSchema(xmp, attr.getPrefix());
loadAttributes(schema, description);
}
// Only process when a schema was successfully found
if (schema != null) {
ComplexPropertyContainer container = schema.getContainer();
PropertyType type = checkPropertyDefinition(xmp, new QName(attr.getNamespaceURI(), attr.getLocalName()));
// Default to text if no type is found
if (type == null) {
type = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
}
try {
AbstractSimpleProperty sp = tm.instanciateSimpleProperty(namespace, schema.getPrefix(), attr.getLocalName(), attr.getValue(), type.type());
container.addProperty(sp);
} catch (IllegalArgumentException e) {
throw new XmpParsingException(ErrorType.Format, e.getMessage() + " in " + schema.getPrefix() + ":" + attr.getLocalName(), e);
}
}
}
use of org.apache.xmpbox.type.ComplexPropertyContainer in project pdfbox by apache.
the class DomXmpParser method parseChildrenAsProperties.
private void parseChildrenAsProperties(XMPMetadata xmp, List<Element> properties, TypeMapping tm, Element description) throws XmpParsingException, XmpSchemaException {
// parse children elements as properties
for (Element property : properties) {
String namespace = property.getNamespaceURI();
PropertyType type = checkPropertyDefinition(xmp, DomHelper.getQName(property));
// create the container
if (!tm.isDefinedSchema(namespace)) {
throw new XmpParsingException(ErrorType.NoSchema, "This namespace is not a schema or a structured type : " + namespace);
}
XMPSchema schema = xmp.getSchema(namespace);
if (schema == null) {
schema = tm.getSchemaFactory(namespace).createXMPSchema(xmp, property.getPrefix());
loadAttributes(schema, description);
}
ComplexPropertyContainer container = schema.getContainer();
// create property
createProperty(xmp, property, type, container);
}
}
Aggregations