annotate Implab/Xml/XmlNameContext.cs @ 230:3e26338eb977 v2

slowly cutting off mono specific settings
author cin
date Wed, 13 Sep 2017 16:55:13 +0300
parents 5f7a3e1d32b9
children
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 {
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
9 class XmlNameContext {
227
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;
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
22 string m_lastNs;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
23 string m_lastPrefix;
227
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 ParentContext { get; private set; }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
26
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
27 public int Depth { get; private set; }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
28
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
29 public XmlNameContext(XmlNameContext parent, int depth) {
227
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
30 ParentContext = parent;
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
31 Depth = depth;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
32
227
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
33 if (parent == null) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
34 DefinePrefixNoCheck(XmlnsNamespace, XmlnsPrefix);
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
35 DefinePrefixNoCheck(XmlNamespace, XmlPrefix);
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
36 } else {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
37 m_nextPrefix = parent.m_nextPrefix;
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 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
40
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
41 public bool LookupNamespacePrefix(string ns, out string prefix) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
42 if (ns == null)
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
43 ns = string.Empty;
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
44 if (ns == m_lastNs) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
45 prefix = m_lastPrefix;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
46 return true;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
47 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
48
227
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 prefix = null;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
51 for (var ctx = this; ctx != null; ctx = ctx.ParentContext) {
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
52 if (ctx.m_ns2prefix != null && ctx.m_ns2prefix.TryGetValue(ns, out prefix)) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
53 m_lastNs = ns;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
54 m_lastPrefix = prefix;
227
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
55 return true;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
56 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
57 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
58 return false;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
59 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
60
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
61 public string CreateNamespacePrefix(string ns) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
62 var prefix = $"p{m_nextPrefix++}";
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
63 DefinePrefixNoCheck(ns, prefix);
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
64 return 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
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
67 void DefinePrefixNoCheck(string ns, string prefix) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
68 if (ns == null)
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
69 ns = string.Empty;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
70 if (prefix == null)
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
71 prefix = string.Empty;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
72
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
73 if (m_ns2prefix == null)
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
74 m_ns2prefix = new Dictionary<string, string>();
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
75 m_ns2prefix[ns] = prefix;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
76
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
77 if (m_prefix2ns == null)
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
78 m_prefix2ns = new Dictionary<string, string>();
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
79 m_prefix2ns[prefix] = ns;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
80 }
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 public void DefinePrefix(string ns, string prefix) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
83 // 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
84
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
85 // 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
86 if (ns == XmlnsNamespace)
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
87 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
88
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
89 // 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
90 if (ns == XmlNamespace && prefix != XmlPrefix)
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
91 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
92
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
93 // add mapping
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
94 DefinePrefixNoCheck(ns, prefix);
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 public string ResolvePrefix(string prefix) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
98 if (prefix == null)
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
99 prefix = string.Empty;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
100 string ns = null;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
101 for(var ctx = this; ctx != null; ctx = ctx.ParentContext) {
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 227
diff changeset
102 if (ctx.m_prefix2ns != null && ctx.m_prefix2ns.TryGetValue(prefix, out ns) == true)
227
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
103 return ns;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
104 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
105 return null;
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
106 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
107
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
108 public XmlQualifiedName Resolve(string name) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
109 Safe.ArgumentNotEmpty(name, nameof(name));
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
110 var parts = name.Split(_qNameDelim, 2, StringSplitOptions.RemoveEmptyEntries);
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
111
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
112 if (parts.Length == 2) {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
113 return new XmlQualifiedName(parts[1], ResolvePrefix(parts[0]));
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
114 } else {
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
115 return new XmlQualifiedName(parts[0], ResolvePrefix(string.Empty));
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
116 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
117 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
118 }
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
diff changeset
119 }