Exemplo n.º 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("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();
        }
        /// <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();
        }
Exemplo n.º 3
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));
        }
        /// <summary>
        /// Read a message in SOAP/XML.
        /// </summary>
        public static IEncodeable ReadSoapMessage(
            Stream istrm,
            string typeName,
            Type messageType,
            ServiceMessageContext messageContext)
        {
            XmlReader reader = XmlReader.Create(istrm, new XmlReaderSettings()
            {
                DtdProcessing = System.Xml.DtdProcessing.Prohibit, ValidationType = ValidationType.None
            });

            reader.MoveToContent();
            reader.ReadStartElement("Envelope", "http://www.w3.org/2003/05/soap-envelope");
            reader.MoveToContent();

            while (!reader.IsStartElement("Body", "http://www.w3.org/2003/05/soap-envelope"))
            {
                reader.Skip();
            }

            XmlDecoder decoder = new XmlDecoder(null, reader, messageContext);

            decoder.PushNamespace(Namespaces.OpcUaXsd);
            IEncodeable message = decoder.ReadEncodeable(typeName, messageType);

            decoder.PopNamespace();

            reader.ReadEndElement();
            reader.ReadEndElement();
            reader.Close();

            return(message);
        }
        /// <summary>
        /// Read a message in SOAP/XML.
        /// </summary>
        public static IEncodeable ReadSoapMessage(
            Stream istrm,
            string typeName,
            Type messageType,
            ServiceMessageContext messageContext)
        {
            XmlReader reader = XmlReader.Create(istrm);

            reader.MoveToContent();
            reader.ReadStartElement("Envelope", "http://www.w3.org/2003/05/soap-envelope");
            reader.MoveToContent();

            while (!reader.IsStartElement("Body", "http://www.w3.org/2003/05/soap-envelope"))
            {
                reader.Skip();
            }

            XmlDecoder decoder = new XmlDecoder(null, reader, messageContext);

            decoder.PushNamespace(Namespaces.OpcUaXsd);
            IEncodeable message = decoder.ReadEncodeable(typeName, messageType);

            if (reader.IsStartElement("ServiceFault", Namespaces.OpcUaXsd))
            {
                message = decoder.ReadEncodeable("ServiceFault", typeof(ServiceFault));
            }
            decoder.PopNamespace();

            reader.ReadEndElement();
            reader.ReadEndElement();
            reader.Close();

            return(message);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Convert xml to encodeable
 /// </summary>
 /// <param name="xmlElement"></param>
 /// <param name="typeId"></param>
 /// <param name="context"></param>
 /// <returns></returns>
 public static IEncodeable ToEncodeable(this XmlElement xmlElement,
                                        ExpandedNodeId typeId, IServiceMessageContext context)
 {
     using (var decoder = new XmlDecoder(xmlElement, context)) {
         var body = decoder.ReadExtensionObjectBody(typeId);
         return(body as IEncodeable);
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Reads the value of a Variant.
        /// </summary>
        public Variant Read(ISystemContext context, XmlElement element)
        {
            XmlDecoder decoder  = CreateDecoder(context, element);
            TypeInfo   typeInfo = null;
            object     value    = decoder.ReadVariantContents(out typeInfo);

            decoder.Close();
            return(new Variant(value, typeInfo));
        }
Exemplo n.º 8
0
        void caseRequestFile(CommandData cmdData)//command to request file
        {
            success = true;
            IFileService fs    = null;
            int          count = 0;

            while (true)
            {
                try
                {
                    fs = TestHarness.fileSender.CreateChannel(THFRcvr);
                    break;
                }
                catch
                {
                    Console.Write("\n  connection to service failed {0} times - trying again", ++count);
                    Thread.Sleep(500);
                    continue;
                }
            }
            Console.Write("\n  Connected to {0}\n", THFRcvr);
            string        filepath = Path.GetFullPath(relativeFilePath);
            List <string> dllFiles;

            dllFiles = XmlDecoder.getDLLFiles("../../../repo/" + cmdData.testAuthor + "_" + cmdData.dateTime + "_" + cmdData.xmlFile);
            dllFiles.Add(cmdData.xmlFile);
            foreach (string dllfile in dllFiles)
            {
                Console.WriteLine("-------------this is for requirement 2----------:\nthe file required in the XML include:" + dllfile);
                string file = filepath + cmdData.testAuthor + "_" + cmdData.dateTime + "_" + dllfile;
                timer.Start();
                if (!fileSender.SendFile(fs, file))
                {
                    timer.Stop();
                    Console.Write("\n  could not send file");
                    cmdData.command = "FileNotMatch";
                    cmdData.from    = "Repository";
                    cmdData.to      = "TestHarness";
                    success         = false;
                }
                else
                {
                    timer.Stop();
                    Console.WriteLine("send file :{0} in {1} ", file, timer.ElapsedTimeSpan);
                }
                Console.Write("\n\n");
            }
            if (success == true)
            {
                sendRunTest(cmdData);
            }
            else
            {
                sendFileNotMatch(cmdData);
            }
        }
Exemplo n.º 9
0
        public static bool HasChanged(Note note)
        {
            string original_xml = GetContent(note.CreateDate, note.Manager);

            if (GetContentWithoutTitle(note.TextContent) ==
                GetContentWithoutTitle(XmlDecoder.Decode(original_xml)))
            {
                return(false);
            }
            return(true);
        }
Exemplo n.º 10
0
        public void Can_decode_xml_response_as_static_object()
        {
            IDecoder decoder = new XmlDecoder();

            var xml = GetXmlContent();
            var customer = decoder.DecodeToStatic<Customer>(xml, HttpContentTypes.ApplicationXml);

            Assert.NotNull(customer);
            Assert.AreEqual("Hadi", customer.Firstname);
            Assert.AreEqual("Eskandari", customer.Lastname);
        }
Exemplo n.º 11
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("DataTypeDefinition"))
            {
                DataTypeDefinition = decoder.ReadExtensionObject("DataTypeDefinition");
            }

            decoder.PopNamespace();
        }
Exemplo n.º 12
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");
            }

            decoder.PopNamespace();
        }
