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);
}
}
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);
}
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));
}
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);
}
}
}
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;
}
}
Aggregations