use of org.apache.xmpbox.type.TypeMapping in project pdfbox by apache.
the class DomXmpParser method checkPropertyDefinition.
private PropertyType checkPropertyDefinition(XMPMetadata xmp, QName prop) throws XmpParsingException {
TypeMapping tm = xmp.getTypeMapping();
// test if namespace is set in xml
if (!nsFinder.containsNamespace(prop.getNamespaceURI())) {
throw new XmpParsingException(ErrorType.NoSchema, "Schema is not set in this document : " + prop.getNamespaceURI());
}
// test if namespace is defined
String nsuri = prop.getNamespaceURI();
if (!tm.isDefinedNamespace(nsuri)) {
throw new XmpParsingException(ErrorType.NoSchema, "Cannot find a definition for the namespace " + prop.getNamespaceURI());
}
try {
return tm.getSpecifiedPropertyType(prop);
} catch (BadFieldValueException e) {
throw new XmpParsingException(ErrorType.InvalidType, "Failed to retrieve property definition", e);
}
}
use of org.apache.xmpbox.type.TypeMapping 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.TypeMapping in project pdfbox by apache.
the class DomXmpParser method manageSimpleType.
private void manageSimpleType(XMPMetadata xmp, Element property, Types type, ComplexPropertyContainer container) throws XmpParsingException {
TypeMapping tm = xmp.getTypeMapping();
String prefix = property.getPrefix();
String name = property.getLocalName();
String namespace = property.getNamespaceURI();
AbstractSimpleProperty sp = tm.instanciateSimpleProperty(namespace, prefix, name, property.getTextContent(), type);
loadAttributes(sp, property);
container.addProperty(sp);
}
use of org.apache.xmpbox.type.TypeMapping in project pdfbox by apache.
the class DomXmpParser method parseDescriptionRoot.
private void parseDescriptionRoot(XMPMetadata xmp, Element description) throws XmpParsingException {
nsFinder.push(description);
TypeMapping tm = xmp.getTypeMapping();
try {
List<Element> properties = DomHelper.getElementChildren(description);
// parse attributes as properties
NamedNodeMap nnm = description.getAttributes();
for (int i = 0; i < nnm.getLength(); i++) {
Attr attr = (Attr) nnm.item(i);
if (XMLConstants.XMLNS_ATTRIBUTE.equals(attr.getPrefix())) {
// do nothing
} else if (XmpConstants.DEFAULT_RDF_PREFIX.equals(attr.getPrefix()) && XmpConstants.ABOUT_NAME.equals(attr.getLocalName())) {
// do nothing
} else if (attr.getPrefix() == null && XmpConstants.ABOUT_NAME.equals(attr.getLocalName())) {
// do nothing
} else {
parseDescriptionRootAttr(xmp, description, attr, tm);
}
}
parseChildrenAsProperties(xmp, properties, tm, description);
} catch (XmpSchemaException e) {
throw new XmpParsingException(ErrorType.Undefined, "Parsing failed", e);
} finally {
nsFinder.pop();
}
}
use of org.apache.xmpbox.type.TypeMapping in project pdfbox by apache.
the class DomXmpParser method parseDescriptionInner.
private void parseDescriptionInner(XMPMetadata xmp, Element description, ComplexPropertyContainer parentContainer) throws XmpParsingException {
nsFinder.push(description);
TypeMapping tm = xmp.getTypeMapping();
try {
List<Element> properties = DomHelper.getElementChildren(description);
for (Element property : properties) {
String name = property.getLocalName();
PropertyType dtype = checkPropertyDefinition(xmp, DomHelper.getQName(property));
PropertyType ptype = tm.getStructuredPropMapping(dtype.type()).getPropertyType(name);
// create property
createProperty(xmp, property, ptype, parentContainer);
}
} finally {
nsFinder.pop();
}
}
Aggregations