Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NodesetCompiler: Correctly handle namespace mapping for encoded objects. #2161

Merged
merged 1 commit into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions tools/nodeset_compiler/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def __init__(self):
self.userWriteMask = 0
self.references = set()
self.hidden = False
self.modelUri = None

def __str__(self):
return self.__class__.__name__ + "(" + str(self.id) + ")"
Expand Down Expand Up @@ -563,9 +564,11 @@ def buildEncoding(self, nodeset, indent=0, force=False):

# This might be a subtype... follow the node defined as datatype to find out
# what encoding to use
if not NodeId(fdtype) in nodeset.nodes:
raise Exception("Node {} not found in nodeset".format(NodeId(fdtype)))
dtnode = nodeset.nodes[NodeId(fdtype)]
fdTypeNodeId = NodeId(fdtype)
fdTypeNodeId.ns = nodeset.namespaceMapping[self.modelUri][fdTypeNodeId.ns]
if not fdTypeNodeId in nodeset.nodes:
raise Exception("Node {} not found in nodeset".format(fdTypeNodeId))
dtnode = nodeset.nodes[fdTypeNodeId]
# The node in the datatype element was found. we inherit its encoding,
# but must still ensure that the dtnode is itself validly encodable
typeDict.append([fname, dtnode])
Expand Down
28 changes: 24 additions & 4 deletions tools/nodeset_compiler/nodeset.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def __init__(self):
self.nodes = {}
self.aliases = {}
self.namespaces = ["http://opcfoundation.org/UA/"]
self.namespaceMapping = {};

def sanitize(self):
for n in self.nodes.values():
Expand Down Expand Up @@ -148,7 +149,7 @@ def getNodeById(self, namespace, id):
def getRoot(self):
return self.getNodeByBrowseName("Root")

def createNode(self, xmlelement, nsMapping, hidden=False):
def createNode(self, xmlelement, modelUri, hidden=False):
ndtype = xmlelement.localName.lower()
if ndtype[:2] == "ua":
ndtype = ndtype[2:]
Expand All @@ -174,6 +175,7 @@ def createNode(self, xmlelement, nsMapping, hidden=False):
if node == None:
return None

node.modelUri = modelUri
node.hidden = hidden
return node

Expand Down Expand Up @@ -212,11 +214,29 @@ def addNodeSet(self, xmlfile, hidden=False, typesArray="UA_TYPES"):
raise Exception(self, self.originXML + " contains no or more then 1 nodeset")
nodeset = nodesets[0]


# Extract the modelUri
modelUri = None
try:
modelTag = nodeset.getElementsByTagName("Models")[0].getElementsByTagName("Model")[0]
modelUri = modelTag.attributes["ModelUri"].nodeValue
except:
# Ignore exception and try to use namespace array
modelUri = None


# Create the namespace mapping
orig_namespaces = extractNamespaces(xmlfile) # List of namespaces used in the xml file
if modelUri is None and len(orig_namespaces) > 0:
modelUri = orig_namespaces[0]

if modelUri is None:
raise Exception(self, self.originXML + " does not define the nodeset URI in Models/Model/ModelUri or NamespaceUris array.")


for ns in orig_namespaces:
self.addNamespace(ns)
nsMapping = self.createNamespaceMapping(orig_namespaces)
self.namespaceMapping[modelUri] = self.createNamespaceMapping(orig_namespaces)

# Extract the aliases
for nd in nodeset.childNodes:
Expand All @@ -231,11 +251,11 @@ def addNodeSet(self, xmlfile, hidden=False, typesArray="UA_TYPES"):
for nd in nodeset.childNodes:
if nd.nodeType != nd.ELEMENT_NODE:
continue
node = self.createNode(nd, nsMapping, hidden)
node = self.createNode(nd, modelUri, hidden)
if not node:
continue
node.replaceAliases(self.aliases)
node.replaceNamespaces(nsMapping)
node.replaceNamespaces(self.namespaceMapping[modelUri])
node.typesArray = typesArray

# Add the node the the global dict
Expand Down