Exemplo n.º 13
0
        public void Can_decode_xml_to_dynamic_object()
        {
            IDecoder decoder = new XmlDecoder();

            var xml = GetXmlContent();
            var customer = decoder.DecodeToDynamic(xml, HttpContentTypes.ApplicationXml);

            Assert.NotNull(customer);
            Assert.AreEqual("Hadi", customer.Firstname.ToString());
            Assert.AreEqual("Eskandari", customer.Lastname.ToString());
            Assert.AreEqual("HEskandari", customer.Info.Twitter.ToString());
            Assert.IsEmpty(customer.Info.Email.ToString());
        }
Exemplo n.º 14
0
        /// <summary>
        /// Extracts the extension object body.
        /// </summary>
        /// <param name="value">Extension object.</param>
        /// <returns>IEncodeable object</returns>
        public static object GetExtensionObjectBody(ExtensionObject value)
        {
            object body = value.Body;

            IEncodeable encodeable = body as IEncodeable;

            if (encodeable != null)
            {
                return(encodeable);
            }

            Type expectedType = EncodeableFactory.GetSystemType(value.TypeId);

            if (expectedType == null)
            {
                return(body);
            }

            IServiceMessageContext context = new ServiceMessageContext()
            {
                Factory = EncodeableFactory
            };

            XmlElement xml = body as XmlElement;

            if (xml != null)
            {
                XmlQualifiedName xmlName = Opc.Ua.EncodeableFactory.GetXmlName(expectedType);
                XmlDecoder       decoder = new XmlDecoder(xml, context);

                decoder.PushNamespace(xmlName.Namespace);
                body = decoder.ReadEncodeable(xmlName.Name, expectedType);
                decoder.PopNamespace();
                decoder.Close();

                return((IEncodeable)body);
            }

            byte[] bytes = body as byte[];

            if (bytes != null)
            {
                BinaryDecoder decoder = new BinaryDecoder(bytes, context);
                body = decoder.ReadEncodeable(null, expectedType);
                decoder.Close();

                return((IEncodeable)body);
            }

            return(body);
        }
