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