Search in sources :

Example 1 with AbstractStructuredType

use of org.apache.xmpbox.type.AbstractStructuredType 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 2 with AbstractStructuredType

use of org.apache.xmpbox.type.AbstractStructuredType in project pdfbox by apache.

the class DomXmpParser method manageStructuredType.

private void manageStructuredType(XMPMetadata xmp, Element property, String prefix, ComplexPropertyContainer container) throws XmpParsingException {
    if (DomHelper.isParseTypeResource(property)) {
        AbstractStructuredType ast = parseLiDescription(xmp, DomHelper.getQName(property), property);
        if (ast != null) {
            ast.setPrefix(prefix);
            container.addProperty(ast);
        }
    } else {
        Element inner = DomHelper.getFirstChildElement(property);
        if (inner != null) {
            nsFinder.push(inner);
            AbstractStructuredType ast = parseLiDescription(xmp, DomHelper.getQName(property), inner);
            ast.setPrefix(prefix);
            container.addProperty(ast);
        }
    }
}
Also used : Element(org.w3c.dom.Element) AbstractStructuredType(org.apache.xmpbox.type.AbstractStructuredType)

Example 3 with AbstractStructuredType

use of org.apache.xmpbox.type.AbstractStructuredType in project pdfbox by apache.

the class DomXmpParser method manageDefinedType.

private void manageDefinedType(XMPMetadata xmp, Element property, String prefix, ComplexPropertyContainer container) throws XmpParsingException {
    if (DomHelper.isParseTypeResource(property)) {
        AbstractStructuredType ast = parseLiDescription(xmp, DomHelper.getQName(property), property);
        ast.setPrefix(prefix);
        container.addProperty(ast);
    } else {
        Element inner = DomHelper.getFirstChildElement(property);
        if (inner == null) {
            throw new XmpParsingException(ErrorType.Format, "property should contain child element : " + property);
        }
        AbstractStructuredType ast = parseLiDescription(xmp, DomHelper.getQName(property), inner);
        ast.setPrefix(prefix);
        container.addProperty(ast);
    }
}
Also used : Element(org.w3c.dom.Element) AbstractStructuredType(org.apache.xmpbox.type.AbstractStructuredType)

Example 4 with AbstractStructuredType

use of org.apache.xmpbox.type.AbstractStructuredType in project pdfbox by apache.

the class XmpSerializer method serializeFields.

public void serializeFields(Document doc, Element parent, List<AbstractField> fields, String resourceNS, String prefix, boolean wrapWithProperty) {
    for (AbstractField field : fields) {
        if (field instanceof AbstractSimpleProperty) {
            AbstractSimpleProperty simple = (AbstractSimpleProperty) field;
            String localPrefix;
            if (prefix != null && !prefix.isEmpty()) {
                localPrefix = prefix;
            } else {
                localPrefix = simple.getPrefix();
            }
            Element esimple = doc.createElement(localPrefix + ":" + simple.getPropertyName());
            esimple.setTextContent(simple.getStringValue());
            List<Attribute> attributes = simple.getAllAttributes();
            for (Attribute attribute : attributes) {
                esimple.setAttributeNS(attribute.getNamespace(), attribute.getName(), attribute.getValue());
            }
            parent.appendChild(esimple);
        } else if (field instanceof ArrayProperty) {
            ArrayProperty array = (ArrayProperty) field;
            // property
            Element asimple = doc.createElement(array.getPrefix() + ":" + array.getPropertyName());
            parent.appendChild(asimple);
            // attributes
            fillElementWithAttributes(asimple, array);
            // the array definition
            Element econtainer = doc.createElement(XmpConstants.DEFAULT_RDF_PREFIX + ":" + array.getArrayType());
            asimple.appendChild(econtainer);
            // for each element of the array
            List<AbstractField> innerFields = array.getAllProperties();
            serializeFields(doc, econtainer, innerFields, resourceNS, XmpConstants.DEFAULT_RDF_PREFIX, false);
        } else if (field instanceof AbstractStructuredType) {
            AbstractStructuredType structured = (AbstractStructuredType) field;
            List<AbstractField> innerFields = structured.getAllProperties();
            // property name attribute
            Element listParent = parent;
            if (wrapWithProperty) {
                Element nstructured = doc.createElement(resourceNS + ":" + structured.getPropertyName());
                parent.appendChild(nstructured);
                listParent = nstructured;
            }
            // element li
            Element estructured = doc.createElement(XmpConstants.DEFAULT_RDF_PREFIX + ":" + XmpConstants.LIST_NAME);
            listParent.appendChild(estructured);
            if (parseTypeResourceForLi) {
                estructured.setAttribute("rdf:parseType", "Resource");
                // all properties
                serializeFields(doc, estructured, innerFields, resourceNS, null, true);
            } else {
                // element description
                Element econtainer = doc.createElement(XmpConstants.DEFAULT_RDF_PREFIX + ":" + "Description");
                estructured.appendChild(econtainer);
                // all properties
                serializeFields(doc, econtainer, innerFields, resourceNS, null, true);
            }
        } else {
            // XXX finish serialization classes
            System.err.println(">> TODO >> " + field.getClass());
        }
    }
}
Also used : ArrayProperty(org.apache.xmpbox.type.ArrayProperty) AbstractField(org.apache.xmpbox.type.AbstractField) Attribute(org.apache.xmpbox.type.Attribute) AbstractSimpleProperty(org.apache.xmpbox.type.AbstractSimpleProperty) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) List(java.util.List) AbstractStructuredType(org.apache.xmpbox.type.AbstractStructuredType)

Aggregations

AbstractStructuredType (org.apache.xmpbox.type.AbstractStructuredType)4 Element (org.w3c.dom.Element)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AbstractField (org.apache.xmpbox.type.AbstractField)2 AbstractSimpleProperty (org.apache.xmpbox.type.AbstractSimpleProperty)2 ArrayProperty (org.apache.xmpbox.type.ArrayProperty)2 Attribute (org.apache.xmpbox.type.Attribute)1 ComplexPropertyContainer (org.apache.xmpbox.type.ComplexPropertyContainer)1 PropertiesDescription (org.apache.xmpbox.type.PropertiesDescription)1 PropertyType (org.apache.xmpbox.type.PropertyType)1 TypeMapping (org.apache.xmpbox.type.TypeMapping)1 Types (org.apache.xmpbox.type.Types)1 NodeList (org.w3c.dom.NodeList)1