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