private QName CreateQName(string localName)
        {
            var result = QName.Get(localName);

            result.IgnoreNamespace = true;
            return(result);
        }
        //private void InternalWriteXml(object writer, object receiver, string modelName,
        //    WriteSettings settings, PrefixTable prefixTable)
        //{
        //}

        private void InternalReadElement(XmlReader reader, object receiver, ReadSettings settings,
                                         QName root, ObjectElementInfo elements, string modelName)
        {
            while (reader.Read())
            {
                XmlNodeType nodeType = reader.NodeType;
                if (nodeType == XmlNodeType.EndElement)
                {
                    QName name = QName.Get(reader);
                    if (name == root)
                    {
                        break;
                    }
                }
                if (nodeType == XmlNodeType.Element)
                {
                    QName name = QName.Get(reader);
                    ObjectPropertyInfo element = elements.GetObjectPerpertyInfo(name,
                                                                                () => reader.GetAttribute(ToolkitConst.VERSION));
                    if (element != null)
                    {
                        element.Attribute.ReadObject(this, reader, receiver, settings, element, null);
                    }
                    else
                    {
                        element = SerializerUtil.CustomRead(receiver, name.LocalName, modelName,
                                                            () => reader.GetAttribute(ToolkitConst.VERSION));
                        if (element != null)
                        {
                            element.Attribute.ReadObject(this, reader, receiver, settings, element, null);
                        }
                    }
                }
            }
        }
Beispiel #3
0
        internal QName GetQName(string name)
        {
            switch (NamespaceType)
            {
            case NamespaceType.None:
                return(name);

            default:
                return(QName.Get(name, NamespaceUri));
            }
        }
        public void ReadDynamicDictionary(DynamicDictionaryAttribute attribute, object reader,
                                          object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XmlReader xml = reader.Convert <XmlReader>();

            QName       name = info.QName;
            IDictionary dict = attribute.GetDictionary(receiver, info);

            if (xml.IsEmptyElement)
            {
                return;
            }
            var configData = attribute.PlugInFactory.ConfigData;
            //var propertyInfo = info.Convert<ReflectorObjectPropertyInfo>().Property;
            var elementReader = new ConfigFactoryElementReader(attribute, info, info.ModelName);

            while (xml.Read())
            {
                if (xml.NodeType == XmlNodeType.EndElement)
                {
                    QName nodeName = QName.Get(xml);
                    if (nodeName == name)
                    {
                        return;
                    }
                }

                if (xml.NodeType == XmlNodeType.Element)
                {
                    QName nodeName = QName.Get(xml);
                    ObjectPropertyInfo subPropertyInfo;
                    if (elementReader.SupportVersion)
                    {
                        string version = xml.GetAttribute(ToolkitConst.VERSION);
                        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);
                        Read(reader, subObject, info.ModelName, settings, nodeName, null);
                        SerializerUtil.SetParent(receiver, subObject);
                        dict[nodeName.LocalName] = subObject;
                    }
                }
            }
        }
        public void ReadComplexContent(ComplexContentAttribute attribute, object reader,
                                       object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XmlReader xml = reader.Convert <XmlReader>();

            TkDebug.Assert(xml.NodeType == XmlNodeType.Element, string.Format(ObjectUtil.SysCulture,
                                                                              "ComplexContent初始要求XmlReader在Element节点上,当前状态是{0},位置不对", xml.NodeType), this);
            QName  name  = QName.Get(xml);
            string value = ReadComplexContent(xml, name);

            SerializerUtil.CheckRequiredContent(attribute, receiver, info, value);
            SerializerUtil.SetObjectValue(receiver, settings, info, value, false);
        }
Beispiel #6
0
        public void WriteObjectDictionary(ObjectDictionaryAttribute attribute, object writer,
                                          object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            var current = serializerData.Convert <XElement>();
            //XElement element = new XElement(info.QName.ToXName());
            //current.Add(element);
            IDictionary dict = value as IDictionary;

            if (dict != null)
            {
                foreach (DictionaryEntry item in dict)
                {
                    XElement subItem = new XElement(item.Key.ToString());
                    current.Add(subItem);
                    Write(writer, item.Value, info.ModelName, settings, QName.Get(item.Key.ToString()), subItem, null);
                    //InternalWriteXml(writer, item.Value, settings, element, info.ModelName);
                }
            }
        }
