Esempio n. 1
0
		bool ReadDataNode (DataItem item, List<string> lines, int lastLine, string prefix, ref int lineNum)
		{
			string s = lines [lineNum].Trim (' ','\t');
			
			if (s.Length == 0) {
				lineNum++;
				return true;
			}
			
			// Check if the line belongs to the current item
			if (prefix.Length > 0) {
				if (!s.StartsWith (prefix + "."))
					return false;
				s = s.Substring (prefix.Length + 1);
			} else {
				if (s.StartsWith ("$"))
					return false;
			}
			
			int i = s.IndexOf ('=');
			if (i == -1) {
				lineNum++;
				return true;
			}

			string name = s.Substring (0, i).Trim (' ','\t');
			if (name.Length == 0) {
				lineNum++;
				return true;
			}
			
			string value = s.Substring (i+1).Trim (' ','\t');
			if (value.StartsWith ("$")) {
				// New item
				DataItem child = new DataItem ();
				child.Name = name;
				lineNum++;
				while (lineNum <= lastLine) {
					if (!ReadDataNode (child, lines, lastLine, value, ref lineNum))
						break;
				}
				item.ItemData.Add (child);
			}
			else {
				value = DecodeString (value);
				DataValue val = new DataValue (name, value);
				item.ItemData.Add (val);
				lineNum++;
			}
			return true;
		}
		public virtual bool StoreAsAttribute (DataValue val)
		{
			if (StoreAllInElements)
				return StoreInElementExceptions != null && ((IList)StoreInElementExceptions).Contains (val.Name);
			else
				return true;
		}
		static bool ReadDataNode (DataItem item, KeyValuePair<string,string>[] lines, int lastLine, string prefix, Dictionary<DataNode,int> ids, ref int lineNum)
		{
			var s = lines [lineNum];

			string name = s.Key;
			if (name.Length == 0) {
				lineNum++;
				return true;
			}

			// Check if the line belongs to the current item
			if (prefix.Length > 0) {
				if (!s.Key.StartsWith (prefix + ".", StringComparison.Ordinal))
					return false;
				name = s.Key.Substring (prefix.Length + 1);
			} else {
				if (s.Key.StartsWith ("$", StringComparison.Ordinal))
					return false;
			}

			string value = s.Value;
			if (value.StartsWith ("$", StringComparison.Ordinal)) {
				// New item
				DataItem child = new DataItem ();
				child.Name = name;
				int id;
				if (ids != null && int.TryParse (value.Substring (1), out id))
					ids [child] = id;
				lineNum++;
				while (lineNum <= lastLine) {
					if (!ReadDataNode (child, lines, lastLine, value, ids, ref lineNum))
						break;
				}
				item.ItemData.Add (child);
			} else {
				value = DecodeString (value);
				DataValue val = new DataValue (name, value);
				item.ItemData.Add (val);
				lineNum++;
			}
			return true;
		}
Esempio n. 4
0
        internal protected override object OnDeserialize(SerializationContext serCtx, object mapData, DataNode data)
        {
            DataItem item = data as DataItem;

            if (item == null)
            {
                throw new InvalidOperationException("Invalid value found for type '" + Name + "' " + data);
            }

            DataValue ctype = item ["ctype"] as DataValue;

            if (ctype != null && ctype.Value != Name)
            {
                bool     isFallbackType;
                DataType stype = FindDerivedType(ctype.Value, mapData, out isFallbackType);

                if (isFallbackType)
                {
                    // Remove the ctype attribute, to make sure it is not checked again
                    // by the fallback type
                    item.ItemData.Remove(ctype);
                }

                if (stype != null)
                {
                    DataNode desData = data;
                    if (stype.IsSimpleType)
                    {
                        desData = item.ItemData ["Value"];
                        if (desData == null)
                        {
                            throw new InvalidOperationException("Value node not found");
                        }
                    }
                    object sobj = stype.Deserialize(serCtx, mapData, desData);

                    // Store the original data type, so it can be serialized back
                    if (isFallbackType && sobj is IExtendedDataItem)
                    {
                        ((IExtendedDataItem)sobj).ExtendedProperties ["__raw_ctype"] = ctype.Value;
                    }

                    return(sobj);
                }
                else
                {
                    throw new InvalidOperationException("Type not found: " + ctype.Value);
                }
            }

            ConstructorInfo ctor = ValueType.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);

            if (ctor == null)
            {
                throw new InvalidOperationException("Default constructor not found for type '" + ValueType + "'");
            }

            object obj = ctor.Invoke(null);

            Deserialize(serCtx, null, item, obj);
            return(obj);
        }
Esempio n. 5
0
		DataCollection ICustomDataItem.Serialize (ITypeSerializer handler)
		{
			DataCollection data = handler.Serialize (this);
			DataItem items = new DataItem ();
			items.Name = "Items";
			items.UniqueNames = false;
			string baseDir = Path.GetDirectoryName (handler.SerializationContext.BaseFile);
			foreach (WorkspaceItem it in Items) {
				DataValue item = new DataValue ("Item", FileService.AbsoluteToRelativePath (baseDir, it.FileName));
				items.ItemData.Add (item);
			}
			data.Add (items);
			return data;
		}
Esempio n. 6
0
        public DataNode Read(XmlReader reader)
        {
            DataItem item = new DataItem();

            item.UniqueNames = false;
            reader.MoveToContent();
            string name = reader.LocalName;

            item.Name = name;

            while (reader.MoveToNextAttribute())
            {
                if (reader.LocalName == "xmlns")
                {
                    continue;
                }
                DataNode data = ReadAttribute(reader.LocalName, reader.Value);
                if (data != null)
                {
                    DataValue val = data as DataValue;
                    if (val != null)
                    {
                        val.StoreAsAttribute = true;
                    }
                    item.ItemData.Add(data);
                }
            }

            reader.MoveToElement();
            if (reader.IsEmptyElement)
            {
                reader.Skip();
                return(item);
            }

            reader.ReadStartElement();

            string text = "";

            while (reader.NodeType != XmlNodeType.EndElement)
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    DataNode data = ReadChild(reader, item);
                    if (data != null)
                    {
                        item.ItemData.Add(data);
                    }
                }
                else if (reader.NodeType == XmlNodeType.Text || reader.NodeType == XmlNodeType.Whitespace)
                {
                    text += reader.Value;
                    reader.Skip();
                }
                else
                {
                    reader.Skip();
                }
            }

            reader.ReadEndElement();

            if (!item.HasItemData && text != "")
            {
                return(new DataValue(name, text));
            }

            return(item);
        }