Exemplo n.º 15
0
        public void TestInvalidData()
        {
            XmlDecoder   decoder = new XmlDecoder();
            MemoryStream stream  = new MemoryStream();

            byte[] bytes = Encoding.ASCII.GetBytes(@"<user lastname=""gauffin""><firstname>jonas</firstname>");
            stream.Write(bytes, 0, bytes.Length);
            stream.Seek(0, SeekOrigin.Begin);
            Assert.Throws(typeof(InvalidDataException), delegate
            {
                decoder.Decode(stream, "text/xml",
                               Encoding.ASCII);
            });
        }
Exemplo n.º 16
0
        public void Can_decode_xml_with_attributes_to_dynamic_object()
        {
            IDecoder decoder = new XmlDecoder();

            var xml = GetXmlContent();
            var customer = decoder.DecodeToDynamic(xml, HttpContentTypes.ApplicationXml);

            string id = customer.Id;
            string count = customer.Info.Count;

            Assert.NotNull(customer);
            Assert.AreEqual("1234", id);
            Assert.AreEqual("3", count);
        }
Exemplo n.º 17
0
        public MainWindow()
        {
            InitializeComponent();
            treeFolderMap        = new List <KeyValuePair <TreeViewItem, FileAndFolderModel> >();
            treeSingleVersionMap = new List <KeyValuePair <TreeViewItem, SingleVersionFile> >();
            treeVersionMap       = new List <KeyValuePair <TreeViewItem, VersionModel> >();
            if (File.Exists("record.xml"))
            {
                XmlDecoder xmlDecoder = new XmlDecoder("record.xml");
                MainFolder = xmlDecoder.MainFolder;
                Versions   = xmlDecoder.Versions;
            }
            else
            {
                MainFolder = new FileAndFolderModel("Folder");
                Versions   = new List <VersionModel>();
            }
            refManager              = new RefManager();
            newFile.IsEnabled       = false;
            refreshFile.IsEnabled   = false;
            deleteFile.IsEnabled    = false;
            newFolder.IsEnabled     = false;
            renameFolder.IsEnabled  = false;
            deleteFolder.IsEnabled  = false;
            createVerion.IsEnabled  = false;
            deleteVersion.IsEnabled = false;
            newRef.IsEnabled        = false;
            deleteRef.IsEnabled     = false;
            //MessageBox.Show(MainFolder.GetTreeInfo(0));
            //MessageBox.Show(VersionModel.ShowAllVersion(Versions));
            ShowFolderTree();
            ShowVersionTree();
            treeView.SelectedItemChanged        += TreeView_SelectedItemChanged;
            treeViewVersion.SelectedItemChanged += TreeViewVersion_SelectedItemChanged;

            /*Directory.CreateDirectory(targetFolder);
             * MessageBox.Show("" + Directory.Exists(targetFolder));
             * Directory.Delete(targetFolder, true); //不能删除非空文件夹
             * MessageBox.Show("" + Directory.Exists(targetFolder));*/
            this.KeyDown        += MainWindow_KeyDown;
            newRef.Click        += NewRef_Click;
            deleteRef.Click     += DeleteRef_Click;
            createVerion.Click  += CreateVerion_Click;
            deleteVersion.Click += DeleteVersion_Click;
            newFolder.Click     += NewFolder_Click;
            newFile.Click       += NewFile_Click;
        }
Exemplo n.º 18
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();
        }
Exemplo n.º 19
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();
        }
Exemplo n.º 20
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("SuperTypeId"))
            {
                SuperTypeId = decoder.ReadNodeId("SuperTypeId");
            }

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

            decoder.PopNamespace();
        }
        /// <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;
            var field = line;

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

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

            var 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>");

            var document = new XmlDocument {
                InnerXml = builder.ToString()
            };

            XmlDecoder decoder = null;

            try {
                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);
            }
            finally {
                decoder?.Dispose();
            }

            return(true);
        }
Exemplo n.º 22
0
        public void Test()
        {
            XmlDecoder decoder = new XmlDecoder();

            Assert.True(decoder.CanParse("text/xml"));
            Assert.False(decoder.CanParse("text/plain"));
            Assert.False(decoder.CanParse("xml"));
            Assert.False(decoder.CanParse("text"));
            Assert.False(decoder.CanParse(null));

            MemoryStream stream = new MemoryStream();

            byte[] bytes = Encoding.ASCII.GetBytes(@"<user lastname=""gauffin""><firstname>jonas</firstname></user>");
            stream.Write(bytes, 0, bytes.Length);
            stream.Seek(0, SeekOrigin.Begin);

            HttpInput input = decoder.Decode(stream, "text/xml", Encoding.ASCII);

            Assert.Equal("gauffin", input["user"]["lastname"].Value);
            Assert.Equal("jonas", input["user"]["firstname"].Value);
            Assert.Null(input["unknow"].Value);
            Assert.Equal(HttpInputItem.Empty, input["unknown"]);
        }
