Search in sources :

Example 1 with AbstractField

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

the class FontMetaDataValidation method analyseFontName.

/**
 * Value of the dc:title must be the same as the FontName in the font descriptor.
 *
 * @param metadata
 *            XMPMetaData of the Font File Stream
 * @param fontDesc
 *            The FontDescriptor dictionary
 * @param ve
 *            the list of validation error to update if the validation fails
 */
public boolean analyseFontName(XMPMetadata metadata, PDFontDescriptor fontDesc, List<ValidationError> ve) {
    String fontName = fontDesc.getFontName();
    String noSubSetName = fontName;
    if (FontDescriptorHelper.isSubSet(fontName)) {
        noSubSetName = fontName.split("\\+")[1];
    }
    DublinCoreSchema dc = metadata.getDublinCoreSchema();
    if (dc != null && dc.getTitleProperty() != null) {
        String defaultTitle = dc.getTitle("x-default");
        if (defaultTitle != null) {
            if (!defaultTitle.equals(fontName) && (noSubSetName != null && !defaultTitle.equals(noSubSetName))) {
                StringBuilder sb = new StringBuilder(80);
                sb.append("FontName").append(" present in the FontDescriptor dictionary doesn't match with XMP information dc:title of the Font File Stream.");
                ve.add(new ValidationError(PreflightConstants.ERROR_METADATA_MISMATCH, sb.toString()));
                return false;
            }
            // --- default value is the right one
            return true;
        } else {
            Iterator<AbstractField> it = dc.getTitleProperty().getContainer().getAllProperties().iterator();
            boolean empty = true;
            while (it.hasNext()) {
                empty = false;
                AbstractField tmp = it.next();
                if (tmp instanceof TextType) {
                    String val = ((TextType) tmp).getStringValue();
                    if (val.equals(fontName) || val.equals(noSubSetName)) {
                        // value found, return
                        return true;
                    }
                }
            }
            // title doesn't match, it is an error.
            StringBuilder sb = new StringBuilder(80);
            sb.append("FontName");
            if (empty) {
                sb.append(" present in the FontDescriptor dictionary can't be found in XMP information the Font File Stream.");
                ve.add(new ValidationError(PreflightConstants.ERROR_METADATA_PROPERTY_MISSING, sb.toString()));
            } else {
                sb.append(" present in the FontDescriptor dictionary doesn't match with XMP information dc:title of the Font File Stream.");
                ve.add(new ValidationError(PreflightConstants.ERROR_METADATA_MISMATCH, sb.toString()));
            }
            return false;
        }
    }
    return true;
}
Also used : AbstractField(org.apache.xmpbox.type.AbstractField) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema) TextType(org.apache.xmpbox.type.TextType)

Example 2 with AbstractField

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

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

the class PdfaExtensionHelper method populatePDFAType.

private static void populatePDFAType(XMPMetadata meta, PDFATypeType type, TypeMapping tm) throws XmpParsingException {
    String ttype = type.getType();
    String tns = type.getNamespaceURI();
    String tprefix = type.getPrefixValue();
    String tdescription = type.getDescription();
    ArrayProperty fields = type.getFields();
    if (ttype == null || tns == null || tprefix == null || tdescription == null) {
        // all fields are mandatory
        throw new XmpParsingException(ErrorType.RequiredProperty, "Missing field in type definition");
    }
    // create the structured type
    // TODO
    DefinedStructuredType structuredType = new DefinedStructuredType(meta, tns, tprefix, null);
    // maybe a name exists
    if (fields != null) {
        List<AbstractField> definedFields = fields.getAllProperties();
        for (AbstractField af3 : definedFields) {
            if (af3 instanceof PDFAFieldType) {
                populatePDFAFieldType((PDFAFieldType) af3, structuredType);
            }
        // else TODO
        }
    }
    // add the structured type to list
    PropertiesDescription pm = new PropertiesDescription();
    for (Map.Entry<String, PropertyType> entry : structuredType.getDefinedProperties().entrySet()) {
        pm.addNewProperty(entry.getKey(), entry.getValue());
    }
    tm.addToDefinedStructuredTypes(ttype, tns, pm);
}
Also used : ArrayProperty(org.apache.xmpbox.type.ArrayProperty) DefinedStructuredType(org.apache.xmpbox.type.DefinedStructuredType) AbstractField(org.apache.xmpbox.type.AbstractField) PropertiesDescription(org.apache.xmpbox.type.PropertiesDescription) PDFAFieldType(org.apache.xmpbox.type.PDFAFieldType) PDFAPropertyType(org.apache.xmpbox.type.PDFAPropertyType) PropertyType(org.apache.xmpbox.type.PropertyType) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 4 with AbstractField

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

the class XMPSchema method setUnqualifiedLanguagePropertyValue.

