Ejemplo n.º 1
0
        /**
         * Test getProperty, deleteProperty and related methods.
         * @param meta a predefined <code>XmpMeta</code> object.
         * @throws XmpException Forwards exceptions
         */
        private static void CoverGetPropertyMethods(IXmpMeta meta)
        {
            writeMajorLabel("Test getProperty, deleteProperty and related methods");

            meta.DeleteProperty(TestData.NS1, "QualProp1");     // ! Start with fresh qualifiers.
            meta.DeleteProperty(TestData.NS1, "ns1:QualProp2");
            meta.DeleteProperty(TestData.NS1, "ns1:QualProp3");
            meta.DeleteProperty(TestData.NS1, "QualProp4");


            writeMinorLabel("Set properties with qualifier");

            meta.SetProperty(TestData.NS1, "QualProp1", "Prop value");
            meta.SetQualifier(TestData.NS1, "QualProp1", TestData.NS2, "Qual1", "Qual1 value");

            meta.SetProperty(TestData.NS1, "QualProp2", "Prop value");
            meta.SetQualifier(TestData.NS1, "QualProp2", XmpConstants.NsXml, "lang", "en-us");

            meta.SetProperty(TestData.NS1, "QualProp3", "Prop value");
            meta.SetQualifier(TestData.NS1, "QualProp3", XmpConstants.NsXml, "lang", "en-us");
            meta.SetQualifier(TestData.NS1, "QualProp3", TestData.NS2, "Qual", "Qual value");

            meta.SetProperty(TestData.NS1, "QualProp4", "Prop value");
            meta.SetQualifier(TestData.NS1, "QualProp4", TestData.NS2, "Qual", "Qual value");
            meta.SetQualifier(TestData.NS1, "QualProp4", XmpConstants.NsXml, "lang", "en-us");

            printXmpMeta(meta, "XMP object");


            writeMinorLabel("Get simple properties");

            var property = meta.GetProperty(TestData.NS1, "Prop");

            log.WriteLine("getProperty ns1:Prop =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetProperty(TestData.NS1, "ns1:XMLProp");
            log.WriteLine("getProperty ns1:XMLProp =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetProperty(TestData.NS1, "ns1:URIProp");
            log.WriteLine("getProperty ns1:URIProp =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetArrayItem(TestData.NS1, "Bag", 2);
            log.WriteLine("getArrayItem ns1:Bag[2] =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            try
            {
                meta.GetArrayItem(null, "ns1:Bag", 1);
            }
            catch (XmpException e)
            {
                log.WriteLine("getArrayItem with no schema URI - threw XmpException #" + e.GetErrorCode() + " :   " + e.Message + ")");
            }


            writeMinorLabel("Get array items and struct fields");

            property = meta.GetArrayItem(TestData.NS1, "ns1:Seq", 1);
            log.WriteLine("getArrayItem ns1:Seq[1] =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetArrayItem(TestData.NS1, "ns1:Alt", 1);
            log.WriteLine("getArrayItem ns1:Alt[1] =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");
            log.WriteLine();

            property = meta.GetStructField(TestData.NS1, "Struct", TestData.NS2, "Field1");
            log.WriteLine("getStructField ns1:Struct/ns2:Field1 =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetStructField(TestData.NS1, "ns1:Struct", TestData.NS2, "Field2");
            log.WriteLine("getStructField ns1:Struct/ns2:Field2 =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetStructField(TestData.NS1, "ns1:Struct", TestData.NS2, "ns2:Field3");
            log.WriteLine("getStructField ns1:Struct/ns2:Field3 =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetStructField(TestData.NS1, "ns1:Struct", TestData.NS2, "ns2:Field3");
            log.WriteLine("getStructField ns1:Struct/ns2:Field3 =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetStructField(TestData.NS1, "ns1:Struct", TestData.NS2, "ns2:Field3");
            log.WriteLine("getStructField ns1:Struct/ns2:Field3 =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");
            log.WriteLine();


            writeMinorLabel("Get qualifier");

            property = meta.GetQualifier(TestData.NS1, "QualProp1", TestData.NS2, "Qual1");
            log.WriteLine("getQualifier  ns1:QualProp1/?ns2:Qual1 =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            try
            {
                meta.GetQualifier(null, "ns1:QualProp1", TestData.NS2, "Qual1");
            }
            catch (XmpException e)
            {
                log.WriteLine("getQualifier with no schema URI - threw XmpException #" + e.GetErrorCode() + " :   " + e.Message);
            }

            property = meta.GetQualifier(TestData.NS1, "QualProp3", XmpConstants.NsXml, "xml:lang");
            log.WriteLine("getQualifier ns1:QualProp3/@xml-lang =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetQualifier(TestData.NS1, "QualProp3", TestData.NS2, "ns2:Qual");
            log.WriteLine("getQualifier ns1:QualProp3/?ns2:Qual =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");
            log.WriteLine();


            writeMinorLabel("Get non-simple properties");

            property = meta.GetProperty(TestData.NS1, "Bag");
            log.WriteLine("getProperty ns1:Bag =   " + property.Value + " ("
                          + property.Options.GetOptionsString() + ")");

            property = meta.GetProperty(TestData.NS1, "Seq");
            log.WriteLine("getProperty ns1:Seq =   " + property.Value + " ("
                          + property.Options.GetOptionsString() + ")");

            property = meta.GetProperty(TestData.NS1, "Alt");
            log.WriteLine("getProperty ns1:Alt =   " + property.Value + " ("
                          + property.Options.GetOptionsString() + ")");

            property = meta.GetProperty(TestData.NS1, "Struct");
            log.WriteLine("getProperty ns1:Struct =   " + property.Value + " ("
                          + property.Options.GetOptionsString() + ")");
            log.WriteLine();


            writeMinorLabel("Get not existing properties");

            try
            {
                meta.GetProperty("ns:bogus/", "Bogus");
            }
            catch (XmpException e)
            {
                log.WriteLine("getProperty with bogus schema URI - threw XmpException #" + e.GetErrorCode() + " :   " + e.Message);
            }

            property = meta.GetProperty(TestData.NS1, "Bogus");
            log.WriteLine("getProperty ns1:Bogus (not existing) =   " + property);

            property = meta.GetArrayItem(TestData.NS1, "Bag", 99);
            log.WriteLine("ArrayItem ns1:Bag[99] (not existing) =   " + property);

            property = meta.GetStructField(TestData.NS1, "Struct", TestData.NS2, "Bogus");
            log.WriteLine("getStructField ns1:Struct/ns2:Bogus (not existing) =   " + property);

            property = meta.GetQualifier(TestData.NS1, "Prop", TestData.NS2, "Bogus");
            log.WriteLine("getQualifier ns1:Prop/?ns2:Bogus (not existing) =   " + property);
        }
        private static void ProcessXmpTag([NotNull] IXmpMeta meta, [NotNull] XmpDirectory directory, int tagType, FormatType formatCode)
        {
            string schemaNs;
            string propName;

            if (!XmpDirectory.TagSchemaMap.TryGetValue(tagType, out schemaNs) || !XmpDirectory.TagPropNameMap.TryGetValue(tagType, out propName))
            {
                return;
            }

            var property = meta.GetPropertyString(schemaNs, propName);

            if (property == null)
            {
                return;
            }

            switch (formatCode)
            {
            case FormatType.Rational:
            {
                // TODO introduce Rational.TryParse
                var rationalParts = property.Split('/').Take(2).ToArray();
                if (rationalParts.Length == 2)
                {
                    // TODO should this really be parsed as float?
                    float numerator;
                    float denominator;
                    if (float.TryParse(rationalParts[0], out numerator) && float.TryParse(rationalParts[1], out denominator))
                    {
                        directory.Set(tagType, new Rational((long)numerator, (long)denominator));
                    }
                    else
                    {
                        directory.AddError($"Unable to parse XMP property {propName} as a Rational.");
                    }
                }
                else
                {
                    directory.AddError($"Error in rational format for tag {tagType}");
                }
                break;
            }

            case FormatType.Int:
            {
                int value;
                if (int.TryParse(property, out value))
                {
                    directory.Set(tagType, value);
                }
                else
                {
                    directory.AddError($"Unable to parse XMP property {propName} as an int.");
                }
                break;
            }

            case FormatType.Double:
            {
                double value;
                if (double.TryParse(property, out value))
                {
                    directory.Set(tagType, value);
                }
                else
                {
                    directory.AddError($"Unable to parse XMP property {propName} as a double.");
                }
                break;
            }

            case FormatType.String:
            {
                directory.Set(tagType, property);
                break;
            }

            case FormatType.StringArray:
            {
                // XMP iterators are 1-based
                var count = meta.CountArrayItems(schemaNs, propName);
                var array = new string[count];
                for (var i = 1; i <= count; i++)
                {
                    array[i - 1] = meta.GetArrayItem(schemaNs, propName, i).Value;
                }
                directory.Set(tagType, array);
                break;
            }

            default:
            {
                directory.AddError($"Unknown format code {formatCode} for tag {tagType}");
                break;
            }
            }
        }
Ejemplo n.º 3
0
        /**
         * Test getProperty, deleteProperty and related methods.
         * @param meta a predefined <code>XmpMeta</code> object.
         * @throws XmpException Forwards exceptions
         */
        private static void CoverGetPropertyMethods(IXmpMeta meta)
        {
            writeMajorLabel ("Test getProperty, deleteProperty and related methods");

            meta.DeleteProperty (TestData.NS1, "QualProp1");    // ! Start with fresh qualifiers.
            meta.DeleteProperty (TestData.NS1, "ns1:QualProp2");
            meta.DeleteProperty (TestData.NS1, "ns1:QualProp3");
            meta.DeleteProperty (TestData.NS1, "QualProp4");

            writeMinorLabel("Set properties with qualifier");

            meta.SetProperty (TestData.NS1, "QualProp1", "Prop value");
            meta.SetQualifier (TestData.NS1, "QualProp1", TestData.NS2, "Qual1", "Qual1 value");

            meta.SetProperty (TestData.NS1, "QualProp2", "Prop value");
            meta.SetQualifier (TestData.NS1, "QualProp2", XmpConstants.NsXml, "lang", "en-us");

            meta.SetProperty (TestData.NS1, "QualProp3", "Prop value");
            meta.SetQualifier (TestData.NS1, "QualProp3", XmpConstants.NsXml, "lang", "en-us");
            meta.SetQualifier (TestData.NS1, "QualProp3", TestData.NS2, "Qual", "Qual value");

            meta.SetProperty (TestData.NS1, "QualProp4", "Prop value");
            meta.SetQualifier (TestData.NS1, "QualProp4", TestData.NS2, "Qual", "Qual value");
            meta.SetQualifier (TestData.NS1, "QualProp4", XmpConstants.NsXml, "lang", "en-us");

            printXmpMeta (meta, "XMP object");

            writeMinorLabel("Get simple properties");

            var property = meta.GetProperty(TestData.NS1, "Prop");
            log.WriteLine("getProperty ns1:Prop =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetProperty(TestData.NS1, "ns1:XMLProp");
            log.WriteLine("getProperty ns1:XMLProp =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetProperty(TestData.NS1, "ns1:URIProp");
            log.WriteLine("getProperty ns1:URIProp =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetArrayItem(TestData.NS1, "Bag", 2);
            log.WriteLine("getArrayItem ns1:Bag[2] =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            try
            {
                meta.GetArrayItem(null, "ns1:Bag", 1);
            }
            catch (XmpException e)
            {
                log.WriteLine("getArrayItem with no schema URI - threw XmpException #" + e.GetErrorCode() +" :   " + e.Message + ")");
            }

            writeMinorLabel("Get array items and struct fields");

            property = meta.GetArrayItem(TestData.NS1, "ns1:Seq", 1);
            log.WriteLine("getArrayItem ns1:Seq[1] =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetArrayItem(TestData.NS1, "ns1:Alt", 1);
            log.WriteLine("getArrayItem ns1:Alt[1] =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");
            log.WriteLine();

            property = meta.GetStructField(TestData.NS1, "Struct", TestData.NS2, "Field1");
            log.WriteLine("getStructField ns1:Struct/ns2:Field1 =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetStructField(TestData.NS1, "ns1:Struct", TestData.NS2, "Field2");
            log.WriteLine("getStructField ns1:Struct/ns2:Field2 =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetStructField(TestData.NS1, "ns1:Struct", TestData.NS2, "ns2:Field3");
            log.WriteLine("getStructField ns1:Struct/ns2:Field3 =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetStructField(TestData.NS1, "ns1:Struct", TestData.NS2, "ns2:Field3");
            log.WriteLine("getStructField ns1:Struct/ns2:Field3 =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetStructField(TestData.NS1, "ns1:Struct", TestData.NS2, "ns2:Field3");
            log.WriteLine("getStructField ns1:Struct/ns2:Field3 =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");
            log.WriteLine();

            writeMinorLabel("Get qualifier");

            property = meta.GetQualifier(TestData.NS1, "QualProp1", TestData.NS2, "Qual1");
            log.WriteLine("getQualifier  ns1:QualProp1/?ns2:Qual1 =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            try
            {
                meta.GetQualifier(null, "ns1:QualProp1", TestData.NS2, "Qual1");
            }
            catch (XmpException e)
            {
                log.WriteLine("getQualifier with no schema URI - threw XmpException #" + e.GetErrorCode() + " :   " + e.Message);
            }

            property = meta.GetQualifier(TestData.NS1, "QualProp3", XmpConstants.NsXml, "xml:lang");
            log.WriteLine("getQualifier ns1:QualProp3/@xml-lang =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");

            property = meta.GetQualifier(TestData.NS1, "QualProp3", TestData.NS2, "ns2:Qual");
            log.WriteLine("getQualifier ns1:QualProp3/?ns2:Qual =   " + property.Value + " (" + property.Options.GetOptionsString() + ")");
            log.WriteLine();

            writeMinorLabel("Get non-simple properties");

            property = meta.GetProperty(TestData.NS1, "Bag");
            log.WriteLine("getProperty ns1:Bag =   " + property.Value + " ("
                    + property.Options.GetOptionsString() + ")");

            property = meta.GetProperty(TestData.NS1, "Seq");
            log.WriteLine("getProperty ns1:Seq =   " + property.Value + " ("
                    + property.Options.GetOptionsString() + ")");

            property = meta.GetProperty(TestData.NS1, "Alt");
            log.WriteLine("getProperty ns1:Alt =   " + property.Value + " ("
                    + property.Options.GetOptionsString() + ")");

            property = meta.GetProperty(TestData.NS1, "Struct");
            log.WriteLine("getProperty ns1:Struct =   " + property.Value + " ("
                    + property.Options.GetOptionsString() + ")");
            log.WriteLine();

            writeMinorLabel("Get not existing properties");

            try
            {
                meta.GetProperty("ns:bogus/", "Bogus");
            }
            catch (XmpException e)
            {
                log.WriteLine("getProperty with bogus schema URI - threw XmpException #" + e.GetErrorCode() + " :   " + e.Message);
            }

            property = meta.GetProperty (TestData.NS1, "Bogus");
            log.WriteLine ("getProperty ns1:Bogus (not existing) =   " + property);

            property = meta.GetArrayItem(TestData.NS1, "Bag", 99);
            log.WriteLine ("ArrayItem ns1:Bag[99] (not existing) =   " + property);

            property = meta.GetStructField(TestData.NS1, "Struct", TestData.NS2, "Bogus");
            log.WriteLine ("getStructField ns1:Struct/ns2:Bogus (not existing) =   " + property);

            property = meta.GetQualifier (TestData.NS1, "Prop", TestData.NS2, "Bogus");
            log.WriteLine ("getQualifier ns1:Prop/?ns2:Bogus (not existing) =   " + property);
        }
        /// <summary>Reads an property value with given namespace URI and property name.</summary>
        /// <remarks>Reads an property value with given namespace URI and property name. Add property value to directory if exists</remarks>
        /// <exception cref="XmpException"/>
        private static void ProcessXmpTag([NotNull] IXmpMeta meta, [NotNull] XmpDirectory directory, int tagType, FormatType formatCode)
        {
            string schemaNs;
            string propName;

            if (!XmpDirectory.TagSchemaMap.TryGetValue(tagType, out schemaNs) || !XmpDirectory.TagPropNameMap.TryGetValue(tagType, out propName))
            {
                return;
            }

            var property = meta.GetPropertyString(schemaNs, propName);

            if (property == null)
            {
                return;
            }

            switch (formatCode)
            {
            case FormatType.Rational:
            {
                var rationalParts = property.Split(new[] { '/' }, 2);
                if (rationalParts.Length == 2)
                {
                    try
                    {
                        var rational = new Rational((long)float.Parse(rationalParts[0]), (long)float.Parse(rationalParts[1]));
                        directory.Set(tagType, rational);
                    }
                    catch (FormatException)
                    {
                        directory.AddError($"Unable to parse XMP property {propName} as a Rational.");
                    }
                }
                else
                {
                    directory.AddError("Error in rational format for tag " + tagType);
                }
                break;
            }

            case FormatType.Int:
            {
                try
                {
                    directory.Set(tagType, int.Parse(property));
                }
                catch (FormatException)
                {
                    directory.AddError($"Unable to parse XMP property {propName} as an int.");
                }
                break;
            }

            case FormatType.Double:
            {
                try
                {
                    directory.Set(tagType, double.Parse(property));
                }
                catch (FormatException)
                {
                    directory.AddError($"Unable to parse XMP property {propName} as an double.");
                }
                break;
            }

            case FormatType.String:
            {
                directory.Set(tagType, property);
                break;
            }

            case FormatType.StringArray:
            {
                //XMP iterators are 1-based
                var count = meta.CountArrayItems(schemaNs, propName);
                var array = new string[count];
                for (var i = 1; i <= count; ++i)
                {
                    array[i - 1] = meta.GetArrayItem(schemaNs, propName, i).Value;
                }
                directory.Set(tagType, array);
                break;
            }

            default:
            {
                directory.AddError($"Unknown format code {formatCode} for tag {tagType}");
                break;
            }
            }
        }