Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.1 KiB

  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Text;
  5. using System.Globalization;
  6. using System.Xml;
  7. namespace Microsoft.Fusion.ADF
  8. {
  9. // A utility class with various useful functions.
  10. public class Util
  11. {
  12. public static string ByteArrToString(Byte[] bArr, string format)
  13. {
  14. if(bArr == null) return "";
  15. StringBuilder sb = new StringBuilder();
  16. if(format == null) format = "x02";
  17. foreach(Byte b in bArr)
  18. {
  19. sb.Append(b.ToString(format));
  20. }
  21. return sb.ToString();
  22. }
  23. public static void CSharpXmlCodeGenerator(XmlNode node, StreamWriter sw, string xmlWriterName)
  24. {
  25. try
  26. {
  27. switch (node.NodeType)
  28. {
  29. case XmlNodeType.Text:
  30. sw.WriteLine(xmlWriterName + ".WriteString(\"" + node.Value + "\");");
  31. break;
  32. case XmlNodeType.Element:
  33. sw.WriteLine(xmlWriterName + ".WriteStartElement(\"" + node.LocalName + "\");");
  34. XmlAttributeCollection attrColl = node.Attributes;
  35. if(attrColl != null)
  36. {
  37. IEnumerator attrEnum = attrColl.GetEnumerator();
  38. while (attrEnum.MoveNext())
  39. {
  40. XmlAttribute attr = (XmlAttribute) attrEnum.Current;
  41. sw.WriteLine(xmlWriterName + ".WriteAttributeString(\"" +
  42. attr.Prefix + "\", \"" +
  43. attr.LocalName + "\", " +
  44. "null" + ", \"" +
  45. attr.Value + "\");");
  46. }
  47. }
  48. if(node.HasChildNodes)
  49. {
  50. XmlNodeList nodeList = node.ChildNodes;
  51. IEnumerator nodeEnum = nodeList.GetEnumerator();
  52. while (nodeEnum.MoveNext())
  53. {
  54. XmlNode currNode = (XmlNode) nodeEnum.Current;
  55. CSharpXmlCodeGenerator(currNode, sw, xmlWriterName);
  56. }
  57. }
  58. sw.WriteLine(xmlWriterName + ".WriteEndElement();");
  59. break;
  60. }
  61. }
  62. catch (XmlException e)
  63. {
  64. throw new XmlException("Error in Util", e);
  65. }
  66. }
  67. public static void InjectNode(XmlNode node, XmlTextWriter xtw, bool writeEnclosingNode, bool writeAttributes)
  68. {
  69. try
  70. {
  71. switch (node.NodeType)
  72. {
  73. case XmlNodeType.Text:
  74. xtw.WriteString(node.Value);
  75. break;
  76. case XmlNodeType.Element:
  77. if(writeEnclosingNode) xtw.WriteStartElement(node.LocalName);
  78. if(writeAttributes)
  79. {
  80. XmlAttributeCollection attrColl = node.Attributes;
  81. if(attrColl != null)
  82. {
  83. IEnumerator attrEnum = attrColl.GetEnumerator();
  84. while (attrEnum.MoveNext())
  85. {
  86. XmlAttribute attr = (XmlAttribute) attrEnum.Current;
  87. xtw.WriteAttributeString(attr.Prefix, attr.LocalName, null, attr.Value);
  88. }
  89. }
  90. }
  91. if(node.HasChildNodes)
  92. {
  93. XmlNodeList nodeList = node.ChildNodes;
  94. IEnumerator nodeEnum = nodeList.GetEnumerator();
  95. while (nodeEnum.MoveNext())
  96. {
  97. XmlNode currNode = (XmlNode) nodeEnum.Current;
  98. InjectNode(currNode, xtw, true, true);
  99. }
  100. }
  101. if(writeEnclosingNode) xtw.WriteEndElement();
  102. break;
  103. default:
  104. break;
  105. }
  106. }
  107. catch (XmlException e)
  108. {
  109. throw new XmlException("Error in Util", e);
  110. }
  111. }
  112. }
  113. }