Beispiel #1
0
        private IEnumerable <XmpProperty> AggregateProperties(IEnumerable value)
        {
            if (value == null)
            {
                yield break;
            }

            if (value is IDictionary <string, object> )
            {
                // unwrap generic container
                value = ((IDictionary <string, object>)value).Values;
            }

            // filter out any values which are not enumerable
            foreach (IEnumerable item in value.OfType <IEnumerable>())
            {
                // each item is either a list of properties or a sequence holding a list of properties
                IEnumerable <XmpProperty> properties =
                    (item is IEnumerable <XmpProperty>) ?
                    (IEnumerable <XmpProperty>)item :
                    this.AggregateProperties(item);

                // aggregate into single sequence
                foreach (XmpProperty property in properties)
                {
                    yield return(XmpPropertyCollection.ProcessValue(property));
                }
            }
        }
Beispiel #2
0
 /// <summary>
 /// Loads XmpProperties from XML
 /// </summary>
 /// <param name="filename"></param>
 /// <returns></returns>
 public static XmpPropertyCollection LoadFromXml(string filename)
 {
     using (TextReader reader = File.OpenText(filename))
     {
         return(XmpPropertyCollection.LoadFromXml(reader));
     }
 }
Beispiel #3
0
        private IEnumerable <XmpProperty> ExtractSchemas(BitmapMetadata metadata, IEnumerable <Enum> schemas)
        {
            foreach (Enum schema in schemas)
            {
                string name  = null;
                object value = null;

                foreach (string query in this.GetQueryForSchema(schema))
                {
                    if (String.IsNullOrEmpty(query))
                    {
                        continue;
                    }

                    value = metadata.GetQuery(query);
                    if (value == null)
                    {
                        continue;
                    }

                    name = query;
                    break;
                }

                if (String.IsNullOrEmpty(name))
                {
                    continue;
                }
                name = name.Substring(name.LastIndexOf('/') + 1);

#if DIAGNOSTICS
                Console.WriteLine("{0} => {1}: {2}", name, value != null ? value.GetType().Name : "null", Convert.ToString(value));
#endif

                if (value is BitmapMetadata)
                {
                    value = this.ProcessMetadata((BitmapMetadata)value, '/' + name, 0);
                }
                else if (value is BitmapMetadataBlob)
                {
                    value = ((BitmapMetadataBlob)value).GetBlobValue();
                }

                if (value == null)
                {
                    continue;
                }

                yield return(XmpPropertyCollection.ProcessValue(new XmpProperty
                {
                    Schema = schema,
                    Value = value
                }));
            }
        }
Beispiel #4
0
 public static XmpPropertyCollection LoadFromImage(string filename, params Enum[] schemas)
 {
     return(XmpPropertyCollection.LoadFromImage(filename, (IEnumerable <Enum>)schemas));
 }
Beispiel #5
0
 public static XmpPropertyCollection LoadFromImage(Stream stream, params Enum[] schemas)
 {
     return(XmpPropertyCollection.LoadFromImage(stream, (IEnumerable <Enum>)schemas));
 }
Beispiel #6
0
        internal static XmpProperty ProcessValue(XmpProperty property)
        {
            if (property.ValueType is XmpBasicType)
            {
                switch ((XmpBasicType)property.ValueType)
                {
                case XmpBasicType.Boolean:
                {
                    bool value;
                    if (Boolean.TryParse(Convert.ToString(property.Value), out value))
                    {
                        property.Value = value;
                    }
                    break;
                }

                case XmpBasicType.Date:
                {
                    DateTime value;
                    if (DateTime.TryParse(Convert.ToString(property.Value), out value))
                    {
                        property.Value = value;
                    }
                    break;
                }

                case XmpBasicType.Integer:
                {
                    int value;
                    if (Int32.TryParse(Convert.ToString(property.Value), out value))
                    {
                        property.Value = value;
                    }
                    break;
                }

                case XmpBasicType.Real:
                {
                    decimal value;
                    if (Decimal.TryParse(Convert.ToString(property.Value), out value))
                    {
                        property.Value = value;
                    }
                    break;
                }
                }
            }
            else if (property.ValueType is ExifType)
            {
                switch ((ExifType)property.ValueType)
                {
                case ExifType.GpsCoordinate:
                {
                    GpsCoordinate gps;
                    if (GpsCoordinate.TryParse(Convert.ToString(property.Value), out gps))
                    {
                        property.Value = gps;
                    }
                    break;
                }

                case ExifType.Rational:
                {
                    // TODO: how best to determine type of Rational<T>
                    XmpPropertyCollection.ProcessRational <long>(property);
                    break;
                }
                }
            }

            return(property);
        }
Beispiel #7
0
        private XmpProperty GetProperty(XmpProperty property, XElement elem)
        {
            switch (property.Quantity)
            {
            case XmpQuantity.Alt:
            {
                elem = elem.Element(XName.Get(property.Quantity.ToString(), RdfNamespace));
                if (elem != null)
                {
                    if (elem.Elements().Count() == 0)
                    {
                        property.Value = elem.Value;
                    }
                    else
                    {
                        if (property.ValueType is XmpBasicType &&
                            ((XmpBasicType)property.ValueType) == XmpBasicType.LangAlt)
                        {
                            // convert to dictionary
                            property.Value = elem.Elements().ToDictionary(
                                n => n.Attribute(XNamespace.Xml + "lang").Value,
                                n => (object)n.Value);
                        }
                        else
                        {
                            // TODO: find how best to process non-lang alts
                            // convert to array
                            property.Value = elem.Elements(XName.Get("li", RdfNamespace)).Select(n => n.Value).ToList();
                        }
                    }
                }
                break;
            }

            case XmpQuantity.Bag:
            case XmpQuantity.Seq:
            {
                elem = elem.Element(XName.Get(property.Quantity.ToString(), RdfNamespace));
                if (elem != null)
                {
                    if (elem.Elements().Count() == 0)
                    {
                        property.Value = elem.Value;
                    }
                    else
                    {
                        // convert to array
                        property.Value = elem.Elements(XName.Get("li", RdfNamespace)).Select(n => n.Value).ToList();
                    }
                }
                break;
            }

            default:
            {
                if (elem.Elements().Count() == 0)
                {
                    property.Value = elem.Value;
                }
                else
                {
                    // convert to dictionary
                    property.Value = elem.Elements().ToDictionary(
                        n => n.Name.LocalName,
                        n => (object)n.Value);
                }
                break;
            }
            }

            return(XmpPropertyCollection.ProcessValue(property));
        }