XmlHelper.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.IO;
  7. using System.ServiceModel.Channels;
  8. using System.Text;
  9. using System.Xml;
  10. static class XmlHelper
  11. {
  12. internal static void AddNamespaceDeclaration(XmlDictionaryWriter writer, string prefix, XmlDictionaryString ns)
  13. {
  14. string p = writer.LookupPrefix(ns.Value);
  15. if (p == null || p != prefix)
  16. {
  17. writer.WriteXmlnsAttribute(prefix, ns);
  18. }
  19. }
  20. internal static string EnsureNamespaceDefined(XmlDictionaryWriter writer, XmlDictionaryString ns, string defaultPrefix)
  21. {
  22. string p = writer.LookupPrefix(ns.Value);
  23. if (p == null)
  24. {
  25. writer.WriteXmlnsAttribute(defaultPrefix, ns);
  26. p = defaultPrefix;
  27. }
  28. return p;
  29. }
  30. internal static XmlQualifiedName GetAttributeValueAsQName(XmlReader reader, string attributeName)
  31. {
  32. string qname = reader.GetAttribute(attributeName);
  33. if (qname == null)
  34. {
  35. return null;
  36. }
  37. return GetValueAsQName(reader, qname);
  38. }
  39. /// <summary>
  40. /// Enforces that parent has exactly 1 child of type XML element and nothing else (barring comments and whitespaces)
  41. /// and returns the child
  42. /// </summary>
  43. internal static XmlElement GetChildElement(XmlElement parent)
  44. {
  45. if (parent == null)
  46. {
  47. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("parent");
  48. }
  49. XmlElement result = null;
  50. for (int i = 0; i < parent.ChildNodes.Count; ++i)
  51. {
  52. XmlNode child = parent.ChildNodes[i];
  53. if (child.NodeType == XmlNodeType.Whitespace || child.NodeType == XmlNodeType.Comment)
  54. {
  55. continue;
  56. }
  57. else if (child.NodeType == XmlNodeType.Element && result == null)
  58. {
  59. result = ((XmlElement) child);
  60. }
  61. else
  62. {
  63. OnUnexpectedChildNodeError(parent, child);
  64. }
  65. }
  66. if (result == null)
  67. {
  68. OnChildNodeTypeMissing(parent, XmlNodeType.Element);
  69. }
  70. return result;
  71. }
  72. internal static XmlElement GetChildElement(XmlElement parent, string childLocalName, string childNamespace)
  73. {
  74. if (parent == null)
  75. {
  76. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("parent");
  77. }
  78. for (int i = 0; i < parent.ChildNodes.Count; ++i)
  79. {
  80. XmlNode child = parent.ChildNodes[i];
  81. if (child.NodeType == XmlNodeType.Whitespace || child.NodeType == XmlNodeType.Comment)
  82. {
  83. continue;
  84. }
  85. else if (child.NodeType == XmlNodeType.Element)
  86. {
  87. if (child.LocalName == childLocalName && child.NamespaceURI == childNamespace)
  88. {
  89. return ((XmlElement) child);
  90. }
  91. }
  92. else
  93. {
  94. OnUnexpectedChildNodeError(parent, child);
  95. }
  96. }
  97. return null;
  98. }
  99. internal static XmlQualifiedName GetValueAsQName(XmlReader reader, string value)
  100. {
  101. string prefix;
  102. string name;
  103. SplitIntoPrefixAndName(value, out prefix, out name);
  104. string ns = reader.LookupNamespace(prefix);
  105. if (ns == null && prefix.Length > 0)
  106. {
  107. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.CouldNotFindNamespaceForPrefix, prefix)));
  108. }
  109. return new XmlQualifiedName(name, ns);
  110. }
  111. internal static string GetWhiteSpace(XmlReader reader)
  112. {
  113. string s = null;
  114. StringBuilder sb = null;
  115. while (reader.NodeType == XmlNodeType.Whitespace || reader.NodeType == XmlNodeType.SignificantWhitespace)
  116. {
  117. if (sb != null)
  118. {
  119. sb.Append(reader.Value);
  120. }
  121. else if (s != null)
  122. {
  123. sb = new StringBuilder(s);
  124. sb.Append(reader.Value);
  125. s = null;
  126. }
  127. else
  128. {
  129. s = reader.Value;
  130. }
  131. if (!reader.Read())
  132. {
  133. break;
  134. }
  135. }
  136. return sb != null ? sb.ToString() : s;
  137. }
  138. internal static bool IsWhitespaceOrComment(XmlReader reader)
  139. {
  140. if (reader.NodeType == XmlNodeType.Comment)
  141. {
  142. return true;
  143. }
  144. else if (reader.NodeType == XmlNodeType.Whitespace)
  145. {
  146. return true;
  147. }
  148. else
  149. {
  150. return false;
  151. }
  152. }
  153. internal static void OnChildNodeTypeMissing(string parentName, XmlNodeType expectedNodeType)
  154. {
  155. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.ChildNodeTypeMissing, parentName, expectedNodeType)));
  156. }
  157. internal static void OnChildNodeTypeMissing(XmlElement parent, XmlNodeType expectedNodeType)
  158. {
  159. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.ChildNodeTypeMissing, parent.Name, expectedNodeType)));
  160. }
  161. internal static void OnEmptyElementError(XmlReader r)
  162. {
  163. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.EmptyXmlElementError, r.Name)));
  164. }
  165. internal static void OnEmptyElementError(XmlElement e)
  166. {
  167. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.EmptyXmlElementError, e.Name)));
  168. }
  169. internal static void OnEOF()
  170. {
  171. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.UnexpectedEndOfFile)));
  172. }
  173. internal static void OnNamespaceMissing(string prefix)
  174. {
  175. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.CouldNotFindNamespaceForPrefix, prefix)));
  176. }
  177. internal static void OnRequiredAttributeMissing(string attrName, string elementName)
  178. {
  179. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.RequiredAttributeMissing, attrName, elementName)));
  180. }
  181. internal static void OnRequiredElementMissing(string elementName, string elementNamespace)
  182. {
  183. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.ExpectedElementMissing, elementName, elementNamespace)));
  184. }
  185. internal static void OnUnexpectedChildNodeError(string parentName, XmlReader r)
  186. {
  187. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.UnexpectedXmlChildNode, r.Name, r.NodeType, parentName)));
  188. }
  189. internal static void OnUnexpectedChildNodeError(XmlElement parent, XmlNode n)
  190. {
  191. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.UnexpectedXmlChildNode, n.Name, n.NodeType, parent.Name)));
  192. }
  193. internal static string ReadEmptyElementAndRequiredAttribute(XmlDictionaryReader reader,
  194. XmlDictionaryString name, XmlDictionaryString namespaceUri, XmlDictionaryString attributeName,
  195. out string prefix)
  196. {
  197. reader.MoveToStartElement(name, namespaceUri);
  198. prefix = reader.Prefix;
  199. bool isEmptyElement = reader.IsEmptyElement;
  200. string value = reader.GetAttribute(attributeName, null);
  201. if (value == null)
  202. {
  203. OnRequiredAttributeMissing(attributeName.Value, null);
  204. }
  205. reader.Read();
  206. if (!isEmptyElement)
  207. {
  208. reader.ReadEndElement();
  209. }
  210. return value;
  211. }
  212. internal static string GetRequiredNonEmptyAttribute(XmlDictionaryReader reader, XmlDictionaryString name, XmlDictionaryString ns)
  213. {
  214. string value = reader.GetAttribute(name, ns);
  215. if (value == null || value.Length == 0)
  216. {
  217. OnRequiredAttributeMissing(name.Value, reader == null ? null : reader.Name);
  218. }
  219. return value;
  220. }
  221. internal static byte[] GetRequiredBase64Attribute(XmlDictionaryReader reader, XmlDictionaryString name, XmlDictionaryString ns)
  222. {
  223. if (!reader.MoveToAttribute(name.Value, ns == null ? null : ns.Value))
  224. {
  225. OnRequiredAttributeMissing(name.Value, ns == null ? null : ns.Value);
  226. }
  227. byte[] value = reader.ReadContentAsBase64();
  228. if (value == null || value.Length == 0)
  229. {
  230. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  231. new XmlException(SR.GetString(SR.EmptyBase64Attribute, name, ns)));
  232. }
  233. return value;
  234. }
  235. internal static string ReadTextElementAsTrimmedString(XmlElement element)
  236. {
  237. if (element == null)
  238. {
  239. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("element");
  240. }
  241. using (XmlReader reader = new XmlNodeReader(element))
  242. {
  243. reader.MoveToContent();
  244. return XmlUtil.Trim(reader.ReadElementContentAsString());
  245. }
  246. }
  247. internal static void SplitIntoPrefixAndName(string qName, out string prefix, out string name)
  248. {
  249. string[] parts = qName.Split(':');
  250. if (parts.Length > 2)
  251. {
  252. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.InvalidQName));
  253. }
  254. if (parts.Length == 2)
  255. {
  256. prefix = parts[0].Trim();
  257. name = parts[1].Trim();
  258. }
  259. else
  260. {
  261. prefix = string.Empty;
  262. name = qName.Trim();
  263. }
  264. }
  265. internal static void ValidateIdPrefix(string idPrefix)
  266. {
  267. if (idPrefix == null)
  268. {
  269. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("idPrefix"));
  270. }
  271. if (idPrefix.Length == 0)
  272. {
  273. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("idPrefix", SR.GetString(SR.ValueMustBeGreaterThanZero)));
  274. }
  275. if ((!Char.IsLetter(idPrefix[0]) && idPrefix[0] != '_'))
  276. {
  277. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("idPrefix", SR.GetString(SR.InValidateIdPrefix, idPrefix[0])));
  278. }
  279. for (int i = 1; i < idPrefix.Length; i++)
  280. {
  281. char c = idPrefix[i];
  282. if (!Char.IsLetter(c) && !Char.IsNumber(c) && c != '.' && c != '_' && c != '-')
  283. {
  284. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("idPrefix", SR.GetString(SR.InValidateId, idPrefix[i])));
  285. }
  286. }
  287. }
  288. internal static UniqueId GetAttributeAsUniqueId(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString ns)
  289. {
  290. return GetAttributeAsUniqueId(reader, localName.Value, (ns != null ? ns.Value : null));
  291. }
  292. static UniqueId GetAttributeAsUniqueId(XmlDictionaryReader reader, string name, string ns)
  293. {
  294. if (!reader.MoveToAttribute(name, ns))
  295. {
  296. return null;
  297. }
  298. UniqueId id = reader.ReadContentAsUniqueId();
  299. reader.MoveToElement();
  300. return id;
  301. }
  302. #if NO
  303. static public void WriteAttributeStringAsUniqueId(XmlWriter writer, string localName, string ns, UniqueId id)
  304. {
  305. WriteAttributeStringAsUniqueId(XmlDictionaryWriter.CreateDictionaryWriter(writer), localName, ns, id);
  306. }
  307. static public void WriteAttributeStringAsUniqueId(XmlWriter writer, string prefix, string localName, string ns, UniqueId id)
  308. {
  309. WriteAttributeStringAsUniqueId(XmlDictionaryWriter.CreateDictionaryWriter(writer), prefix, localName, ns, id);
  310. }
  311. static public void WriteAttributeStringAsUniqueId(XmlDictionaryWriter writer, string localName, string ns, UniqueId id)
  312. {
  313. WriteAttributeStringAsUniqueId(writer, null, localName, ns, id);
  314. }
  315. static public void WriteAttributeStringAsUniqueId(XmlDictionaryWriter writer, string prefix, string localName, string ns, UniqueId id)
  316. {
  317. writer.WriteStartAttribute(prefix, localName, ns);
  318. writer.WriteValue(id);
  319. writer.WriteEndAttribute();
  320. }
  321. static public void WriteAttributeStringAsUniqueId(XmlDictionaryWriter writer, XmlDictionaryString localName, XmlDictionaryString ns, UniqueId id)
  322. {
  323. WriteAttributeStringAsUniqueId(writer, null, localName, ns, id);
  324. }
  325. #endif
  326. static public void WriteAttributeStringAsUniqueId(XmlDictionaryWriter writer, string prefix, XmlDictionaryString localName, XmlDictionaryString ns, UniqueId id)
  327. {
  328. writer.WriteStartAttribute(prefix, localName, ns);
  329. writer.WriteValue(id);
  330. writer.WriteEndAttribute();
  331. }
  332. static public void WriteElementStringAsUniqueId(XmlWriter writer, string localName, UniqueId id)
  333. {
  334. writer.WriteStartElement(localName);
  335. writer.WriteValue(id);
  336. writer.WriteEndElement();
  337. }
  338. #if NO
  339. static public void WriteElementStringAsUniqueId(XmlDictionaryWriter writer, string localName, UniqueId id)
  340. {
  341. WriteElementStringAsUniqueId(writer, localName, null, id);
  342. }
  343. static public void WriteElementStringAsUniqueId(XmlDictionaryWriter writer, string localName, string ns, UniqueId id)
  344. {
  345. writer.WriteStartElement(localName, ns);
  346. writer.WriteValue(id);
  347. writer.WriteEndElement();
  348. }
  349. #endif
  350. static public void WriteElementStringAsUniqueId(XmlDictionaryWriter writer, XmlDictionaryString localName, XmlDictionaryString ns, UniqueId id)
  351. {
  352. writer.WriteStartElement(localName, ns);
  353. writer.WriteValue(id);
  354. writer.WriteEndElement();
  355. }
  356. static public void WriteElementContentAsInt64(XmlDictionaryWriter writer, XmlDictionaryString localName, XmlDictionaryString ns, Int64 value)
  357. {
  358. writer.WriteStartElement(localName, ns);
  359. writer.WriteValue(value);
  360. writer.WriteEndElement();
  361. }
  362. static public Int64 ReadElementContentAsInt64(XmlDictionaryReader reader)
  363. {
  364. reader.ReadFullStartElement();
  365. Int64 i = reader.ReadContentAsLong();
  366. reader.ReadEndElement();
  367. return i;
  368. }
  369. static public void WriteStringAsUniqueId(XmlDictionaryWriter writer, UniqueId id)
  370. {
  371. writer.WriteValue(id);
  372. }
  373. static public UniqueId ReadElementStringAsUniqueId(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString ns)
  374. {
  375. if (reader.IsStartElement(localName, ns) && reader.IsEmptyElement)
  376. {
  377. reader.Read();
  378. return new UniqueId(string.Empty);
  379. }
  380. reader.ReadStartElement(localName, ns);
  381. UniqueId id = reader.ReadContentAsUniqueId();
  382. reader.ReadEndElement();
  383. return id;
  384. }
  385. static public UniqueId ReadElementStringAsUniqueId(XmlDictionaryReader reader)
  386. {
  387. if (reader.IsStartElement() && reader.IsEmptyElement)
  388. {
  389. reader.Read();
  390. return new UniqueId(string.Empty);
  391. }
  392. reader.ReadStartElement();
  393. UniqueId id = reader.ReadContentAsUniqueId();
  394. reader.ReadEndElement();
  395. return id;
  396. }
  397. #if NO
  398. internal static UniqueId ReadEmptyElementAndRequiredAttributeAsUniqueId(XmlDictionaryReader reader, XmlDictionaryString name, XmlDictionaryString namespaceUri, XmlDictionaryString attributeName, out string prefix)
  399. {
  400. string s = ReadEmptyElementAndRequiredAttribute(reader, name, namespaceUri, attributeName, out prefix);
  401. if (s == null)
  402. return null;
  403. return new UniqueId(s);
  404. }
  405. static public UniqueId ReadTextElementAsUniqueId(XmlDictionaryReader reader)
  406. {
  407. return new UniqueId(ReadTextElement(reader));
  408. }
  409. #endif
  410. static public UniqueId ReadTextElementAsUniqueId(XmlElement element)
  411. {
  412. return new UniqueId(ReadTextElementAsTrimmedString(element));
  413. }
  414. #if NO
  415. static public UniqueId GetAttributeAsUniqueId(XmlElement element, string localName, string ns)
  416. {
  417. XmlAttribute attr = element.Attributes[localName, ns];
  418. if (attr == null)
  419. return null;
  420. return new UniqueId(attr.Value);
  421. }
  422. #endif
  423. }
  424. }