Search in sources :

Example 1 with Types

use of org.apache.xmpbox.type.Types 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 Types

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

the class PdfaExtensionHelper method populatePDFAFieldType.

private static void populatePDFAFieldType(PDFAFieldType field, DefinedStructuredType structuredType) throws XmpParsingException {
    String fName = field.getName();
    String fDescription = field.getDescription();
    String fValueType = field.getValueType();
    if (fName == null || fDescription == null || fValueType == null) {
        throw new XmpParsingException(ErrorType.RequiredProperty, "Missing field in field definition");
    }
    try {
        Types fValue = Types.valueOf(fValueType);
        structuredType.addProperty(fName, TypeMapping.createPropertyType(fValue, Cardinality.Simple));
    } catch (IllegalArgumentException e) {
        throw new XmpParsingException(ErrorType.NoValueType, "Type not defined : " + fValueType, e);
    // TODO could fValueType be a structured type ?
    }
}
Also used : Types(org.apache.xmpbox.type.Types)

Example 3 with Types

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

the class PdfaExtensionHelper method transformValueType.

private static PropertyType transformValueType(TypeMapping tm, String valueType) throws XmpParsingException {
    if ("Lang Alt".equals(valueType)) {
        return TypeMapping.createPropertyType(Types.LangAlt, Cardinality.Simple);
    }
    // else all other cases
    if (valueType.startsWith(CLOSED_CHOICE)) {
        valueType = valueType.substring(CLOSED_CHOICE.length());
    } else if (valueType.startsWith(OPEN_CHOICE)) {
        valueType = valueType.substring(OPEN_CHOICE.length());
    }
    int pos = valueType.indexOf(' ');
    Cardinality card = Cardinality.Simple;
    if (pos > 0) {
        String scard = valueType.substring(0, pos);
        if ("seq".equals(scard)) {
            card = Cardinality.Seq;
        } else if ("bag".equals(scard)) {
            card = Cardinality.Bag;
        } else if ("alt".equals(scard)) {
            card = Cardinality.Alt;
        } else {
            return null;
        }
    }
    String vt = valueType.substring(pos + 1);
    Types type = null;
    try {
        type = pos < 0 ? Types.valueOf(valueType) : Types.valueOf(vt);
    } catch (IllegalArgumentException e) {
        if (tm.isDefinedType(vt)) {
            type = Types.DefinedType;
        }
    }
    return TypeMapping.createPropertyType(type, card);
}
Also used : Types(org.apache.xmpbox.type.Types) Cardinality(org.apache.xmpbox.type.Cardinality)

Aggregations

Types (org.apache.xmpbox.type.Types)3 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AbstractField (org.apache.xmpbox.type.AbstractField)1 AbstractSimpleProperty (org.apache.xmpbox.type.AbstractSimpleProperty)1 AbstractStructuredType (org.apache.xmpbox.type.AbstractStructuredType)1 ArrayProperty (org.apache.xmpbox.type.ArrayProperty)1 Cardinality (org.apache.xmpbox.type.Cardinality)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 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1