XmlUtil.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System.Runtime;
  7. using System.Xml;
  8. static class XmlUtil
  9. {
  10. public const string XmlNs = "http://www.w3.org/XML/1998/namespace";
  11. public const string XmlNsNs = "http://www.w3.org/2000/xmlns/";
  12. public static string GetXmlLangAttribute(XmlReader reader)
  13. {
  14. string xmlLang = null;
  15. if (reader.MoveToAttribute("lang", XmlNs))
  16. {
  17. xmlLang = reader.Value;
  18. reader.MoveToElement();
  19. }
  20. if (xmlLang == null)
  21. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlLangAttributeMissing)));
  22. return xmlLang;
  23. }
  24. // FIX for 7455
  25. public static bool IsTrue(string booleanValue)
  26. {
  27. if (string.IsNullOrEmpty(booleanValue))
  28. {
  29. return false;
  30. }
  31. return XmlConvert.ToBoolean(booleanValue);
  32. }
  33. public static void ReadContentAsQName(XmlReader reader, out string localName, out string ns)
  34. {
  35. ParseQName(reader, reader.ReadContentAsString(), out localName, out ns);
  36. }
  37. public static bool IsWhitespace(char ch)
  38. {
  39. return (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n');
  40. }
  41. public static string TrimEnd(string s)
  42. {
  43. int i;
  44. for (i = s.Length; i > 0 && IsWhitespace(s[i - 1]); i--);
  45. if (i != s.Length)
  46. {
  47. return s.Substring(0, i);
  48. }
  49. return s;
  50. }
  51. public static string TrimStart(string s)
  52. {
  53. int i;
  54. for (i = 0; i < s.Length && IsWhitespace(s[i]); i++);
  55. if (i != 0)
  56. {
  57. return s.Substring(i);
  58. }
  59. return s;
  60. }
  61. public static string Trim(string s)
  62. {
  63. int i;
  64. for (i = 0; i < s.Length && IsWhitespace(s[i]); i++);
  65. if (i >= s.Length)
  66. {
  67. return string.Empty;
  68. }
  69. int j;
  70. for (j = s.Length; j > 0 && IsWhitespace(s[j - 1]); j--);
  71. Fx.Assert(j > i, "Logic error in XmlUtil.Trim().");
  72. if (i != 0 || j != s.Length)
  73. {
  74. return s.Substring(i, j - i);
  75. }
  76. return s;
  77. }
  78. public static void ParseQName(XmlReader reader, string qname, out string localName, out string ns)
  79. {
  80. int index = qname.IndexOf(':');
  81. string prefix;
  82. if (index < 0)
  83. {
  84. prefix = "";
  85. localName = TrimStart(TrimEnd(qname));
  86. }
  87. else
  88. {
  89. if (index == qname.Length - 1)
  90. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.InvalidXmlQualifiedName, qname)));
  91. prefix = TrimStart(qname.Substring(0, index));
  92. localName = TrimEnd(qname.Substring(index + 1));
  93. }
  94. ns = reader.LookupNamespace(prefix);
  95. if (ns == null)
  96. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.UnboundPrefixInQName, qname)));
  97. }
  98. }
  99. }