Beispiel #7
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;
                }
            }
        }
        public void ReadObjectDictionary(ObjectDictionaryAttribute attribute, object reader,
                                         object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XmlReader xml = reader.Convert <XmlReader>();

            QName       name       = info.QName;
            IDictionary dict       = attribute.GetDictionary(receiver, info);
            Type        objectType = info.ObjectType;

            if (xml.IsEmptyElement)
            {
                return;
            }

            while (xml.Read())
            {
                if (xml.NodeType == XmlNodeType.EndElement)
                {
                    QName nodeName = QName.Get(xml);
                    if (nodeName == name)
                    {
                        return;
                    }
                }

                if (xml.NodeType == XmlNodeType.Element)
                {
                    QName  nodeName  = QName.Get(xml);
                    object subObject = attribute.UseConstructor ? ObjectUtil.CreateObjectWithCtor(objectType)
                        : ObjectUtil.CreateObject(objectType);
                    Read(reader, subObject, info.ModelName, settings, nodeName, null);
                    SerializerUtil.SetParent(receiver, subObject);

                    //string nodeValue = ReadString(xml);
                    //object objValue = SerializerUtil.GetPropertyObject(receiver, settings,
                    //    info, nodeValue, objectType);
                    dict[nodeName.LocalName] = subObject;
                }
            }
        }
Beispiel #9
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;
            }
        }
        public void ReadDictionary(DictionaryAttribute attribute, object reader,
                                   object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XmlReader xml = reader.Convert <XmlReader>();

            QName       name       = info.QName;
            IDictionary dict       = attribute.GetDictionary(receiver, info);
            Type        objectType = info.ObjectType;

            if (xml.IsEmptyElement)
            {
                return;
            }

            while (xml.Read())
            {
                if (xml.NodeType == XmlNodeType.EndElement)
                {
                    QName nodeName = QName.Get(xml);
                    if (nodeName == name)
                    {
                        return;
                    }
                }

                if (xml.NodeType == XmlNodeType.Element)
                {
                    QName  nodeName  = QName.Get(xml);
                    string nodeValue = ReadString(xml);
                    if (attribute.AutoTrim && nodeValue != null)
                    {
                        nodeValue = nodeValue.Trim();
                    }
                    object objValue = SerializerUtil.GetPropertyObject(receiver, settings,
                                                                       info, nodeValue, objectType);
                    dict[nodeName.LocalName] = objValue;
                }
            }
        }
        public void WriteObjectDictionary(ObjectDictionaryAttribute attribute, object writer,
                                          object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XmlWriter   xmlWriter   = writer.Convert <XmlWriter>();
            PrefixTable prefixTable = serializerData.Convert <PrefixTable>();

            WriteStartElement(xmlWriter, info.QName, prefixTable);
            IDictionary dict = value as IDictionary;

            if (dict != null)
            {
                foreach (DictionaryEntry item in dict)
                {
                    Write(writer, item.Value, info.ModelName, settings,
                          QName.Get(item.Key.ToString()), prefixTable, null);
                    //xmlWriter.WriteStartElement(item.Key.ToString());
                    //InternalWriteXml(writer, item.Value, info.ModelName, settings, prefixTable);
                    //xmlWriter.WriteEndElement();
                }
            }

            xmlWriter.WriteEndElement();
        }
        public static void WriteNode(XmlWriter writer, XmlReader reader, QName endName, bool defattr)
        {
            bool canReadValueChunk = reader.CanReadValueChunk;

            char[] writeNodeBuffer = null;

            do
            {
                if (reader.IsEmptyElement || reader.NodeType == XmlNodeType.EndElement)
                {
                    var name = QName.Get(reader);
                    if (name == endName)
                    {
                        break;
                    }
                }
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:
                    writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                    writer.WriteAttributes(reader, defattr);
                    if (reader.IsEmptyElement)
                    {
                        writer.WriteEndElement();
                    }
                    break;

                case XmlNodeType.Text:
                    int num2;
                    if (!canReadValueChunk)
                    {
                        writer.WriteString(reader.Value);
                        break;
                    }
                    if (writeNodeBuffer == null)
                    {
                        writeNodeBuffer = new char[BUFFER_SIZE];
                    }
                    while ((num2 = reader.ReadValueChunk(writeNodeBuffer, 0, BUFFER_SIZE)) > 0)
                    {
                        writer.WriteChars(writeNodeBuffer, 0, num2);
                    }
                    break;

                case XmlNodeType.CDATA:
                    writer.WriteCData(reader.Value);
                    break;

                case XmlNodeType.EntityReference:
                    writer.WriteEntityRef(reader.Name);
                    break;

                case XmlNodeType.ProcessingInstruction:
                case XmlNodeType.XmlDeclaration:
                    writer.WriteProcessingInstruction(reader.Name, reader.Value);
                    break;

                case XmlNodeType.Comment:
                    writer.WriteComment(reader.Value);
                    break;

                case XmlNodeType.DocumentType:
                    writer.WriteDocType(reader.Name, reader.GetAttribute("PUBLIC"),
                                        reader.GetAttribute("SYSTEM"), reader.Value);
                    break;

                case XmlNodeType.Whitespace:
                case XmlNodeType.SignificantWhitespace:
                    writer.WriteWhitespace(reader.Value);
                    break;

                case XmlNodeType.EndElement:
                    writer.WriteFullEndElement();
                    break;
                }
            }while (reader.Read());
        }