Reads objects from a XML stream.
Inheritance: IDecoder, IDisposable
コード例 #1
0
        /// <summary>
        /// Updates the attributes from the stream.
        /// </summary>
        /// <param name="context">The context for the system being accessed.</param>
        /// <param name="decoder">The decoder wrapping the stream to read.</param>
        public override void Update(ISystemContext context, XmlDecoder decoder)
        {
            base.Update(context, decoder);

            decoder.PushNamespace(Namespaces.OpcUaXsd);

            if (decoder.Peek("Executable"))
            {
                Executable = decoder.ReadBoolean("Executable");
            }

            if (decoder.Peek("UserExecutable"))
            {
                UserExecutable = decoder.ReadBoolean("UserExecutable");
            }

            decoder.PopNamespace();
        }
コード例 #2
0
ファイル: DataSource.cs プロジェクト: OPCFoundation/UA-.NET
        /// <summary>
        /// Creates an decoder to restore Variant values.
        /// </summary>
        private XmlDecoder CreateDecoder(ISystemContext context, XmlElement source)
        {
            ServiceMessageContext messageContext = new ServiceMessageContext();
            messageContext.NamespaceUris = context.NamespaceUris;
            messageContext.ServerUris = context.ServerUris;
            messageContext.Factory = context.EncodeableFactory;

            XmlDecoder decoder = new XmlDecoder(source, messageContext);

            NamespaceTable namespaceUris = new NamespaceTable();

            if (NamespaceUris != null)
            {
                for (int ii = 0; ii < NamespaceUris.Length; ii++)
                {
                    namespaceUris.Append(NamespaceUris[ii]);
                }
            }

            StringTable serverUris = new StringTable();

            if (ServerUris != null)
            {
                serverUris.Append(context.ServerUris.GetString(0));

                for (int ii = 0; ii < ServerUris.Length; ii++)
                {
                    serverUris.Append(ServerUris[ii]);
                }
            }

            decoder.SetMappingTables(namespaceUris, serverUris);

            return decoder;
        }
コード例 #3
0
        /// <summary>
        /// Updates the attributes from the stream.
        /// </summary>
        /// <param name="context">The context for the system being accessed.</param>
        /// <param name="decoder">The decoder wrapping the stream to read.</param>
        public override void Update(ISystemContext context, XmlDecoder decoder)
        {
            base.Update(context, decoder);

            decoder.PushNamespace(Namespaces.OpcUaXsd);

            if (decoder.Peek("ReferenceTypeId"))
            {
                ReferenceTypeId = decoder.ReadNodeId("ReferenceTypeId");
            }

            if (decoder.Peek("TypeDefinitionId"))
            {
               TypeDefinitionId = decoder.ReadNodeId("TypeDefinitionId");
            }

            if (decoder.Peek("ModellingRuleId"))
            {
                ModellingRuleId = decoder.ReadNodeId("ModellingRuleId");
            }

            if (decoder.Peek("NumericId"))
            {
                NumericId = decoder.ReadUInt32("NumericId");
            }

            decoder.PopNamespace();
        }
        /// <summary>
        /// Updates the attributes from the stream.
        /// </summary>
        /// <param name="context">The context for the system being accessed.</param>
        /// <param name="decoder">The decoder wrapping the stream to read.</param>
        public override void Update(ISystemContext context, XmlDecoder decoder)
        {
            base.Update(context, decoder);

            decoder.PushNamespace(Namespaces.OpcUaXsd);

            if (decoder.Peek("Value"))
            {
                WrappedValue = decoder.ReadVariant("Value");
            }

            if (decoder.Peek("DataType"))
            {
                DataType = decoder.ReadNodeId("DataType");
            }

            if (decoder.Peek("ValueRank"))
            {
                ValueRank = decoder.ReadInt32("ValueRank");
            }

            if (decoder.Peek("ArrayDimensions"))
            {
                ArrayDimensions = BaseVariableState.ArrayDimensionsFromXml(decoder.ReadString("ArrayDimensions"));
            }

            decoder.PopNamespace();
        }
