Package org.apache.xmpbox.type

Examples of org.apache.xmpbox.type.TypeMapping


    @Test
    public void testGenerate () throws Exception
    {
        XMPMetadata metadata = XMPMetadata.createXMPMetadata();
        TypeMapping tmapping = metadata.getTypeMapping();
        ExifSchema exif = new ExifSchema(metadata);
        metadata.addSchema(exif);
        OECFType oecf = new OECFType(metadata);
        oecf.addProperty(tmapping.createInteger(oecf.getNamespace(), oecf.getPrefix(), OECFType.COLUMNS, 14));
        oecf.setPropertyName(ExifSchema.OECF);
        exif.addProperty(oecf);

        XmpSerializer serializer = new XmpSerializer();
View Full Code Here


    }

    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)
                    {
                        PDFASchemaType st = (PDFASchemaType) af;
                        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)
                                {
                                    PDFATypeType type = (PDFATypeType) af2;
                                    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
                                    DefinedStructuredType structuredType = new DefinedStructuredType(meta, tns,
                                            tprefix, null); // TODO
                                                            // maybe
                                                            // a name
                                                            // exists
                                    if (fields != null)
                                    {
                                        List<AbstractField> definedFields = fields.getAllProperties();
                                        for (AbstractField af3 : definedFields)
                                        {
                                            if (af3 instanceof PDFAFieldType)
                                            {
                                                PDFAFieldType field = (PDFAFieldType) af3;
                                                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 ?
                                                }
                                            } // 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);
                                }
                            }
                        }
                        // populate properties
                        if (properties == null)
View Full Code Here

    @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));
View Full Code Here

     *            Value of xpacket encoding
     */
    protected XMPMetadata(String xpacketBegin, String xpacketId, String xpacketBytes, String xpacketEncoding)
    {
        this.schemas = new ArrayList<XMPSchema>();
        this.typeMapping = new TypeMapping(this);

        this.xpacketBegin = xpacketBegin;
        this.xpacketId = xpacketId;
        this.xpacketBytes = xpacketBytes;
        this.xpacketEncoding = xpacketEncoding;
View Full Code Here

    }

    private void parseDescriptionRoot(XMPMetadata xmp, Element description) throws XmpParsingException
    {
        nsFinder.push(description);
        TypeMapping tm = xmp.getTypeMapping();
        try
        {
            List<Element> properties = DomHelper.getElementChildren(description);
            // parse attributes as properties
            NamedNodeMap nnm = description.getAttributes();
            for (int i = 0; i < nnm.getLength(); i++)
            {
                Attr attr = (Attr) nnm.item(i);
                if (XMLConstants.XMLNS_ATTRIBUTE.equals(attr.getPrefix()))
                {
                    // do nothing
                }
                else if (XmpConstants.DEFAULT_RDF_PREFIX.equals(attr.getPrefix())
                        && XmpConstants.ABOUT_NAME.equals(attr.getLocalName()))
                {
                    // do nothing
                }
                else if (attr.getPrefix() == null && XmpConstants.ABOUT_NAME.equals(attr.getLocalName()))
                {
                    // do nothing
                }
                else
                {
                    String namespace = attr.getNamespaceURI();
                    XMPSchema schema = xmp.getSchema(namespace);
                    if (schema == null && tm.getSchemaFactory(namespace) != null)
                    {
                        schema = tm.getSchemaFactory(namespace).createXMPSchema(xmp, attr.getPrefix());
                        loadAttributes(schema, description);
                    }
                    // Only process when a schema was successfully found
                    if( schema != null )
                    {
                        ComplexPropertyContainer container = schema.getContainer();
                        PropertyType type = checkPropertyDefinition(xmp,
                                new QName(attr.getNamespaceURI(), attr.getLocalName()));
                       
                        //Default to text if no type is found
                        if( type == null)
                        {
                            type = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
                        }
                                          
                        try
                        {
                            AbstractSimpleProperty sp = tm.instanciateSimpleProperty(namespace, schema.getPrefix(),
                                    attr.getLocalName(), attr.getValue(), type.type());
                            container.addProperty(sp);
                        }
                        catch( IllegalArgumentException exception)
                        {
                            //Swallow, and continue adding additional properties
                            LOG.warn("Unable to add property: "+ attr.getLocalName() + " value: "+attr.getValue(),exception);
                        }
                    }
                }
            }
            // parse children elements as properties
            for (Element property : properties)
            {
                String namespace = property.getNamespaceURI();
                PropertyType type = checkPropertyDefinition(xmp, DomHelper.getQName(property));
                // create the container
                if (!tm.isDefinedSchema(namespace))
                {
                    throw new XmpParsingException(ErrorType.NoSchema,
                            "This namespace is not a schema or a structured type : " + namespace);
                }
                XMPSchema schema = xmp.getSchema(namespace);
                if (schema == null)
                {
                    schema = tm.getSchemaFactory(namespace).createXMPSchema(xmp, property.getPrefix());
                    loadAttributes(schema, description);
                }
                ComplexPropertyContainer container = schema.getContainer();
                // create property
                createProperty(xmp, property, type, container);
View Full Code Here

    }

    private void manageSimpleType(XMPMetadata xmp, Element property, Types type, ComplexPropertyContainer container)
            throws XmpParsingException
    {
        TypeMapping tm = xmp.getTypeMapping();
        String prefix = property.getPrefix();
        String name = property.getLocalName();
        String namespace = property.getNamespaceURI();
        AbstractSimpleProperty sp = tm.instanciateSimpleProperty(namespace, prefix, name, property.getTextContent(),
                type);
        loadAttributes(sp, property);
        container.addProperty(sp);
    }
View Full Code Here

    }

    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)
        {
View Full Code Here

    private void parseDescriptionInner(XMPMetadata xmp, Element description, ComplexPropertyContainer parentContainer)
            throws XmpParsingException
    {
        nsFinder.push(description);
        TypeMapping tm = xmp.getTypeMapping();
        try
        {
            List<Element> properties = DomHelper.getElementChildren(description);
            for (Element property : properties)
            {
                String name = property.getLocalName();
                PropertyType dtype = checkPropertyDefinition(xmp, DomHelper.getQName(property));
                PropertyType ptype = tm.getStructuredPropMapping(dtype.type()).getPropertyType(name);
                // create property
                createProperty(xmp, property, ptype, parentContainer);
            }
        }
        finally
View Full Code Here

        }
        else
        {
            // no child, so consider as simple text
            String text = liElement.getTextContent();
            TypeMapping tm = xmp.getTypeMapping();
            AbstractSimpleProperty sp = tm.instanciateSimpleProperty(descriptor.getNamespaceURI(),
                    descriptor.getPrefix(), descriptor.getLocalPart(), text, Types.Text);
            loadAttributes(sp, liElement);
            return sp;
        }
    }
View Full Code Here

    }

    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);
                    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())
View Full Code Here

TOP

Related Classes of org.apache.xmpbox.type.TypeMapping

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.