예제 #1
0
        private void InternalReadElement(XElementData reader, object receiver, ReadSettings settings,

                                         QName root, ObjectElementInfo elements, string modelName)
        {
            foreach (var item in reader.Current.Elements())
            {
                QName name = item.Name.ToQName();
                ObjectPropertyInfo element = elements.GetObjectPerpertyInfo(name,
                                                                            () => reader.Current.Attribute(ToolkitConst.VERSION).Value);
                if (element != null)
                {
                    XElementData next = new XElementData {
                        Current = item, Root = reader.Root
                    };
                    element.Attribute.ReadObject(this, next, receiver, settings, element, null);
                }
                else
                {
                    element = SerializerUtil.CustomRead(receiver, name.LocalName, modelName,
                                                        () => reader.Current.Attribute(ToolkitConst.VERSION).Value);
                    if (element != null)
                    {
                        element.Attribute.ReadObject(this, reader, receiver, settings, element, null);
                    }
                }
            }
        }
예제 #2
0
        public object BeginWrite(object writer, object receiver, WriteSettings settings, QName root)
        {
            XElementData doc         = writer.Convert <XElementData>();
            XElement     rootElement = new XElement(root.ToXName());

            doc.Root.Add(rootElement);
            return(rootElement);
        }
예제 #3
0
        public void ReadTagElement(TagElementAttribute attribute, object reader,
                                   object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElementData data = reader.Convert <XElementData>();

            //XElementData next = new XElementData { Current = data.Current.Elements().First(), Root = data.Root };
            InternalReadElement(data, receiver, settings, info.QName, attribute.ChildElements, info.ModelName);
            SerializerUtil.CheckElementRequired(attribute.ChildElements, receiver);
        }
예제 #4
0
        public object CreateReader(string input, ReadSettings settings)
        {
            XDocument    doc  = XDocument.Parse(input);
            XElementData data = new XElementData {
                Root = doc, Current = doc.Root
            };

            return(data);
        }
예제 #5
0
        public void ReadComplexContent(ComplexContentAttribute attribute, object reader,
                                       object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElement current = XElementData.GetCurrent(reader);

            string value = current.HasElements ? current.FirstNode.ToString() : null;

            SerializerUtil.CheckRequiredContent(attribute, receiver, info, value);
            SerializerUtil.SetObjectValue(receiver, settings, info, value, false);
        }
예제 #6
0
        public void ReadTextContent(TextContentAttribute attribute, object reader,
                                    object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElement current = XElementData.GetCurrent(reader);

            string value = current.Value;

            SerializerUtil.CheckRequiredContent(attribute, receiver, info, value);
            SerializerUtil.SetObjectValue(receiver, settings, info, value, attribute.AutoTrim);
        }
예제 #7
0
        public void ReadComplexElement(SimpleComplexElementAttribute attribute, object reader,
                                       object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElement current = XElementData.GetCurrent(reader);

            string value    = current.HasElements ? current.FirstNode.ToString() : null;
            object objValue = SerializerUtil.GetPropertyObject(receiver, settings, info, value, false);

            SerializerUtil.AddElementValue(attribute, receiver, info, objValue);
        }
예제 #8
0
        public void ReadObject(object reader, object receiver, string modelName, ReadSettings settings,
                               QName root, object serializerData)
        {
            TkDebug.AssertArgumentNull(receiver, "receiver", this);
            TkDebug.AssertArgumentNull(settings, "settings", this);
            TkDebug.AssertArgumentNull(root, "root", this);

            XElementData currentData = reader.Convert <XElementData>();
            XElement     current     = currentData.Current;
            ObjectInfo   info        = ObjectInfo.Create(receiver, modelName);

            if (info.IsObjectContext)
            {
                TkDebug.ThrowIfNoGlobalVariable();
                BaseGlobalVariable.Current.ObjectContext.Push(receiver);
            }

            foreach (var item in info.Attributes)
            {
                item.Attribute.ReadObject(this, reader, receiver, settings, item, serializerData);
            }

            if (current.IsEmpty)
            {
                SerializerUtil.ReadObjectCallBack(receiver);
                if (info.IsObjectContext)
                {
                    BaseGlobalVariable.Current.ObjectContext.Pop();
                }
                return;
            }

            bool readElement = false;

            if (info.Content != null)
            {
                info.Content.Attribute.ReadObject(this, reader, receiver, settings,
                                                  info.Content, serializerData);
            }
            else
            {
                readElement = true;
                InternalReadElement(currentData, receiver, settings, root, info.Elements, modelName);
            }

            SerializerUtil.ReadObjectCallBack(receiver);
            if (readElement)
            {
                SerializerUtil.CheckElementRequired(info.Elements, receiver);
            }
            if (info.IsObjectContext)
            {
                BaseGlobalVariable.Current.ObjectContext.Pop();
            }
        }
예제 #9
0
        public void ReadElement(SimpleElementAttribute attribute, object reader,
                                object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElement current = XElementData.GetCurrent(reader);

            string value    = current.Value;
            object objValue = SerializerUtil.GetPropertyObject(receiver, settings, info,
                                                               value, attribute.AutoTrim);

            SerializerUtil.AddElementValue(attribute, receiver, info, objValue);
        }