コード例 #5
0
        /// <summary>
        /// Converts the XML back to a value.
        /// </summary>
        private Variant GetValue()
        {
            if (!m_textChanged)
            {
                return m_value;
            }

            XmlDocument document = new XmlDocument();
            document.InnerXml = ValueTB.Text;
            
            // find the first element.
            XmlElement element = null;
            
            for (XmlNode node = document.DocumentElement.FirstChild; node != null; node = node.NextSibling)
            {
                element = node as XmlElement;

                if (element != null)
                {
                    break;
                }
            }

            XmlDecoder decoder = new XmlDecoder(element, m_session.MessageContext);

            decoder.PushNamespace(Namespaces.OpcUaXsd);
            TypeInfo typeInfo = null;
            object value = decoder.ReadVariantContents(out typeInfo);

            return new Variant(value, typeInfo);
        }
コード例 #6
0
        /// <summary>
        /// Updates the attributes from the stream.
        /// </summary>
        /// <param name="context">The context for the system being accessed.</param>
        /// <param name="decoder">The decoder wrapping the stream to read.</param>
        public override void Update(ISystemContext context, XmlDecoder decoder)
        {
            base.Update(context, decoder);

            decoder.PushNamespace(Namespaces.OpcUaXsd);

            if (decoder.Peek("InverseName"))
            {
                InverseName = decoder.ReadLocalizedText("InverseName");
            }

            if (decoder.Peek("Symmetric"))
            {
                Symmetric = decoder.ReadBoolean("Symmetric");
            }

            decoder.PopNamespace();
        }
コード例 #7
0
        /// <summary>
        /// Reads an extension object from the stream.
        /// </summary>
        private ExtensionObject ReadExtensionObject()
        {
            ExtensionObject extension = new ExtensionObject();

            // read type id.
            NodeId typeId = ReadNodeId(null);
            
            // convert to absolute node id.
            extension.TypeId = NodeId.ToExpandedNodeId(typeId, m_context.NamespaceUris);
            
            if (!NodeId.IsNull(typeId) && NodeId.IsNull(extension.TypeId))
            {             
                Utils.Trace(
                    "Cannot de-serialized extension objects if the NamespaceUri is not in the NamespaceTable: Type = {0}", 
                    typeId);
            }

            // read encoding.
            ExtensionObjectEncoding encoding = (ExtensionObjectEncoding)Enum.ToObject(typeof(ExtensionObjectEncoding), m_reader.ReadByte());

            // nothing more to do for empty bodies.
            if (encoding == ExtensionObjectEncoding.None)
            {
                return extension;
            }

            // check for known type.
            Type systemType = m_context.Factory.GetSystemType(extension.TypeId);

            // check for XML bodies.
            if (encoding == ExtensionObjectEncoding.Xml)
            {
                extension.Body = ReadXmlElement(null);

                // attempt to decode a known type.
                if (systemType != null)
                {
                    XmlElement element = extension.Body as XmlElement;
                    XmlDecoder xmlDecoder = new XmlDecoder(element, this.Context);

                    try
                    {
                        xmlDecoder.PushNamespace(element.NamespaceURI);
                        IEncodeable body = xmlDecoder.ReadEncodeable(element.LocalName, systemType);
                        xmlDecoder.PopNamespace();

                        // update body.
                        extension.Body = body;
                    }
                    catch (Exception e)
                    {
                        Utils.Trace("Could not decode known type {0}. Error={1}, Value={2}", systemType.FullName, e.Message, element.OuterXml);
                    }
                }

                return extension;
            }

            // create instance of type.
            IEncodeable encodeable = null;

            if (systemType != null)
            {
                encodeable = Activator.CreateInstance(systemType) as IEncodeable;
            }

            // get the length.
            int length = ReadInt32(null);

            // process unknown type.
            if (encodeable == null)
            {
                // figure out how long the object is.
                if (length == -1)
                {
                    throw new ServiceResultException(
                        StatusCodes.BadDecodingError,
                        Utils.Format("Cannot determine length of unknown extension object body with type '{0}'.", extension.TypeId));
                }
                
                // check the length.
                if (m_context.MaxByteStringLength > 0 && m_context.MaxByteStringLength < length)
                {
                    throw ServiceResultException.Create(
                        StatusCodes.BadEncodingLimitsExceeded, 
                        "MaxByteStringLength {0} < {1}", 
                        m_context.MaxByteStringLength,
                        length);
                }

                // read the bytes of the body.
                extension.Body = m_reader.ReadBytes(length);
                return extension;
            }

            // save the current position.
            int start = Position;
            
            // decode body.            
            encodeable.Decode(this);

            // skip any unread data.
            int unused = length - (Position - start);

            if (unused > 0)
            {
                m_reader.ReadBytes(unused);
            }
                        
            extension.Body = encodeable;
            return extension;
        }
