public static ParsedNodeId Parse(NodeId nodeId)
{
// can only parse non-null string node identifiers.
if (NodeId.IsNull(nodeId))
{
return null;
}
string identifier = nodeId.Identifier as string;
if (String.IsNullOrEmpty(identifier))
{
return null;
}
ParsedNodeId parsedNodeId = new ParsedNodeId();
parsedNodeId.NamespaceIndex = nodeId.NamespaceIndex;
int start = 0;
// extract the type of identifier.
parsedNodeId.RootType = (int)ExtractNumber(identifier, ref start);
if (start >= identifier.Length || identifier[start] != ':')
{
return null;
}
// extract any component path.
StringBuilder buffer = new StringBuilder();
int index = start+1;
int end = identifier.Length;
bool escaped = false;
while (index < end)
{
char ch = identifier[index++];
// skip any escape character but keep the one after it.
if (ch == '&')
{
escaped = true;
continue;
}
if (!escaped && ch == '?')
{
end = index;
break;
}
buffer.Append(ch);
escaped = false;
}
// extract any component.
parsedNodeId.RootId = buffer.ToString();
parsedNodeId.ComponentPath = null;
if (end < identifier.Length)
{
parsedNodeId.ComponentPath = identifier.Substring(end);
}
return parsedNodeId;
}