예제 #10
0
        public void ReadAttribute(SimpleAttributeAttribute attribute, object reader,
                                  object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElement current = XElementData.GetCurrent(reader);

            XName      name  = info.QName.ToXName();
            XAttribute attr  = current.Attribute(name);
            string     value = attr == null ? null : attr.Value;

            SerializerUtil.CheckRequiredAttribute(attribute, receiver, info, value);
            SerializerUtil.SetObjectValue(receiver, settings, info, value, attribute.AutoTrim);
        }
예제 #11
0
        public void EndWrite(object writer, object receiver, WriteSettings settings)
        {
            XElementData doc = writer.Convert <XElementData>();

            if (doc.Stream != null)
            {
                StreamWriter sw = new StreamWriter(doc.Stream, settings.Encoding);
                using (sw)
                {
                    sw.Write(doc.Root.ToString());
                }
            }
        }
예제 #12
0
        public void ReadDictionary(DictionaryAttribute attribute, object reader,
                                   object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElement    current    = XElementData.GetCurrent(reader);
            IDictionary dict       = attribute.GetDictionary(receiver, info);
            Type        objectType = info.ObjectType;

            foreach (XElement child in current.Elements())
            {
                string nodeName  = child.Name.LocalName;
                string nodeValue = child.Value;
                if (attribute.AutoTrim && nodeValue != null)
                {
                    nodeValue = nodeValue.Trim();
                }
                object objValue = SerializerUtil.GetPropertyObject(receiver, settings, info, nodeValue, objectType);
                dict[nodeName] = objValue;
            }
        }
예제 #13
0
        public void ReadDynamicDictionary(DynamicDictionaryAttribute attribute, object reader,
                                          object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElementData currentData = reader.Convert <XElementData>();
            XElement     current     = currentData.Current;
            IDictionary  dict        = attribute.GetDictionary(receiver, info);
            var          configData  = attribute.PlugInFactory.ConfigData;
            //var propertyInfo = info.Convert<ReflectorObjectPropertyInfo>().Property;
            var elementReader = new ConfigFactoryElementReader(attribute, info, info.ModelName);

            foreach (XElement child in current.Elements())
            {
                string             nodeName = child.Name.LocalName;
                QName              qName    = child.Name.ToQName();
                ObjectPropertyInfo subPropertyInfo;
                if (elementReader.SupportVersion)
                {
                    var    verAttr = child.Attribute(ToolkitConst.VERSION);
                    string version = verAttr != null ? verAttr.Value : string.Empty;
                    subPropertyInfo = elementReader[nodeName, version];
                }
                else
                {
                    subPropertyInfo = elementReader[nodeName];
                }
                if (subPropertyInfo != null)
                {
                    ObjectElementAttribute elementAttr = subPropertyInfo.Attribute.Convert <ObjectElementAttribute>();
                    Type   objectType = elementAttr.ObjectType;
                    object subObject  = elementAttr.UseConstructor ? ObjectUtil.CreateObjectWithCtor(objectType)
                        : ObjectUtil.CreateObject(objectType);
                    XElementData next = new XElementData {
                        Current = child, Root = currentData.Root
                    };

                    Read(next, subObject, info.ModelName, settings, QName.Get(nodeName), null);
                    SerializerUtil.SetParent(receiver, subObject);
                    dict[nodeName] = subObject;
                }
            }
        }
예제 #14
0
        public bool ReadToRoot(object reader, QName root)
        {
            XElementData data     = reader.Convert <XElementData>();
            XName        rootName = root.ToXName();

            if (data.Current.Name == rootName)
            {
                return(true);
            }
            else
            {
                var element = (from item in data.Root.Descendants()
                               where item.Name == rootName
                               select item).FirstOrDefault();
                if (element == null)
                {
                    return(false);
                }
                data.Current = element;
                return(true);
            }
        }
예제 #15
0
        public void ReadObjectDictionary(ObjectDictionaryAttribute attribute, object reader,
                                         object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XElementData currentData = reader.Convert <XElementData>();
            XElement     current     = currentData.Current;
            IDictionary  dict        = attribute.GetDictionary(receiver, info);
            Type         objectType  = info.ObjectType;

            foreach (XElement child in current.Elements())
            {
                string nodeName  = child.Name.LocalName;
                object subObject = attribute.UseConstructor ?
                                   ObjectUtil.CreateObjectWithCtor(objectType) : ObjectUtil.CreateObject(objectType);

                XElementData next = new XElementData {
                    Current = child, Root = currentData.Root
                };
                Read(next, subObject, info.ModelName, settings, QName.Get(nodeName), null);
                SerializerUtil.SetParent(receiver, subObject);
                dict[nodeName] = subObject;
            }
        }
예제 #16
0
 public XElementData(XElementData data, XElement element)
 {
     Root    = data.Root;
     Current = element;
     Stream  = data.Stream;
 }