Exemplo n.º 23
0
        /// <summary>
        /// Creates an decoder to restore Variant values.
        /// </summary>
        private XmlDecoder CreateDecoder(ISystemContext context, XmlElement source)
        {
            IServiceMessageContext messageContext = new ServiceMessageContext()
            {
                NamespaceUris = context.NamespaceUris,
                ServerUris    = context.ServerUris,
                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);
        }
        /// <summary>
        /// Reads the schema information from a XML document.
        /// </summary>
        public void LoadFromXml(ISystemContext context, Stream istrm, bool updateTables)
        {
            ServiceMessageContext messageContext = new ServiceMessageContext();

            messageContext.NamespaceUris = context.NamespaceUris;
            messageContext.ServerUris    = context.ServerUris;
            messageContext.Factory       = context.EncodeableFactory;

            using (XmlReader reader = XmlReader.Create(istrm))
            {
                XmlQualifiedName root    = new XmlQualifiedName("ListOfNodeState", Namespaces.OpcUaXsd);
                XmlDecoder       decoder = new XmlDecoder(null, reader, messageContext);

                NamespaceTable namespaceUris = new NamespaceTable();

                if (!decoder.LoadStringTable("NamespaceUris", "NamespaceUri", namespaceUris))
                {
                    namespaceUris = null;
                }

                // update namespace table.
                if (updateTables)
                {
                    if (namespaceUris != null && context.NamespaceUris != null)
                    {
                        for (int ii = 0; ii < namespaceUris.Count; ii++)
                        {
                            context.NamespaceUris.GetIndexOrAppend(namespaceUris.GetString((uint)ii));
                        }
                    }
                }

                StringTable serverUris = new StringTable();

                if (!decoder.LoadStringTable("ServerUris", "ServerUri", context.ServerUris))
                {
                    serverUris = null;
                }

                // update server table.
                if (updateTables)
                {
                    if (serverUris != null && context.ServerUris != null)
                    {
                        for (int ii = 0; ii < serverUris.Count; ii++)
                        {
                            context.ServerUris.GetIndexOrAppend(serverUris.GetString((uint)ii));
                        }
                    }
                }

                // set mapping.
                decoder.SetMappingTables(namespaceUris, serverUris);

                decoder.PushNamespace(Namespaces.OpcUaXsd);

                NodeState state = NodeState.LoadNode(context, decoder);

                while (state != null)
                {
                    this.Add(state);

                    state = NodeState.LoadNode(context, decoder);
                }

                decoder.Close();
            }
        }
Exemplo n.º 25
0
        public void TestNull()
        {
            XmlDecoder decoder = new XmlDecoder();

            Assert.Null(decoder.Decode(new MemoryStream(), "text/xml", Encoding.ASCII));
        }
Exemplo n.º 26
0
        public void TestNull2()
        {
            XmlDecoder decoder = new XmlDecoder();

            decoder.Decode(null, null, null);
        }
