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.

182 lines
5.7 KiB

  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Xml;
  5. using System.Xml.Schema;
  6. using System.Reflection;
  7. namespace Microsoft.Fusion.ADF
  8. {
  9. // 07/18/02: DON'T DO THE EXTRA WORK THAT INPUT SCHEMA ALREADY DOES FOR YOU
  10. public class MGParamParser
  11. {
  12. private Stream xmlInput;
  13. private XmlDocument paramInput;
  14. private string appName;
  15. private XmlNode assmIDNode = null, descNode = null, appNode = null, subNode = null, platNode = null;
  16. private XmlNode appAssmIDNode, subAssmIDNode;
  17. public MGParamParser(Stream xmlInput)
  18. {
  19. this.xmlInput = xmlInput;
  20. try
  21. {
  22. XmlNodeList temp;
  23. IEnumerator nodeEnum;
  24. // Make document out of stream
  25. // Begin by adding schema
  26. Assembly thisAssm = Assembly.GetExecutingAssembly();
  27. Stream xmlInputSchema = thisAssm.GetManifestResourceStream("inputSchema");
  28. XmlSchemaCollection myXmlSchemaCollection = new XmlSchemaCollection();
  29. myXmlSchemaCollection.Add(null , new XmlTextReader(xmlInputSchema));
  30. // Then validate file (validation fills in defaults)
  31. XmlTextReader myXmlTextReader = new XmlTextReader(xmlInput);
  32. XmlValidatingReader myXmlValidatingReader = new XmlValidatingReader(myXmlTextReader);
  33. myXmlValidatingReader.Schemas.Add(myXmlSchemaCollection);
  34. myXmlValidatingReader.ValidationType = ValidationType.Schema;
  35. this.paramInput = new XmlDocument();
  36. paramInput.Load(myXmlValidatingReader);
  37. // Extract nodes we care about
  38. temp = paramInput.GetElementsByTagName("assemblyIdentity");
  39. if(temp.Count != 1) throw new MGParseErrorException("XML parameters should only have 1 assemblyIdentity element");
  40. else
  41. {
  42. nodeEnum = temp.GetEnumerator();
  43. nodeEnum.MoveNext();
  44. assmIDNode = (XmlNode) nodeEnum.Current;
  45. }
  46. temp = paramInput.GetElementsByTagName("description");
  47. if(temp.Count != 1) throw new MGParseErrorException("XML parameters should only have 1 description element");
  48. else
  49. {
  50. nodeEnum = temp.GetEnumerator();
  51. nodeEnum.MoveNext();
  52. descNode = (XmlNode) nodeEnum.Current;
  53. }
  54. temp = paramInput.GetElementsByTagName("applicationParams");
  55. if(temp.Count != 1) throw new MGParseErrorException("XML parameters should only have 1 applicationParams element");
  56. else
  57. {
  58. nodeEnum = temp.GetEnumerator();
  59. nodeEnum.MoveNext();
  60. appNode = (XmlNode) nodeEnum.Current;
  61. }
  62. temp = paramInput.GetElementsByTagName("subscriptionParams");
  63. if(temp.Count != 1) throw new MGParseErrorException("XML parameters should only have 1 subscriptionParams element");
  64. else
  65. {
  66. nodeEnum = temp.GetEnumerator();
  67. nodeEnum.MoveNext();
  68. subNode = (XmlNode) nodeEnum.Current;
  69. }
  70. temp = paramInput.GetElementsByTagName("platformParams");
  71. if(temp.Count > 1) throw new MGParseErrorException("XML parameters should have at most 1 platformParams element");
  72. else if(temp.Count == 1)
  73. {
  74. nodeEnum = temp.GetEnumerator();
  75. nodeEnum.MoveNext();
  76. platNode = (XmlNode) nodeEnum.Current;
  77. }
  78. // Extract application name
  79. appName = assmIDNode.Attributes["name"].Value;
  80. // Set up specialized assembly ID nodes
  81. appAssmIDNode = assmIDNode.Clone();
  82. subAssmIDNode = assmIDNode.Clone();
  83. XmlAttribute typeAttr1 = paramInput.CreateAttribute("type");
  84. XmlAttribute typeAttr2 = (XmlAttribute) typeAttr1.CloneNode(true);
  85. typeAttr1.Value = "application";
  86. appAssmIDNode.Attributes.Prepend(typeAttr1);
  87. typeAttr2.Value = "subscription";
  88. subAssmIDNode.Attributes.Prepend(typeAttr2);
  89. }
  90. catch (XmlException xmle)
  91. {
  92. throw new MGParseErrorException("XML parameter parsing failed", xmle);
  93. }
  94. catch (XmlSchemaException xmlse)
  95. {
  96. throw new MGParseErrorException("XML parameter validation failed", xmlse);
  97. }
  98. }
  99. public string AppName
  100. {
  101. get
  102. {
  103. return appName;
  104. }
  105. }
  106. // USE WRITE, WRITETO, OUTERXML, OTHER STUFF FROM .NET APIS INSTEAD OF INJECTNODE UTILITY
  107. public void WriteAppParams(XmlTextWriter xtw)
  108. {
  109. try
  110. {
  111. Util.InjectNode(appAssmIDNode, xtw, true, true);
  112. Util.InjectNode(descNode, xtw, true, true);
  113. Util.InjectNode(appNode, xtw, false, false);
  114. }
  115. catch (XmlException xmle)
  116. {
  117. throw new MGParseErrorException("XML parameter parsing failed", xmle);
  118. }
  119. }
  120. public void WriteSubParams(XmlTextWriter xtw)
  121. {
  122. try
  123. {
  124. Util.InjectNode(subAssmIDNode, xtw, true, true);
  125. Util.InjectNode(descNode, xtw, true, true);
  126. xtw.WriteStartElement("dependency");
  127. xtw.WriteStartElement("dependentAssembly");
  128. Util.InjectNode(appAssmIDNode, xtw, true, true);
  129. // The following is to ensure that filenames match everywhere; I may take it out
  130. XmlNodeList temp = subNode.SelectNodes("//install");
  131. IEnumerator tempEnum = temp.GetEnumerator();
  132. tempEnum.MoveNext();
  133. string tempCodebase = ((XmlNode) tempEnum.Current).Attributes["codebase"].Value;
  134. tempCodebase = tempCodebase.Substring(0, tempCodebase.LastIndexOf('/') + 1);
  135. if(!tempCodebase.Equals("")) ((XmlNode) tempEnum.Current).Attributes["codebase"].Value = String.Concat(tempCodebase, appName + ".manifest");
  136. Util.InjectNode(subNode, xtw, false, false);
  137. xtw.WriteEndElement();
  138. xtw.WriteEndElement();
  139. }
  140. catch (XmlException xmle)
  141. {
  142. throw new MGParseErrorException("XML parameter parsing failed", xmle);
  143. }
  144. }
  145. public void WritePlatParams(XmlTextWriter xtw)
  146. {
  147. try
  148. {
  149. if(platNode != null) Util.InjectNode(platNode, xtw, false, false);
  150. else MGPlatformWriter.XmlOutput(xtw); // write defaults
  151. }
  152. catch (XmlException xmle)
  153. {
  154. throw new MGParseErrorException("XML parameter parsing failed", xmle);
  155. }
  156. }
  157. }
  158. }