use of org.apache.xmpbox.type.ArrayProperty in project pdfbox by apache.
the class PdfaExtensionHelper method populateSchemaMapping.
public static void populateSchemaMapping(XMPMetadata meta) throws XmpParsingException {
List<XMPSchema> schems = meta.getAllSchemas();
TypeMapping tm = meta.getTypeMapping();
StructuredType stPdfaExt = PDFAExtensionSchema.class.getAnnotation(StructuredType.class);
for (XMPSchema xmpSchema : schems) {
if (xmpSchema.getNamespace().equals(stPdfaExt.namespace())) {
// ensure the prefix is the preferred one (cannot use other definition)
if (!xmpSchema.getPrefix().equals(stPdfaExt.preferedPrefix())) {
throw new XmpParsingException(ErrorType.InvalidPrefix, "Found invalid prefix for PDF/A extension, found '" + xmpSchema.getPrefix() + "', should be '" + stPdfaExt.preferedPrefix() + "'");
}
// create schema and types
PDFAExtensionSchema pes = (PDFAExtensionSchema) xmpSchema;
ArrayProperty sp = pes.getSchemasProperty();
for (AbstractField af : sp.getAllProperties()) {
if (af instanceof PDFASchemaType) {
populatePDFASchemaType(meta, (PDFASchemaType) af, tm);
}
// TODO unmanaged ?
}
}
}
}
use of org.apache.xmpbox.type.ArrayProperty in project pdfbox by apache.
the class PdfaExtensionHelper method populatePDFASchemaType.
private static void populatePDFASchemaType(XMPMetadata meta, PDFASchemaType st, TypeMapping tm) throws XmpParsingException {
String namespaceUri = st.getNamespaceURI().trim();
String prefix = st.getPrefixValue();
ArrayProperty properties = st.getProperty();
ArrayProperty valueTypes = st.getValueType();
XMPSchemaFactory xsf = tm.getSchemaFactory(namespaceUri);
// retrieve namespaces
if (xsf == null) {
// create namespace with no field
tm.addNewNameSpace(namespaceUri, prefix);
xsf = tm.getSchemaFactory(namespaceUri);
}
// populate value type
if (valueTypes != null) {
for (AbstractField af2 : valueTypes.getAllProperties()) {
if (af2 instanceof PDFATypeType) {
populatePDFAType(meta, (PDFATypeType) af2, tm);
}
}
}
// populate properties
if (properties == null) {
throw new XmpParsingException(ErrorType.RequiredProperty, "Missing pdfaSchema:property in type definition");
}
for (AbstractField af2 : properties.getAllProperties()) {
if (af2 instanceof PDFAPropertyType) {
populatePDFAPropertyType((PDFAPropertyType) af2, tm, xsf);
}
// TODO unmanaged ?
}
}
use of org.apache.xmpbox.type.ArrayProperty 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());
}
}
}
use of org.apache.xmpbox.type.ArrayProperty in project pdfbox by apache.
the class AbstractSchemaTester method internalTestPropertySetterInArray.
private void internalTestPropertySetterInArray() throws Exception {
if (cardinality == Cardinality.Simple) {
return;
}
XMPSchema schema = getSchema();
// add value
String setter = "add" + calculateFieldNameForMethod(fieldName);
// TypeDescription<AbstractSimpleProperty> td =
// typeMapping.getSimpleDescription(type);
Object value1 = getJavaValue(type);
Method set = getSchemaClass().getMethod(setter, getJavaType(type));
set.invoke(schema, value1);
// retrieve complex property
String getter = calculateArrayGetter(fieldName) + "Property";
Method getcp = getSchemaClass().getMethod(getter);
Object ocp = getcp.invoke(schema);
Assert.assertTrue(ocp instanceof ArrayProperty);
ArrayProperty cp = (ArrayProperty) ocp;
// check size is ok (1)
Assert.assertEquals(1, cp.getContainer().getAllProperties().size());
// add a new one
Object value2 = getJavaValue(type);
set.invoke(schema, value2);
Assert.assertEquals(2, cp.getContainer().getAllProperties().size());
// remove the first
String remover = "remove" + calculateFieldNameForMethod(fieldName);
Method remove = getSchemaClass().getMethod(remover, getJavaType(type));
remove.invoke(schema, value1);
Assert.assertEquals(1, cp.getContainer().getAllProperties().size());
}
use of org.apache.xmpbox.type.ArrayProperty in project pdfbox by apache.
the class XMPSchema method getUnqualifiedSequenceDateValueList.
/**
* Get all the date values in a sequence property.
*
* @param seqName
* The name of the sequence property, it must include the namespace prefix, e.g. "pdf:Keywords".
*
* @return A read-only list of java.util.Calendar objects or null if the property does not exist.
*/
public List<Calendar> getUnqualifiedSequenceDateValueList(String seqName) {
List<Calendar> retval = null;
ArrayProperty seq = (ArrayProperty) getAbstractProperty(seqName);
if (seq != null) {
retval = new ArrayList<>();
for (AbstractField child : seq.getContainer().getAllProperties()) {
if (child instanceof DateType) {
retval.add(((DateType) child).getValue());
}
}
}
return retval;
}
Aggregations