/// <summary> /// Returns true if the QualifiedName is valid. /// </summary> /// <param name="value">The name to be validated.</param> /// <param name="namespaceUris">The table namespaces known to the server.</param> /// <returns>True if the name is value.</returns> public static bool IsValid(QualifiedName value, NamespaceTable namespaceUris) { if (value == null || String.IsNullOrEmpty(value.m_name)) { return false; } if (namespaceUris != null) { if (namespaceUris.GetString(value.m_namespaceIndex) == null) { return false; } } return true; }
/// <inheritdoc cref="IEncodeable.Encode(IEncoder)" /> public void Encode(IEncoder encoder) { if (NamespaceUris != null && NamespaceUris.Count > 1) { string[] uris = new string[NamespaceUris.Count - 1]; for (int ii = 1; ii < NamespaceUris.Count; ii++) { uris[ii - 1] = NamespaceUris.GetString((uint)ii); } encoder.WriteStringArray("NamespaceUris", uris); } else { encoder.WriteStringArray("NamespaceUris", new string[0]); } if (ServerUris != null && ServerUris.Count > 1) { string[] uris = new string[ServerUris.Count - 1]; for (int ii = 1; ii < NamespaceUris.Count; ii++) { uris[ii - 1] = ServerUris.GetString((uint)ii); } encoder.WriteStringArray("ServerUris", uris); } else { encoder.WriteStringArray("ServerUris", new string[0]); } if (Message != null) { encoder.SetMappingTables(NamespaceUris, ServerUris); if (Message.TypeId == null || Message.TypeId.IdType != IdType.Numeric) { throw ServiceResultException.Create(StatusCodes.BadEncodingError, "SessionLessServiceMessage message body must have a numeric TypeId defined. ({0})", Message.TypeId); } encoder.WriteUInt32("ServiceId", (uint)Message.TypeId.Identifier); encoder.WriteEncodeable("Body", Message, null); } else { encoder.WriteUInt32("TypeId", (uint)0); } }
/// <summary> /// Parses a expanded node id string, translated any namespace indexes and returns the result. /// </summary> public static ExpandedNodeId Parse(string text, NamespaceTable currentNamespaces, NamespaceTable targetNamespaces) { // parse the string. ExpandedNodeId nodeId = Parse(text); // lookup the namespace uri. string uri = nodeId.m_namespaceUri; if (nodeId.m_nodeId.NamespaceIndex != 0) { uri = currentNamespaces.GetString(nodeId.m_nodeId.NamespaceIndex); } // translate the namespace uri. ushort namespaceIndex = 0; if (!String.IsNullOrEmpty(uri)) { int index = targetNamespaces.GetIndex(uri); if (index == -1) { throw ServiceResultException.Create( StatusCodes.BadNodeIdInvalid, "Cannot map namespace URI onto an index in the target namespace table: {0}", uri); } namespaceIndex = (ushort)index; } // check for absolute node id. if (nodeId.ServerIndex != 0) { nodeId.m_nodeId = new NodeId(nodeId.m_nodeId.Identifier, 0); nodeId.m_namespaceUri = uri; return(nodeId); } // local node id. nodeId.m_nodeId = new NodeId(nodeId.m_nodeId.Identifier, namespaceIndex); nodeId.m_namespaceUri = null; return(nodeId); }
/// <summary> /// Updates the path to use the indexes from the target table. /// </summary> /// <param name="currentTable">The NamespaceTable which the RelativePathString currently references</param> /// <param name="targetTable">The NamespaceTable which the RelativePathString should reference</param> public void TranslateNamespaceIndexes(NamespaceTable currentTable, NamespaceTable targetTable) { // build mapping table. int[] mappings = new int[currentTable.Count]; mappings[0] = 0; // copy mappings. string[] uris = new string[mappings.Length]; for (int ii = 1; ii < mappings.Length; ii++) { uris[ii] = currentTable.GetString((uint)ii); if (uris[ii] != null) { mappings[ii] = targetTable.GetIndex(uris[ii]); } } // update each element. foreach (Element element in m_elements) { QualifiedName qname = element.ReferenceTypeName; if (qname != null && qname.NamespaceIndex > 0) { if (qname.NamespaceIndex < mappings.Length && mappings[qname.NamespaceIndex] > 0) { element.ReferenceTypeName = new QualifiedName(qname.Name, (ushort)mappings[qname.NamespaceIndex]); } } qname = element.TargetName; if (qname != null && qname.NamespaceIndex > 0) { if (qname.NamespaceIndex < mappings.Length && mappings[qname.NamespaceIndex] > 0) { element.TargetName = new QualifiedName(qname.Name, (ushort)mappings[qname.NamespaceIndex]); } } } }
/// <summary> /// Updates the nodeset string tables and returns a NodeId that references those tables. /// </summary> /// <param name="qname">The qualified name.</param> /// <param name="targetNamespaceUris">The target namespace URIs.</param> /// <param name="sourceNamespaceUris">The source namespace URIs.</param> /// <returns>A NodeId that references those tables.</returns> private static QualifiedName Translate( QualifiedName qname, NamespaceTable targetNamespaceUris, NamespaceTable sourceNamespaceUris) { if (targetNamespaceUris == null) { throw new ArgumentNullException("targetNamespaceUris"); } if (sourceNamespaceUris == null) { throw new ArgumentNullException("sourceNamespaceUris"); } if (QualifiedName.IsNull(qname)) { return(qname); } ushort namespaceIndex = 0; if (qname.NamespaceIndex > 0) { string uri = sourceNamespaceUris.GetString(qname.NamespaceIndex); if (uri == null) { return(qname); } int index = targetNamespaceUris.GetIndex(uri); if (index == -1) { index = targetNamespaceUris.Append(uri); } namespaceIndex = (ushort)index; } return(new QualifiedName(qname.Name, namespaceIndex)); }
/// <summary> /// Updates the nodeset string tables and returns a NodeId that references those tables. /// </summary> /// <param name="nodeId">The node identifier.</param> /// <param name="targetNamespaceUris">The target namespace URIs.</param> /// <param name="sourceNamespaceUris">The source namespace URIs.</param> /// <returns>A NodeId that references those tables.</returns> private static NodeId Translate( NodeId nodeId, NamespaceTable targetNamespaceUris, NamespaceTable sourceNamespaceUris) { if (targetNamespaceUris == null) { throw new ArgumentNullException("targetNamespaceUris"); } if (sourceNamespaceUris == null) { throw new ArgumentNullException("sourceNamespaceUris"); } if (NodeId.IsNull(nodeId)) { return(nodeId); } ushort namespaceIndex = 0; if (nodeId.NamespaceIndex > 0) { string uri = sourceNamespaceUris.GetString(nodeId.NamespaceIndex); if (String.IsNullOrEmpty(uri)) { return(nodeId); } int index = targetNamespaceUris.GetIndex(uri); if (index == -1) { index = targetNamespaceUris.Append(uri); } namespaceIndex = (ushort)index; } return(new NodeId(nodeId.Identifier, namespaceIndex)); }
/// <summary> /// Returns the node in the set. /// </summary> /// <param name="nodeId">The node identifier.</param> /// <param name="namespaceUris">The namespace URIs.</param> /// <returns>The node in the set.</returns> /// <remarks> /// The NodeId namespace is translated before the node is looked up. /// </remarks> public Node Find(NodeId nodeId, NamespaceTable namespaceUris) { if (nodeId == null) { throw new ArgumentNullException("nodeId"); } if (namespaceUris == null) { throw new ArgumentNullException("namespaceUris"); } // check for unknown namespace index. string ns = namespaceUris.GetString(nodeId.NamespaceIndex); if (ns == null) { return(null); } // check for unknown namespace uri. int nsIndex = m_namespaceUris.GetIndex(ns); if (nsIndex < 0) { return(null); } // create translated node identifier. NodeId localId = new NodeId(nodeId.Identifier, (ushort)nsIndex); // look up node. Node node = null; if (m_nodes.TryGetValue(localId, out node)) { return(node); } return(null); }
/// <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(); } }
/// <summary> /// Reads the schema information from a XML document. /// </summary> public void LoadFromBinary(ISystemContext context, Stream istrm, bool updateTables) { ServiceMessageContext messageContext = new ServiceMessageContext(); messageContext.NamespaceUris = context.NamespaceUris; messageContext.ServerUris = context.ServerUris; messageContext.Factory = context.EncodeableFactory; using (BinaryDecoder decoder = new BinaryDecoder(istrm, messageContext)) { // check if a namespace table was provided. NamespaceTable namespaceUris = new NamespaceTable(); if (!decoder.LoadStringTable(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)); } } } // check if a server uri table was provided. StringTable serverUris = new StringTable(); if (namespaceUris != null && namespaceUris.Count > 1) { serverUris.Append(namespaceUris.GetString(1)); } if (!decoder.LoadStringTable(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)); } } } // setup the mappings to use during decoding. decoder.SetMappingTables(namespaceUris, serverUris); int count = decoder.ReadInt32(null); for (int ii = 0; ii < count; ii++) { NodeState state = NodeState.LoadNode(context, decoder); this.Add(state); } } }
/// <summary> /// Updates the namespace table with URI used in the relative path. /// </summary> /// <param name="currentTable">The current table.</param> /// <param name="targetTable">The target table.</param> public void UpdateNamespaceTable(NamespaceTable currentTable, NamespaceTable targetTable) { // build mapping table. int[] mappings = new int[currentTable.Count]; mappings[0] = 0; if (mappings.Length > 0) { mappings[1] = 1; } // ensure a placeholder for the local namespace. if (targetTable.Count <= 1) { targetTable.Append("---"); } string[] uris = new string[mappings.Length]; for (int ii = 2; ii < mappings.Length; ii++) { uris[ii] = currentTable.GetString((uint)ii); if (uris[ii] != null) { mappings[ii] = targetTable.GetIndex(uris[ii]); } } // update each element. foreach (Element element in m_elements) { // check reference type name. QualifiedName qname = element.ReferenceTypeName; if (qname != null && qname.NamespaceIndex > 1) { if (qname.NamespaceIndex < mappings.Length) { if (mappings[qname.NamespaceIndex] == -1) { mappings[qname.NamespaceIndex] = targetTable.GetIndexOrAppend(uris[qname.NamespaceIndex]); } } } // check target name. qname = element.TargetName; if (qname != null && qname.NamespaceIndex > 1) { if (qname.NamespaceIndex < mappings.Length) { if (mappings[qname.NamespaceIndex] == -1) { mappings[qname.NamespaceIndex] = targetTable.GetIndexOrAppend(uris[qname.NamespaceIndex]); } } } } }
/// <summary> /// Updates the nodeset string tables and returns a NodeId that references those tables. /// </summary> /// <param name="nodeId">The node identifier.</param> /// <param name="targetNamespaceUris">The target namespace URIs.</param> /// <param name="targetServerUris">The target server URIs.</param> /// <param name="sourceNamespaceUris">The source namespace URIs.</param> /// <param name="sourceServerUris">The source server URIs.</param> /// <returns>A NodeId that references those tables.</returns> private static ExpandedNodeId Translate( ExpandedNodeId nodeId, NamespaceTable targetNamespaceUris, StringTable targetServerUris, NamespaceTable sourceNamespaceUris, StringTable sourceServerUris) { if (targetNamespaceUris == null) { throw new ArgumentNullException("targetNamespaceUris"); } if (sourceNamespaceUris == null) { throw new ArgumentNullException("sourceNamespaceUris"); } if (nodeId.ServerIndex > 0) { if (targetServerUris == null) { throw new ArgumentNullException("targetServerUris"); } if (sourceServerUris == null) { throw new ArgumentNullException("sourceServerUris"); } } if (NodeId.IsNull(nodeId)) { return(nodeId); } if (!nodeId.IsAbsolute) { return(Translate((NodeId)nodeId, targetNamespaceUris, sourceNamespaceUris)); } string namespaceUri = nodeId.NamespaceUri; if (nodeId.ServerIndex > 0) { if (String.IsNullOrEmpty(namespaceUri)) { namespaceUri = sourceNamespaceUris.GetString(nodeId.NamespaceIndex); } string serverUri = sourceServerUris.GetString(nodeId.ServerIndex); int index = targetServerUris.GetIndex(serverUri); if (index == -1) { index = targetServerUris.Append(serverUri); } return(new ExpandedNodeId(new NodeId(nodeId.Identifier, 0), namespaceUri, (uint)index)); } ushort namespaceIndex = 0; if (!String.IsNullOrEmpty(namespaceUri)) { int index = targetNamespaceUris.GetIndex(namespaceUri); if (index == -1) { index = targetNamespaceUris.Append(namespaceUri); } namespaceIndex = (ushort)index; } return(new NodeId(nodeId.Identifier, namespaceIndex)); }
/// <summary> /// Imports a ExpandedNodeId /// </summary> private Opc.Ua.ExpandedNodeId ImportExpandedNodeId(string source, NamespaceTable namespaceUris, StringTable serverUris) { if (String.IsNullOrEmpty(source)) { return Opc.Ua.ExpandedNodeId.Null; } // parse the node. Opc.Ua.ExpandedNodeId nodeId = Opc.Ua.ExpandedNodeId.Parse(source); if (nodeId.ServerIndex <= 0 && nodeId.NamespaceIndex <= 0 && String.IsNullOrEmpty(nodeId.NamespaceUri)) { return nodeId; } uint serverIndex = ImportServerIndex(nodeId.ServerIndex, serverUris); ushort namespaceIndex = ImportNamespaceIndex(nodeId.NamespaceIndex, namespaceUris); if (serverIndex > 0) { string namespaceUri = nodeId.NamespaceUri; if (String.IsNullOrEmpty(nodeId.NamespaceUri)) { namespaceUri = namespaceUris.GetString(namespaceIndex); } nodeId = new Opc.Ua.ExpandedNodeId(nodeId.Identifier, 0, namespaceUri, serverIndex); return nodeId; } nodeId = new Opc.Ua.ExpandedNodeId(nodeId.Identifier, namespaceIndex, null, 0); return nodeId; }
/// <summary> /// Returns true if the QualifiedName is valid. /// </summary> public static bool IsValid(QualifiedName value, NamespaceTable namespaceUris) { if (value == null || String.IsNullOrEmpty(value.Name)) { return false; } if (namespaceUris != null) { if (namespaceUris.GetString((ushort)value.NamespaceIndex) == null) { return false; } } return true; }