/**
 * Set the value of a multi-lingual property.
 *
 * @param name
 *            The name of the property, it must include the namespace prefix, e.g. "pdf:Keywords"
 * @param language
 *            The language code of the value. If null then "x-default" is assumed.
 * @param value
 *            The value of the property in the specified language.
 */
public void setUnqualifiedLanguagePropertyValue(String name, String language, String value) {
    if (language == null || language.isEmpty()) {
        language = XmpConstants.X_DEFAULT;
    }
    AbstractField property = getAbstractProperty(name);
    ArrayProperty arrayProp;
    if (property != null) {
        // Analyzing content of property
        if (property instanceof ArrayProperty) {
            arrayProp = (ArrayProperty) property;
            // Try to find a definition
            for (AbstractField child : arrayProp.getContainer().getAllProperties()) {
                // try to find the same lang definition
                if (child.getAttribute(XmpConstants.LANG_NAME).getValue().equals(language)) {
                    // the same language has been found
                    arrayProp.getContainer().removeProperty(child);
                    if (value != null) {
                        TextType langValue = createTextType(XmpConstants.LIST_NAME, value);
                        langValue.setAttribute(new Attribute(XMLConstants.XML_NS_URI, XmpConstants.LANG_NAME, language));
                        arrayProp.getContainer().addProperty(langValue);
                    }
                    reorganizeAltOrder(arrayProp.getContainer());
                    return;
                }
            }
            // if no definition found, we add a new one
            TextType langValue = createTextType(XmpConstants.LIST_NAME, value);
            langValue.setAttribute(new Attribute(XMLConstants.XML_NS_URI, XmpConstants.LANG_NAME, language));
            arrayProp.getContainer().addProperty(langValue);
            reorganizeAltOrder(arrayProp.getContainer());
        }
    } else {
        arrayProp = createArrayProperty(name, Cardinality.Alt);
        TextType langValue = createTextType(XmpConstants.LIST_NAME, value);
        langValue.setAttribute(new Attribute(XMLConstants.XML_NS_URI, XmpConstants.LANG_NAME, language));
        arrayProp.getContainer().addProperty(langValue);
        addProperty(arrayProp);
    }
}
Also used : ArrayProperty(org.apache.xmpbox.type.ArrayProperty) AbstractField(org.apache.xmpbox.type.AbstractField) Attribute(org.apache.xmpbox.type.Attribute) TextType(org.apache.xmpbox.type.TextType)

Example 5 with AbstractField

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

the class XMPSchema method getUnqualifiedLanguagePropertyValue.

/**
 * Get the value of a multi-lingual property.
 *
 * @param name
 *            The name of the property, without the namespace prefix.
 * @param expectedLanguage
 *            The language code of the value. If null then "x-default" is assumed.
 *
 * @return The value of the language property.
 */
public String getUnqualifiedLanguagePropertyValue(String name, String expectedLanguage) {
    String language = (expectedLanguage != null) ? expectedLanguage : XmpConstants.X_DEFAULT;
    AbstractField property = getAbstractProperty(name);
    if (property != null) {
        if (property instanceof ArrayProperty) {
            ArrayProperty arrayProp = (ArrayProperty) property;
            for (AbstractField child : arrayProp.getContainer().getAllProperties()) {
                Attribute text = child.getAttribute(XmpConstants.LANG_NAME);
                if (text != null && text.getValue().equals(language)) {
                    return ((TextType) child).getStringValue();
                }
            }
            return null;
        } else {
            throw new IllegalArgumentException("The property '" + name + "' is not of Lang Alt type");
        }
    }
    return null;
}
Also used : ArrayProperty(org.apache.xmpbox.type.ArrayProperty) AbstractField(org.apache.xmpbox.type.AbstractField) Attribute(org.apache.xmpbox.type.Attribute) TextType(org.apache.xmpbox.type.TextType)

Aggregations

AbstractField (org.apache.xmpbox.type.AbstractField)18 ArrayProperty (org.apache.xmpbox.type.ArrayProperty)11 ArrayList (java.util.ArrayList)5 Attribute (org.apache.xmpbox.type.Attribute)5 TextType (org.apache.xmpbox.type.TextType)5 TypeMapping (org.apache.xmpbox.type.TypeMapping)5 Element (org.w3c.dom.Element)4 AbstractSimpleProperty (org.apache.xmpbox.type.AbstractSimpleProperty)3 AbstractStructuredType (org.apache.xmpbox.type.AbstractStructuredType)3 List (java.util.List)2 DefinedStructuredType (org.apache.xmpbox.type.DefinedStructuredType)2 PDFAPropertyType (org.apache.xmpbox.type.PDFAPropertyType)2 PropertiesDescription (org.apache.xmpbox.type.PropertiesDescription)2 PropertyType (org.apache.xmpbox.type.PropertyType)2 IOException (java.io.IOException)1 Calendar (java.util.Calendar)1 Map (java.util.Map)1 QName (javax.xml.namespace.QName)1 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)1 XMPMetadata (org.apache.xmpbox.XMPMetadata)1