コード例 #8
0
        /// <summary>
        /// Updates the attributes from the stream.
        /// </summary>
        /// <param name="context">The context for the system being accessed.</param>
        /// <param name="decoder">The decoder wrapping the stream to read.</param>
        public override void Update(ISystemContext context, XmlDecoder decoder)
        {
            base.Update(context, decoder);

            decoder.PushNamespace(Namespaces.OpcUaXsd);

            if (decoder.Peek("EventNotifier"))
            {
                EventNotifier = decoder.ReadByte("EventNotifier");
            }

            if (decoder.Peek("ContainsNoLoops"))
            {
                ContainsNoLoops = decoder.ReadBoolean("ContainsNoLoops");
            }

            decoder.PopNamespace();
        }
コード例 #9
0
ファイル: BaseTypeState.cs プロジェクト: yuriik83/UA-.NET
        /// <summary>
        /// Updates the attributes from the stream.
        /// </summary>
        /// <param name="context">The context for the system being accessed.</param>
        /// <param name="decoder">The decoder wrapping the stream to read.</param>
        public override void Update(ISystemContext context, XmlDecoder decoder)
        {
            base.Update(context, decoder);

            decoder.PushNamespace(Namespaces.OpcUaXsd);

            if (decoder.Peek("SuperTypeId"))
            {
                SuperTypeId = decoder.ReadNodeId("SuperTypeId");
            }

            if (decoder.Peek("IsAbstract"))
            {
                IsAbstract = decoder.ReadBoolean("IsAbstract");
            }

            decoder.PopNamespace();
        }
コード例 #10
0
ファイル: DataFileReader.cs プロジェクト: yuriik83/UA-.NET
        /// <summary>
        /// Extracts a BuiltInType value from the line.
        /// </summary>
        private bool ExtractField(int lineCount, ref string line, ServiceMessageContext context, BuiltInType valueType, out Variant value)
        {
            value = Variant.Null;
            string field = line;

            if (field == null)
            {
                return true;
            }

            if (valueType == BuiltInType.Null)
            {
                return true;
            }

            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("<Value xmlns=\"{0}\">", Opc.Ua.Namespaces.OpcUaXsd);
            builder.AppendFormat("<{0}>", valueType);
            builder.Append(line);
            builder.AppendFormat("</{0}>", valueType);
            builder.Append("</Value>");

            XmlDocument document = new XmlDocument();
            document.InnerXml = builder.ToString();

            try
            {
                XmlDecoder decoder = new XmlDecoder(document.DocumentElement, context);
                value = decoder.ReadVariant(null);
            }
            catch (Exception e)
            {
                Utils.Trace("PARSE ERROR [Line:{0}] - '{1}': {2}", lineCount, field, e.Message);
                return false;
            }

            return true;
        }