Search in sources :

Example 6 with TypeMapping

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

the class XMPSchema method setSpecifiedSimpleTypeProperty.

private void setSpecifiedSimpleTypeProperty(Types type, String qualifiedName, Object propertyValue) {
    if (propertyValue == null) {
        // Search in properties to erase
        for (AbstractField child : getContainer().getAllProperties()) {
            if (child.getPropertyName().equals(qualifiedName)) {
                getContainer().removeProperty(child);
                return;
            }
        }
    } else {
        AbstractSimpleProperty specifiedTypeProperty;
        try {
            TypeMapping tm = getMetadata().getTypeMapping();
            specifiedTypeProperty = tm.instanciateSimpleProperty(null, getPrefix(), qualifiedName, propertyValue, type);
        } catch (Exception e) {
            throw new IllegalArgumentException("Failed to create property with the specified type given in parameters", e);
        }
        // Search in properties to erase
        for (AbstractField child : getAllProperties()) {
            if (child.getPropertyName().equals(qualifiedName)) {
                removeProperty(child);
                addProperty(specifiedTypeProperty);
                return;
            }
        }
        addProperty(specifiedTypeProperty);
    }
}
Also used : AbstractField(org.apache.xmpbox.type.AbstractField) AbstractSimpleProperty(org.apache.xmpbox.type.AbstractSimpleProperty) TypeMapping(org.apache.xmpbox.type.TypeMapping) IOException(java.io.IOException) BadFieldValueException(org.apache.xmpbox.type.BadFieldValueException)

Example 7 with TypeMapping

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

the class TestValidatePermitedMetadata method checkExistence.

@Test
public void checkExistence() throws Exception {
    // ensure schema exists
    XMPMetadata xmpmd = new XMPMetadata();
    TypeMapping mapping = new TypeMapping(xmpmd);
    XMPSchemaFactory factory = mapping.getSchemaFactory(namespace);
    assertNotNull("Schema not existing: " + namespace, factory);
    // ensure preferred is as expected
    XMPSchema schema = factory.createXMPSchema(xmpmd, "aa");
    assertEquals(preferred, schema.getPreferedPrefix());
    // ensure field is defined
    boolean found = false;
    Class<?> clz = schema.getClass();
    for (Field dfield : clz.getDeclaredFields()) {
        PropertyType ptype = dfield.getAnnotation(PropertyType.class);
        if (ptype != null) {
            // is a field definition
            if (String.class.equals(dfield.getType())) {
                String value = (String) dfield.get(clz);
                if (fieldname.equals(value)) {
                    // found the field defining
                    found = true;
                    break;
                }
            } else {
                // All field declaration are string
                throw new IllegalArgumentException("Should be a string : " + dfield.getName());
            }
        }
    }
    String msg = String.format("Did not find field definition for '%s' in %s (%s)", fieldname, clz.getSimpleName(), namespace);
    assertTrue(msg, found);
}
Also used : Field(java.lang.reflect.Field) XMPSchemaFactory(org.apache.xmpbox.schema.XMPSchemaFactory) XMPSchema(org.apache.xmpbox.schema.XMPSchema) TypeMapping(org.apache.xmpbox.type.TypeMapping) PropertyType(org.apache.xmpbox.type.PropertyType) Test(org.junit.Test)

Example 8 with TypeMapping

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

the class XMPSchemaTest method testArrayList.

@Test
public void testArrayList() throws Exception {
    XMPMetadata meta = XMPMetadata.createXMPMetadata();
    ArrayProperty newSeq = meta.getTypeMapping().createArrayProperty(null, "nsSchem", "seqType", Cardinality.Seq);
    TypeMapping tm = meta.getTypeMapping();
    TextType li1 = tm.createText(null, "rdf", "li", "valeur1");
    TextType li2 = tm.createText(null, "rdf", "li", "valeur2");
    newSeq.getContainer().addProperty(li1);
    newSeq.getContainer().addProperty(li2);
    schem.addProperty(newSeq);
    List<AbstractField> list = schem.getUnqualifiedArrayList("seqType");
    Assert.assertTrue(list.contains(li1));
    Assert.assertTrue(list.contains(li2));
}
Also used : ArrayProperty(org.apache.xmpbox.type.ArrayProperty) AbstractField(org.apache.xmpbox.type.AbstractField) XMPMetadata(org.apache.xmpbox.XMPMetadata) TypeMapping(org.apache.xmpbox.type.TypeMapping) TextType(org.apache.xmpbox.type.TextType) Test(org.junit.Test)

Example 9 with TypeMapping

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

the class DomXmpParser method manageArray.

private void manageArray(XMPMetadata xmp, Element property, PropertyType type, ComplexPropertyContainer container) throws XmpParsingException {
    TypeMapping tm = xmp.getTypeMapping();
    String prefix = property.getPrefix();
    String name = property.getLocalName();
    String namespace = property.getNamespaceURI();
    Element bagOrSeq = DomHelper.getUniqueElementChild(property);
    // ensure this is the good type of array
    if (bagOrSeq == null) {
        // not an array
        String whatFound = "nothing";
        if (property.getFirstChild() != null) {
            whatFound = property.getFirstChild().getClass().getName();
        }
        throw new XmpParsingException(ErrorType.Format, "Invalid array definition, expecting " + type.card() + " and found " + whatFound + " [prefix=" + prefix + "; name=" + name + "]");
    }
    if (!bagOrSeq.getLocalName().equals(type.card().name())) {
        // not the good array type
        throw new XmpParsingException(ErrorType.Format, "Invalid array type, expecting " + type.card() + " and found " + bagOrSeq.getLocalName() + " [prefix=" + prefix + "; name=" + name + "]");
    }
    ArrayProperty array = tm.createArrayProperty(namespace, prefix, name, type.card());
    container.addProperty(array);
    List<Element> lis = DomHelper.getElementChildren(bagOrSeq);
    for (Element element : lis) {
        QName propertyQName = new QName(element.getLocalName());
        AbstractField ast = parseLiElement(xmp, propertyQName, element, type.type());
        if (ast != null) {
            array.addProperty(ast);
        }
    }
}
Also used : ArrayProperty(org.apache.xmpbox.type.ArrayProperty) AbstractField(org.apache.xmpbox.type.AbstractField) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) TypeMapping(org.apache.xmpbox.type.TypeMapping)

Example 10 with TypeMapping

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

the class DomXmpParser method parseLiElement.

private AbstractField parseLiElement(XMPMetadata xmp, QName descriptor, Element liElement, Types type) throws XmpParsingException {
    if (DomHelper.isParseTypeResource(liElement)) {
        return parseLiDescription(xmp, descriptor, liElement);
    }
    // will find rdf:Description
    Element liChild = DomHelper.getUniqueElementChild(liElement);
    if (liChild != null) {
        nsFinder.push(liChild);
        return parseLiDescription(xmp, descriptor, liChild);
    } else {
        // no child
        String text = liElement.getTextContent();
        TypeMapping tm = xmp.getTypeMapping();
        AbstractSimpleProperty sp = tm.instanciateSimpleProperty(descriptor.getNamespaceURI(), descriptor.getPrefix(), descriptor.getLocalPart(), text, type);
        loadAttributes(sp, liElement);
        return sp;
    }
}
Also used : Element(org.w3c.dom.Element) AbstractSimpleProperty(org.apache.xmpbox.type.AbstractSimpleProperty) TypeMapping(org.apache.xmpbox.type.TypeMapping)

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