Exemple #1
0
        private PlistDictionary LoadDictionaryContents(XmlReader reader, PlistDictionary dict)
        {
            Debug.Assert(reader.NodeType == XmlNodeType.Element && reader.LocalName == "key");
            while (!reader.EOF && reader.NodeType == XmlNodeType.Element)
            {
                //string key = reader.ReadElementString ();
                string key = reader.ReadElementContentAsString();
                while (reader.NodeType != XmlNodeType.Element && reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        throw new Exception(String.Format("No value found for key {0}", key));
                    }
                }
                PlistObjectBase result = LoadFromNode(reader);
                if (result != null)
                {
                    dict.Add(key, result);
                }

                // when there is no whitespace between nodes, we might already be at
                // the next key element, so reading to next sibling would jump over
                // the next (current) key element
                if (!"key".Equals(reader.Name))
                {
                    reader.ReadToNextSibling("key");
                }
            }
            return(dict);
        }
Exemple #2
0
            protected PlistObjectBase ReadValue(ContentReader input)
            {
                var type = (ValueType)input.ReadByte();

                switch (type)
                {
                case ValueType.Array:
                    var count = input.ReadInt32();
                    var array = new PlistArray(count);
                    for (int i = 0; i < count; i++)
                    {
                        array.Add(ReadValue(input));
                    }
                    return(array);

                case ValueType.Bool:
                    return(new PlistBoolean(input.ReadBoolean()));

                case ValueType.Data:
                    count = input.ReadInt32();
                    return(new PlistData(input.ReadBytes(count)));

                case ValueType.Date:
                    return(new PlistDate(input.ReadObject <DateTime>()));

                case ValueType.Dictionary:
                    count = input.ReadInt32();
                    var dict = new PlistDictionary();
                    for (int i = 0; i < count; i++)
                    {
                        string key = input.ReadString();
                        dict.Add(key, ReadValue(input));
                    }
                    return(dict);

                case ValueType.Integer:
                    return(new PlistInteger(input.ReadInt32()));

                case ValueType.Null:
                    return(new PlistNull());

                case ValueType.Real:
                    return(new PlistReal(input.ReadSingle()));

                case ValueType.String:
                    return(new PlistString(input.ReadString()));

                default:
                    throw new InvalidOperationException();
                }
            }
Exemple #3
0
        private PlistObjectBase LoadFromNode(XmlReader reader)
        {
            Debug.Assert(reader.NodeType == XmlNodeType.Element);
            bool isEmpty = reader.IsEmptyElement;

            switch (reader.LocalName)
            {
            case "dict":
                var dict = new PlistDictionary(true);
                if (!isEmpty)
                {
                    if (reader.ReadToDescendant("key"))
                    {
                        dict = LoadDictionaryContents(reader, dict);
                    }
                    reader.ReadEndElement();
                }
                return(dict);

            case "array":
                if (isEmpty)
                {
                    return(new PlistArray());
                }

                //advance to first node
                reader.ReadStartElement();
                while (reader.Read() && reader.NodeType != XmlNodeType.Element)
                {
                    ;
                }

                // HACK: plist data in iPods is not even valid in some cases! Way to go Apple!
                // This hack checks to see if they really meant for this array to be a dict.
                if (reader.LocalName == "key")
                {
                    var ret = LoadDictionaryContents(reader, new PlistDictionary(true));
                    reader.ReadEndElement();
                    return(ret);
                }

                var arr = new PlistArray();
                do
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        var val = LoadFromNode(reader);
                        if (val != null)
                        {
                            arr.Add(val);
                        }
                    }
                } while (reader.Read() && reader.NodeType != XmlNodeType.EndElement);
                reader.ReadEndElement();
                return(arr);

            case "string":
                return(new PlistString(reader.ReadElementContentAsString()));

            case "integer":
                return(new PlistInteger(reader.ReadElementContentAsInt()));

            case "real":
                return(new PlistReal(reader.ReadElementContentAsFloat()));

            case "false":
                reader.ReadStartElement();
                if (!isEmpty)
                {
                    reader.ReadEndElement();
                }
                return(new PlistBoolean(false));

            case "true":
                reader.ReadStartElement();
                if (!isEmpty)
                {
                    reader.ReadEndElement();
                }
                return(new PlistBoolean(true));

            case "data":
                return(new PlistData(reader.ReadElementContentAsString()));

            case "date":
#if NETFX_CORE
                return(new PlistDate(DateTime.Parse(reader.ReadElementContentAsString())));
#else
                return(new PlistDate(reader.ReadElementContentAsDateTime()));
#endif
            default:
                throw new XmlException(String.Format("Plist Node `{0}' is not supported", reader.LocalName));
            }
        }