comparison Implab/Xml/XmlNameContext.cs @ 229:5f7a3e1d32b9 v2

JsonXmlReader performance tuning JsonScanner now operates strings and doesn't parses number and literals. Added SerializationHelpers to common serialize/deserialize operations
author cin
date Tue, 12 Sep 2017 19:07:42 +0300
parents 8d5de4eb9c2c
children
comparison
equal deleted inserted replaced
228:6fa235c5a760 229:5f7a3e1d32b9
4 using System.Text; 4 using System.Text;
5 using System.Threading.Tasks; 5 using System.Threading.Tasks;
6 using System.Xml; 6 using System.Xml;
7 7
8 namespace Implab.Xml { 8 namespace Implab.Xml {
9 public class XmlNameContext { 9 class XmlNameContext {
10 public const string XmlnsNamespace = "http://www.w3.org/2000/xmlns/"; 10 public const string XmlnsNamespace = "http://www.w3.org/2000/xmlns/";
11 public const string XmlnsPrefix = "xmlns"; 11 public const string XmlnsPrefix = "xmlns";
12 public const string XmlNamespace = "http://www.w3.org/XML/1998/namespace"; 12 public const string XmlNamespace = "http://www.w3.org/XML/1998/namespace";
13 public const string XmlPrefix = "xml"; 13 public const string XmlPrefix = "xml";
14 public const string XsiNamespace = "http://www.w3.org/2001/XMLSchema-instance"; 14 public const string XsiNamespace = "http://www.w3.org/2001/XMLSchema-instance";
17 readonly static char[] _qNameDelim = new[] { ':' }; 17 readonly static char[] _qNameDelim = new[] { ':' };
18 18
19 Dictionary<string, string> m_ns2prefix; 19 Dictionary<string, string> m_ns2prefix;
20 Dictionary<string, string> m_prefix2ns; 20 Dictionary<string, string> m_prefix2ns;
21 int m_nextPrefix = 1; 21 int m_nextPrefix = 1;
22 string m_lastNs;
23 string m_lastPrefix;
22 24
23 public XmlNameContext ParentContext { get; private set; } 25 public XmlNameContext ParentContext { get; private set; }
24 26
25 public XmlNameContext(XmlNameContext parent) { 27 public int Depth { get; private set; }
28
29 public XmlNameContext(XmlNameContext parent, int depth) {
26 ParentContext = parent; 30 ParentContext = parent;
31 Depth = depth;
32
27 if (parent == null) { 33 if (parent == null) {
28 DefinePrefixNoCheck(XmlnsNamespace, XmlnsPrefix); 34 DefinePrefixNoCheck(XmlnsNamespace, XmlnsPrefix);
29 DefinePrefixNoCheck(XmlNamespace, XmlPrefix); 35 DefinePrefixNoCheck(XmlNamespace, XmlPrefix);
30 } else { 36 } else {
31 m_nextPrefix = parent.m_nextPrefix; 37 m_nextPrefix = parent.m_nextPrefix;
33 } 39 }
34 40
35 public bool LookupNamespacePrefix(string ns, out string prefix) { 41 public bool LookupNamespacePrefix(string ns, out string prefix) {
36 if (ns == null) 42 if (ns == null)
37 ns = string.Empty; 43 ns = string.Empty;
44 if (ns == m_lastNs) {
45 prefix = m_lastPrefix;
46 return true;
47 }
48
38 49
39 prefix = null; 50 prefix = null;
40 for (var ctx = this; ctx != null; ctx = ctx.ParentContext) { 51 for (var ctx = this; ctx != null; ctx = ctx.ParentContext) {
41 if (ctx.m_ns2prefix?.TryGetValue(ns, out prefix) == true) { 52 if (ctx.m_ns2prefix != null && ctx.m_ns2prefix.TryGetValue(ns, out prefix)) {
42 if (ctx != this) // cache for the future use 53 m_lastNs = ns;
43 DefinePrefixNoCheck(ns, prefix); 54 m_lastPrefix = prefix;
44 return true; 55 return true;
45 } 56 }
46 } 57 }
47 return false; 58 return false;
48 } 59 }
86 public string ResolvePrefix(string prefix) { 97 public string ResolvePrefix(string prefix) {
87 if (prefix == null) 98 if (prefix == null)
88 prefix = string.Empty; 99 prefix = string.Empty;
89 string ns = null; 100 string ns = null;
90 for(var ctx = this; ctx != null; ctx = ctx.ParentContext) { 101 for(var ctx = this; ctx != null; ctx = ctx.ParentContext) {
91 if (ctx.m_prefix2ns?.TryGetValue(prefix, out ns) == true) { 102 if (ctx.m_prefix2ns != null && ctx.m_prefix2ns.TryGetValue(prefix, out ns) == true)
92 if (ctx != this) // cache for the future use
93 DefinePrefixNoCheck(ns, prefix);
94 return ns; 103 return ns;
95 }
96 } 104 }
97 return null; 105 return null;
98 } 106 }
99 107
100 public XmlQualifiedName Resolve(string name) { 108 public XmlQualifiedName Resolve(string name) {