annotate Implab/Xml/XmlNameContext.cs @ 227:8d5de4eb9c2c v2

Reimplemented JsonXmlReader, added support for null values: JSON null values are mapped to empty nodes with 'xsi:nil' attribute set to 'true'
author cin
date Sat, 09 Sep 2017 03:53:13 +0300
parents
children 5f7a3e1d32b9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
227
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
1 using System;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
2 using System.Collections.Generic;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
3 using System.Linq;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
4 using System.Text;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
5 using System.Threading.Tasks;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
6 using System.Xml;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
7
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
8 namespace Implab.Xml {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
9 public class XmlNameContext {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
10 public const string XmlnsNamespace = "http://www.w3.org/2000/xmlns/";
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
11 public const string XmlnsPrefix = "xmlns";
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
12 public const string XmlNamespace = "http://www.w3.org/XML/1998/namespace";
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
13 public const string XmlPrefix = "xml";
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
14 public const string XsiNamespace = "http://www.w3.org/2001/XMLSchema-instance";
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
15 public const string XsiPrefix = "xsi";
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
16
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
17 readonly static char[] _qNameDelim = new[] { ':' };
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
18
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
19 Dictionary<string, string> m_ns2prefix;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
20 Dictionary<string, string> m_prefix2ns;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
21 int m_nextPrefix = 1;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
22
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
23 public XmlNameContext ParentContext { get; private set; }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
24
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
25 public XmlNameContext(XmlNameContext parent) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
26 ParentContext = parent;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
27 if (parent == null) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
28 DefinePrefixNoCheck(XmlnsNamespace, XmlnsPrefix);
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
29 DefinePrefixNoCheck(XmlNamespace, XmlPrefix);
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
30 } else {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
31 m_nextPrefix = parent.m_nextPrefix;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
32 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
33 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
34
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
35 public bool LookupNamespacePrefix(string ns, out string prefix) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
36 if (ns == null)
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
37 ns = string.Empty;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
38
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
39 prefix = null;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
40 for (var ctx = this; ctx != null; ctx = ctx.ParentContext) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
41 if (ctx.m_ns2prefix?.TryGetValue(ns, out prefix) == true) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
42 if (ctx != this) // cache for the future use
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
43 DefinePrefixNoCheck(ns, prefix);
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
44 return true;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
45 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
46 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
47 return false;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
48 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
49
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
50 public string CreateNamespacePrefix(string ns) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
51 var prefix = $"p{m_nextPrefix++}";
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
52 DefinePrefixNoCheck(ns, prefix);
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
53 return prefix;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
54 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
55
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
56 void DefinePrefixNoCheck(string ns, string prefix) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
57 if (ns == null)
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
58 ns = string.Empty;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
59 if (prefix == null)
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
60 prefix = string.Empty;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
61
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
62 if (m_ns2prefix == null)
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
63 m_ns2prefix = new Dictionary<string, string>();
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
64 m_ns2prefix[ns] = prefix;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
65
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
66 if (m_prefix2ns == null)
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
67 m_prefix2ns = new Dictionary<string, string>();
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
68 m_prefix2ns[prefix] = ns;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
69 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
70
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
71 public void DefinePrefix(string ns, string prefix) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
72 // according to https://www.w3.org/TR/xml-names/#ns-decl
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
73
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
74 // It MUST NOT be declared . Other prefixes MUST NOT be bound to this namespace name, and it MUST NOT be declared as the default namespace
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
75 if (ns == XmlnsNamespace)
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
76 throw new Exception($"Attempt to define xmlns:{prefix}='{ns}'");
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
77
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
78 // It MAY, but need not, be declared, and MUST NOT be bound to any other namespace name
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
79 if (ns == XmlNamespace && prefix != XmlPrefix)
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
80 throw new Exception($"Attempt to define xmlns:{prefix}='{ns}'");
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
81
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
82 // add mapping
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
83 DefinePrefixNoCheck(ns, prefix);
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
84 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
85
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
86 public string ResolvePrefix(string prefix) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
87 if (prefix == null)
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
88 prefix = string.Empty;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
89 string ns = null;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
90 for(var ctx = this; ctx != null; ctx = ctx.ParentContext) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
91 if (ctx.m_prefix2ns?.TryGetValue(prefix, out ns) == true) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
92 if (ctx != this) // cache for the future use
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
93 DefinePrefixNoCheck(ns, prefix);
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
94 return ns;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
95 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
96 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
97 return null;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
98 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
99
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
100 public XmlQualifiedName Resolve(string name) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
101 Safe.ArgumentNotEmpty(name, nameof(name));
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
102 var parts = name.Split(_qNameDelim, 2, StringSplitOptions.RemoveEmptyEntries);
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
103
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
104 if (parts.Length == 2) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
105 return new XmlQualifiedName(parts[1], ResolvePrefix(parts[0]));
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
106 } else {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
107 return new XmlQualifiedName(parts[0], ResolvePrefix(string.Empty));
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
108 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
109 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
110 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
111 }