< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.XmlUtil
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/XmlUtil.cs
Line coverage
24%
Covered lines: 28
Uncovered lines: 87
Coverable lines: 115
Total lines: 316
Line coverage: 24.3%
Branch coverage
30%
Covered branches: 26
Total branches: 84
Branch coverage: 30.9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/XmlUtil.cs

#LineLine coverage
 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
 4using System;
 5using System.Collections.Generic;
 6using System.Globalization;
 7using System.IO;
 8using System.Text;
 9using System.Xml;
 10using System.Xml.Schema;
 11using CoreWCF.IdentityModel.Selectors;
 12using CoreWCF.IdentityModel.Tokens;
 13using CoreWCF.Runtime;
 14
 15namespace 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        {
 026            string xsiType = reader.GetAttribute("type", XmlSchema.InstanceNamespace);
 027            reader.MoveToElement();
 28
 029            if (string.IsNullOrEmpty(xsiType))
 30            {
 031                return null;
 32            }
 33
 034            return ResolveQName(reader, xsiType);
 35        }
 36
 37        public static bool EqualsQName(XmlQualifiedName qname, string localName, string namespaceUri)
 38        {
 039            return null != qname
 040                && StringComparer.Ordinal.Equals(localName, qname.Name)
 041                && StringComparer.Ordinal.Equals(namespaceUri, qname.Namespace);
 42        }
 43
 44        public static bool IsNil(XmlReader reader)
 45        {
 046            string xsiNil = reader.GetAttribute("nil", XmlSchema.InstanceNamespace);
 047            return !string.IsNullOrEmpty(xsiNil) && XmlConvert.ToBoolean(xsiNil);
 48        }
 49
 50        public static string NormalizeEmptyString(string s)
 51        {
 052            return string.IsNullOrEmpty(s) ? null : s;
 53        }
 54
 55        public static XmlQualifiedName ResolveQName(XmlReader reader, string qstring)
 56        {
 057            string name = qstring;
 058            string prefix = String.Empty;
 059            string ns = null;
 60
 061            int colon = qstring.IndexOf(':'); // index of char is always ordinal
 062            if (colon > -1)
 63            {
 064                prefix = qstring.Substring(0, colon);
 065                name = qstring.Substring(colon + 1, qstring.Length - (colon + 1));
 66            }
 67
 068            ns = reader.LookupNamespace(prefix);
 69
 070            return new XmlQualifiedName(name, ns);
 71        }
 72
 73        public static void ValidateXsiType(XmlReader reader, string expectedTypeName, string expectedTypeNamespace)
 74        {
 075            ValidateXsiType(reader, expectedTypeName, expectedTypeNamespace, false);
 076        }
 77
 78        public static void ValidateXsiType(XmlReader reader, string expectedTypeName, string expectedTypeNamespace, bool
 79        {
 080            XmlQualifiedName declaredType = GetXsiType(reader);
 81
 082            if (null == declaredType)
 83            {
 084                if (requireDeclaration)
 85                {
 086                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperXml(reader,
 087                        SR.Format(SR.ID4104, reader.LocalName, reader.NamespaceURI));
 88                }
 89            }
 090            else if (!(StringComparer.Ordinal.Equals(expectedTypeNamespace, declaredType.Namespace)
 091                && StringComparer.Ordinal.Equals(expectedTypeName, declaredType.Name)))
 92            {
 093                throw DiagnosticUtility.ExceptionUtility.ThrowHelperXml(reader,
 094                    SR.Format(SR.ID4102, expectedTypeName, expectedTypeNamespace, declaredType.Name, declaredType.Namesp
 95            }
 096        }
 97
 98        public static string SerializeSecurityKeyIdentifier(SecurityKeyIdentifier ski, SecurityTokenSerializer tokenSeri
 99        {
 0100            StringBuilder sb = new StringBuilder();
 0101            using (StringWriter stringWriter = new StringWriter(sb, CultureInfo.InvariantCulture))
 102            {
 0103                XmlWriterSettings settings = new XmlWriterSettings();
 0104                settings.OmitXmlDeclaration = true;
 0105                using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, settings))
 106                {
 0107                    tokenSerializer.WriteKeyIdentifierClause(xmlWriter, ski[0]);
 0108                }
 109            }
 110
 0111            return sb.ToString();
 112        }
 113
 114        public static bool IsValidXmlIDValue(string val)
 115        {
 0116            if (string.IsNullOrEmpty(val))
 117            {
 0118                return false;
 119            }
 120
 121            // The first character of the ID should be a letter, '_' or ':'
 0122            return (((val[0] >= 'A') && (val[0] <= 'Z')) ||
 0123                ((val[0] >= 'a') && (val[0] <= 'z')) ||
 0124                (val[0] == '_') || (val[0] == ':'));
 125        }
 126
 127        public static string GetXmlLangAttribute(XmlReader reader)
 128        {
 4129            string xmlLang = null;
 4130            if (reader.MoveToAttribute("lang", XmlNs))
 131            {
 4132                xmlLang = reader.Value;
 4133                reader.MoveToElement();
 134            }
 135
 4136            if (xmlLang == null)
 137            {
 0138                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.XmlLangAttributeMissing));
 139            }
 140
 4141            return xmlLang;
 142        }
 143
 144        public static void WriteElementStringAsUniqueId(XmlDictionaryWriter writer, XmlDictionaryString localName, XmlDi
 145        {
 0146            writer.WriteStartElement(localName, ns);
 0147            writer.WriteValue(id);
 0148            writer.WriteEndElement();
 0149        }
 150
 151        public static void WriteElementContentAsInt64(XmlDictionaryWriter writer, XmlDictionaryString localName, XmlDict
 152        {
 0153            writer.WriteStartElement(localName, ns);
 0154            writer.WriteValue(value);
 0155            writer.WriteEndElement();
 0156        }
 157
 158        public static Int64 ReadElementContentAsInt64(XmlDictionaryReader reader)
 159        {
 0160            reader.ReadFullStartElement();
 0161            Int64 i = reader.ReadContentAsLong();
 0162            reader.ReadEndElement();
 0163            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        {
 0170            if (nodeList == null)
 171            {
 0172                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(nodeList));
 173            }
 174
 0175            List<XmlElement> xmlElements = new List<XmlElement>();
 0176            foreach (XmlNode node in nodeList)
 177            {
 0178                XmlElement tempElement = node as XmlElement;
 0179                if (tempElement != null)
 180                {
 0181                    xmlElements.Add(tempElement);
 182                }
 183            }
 184
 0185            return xmlElements;
 186        }
 187
 188        public static void ReadContentAsQName(XmlReader reader, out string localName, out string ns)
 189        {
 2190            ParseQName(reader, reader.ReadContentAsString(), out localName, out ns);
 2191        }
 192
 193        public static bool IsWhitespace(char ch)
 194        {
 84195            return (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n');
 196        }
 197
 198        public static string TrimEnd(string s)
 199        {
 200            int i;
 4201            for (i = s.Length; i > 0 && IsWhitespace(s[i - 1]); i--)
 202            {
 203                ;
 204            }
 205
 2206            if (i != s.Length)
 207            {
 0208                return s.Substring(0, i);
 209            }
 210
 2211            return s;
 212        }
 213
 214        public static string TrimStart(string s)
 215        {
 216            int i;
 4217            for (i = 0; i < s.Length && IsWhitespace(s[i]); i++)
 218            {
 219                ;
 220            }
 221
 2222            if (i != 0)
 223            {
 0224                return s.Substring(i);
 225            }
 226
 2227            return s;
 228        }
 229
 230        public static string Trim(string s)
 231        {
 232            int i;
 80233            for (i = 0; i < s.Length && IsWhitespace(s[i]); i++)
 234            {
 235                ;
 236            }
 237
 40238            if (i >= s.Length)
 239            {
 0240                return string.Empty;
 241            }
 242
 243            int j;
 80244            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
 40251            if (i != 0 || j != s.Length)
 252            {
 0253                return s.Substring(i, j - i);
 254            }
 40255            return s;
 256        }
 257
 258        public static void ParseQName(XmlReader reader, string qname, out string localName, out string ns)
 259        {
 2260            int index = qname.IndexOf(':');
 261            string prefix;
 2262            if (index < 0)
 263            {
 0264                prefix = "";
 0265                localName = TrimStart(TrimEnd(qname));
 266            }
 267            else
 268            {
 2269                if (index == qname.Length - 1)
 270                {
 0271                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.InvalidXmlQu
 272                }
 273
 2274                prefix = TrimStart(qname.Substring(0, index));
 2275                localName = TrimEnd(qname.Substring(index + 1));
 276            }
 2277            ns = reader.LookupNamespace(prefix);
 2278            if (ns == null)
 279            {
 0280                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnboundPrefixInQ
 281            }
 2282        }
 283
 284        // This code was copied from XmlDictionaryReader.ReadElementContentAsDateTime which is an internal method
 285        public static DateTime ReadElementContentAsDateTime(this XmlDictionaryReader reader)
 286        {
 0287            bool isEmptyElement = reader.IsStartElement() && reader.IsEmptyElement;
 288            DateTime value;
 289
 0290            if (isEmptyElement)
 291            {
 0292                reader.Read();
 293                try
 294                {
 0295                    value = DateTime.Parse(string.Empty, NumberFormatInfo.InvariantInfo);
 0296                }
 0297                catch (ArgumentException exception)
 298                {
 0299                    throw new XmlException(SR.Format(SR.XmlInvalidConversion, string.Empty, "DateTime"), exception);
 300                }
 0301                catch (FormatException exception)
 302                {
 0303                    throw new XmlException(SR.Format(SR.XmlInvalidConversion, string.Empty, "DateTime"), exception);
 304                }
 305            }
 306            else
 307            {
 0308                reader.ReadStartElement();
 0309                value = reader.ReadContentAsDateTimeOffset().DateTime;
 0310                reader.ReadEndElement();
 311            }
 312
 0313            return value;
 314        }
 315    }
 316}