| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Text; |
| | | 9 | | using System.Xml; |
| | | 10 | | using System.Xml.Schema; |
| | | 11 | | using CoreWCF.IdentityModel.Selectors; |
| | | 12 | | using CoreWCF.IdentityModel.Tokens; |
| | | 13 | | using CoreWCF.Runtime; |
| | | 14 | | |
| | | 15 | | namespace CoreWCF |
| | | 16 | | { |
| | | 17 | | internal static class XmlUtil |
| | | 18 | | { |
| | | 19 | | public const string XmlNs = "http://www.w3.org/XML/1998/namespace"; |
| | | 20 | | public const string XmlNsNs = "http://www.w3.org/2000/xmlns/"; |
| | | 21 | | public const string XmlSerializerSchemaInstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance"; |
| | | 22 | | public const string XmlSerializerSchemaNamespace = "http://www.w3.org/2001/XMLSchema"; |
| | | 23 | | |
| | | 24 | | public static XmlQualifiedName GetXsiType(XmlReader reader) |
| | | 25 | | { |
| | 0 | 26 | | string xsiType = reader.GetAttribute("type", XmlSchema.InstanceNamespace); |
| | 0 | 27 | | reader.MoveToElement(); |
| | | 28 | | |
| | 0 | 29 | | if (string.IsNullOrEmpty(xsiType)) |
| | | 30 | | { |
| | 0 | 31 | | return null; |
| | | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | return ResolveQName(reader, xsiType); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public static bool EqualsQName(XmlQualifiedName qname, string localName, string namespaceUri) |
| | | 38 | | { |
| | 0 | 39 | | return null != qname |
| | 0 | 40 | | && StringComparer.Ordinal.Equals(localName, qname.Name) |
| | 0 | 41 | | && StringComparer.Ordinal.Equals(namespaceUri, qname.Namespace); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | public static bool IsNil(XmlReader reader) |
| | | 45 | | { |
| | 0 | 46 | | string xsiNil = reader.GetAttribute("nil", XmlSchema.InstanceNamespace); |
| | 0 | 47 | | return !string.IsNullOrEmpty(xsiNil) && XmlConvert.ToBoolean(xsiNil); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | public static string NormalizeEmptyString(string s) |
| | | 51 | | { |
| | 0 | 52 | | return string.IsNullOrEmpty(s) ? null : s; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public static XmlQualifiedName ResolveQName(XmlReader reader, string qstring) |
| | | 56 | | { |
| | 0 | 57 | | string name = qstring; |
| | 0 | 58 | | string prefix = String.Empty; |
| | 0 | 59 | | string ns = null; |
| | | 60 | | |
| | 0 | 61 | | int colon = qstring.IndexOf(':'); // index of char is always ordinal |
| | 0 | 62 | | if (colon > -1) |
| | | 63 | | { |
| | 0 | 64 | | prefix = qstring.Substring(0, colon); |
| | 0 | 65 | | name = qstring.Substring(colon + 1, qstring.Length - (colon + 1)); |
| | | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | ns = reader.LookupNamespace(prefix); |
| | | 69 | | |
| | 0 | 70 | | return new XmlQualifiedName(name, ns); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | public static void ValidateXsiType(XmlReader reader, string expectedTypeName, string expectedTypeNamespace) |
| | | 74 | | { |
| | 0 | 75 | | ValidateXsiType(reader, expectedTypeName, expectedTypeNamespace, false); |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | public static void ValidateXsiType(XmlReader reader, string expectedTypeName, string expectedTypeNamespace, bool |
| | | 79 | | { |
| | 0 | 80 | | XmlQualifiedName declaredType = GetXsiType(reader); |
| | | 81 | | |
| | 0 | 82 | | if (null == declaredType) |
| | | 83 | | { |
| | 0 | 84 | | if (requireDeclaration) |
| | | 85 | | { |
| | 0 | 86 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperXml(reader, |
| | 0 | 87 | | SR.Format(SR.ID4104, reader.LocalName, reader.NamespaceURI)); |
| | | 88 | | } |
| | | 89 | | } |
| | 0 | 90 | | else if (!(StringComparer.Ordinal.Equals(expectedTypeNamespace, declaredType.Namespace) |
| | 0 | 91 | | && StringComparer.Ordinal.Equals(expectedTypeName, declaredType.Name))) |
| | | 92 | | { |
| | 0 | 93 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperXml(reader, |
| | 0 | 94 | | SR.Format(SR.ID4102, expectedTypeName, expectedTypeNamespace, declaredType.Name, declaredType.Namesp |
| | | 95 | | } |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | public static string SerializeSecurityKeyIdentifier(SecurityKeyIdentifier ski, SecurityTokenSerializer tokenSeri |
| | | 99 | | { |
| | 0 | 100 | | StringBuilder sb = new StringBuilder(); |
| | 0 | 101 | | using (StringWriter stringWriter = new StringWriter(sb, CultureInfo.InvariantCulture)) |
| | | 102 | | { |
| | 0 | 103 | | XmlWriterSettings settings = new XmlWriterSettings(); |
| | 0 | 104 | | settings.OmitXmlDeclaration = true; |
| | 0 | 105 | | using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, settings)) |
| | | 106 | | { |
| | 0 | 107 | | tokenSerializer.WriteKeyIdentifierClause(xmlWriter, ski[0]); |
| | 0 | 108 | | } |
| | | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | return sb.ToString(); |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | public static bool IsValidXmlIDValue(string val) |
| | | 115 | | { |
| | 0 | 116 | | if (string.IsNullOrEmpty(val)) |
| | | 117 | | { |
| | 0 | 118 | | return false; |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | // The first character of the ID should be a letter, '_' or ':' |
| | 0 | 122 | | return (((val[0] >= 'A') && (val[0] <= 'Z')) || |
| | 0 | 123 | | ((val[0] >= 'a') && (val[0] <= 'z')) || |
| | 0 | 124 | | (val[0] == '_') || (val[0] == ':')); |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | public static string GetXmlLangAttribute(XmlReader reader) |
| | | 128 | | { |
| | 4 | 129 | | string xmlLang = null; |
| | 4 | 130 | | if (reader.MoveToAttribute("lang", XmlNs)) |
| | | 131 | | { |
| | 4 | 132 | | xmlLang = reader.Value; |
| | 4 | 133 | | reader.MoveToElement(); |
| | | 134 | | } |
| | | 135 | | |
| | 4 | 136 | | if (xmlLang == null) |
| | | 137 | | { |
| | 0 | 138 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.XmlLangAttributeMissing)); |
| | | 139 | | } |
| | | 140 | | |
| | 4 | 141 | | return xmlLang; |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | public static void WriteElementStringAsUniqueId(XmlDictionaryWriter writer, XmlDictionaryString localName, XmlDi |
| | | 145 | | { |
| | 0 | 146 | | writer.WriteStartElement(localName, ns); |
| | 0 | 147 | | writer.WriteValue(id); |
| | 0 | 148 | | writer.WriteEndElement(); |
| | 0 | 149 | | } |
| | | 150 | | |
| | | 151 | | public static void WriteElementContentAsInt64(XmlDictionaryWriter writer, XmlDictionaryString localName, XmlDict |
| | | 152 | | { |
| | 0 | 153 | | writer.WriteStartElement(localName, ns); |
| | 0 | 154 | | writer.WriteValue(value); |
| | 0 | 155 | | writer.WriteEndElement(); |
| | 0 | 156 | | } |
| | | 157 | | |
| | | 158 | | public static Int64 ReadElementContentAsInt64(XmlDictionaryReader reader) |
| | | 159 | | { |
| | 0 | 160 | | reader.ReadFullStartElement(); |
| | 0 | 161 | | Int64 i = reader.ReadContentAsLong(); |
| | 0 | 162 | | reader.ReadEndElement(); |
| | 0 | 163 | | return i; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | // Takes a collection of node list and returns a list of XmlElements |
| | | 167 | | // from the list (skipping past any XmlComments and CDATA nodes). |
| | | 168 | | public static List<XmlElement> GetXmlElements(XmlNodeList nodeList) |
| | | 169 | | { |
| | 0 | 170 | | if (nodeList == null) |
| | | 171 | | { |
| | 0 | 172 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(nodeList)); |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | List<XmlElement> xmlElements = new List<XmlElement>(); |
| | 0 | 176 | | foreach (XmlNode node in nodeList) |
| | | 177 | | { |
| | 0 | 178 | | XmlElement tempElement = node as XmlElement; |
| | 0 | 179 | | if (tempElement != null) |
| | | 180 | | { |
| | 0 | 181 | | xmlElements.Add(tempElement); |
| | | 182 | | } |
| | | 183 | | } |
| | | 184 | | |
| | 0 | 185 | | return xmlElements; |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | public static void ReadContentAsQName(XmlReader reader, out string localName, out string ns) |
| | | 189 | | { |
| | 2 | 190 | | ParseQName(reader, reader.ReadContentAsString(), out localName, out ns); |
| | 2 | 191 | | } |
| | | 192 | | |
| | | 193 | | public static bool IsWhitespace(char ch) |
| | | 194 | | { |
| | 84 | 195 | | return (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n'); |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | public static string TrimEnd(string s) |
| | | 199 | | { |
| | | 200 | | int i; |
| | 4 | 201 | | for (i = s.Length; i > 0 && IsWhitespace(s[i - 1]); i--) |
| | | 202 | | { |
| | | 203 | | ; |
| | | 204 | | } |
| | | 205 | | |
| | 2 | 206 | | if (i != s.Length) |
| | | 207 | | { |
| | 0 | 208 | | return s.Substring(0, i); |
| | | 209 | | } |
| | | 210 | | |
| | 2 | 211 | | return s; |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | public static string TrimStart(string s) |
| | | 215 | | { |
| | | 216 | | int i; |
| | 4 | 217 | | for (i = 0; i < s.Length && IsWhitespace(s[i]); i++) |
| | | 218 | | { |
| | | 219 | | ; |
| | | 220 | | } |
| | | 221 | | |
| | 2 | 222 | | if (i != 0) |
| | | 223 | | { |
| | 0 | 224 | | return s.Substring(i); |
| | | 225 | | } |
| | | 226 | | |
| | 2 | 227 | | return s; |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | public static string Trim(string s) |
| | | 231 | | { |
| | | 232 | | int i; |
| | 80 | 233 | | for (i = 0; i < s.Length && IsWhitespace(s[i]); i++) |
| | | 234 | | { |
| | | 235 | | ; |
| | | 236 | | } |
| | | 237 | | |
| | 40 | 238 | | if (i >= s.Length) |
| | | 239 | | { |
| | 0 | 240 | | return string.Empty; |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | int j; |
| | 80 | 244 | | for (j = s.Length; j > 0 && IsWhitespace(s[j - 1]); j--) |
| | | 245 | | { |
| | | 246 | | ; |
| | | 247 | | } |
| | | 248 | | |
| | | 249 | | Fx.Assert(j > i, "Logic error in XmlUtil.Trim()."); |
| | | 250 | | |
| | 40 | 251 | | if (i != 0 || j != s.Length) |
| | | 252 | | { |
| | 0 | 253 | | return s.Substring(i, j - i); |
| | | 254 | | } |
| | 40 | 255 | | return s; |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | public static void ParseQName(XmlReader reader, string qname, out string localName, out string ns) |
| | | 259 | | { |
| | 2 | 260 | | int index = qname.IndexOf(':'); |
| | | 261 | | string prefix; |
| | 2 | 262 | | if (index < 0) |
| | | 263 | | { |
| | 0 | 264 | | prefix = ""; |
| | 0 | 265 | | localName = TrimStart(TrimEnd(qname)); |
| | | 266 | | } |
| | | 267 | | else |
| | | 268 | | { |
| | 2 | 269 | | if (index == qname.Length - 1) |
| | | 270 | | { |
| | 0 | 271 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.InvalidXmlQu |
| | | 272 | | } |
| | | 273 | | |
| | 2 | 274 | | prefix = TrimStart(qname.Substring(0, index)); |
| | 2 | 275 | | localName = TrimEnd(qname.Substring(index + 1)); |
| | | 276 | | } |
| | 2 | 277 | | ns = reader.LookupNamespace(prefix); |
| | 2 | 278 | | if (ns == null) |
| | | 279 | | { |
| | 0 | 280 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnboundPrefixInQ |
| | | 281 | | } |
| | 2 | 282 | | } |
| | | 283 | | |
| | | 284 | | // This code was copied from XmlDictionaryReader.ReadElementContentAsDateTime which is an internal method |
| | | 285 | | public static DateTime ReadElementContentAsDateTime(this XmlDictionaryReader reader) |
| | | 286 | | { |
| | 0 | 287 | | bool isEmptyElement = reader.IsStartElement() && reader.IsEmptyElement; |
| | | 288 | | DateTime value; |
| | | 289 | | |
| | 0 | 290 | | if (isEmptyElement) |
| | | 291 | | { |
| | 0 | 292 | | reader.Read(); |
| | | 293 | | try |
| | | 294 | | { |
| | 0 | 295 | | value = DateTime.Parse(string.Empty, NumberFormatInfo.InvariantInfo); |
| | 0 | 296 | | } |
| | 0 | 297 | | catch (ArgumentException exception) |
| | | 298 | | { |
| | 0 | 299 | | throw new XmlException(SR.Format(SR.XmlInvalidConversion, string.Empty, "DateTime"), exception); |
| | | 300 | | } |
| | 0 | 301 | | catch (FormatException exception) |
| | | 302 | | { |
| | 0 | 303 | | throw new XmlException(SR.Format(SR.XmlInvalidConversion, string.Empty, "DateTime"), exception); |
| | | 304 | | } |
| | | 305 | | } |
| | | 306 | | else |
| | | 307 | | { |
| | 0 | 308 | | reader.ReadStartElement(); |
| | 0 | 309 | | value = reader.ReadContentAsDateTimeOffset().DateTime; |
| | 0 | 310 | | reader.ReadEndElement(); |
| | | 311 | | } |
| | | 312 | | |
| | 0 | 313 | | return value; |
| | | 314 | | } |
| | | 315 | | } |
| | | 316 | | } |