Search in sources :

Example 1 with TypeMapping

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);
    }
}
Also used : BadFieldValueException(org.apache.xmpbox.type.BadFieldValueException) TypeMapping(org.apache.xmpbox.type.TypeMapping)

Example 2 with TypeMapping

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;
}
Also used : Types(org.apache.xmpbox.type.Types) ArrayProperty(org.apache.xmpbox.type.ArrayProperty) AbstractField(org.apache.xmpbox.type.AbstractField) PropertiesDescription(org.apache.xmpbox.type.PropertiesDescription) Element(org.w3c.dom.Element) AbstractSimpleProperty(org.apache.xmpbox.type.AbstractSimpleProperty) ComplexPropertyContainer(org.apache.xmpbox.type.ComplexPropertyContainer) PropertyType(org.apache.xmpbox.type.PropertyType) TypeMapping(org.apache.xmpbox.type.TypeMapping) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) AbstractStructuredType(org.apache.xmpbox.type.AbstractStructuredType)

Example 3 with TypeMapping

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);
}
Also used : AbstractSimpleProperty(org.apache.xmpbox.type.AbstractSimpleProperty) TypeMapping(org.apache.xmpbox.type.TypeMapping)

Example 4 with TypeMapping

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();
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Element(org.w3c.dom.Element) TypeMapping(org.apache.xmpbox.type.TypeMapping) XmpSchemaException(org.apache.xmpbox.schema.XmpSchemaException) Attr(org.w3c.dom.Attr)

Example 5 with TypeMapping

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();
    }
}
Also used : Element(org.w3c.dom.Element) TypeMapping(org.apache.xmpbox.type.TypeMapping) PropertyType(org.apache.xmpbox.type.PropertyType)

Aggregations

TypeMapping (org.apache.xmpbox.type.TypeMapping)12 AbstractField (org.apache.xmpbox.type.AbstractField)5 Element (org.w3c.dom.Element)5 AbstractSimpleProperty (org.apache.xmpbox.type.AbstractSimpleProperty)4 ArrayProperty (org.apache.xmpbox.type.ArrayProperty)4 PropertyType (org.apache.xmpbox.type.PropertyType)3 Test (org.junit.Test)3 XMPMetadata (org.apache.xmpbox.XMPMetadata)2 XMPSchema (org.apache.xmpbox.schema.XMPSchema)2 AbstractStructuredType (org.apache.xmpbox.type.AbstractStructuredType)2 BadFieldValueException (org.apache.xmpbox.type.BadFieldValueException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 QName (javax.xml.namespace.QName)1 PDFAExtensionSchema (org.apache.xmpbox.schema.PDFAExtensionSchema)1 XMPSchemaFactory (org.apache.xmpbox.schema.XMPSchemaFactory)1 XmpSchemaException (org.apache.xmpbox.schema.XmpSchemaException)1