Exemplo n.º 27
0
        /// <summary>
        /// Imports a node from the set.
        /// </summary>
        private NodeState Import(ISystemContext context, UANode node)
        {
            NodeState importedNode = null;

            NodeClass nodeClass = NodeClass.Unspecified;

            if (node is UAObject)
            {
                nodeClass = NodeClass.Object;
            }
            else if (node is UAVariable)
            {
                nodeClass = NodeClass.Variable;
            }
            else if (node is UAMethod)
            {
                nodeClass = NodeClass.Method;
            }
            else if (node is UAObjectType)
            {
                nodeClass = NodeClass.ObjectType;
            }
            else if (node is UAVariableType)
            {
                nodeClass = NodeClass.VariableType;
            }
            else if (node is UADataType)
            {
                nodeClass = NodeClass.DataType;
            }
            else if (node is UAReferenceType)
            {
                nodeClass = NodeClass.ReferenceType;
            }
            else if (node is UAView)
            {
                nodeClass = NodeClass.View;
            }

            switch (nodeClass)
            {
            case NodeClass.Object:
            {
                UAObject        o     = (UAObject)node;
                BaseObjectState value = new BaseObjectState(null);
                value.EventNotifier = o.EventNotifier;
                importedNode        = value;
                break;
            }

            case NodeClass.Variable:
            {
                UAVariable o = (UAVariable)node;

                NodeId typeDefinitionId = null;

                if (node.References != null)
                {
                    for (int ii = 0; ii < node.References.Length; ii++)
                    {
                        Opc.Ua.NodeId         referenceTypeId = ImportNodeId(node.References[ii].ReferenceType, context.NamespaceUris, true);
                        bool                  isInverse       = !node.References[ii].IsForward;
                        Opc.Ua.ExpandedNodeId targetId        = ImportExpandedNodeId(node.References[ii].Value, context.NamespaceUris, context.ServerUris);

                        if (referenceTypeId == ReferenceTypeIds.HasTypeDefinition && !isInverse)
                        {
                            typeDefinitionId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris);
                            break;
                        }
                    }
                }

                BaseVariableState value = null;

                if (typeDefinitionId == Opc.Ua.VariableTypeIds.PropertyType)
                {
                    value = new PropertyState(null);
                }
                else
                {
                    value = new BaseDataVariableState(null);
                }

                value.DataType                = ImportNodeId(o.DataType, context.NamespaceUris, true);
                value.ValueRank               = o.ValueRank;
                value.ArrayDimensions         = ImportArrayDimensions(o.ArrayDimensions);
                value.AccessLevelEx           = o.AccessLevel;
                value.UserAccessLevel         = (byte)(o.AccessLevel & 0xFF);
                value.MinimumSamplingInterval = o.MinimumSamplingInterval;
                value.Historizing             = o.Historizing;

                if (o.Value != null)
                {
                    XmlDecoder decoder  = CreateDecoder(context, o.Value);
                    TypeInfo   typeInfo = null;
                    value.Value = decoder.ReadVariantContents(out typeInfo);
                    decoder.Close();
                }

                importedNode = value;
                break;
            }

            case NodeClass.Method:
            {
                UAMethod    o     = (UAMethod)node;
                MethodState value = new MethodState(null);
                value.Executable       = o.Executable;
                value.UserExecutable   = o.Executable;
                value.TypeDefinitionId = ImportNodeId(o.MethodDeclarationId, context.NamespaceUris, true);
                importedNode           = value;
                break;
            }

            case NodeClass.View:
            {
                UAView    o     = (UAView)node;
                ViewState value = new ViewState();
                value.ContainsNoLoops = o.ContainsNoLoops;
                importedNode          = value;
                break;
            }

            case NodeClass.ObjectType:
            {
                UAObjectType        o     = (UAObjectType)node;
                BaseObjectTypeState value = new BaseObjectTypeState();
                value.IsAbstract = o.IsAbstract;
                importedNode     = value;
                break;
            }

            case NodeClass.VariableType:
            {
                UAVariableType        o     = (UAVariableType)node;
                BaseVariableTypeState value = new BaseDataVariableTypeState();
                value.IsAbstract      = o.IsAbstract;
                value.DataType        = ImportNodeId(o.DataType, context.NamespaceUris, true);
                value.ValueRank       = o.ValueRank;
                value.ArrayDimensions = ImportArrayDimensions(o.ArrayDimensions);

                if (o.Value != null)
                {
                    XmlDecoder decoder  = CreateDecoder(context, o.Value);
                    TypeInfo   typeInfo = null;
                    value.Value = decoder.ReadVariantContents(out typeInfo);
                    decoder.Close();
                }

                importedNode = value;
                break;
            }

            case NodeClass.DataType:
            {
                UADataType    o     = (UADataType)node;
                DataTypeState value = new DataTypeState();
                value.IsAbstract = o.IsAbstract;
                Opc.Ua.DataTypeDefinition dataTypeDefinition = Import(o, o.Definition, context.NamespaceUris);
                value.DataTypeDefinition = new ExtensionObject(dataTypeDefinition);
                value.Purpose            = o.Purpose;
                value.DataTypeModifier   = DataTypeModifier.None;

                if (o.Definition != null)
                {
                    if (o.Definition.IsOptionSet)
                    {
                        value.DataTypeModifier = DataTypeModifier.OptionSet;
                    }
                    else if (o.Definition.IsUnion)
                    {
                        value.DataTypeModifier = DataTypeModifier.Union;
                    }
                }

                importedNode = value;
                break;
            }

            case NodeClass.ReferenceType:
            {
                UAReferenceType    o     = (UAReferenceType)node;
                ReferenceTypeState value = new ReferenceTypeState();
                value.IsAbstract  = o.IsAbstract;
                value.InverseName = Import(o.InverseName);
                value.Symmetric   = o.Symmetric;
                importedNode      = value;
                break;
            }
            }

            importedNode.NodeId      = ImportNodeId(node.NodeId, context.NamespaceUris, false);
            importedNode.BrowseName  = ImportQualifiedName(node.BrowseName, context.NamespaceUris);
            importedNode.DisplayName = Import(node.DisplayName);

            if (importedNode.DisplayName == null)
            {
                importedNode.DisplayName = new Ua.LocalizedText(importedNode.BrowseName.Name);
            }

            importedNode.Description   = Import(node.Description);
            importedNode.Categories    = (node.Category != null && node.Category.Length > 0) ? node.Category : null;
            importedNode.ReleaseStatus = node.ReleaseStatus;
            importedNode.WriteMask     = (AttributeWriteMask)node.WriteMask;
            importedNode.UserWriteMask = (AttributeWriteMask)node.UserWriteMask;
            importedNode.Extensions    = node.Extensions;

            if (!String.IsNullOrEmpty(node.SymbolicName))
            {
                importedNode.SymbolicName = node.SymbolicName;
            }

            if (node.References != null)
            {
                BaseInstanceState instance = importedNode as BaseInstanceState;
                BaseTypeState     type     = importedNode as BaseTypeState;

                for (int ii = 0; ii < node.References.Length; ii++)
                {
                    Opc.Ua.NodeId         referenceTypeId = ImportNodeId(node.References[ii].ReferenceType, context.NamespaceUris, true);
                    bool                  isInverse       = !node.References[ii].IsForward;
                    Opc.Ua.ExpandedNodeId targetId        = ImportExpandedNodeId(node.References[ii].Value, context.NamespaceUris, context.ServerUris);

                    if (instance != null)
                    {
                        if (referenceTypeId == ReferenceTypeIds.HasModellingRule && !isInverse)
                        {
                            instance.ModellingRuleId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris);
                            continue;
                        }

                        if (referenceTypeId == ReferenceTypeIds.HasTypeDefinition && !isInverse)
                        {
                            instance.TypeDefinitionId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris);
                            continue;
                        }
                    }

                    if (type != null)
                    {
                        if (referenceTypeId == ReferenceTypeIds.HasSubtype && isInverse)
                        {
                            type.SuperTypeId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris);
                            continue;
                        }
                    }

                    importedNode.AddReference(referenceTypeId, isInverse, targetId);
                }
            }

            return(importedNode);
        }
Exemplo n.º 28
0
        /// <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;
        }
        /// <summary>
        /// Read a message in SOAP/XML.
        /// </summary>
        public static IEncodeable ReadSoapMessage(
            Stream istrm,
            string typeName,
            Type messageType,
            ServiceMessageContext messageContext)
        {
            XmlReader reader = XmlReader.Create(istrm);
            reader.MoveToContent();
            reader.ReadStartElement("Envelope", "http://www.w3.org/2003/05/soap-envelope");
            reader.MoveToContent();

            while (!reader.IsStartElement("Body", "http://www.w3.org/2003/05/soap-envelope"))
            {
                reader.Skip();
            }

            XmlDecoder decoder = new XmlDecoder(null, reader, messageContext);

            decoder.PushNamespace(Namespaces.OpcUaXsd);
            IEncodeable message = decoder.ReadEncodeable(typeName, messageType);
            decoder.PopNamespace();

            reader.ReadEndElement();
            reader.ReadEndElement();
            reader.Close();

            return message;
        }