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.

247 lines
7.0 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. public class ManifestGenerator
  10. {
  11. private Stream appManStream, subManStream, extraXmlStream;
  12. private Stream appManSchema, subManSchema;
  13. private XmlDocument extraXmlDoc;
  14. private MGParamParser mgPP;
  15. private string sourceDir;
  16. public void CloseStreams()
  17. {
  18. appManStream.Close();
  19. subManStream.Close();
  20. }
  21. public Stream AppManStream
  22. {
  23. get
  24. {
  25. return appManStream;
  26. }
  27. }
  28. public Stream SubManStream
  29. {
  30. get
  31. {
  32. return subManStream;
  33. }
  34. }
  35. private void GenerateManifests()
  36. {
  37. // Read in extra xml; make sure it's valid
  38. if(this.extraXmlStream == null) this.extraXmlDoc = null;
  39. else
  40. {
  41. try
  42. {
  43. this.extraXmlDoc = new XmlDocument();
  44. this.extraXmlDoc.Load(this.extraXmlStream);
  45. }
  46. catch(XmlException xmle)
  47. {
  48. throw new XmlException("Problem with extra input XML", xmle);
  49. }
  50. }
  51. // Start doing work
  52. XmlTextReader manReader = null;
  53. XmlValidatingReader manValidatingReader = null;
  54. XmlSchemaCollection appXmlSchemaCollection = null, subXmlSchemaCollection = null;
  55. // Now scan manifest dependencies
  56. MGDepTracker mgDT = new MGDepTracker();
  57. DirScanner.BeginScan((IFileOperator) mgDT, sourceDir);
  58. try
  59. {
  60. mgDT.VerifyDependencies();
  61. }
  62. catch(MGDependencyException mgde)
  63. {
  64. throw mgde;
  65. }
  66. mgDT.CalculateSizes();
  67. // Write application manifest
  68. XmlTextWriter appGen = new XmlTextWriter(appManStream, null);
  69. //XmlTextWriter appGen = new XmlTextWriter(Console.Out);
  70. appGen.Formatting = Formatting.Indented;
  71. appGen.WriteStartDocument();
  72. appGen.WriteComment("Generated by mg v0.1 (managed)");
  73. appGen.WriteStartElement("assembly");
  74. appGen.WriteAttributeString("xmlns", "asm_namespace_v1", null, "urn:schemas-microsoft-com:asm.v1");
  75. appGen.WriteAttributeString("manifestVersion", "1.0");
  76. //MGTemplateWriter.XmlOutput(appGen, mgPR.AppKeyValPairs, mgPR.DescriptionStr);
  77. mgPP.WriteAppParams(appGen);
  78. mgDT.FileXmlOutput(appGen);
  79. appGen.WriteStartElement("dependency");
  80. mgDT.AssmXmlOutput(appGen);
  81. //MGPlatformWriter.XmlOutput(appGen);
  82. mgPP.WritePlatParams(appGen);
  83. appGen.WriteEndElement(); // dependency
  84. appGen.WriteEndElement(); // assembly
  85. appGen.WriteEndDocument();
  86. appGen.Flush();
  87. appManStream.Position = 0;
  88. // Verify application manifest
  89. appXmlSchemaCollection = new XmlSchemaCollection();
  90. appXmlSchemaCollection.Add(null , new XmlTextReader(appManSchema));
  91. manReader = new XmlTextReader(appManStream);
  92. manValidatingReader = new XmlValidatingReader(manReader);
  93. manValidatingReader.Schemas.Add(appXmlSchemaCollection);
  94. manValidatingReader.ValidationType = ValidationType.Schema;
  95. XmlDocument appManifest = new XmlDocument();
  96. try
  97. {
  98. appManifest.Load(manValidatingReader);
  99. }
  100. catch (XmlSchemaException xmlse)
  101. {
  102. // Validation error
  103. throw new XmlException("Error validating final application manifest; check parameters", xmlse);
  104. }
  105. appManStream.Close();
  106. if(extraXmlDoc != null)
  107. {
  108. XmlNode extraCopy = appManifest.ImportNode(extraXmlDoc.DocumentElement, true);
  109. appManifest.DocumentElement.AppendChild(extraCopy);
  110. }
  111. appManStream = new BufferedStream(new MemoryStream());
  112. appManifest.Save(appManStream);
  113. appManStream.Position = 0;
  114. // Write subscription manifest
  115. XmlTextWriter subGen = new XmlTextWriter(subManStream, null);
  116. subGen.Formatting = Formatting.Indented;
  117. subGen.WriteStartDocument();
  118. subGen.WriteComment("Generated by mg v0.1 (managed)");
  119. subGen.WriteStartElement("assembly");
  120. subGen.WriteAttributeString("xmlns", "asm_namespace_v1", null, "urn:schemas-microsoft-com:asm.v1");
  121. subGen.WriteAttributeString("manifestVersion", "1.0");
  122. //MGSubManifestWriter.XmlOutput(subGen, mgPR.AppKeyValPairs, mgPR.SubKeyValPairs, mgPR.DescriptionStr);
  123. mgPP.WriteSubParams(subGen);
  124. subGen.WriteEndElement();
  125. subGen.WriteEndDocument();
  126. subGen.Flush();
  127. subManStream.Position = 0;
  128. // Verify subscription manifest
  129. subXmlSchemaCollection = new XmlSchemaCollection();
  130. subXmlSchemaCollection.Add(null , new XmlTextReader(subManSchema));
  131. manReader = new XmlTextReader(subManStream);
  132. manValidatingReader = new XmlValidatingReader(manReader);
  133. manValidatingReader.Schemas.Add(subXmlSchemaCollection);
  134. manValidatingReader.ValidationType = ValidationType.Schema;
  135. try
  136. {
  137. while(manValidatingReader.Read());
  138. }
  139. catch (XmlSchemaException xmlse)
  140. {
  141. // Validation error
  142. throw new XmlException("Error validating final subscription manifest; check parameters", xmlse);
  143. }
  144. subManStream.Position = 0;
  145. }
  146. public ManifestGenerator(string sourceDir, Stream paramStream, Stream extraXmlStream)
  147. {
  148. MGParamParser tempMGPP;
  149. // Check for null arguments
  150. if(sourceDir == null) throw new ArgumentNullException("Source directory (arg 1) is null.");
  151. if(paramStream == null) throw new ArgumentNullException("Parameter stream (arg 2) is null.");
  152. // Check that paths and files are valid
  153. if(!Directory.Exists(sourceDir)) throw new ArgumentException("Source directory " + sourceDir + " does not exist.");
  154. this.appManStream = new BufferedStream(new MemoryStream());
  155. this.subManStream = new BufferedStream(new MemoryStream());
  156. // Get the schemas from the assembly as resource streams
  157. Assembly thisAssm = Assembly.GetExecutingAssembly();
  158. this.appManSchema = thisAssm.GetManifestResourceStream("appManSchema");
  159. this.subManSchema = thisAssm.GetManifestResourceStream("subManSchema");
  160. this.sourceDir = sourceDir;
  161. this.extraXmlStream = extraXmlStream;
  162. // Read param stream
  163. try
  164. {
  165. this.mgPP = new MGParamParser(paramStream);
  166. }
  167. catch(MGParseErrorException mgpee)
  168. {
  169. throw mgpee;
  170. }
  171. GenerateManifests();
  172. }
  173. public ManifestGenerator(string sourceDir, MGParamParser mgPP, Stream extraXmlStream)
  174. {
  175. // Check for null arguments
  176. if(sourceDir == null) throw new ArgumentNullException("Source directory (arg 1) is null.");
  177. if(mgPP == null) throw new ArgumentNullException("Parameter object (arg 2) is null.");
  178. // Check that paths and files are valid
  179. if(!Directory.Exists(sourceDir)) throw new ArgumentException("Source directory " + sourceDir + " does not exist.");
  180. this.appManStream = new BufferedStream(new MemoryStream());
  181. this.subManStream = new BufferedStream(new MemoryStream());
  182. // Get the schemas from the assembly as resource streams
  183. Assembly thisAssm = Assembly.GetExecutingAssembly();
  184. this.appManSchema = thisAssm.GetManifestResourceStream("appManSchema");
  185. this.subManSchema = thisAssm.GetManifestResourceStream("subManSchema");
  186. this.sourceDir = sourceDir;
  187. this.extraXmlStream = extraXmlStream;
  188. this.mgPP = mgPP;
  189. GenerateManifests();
  190. }
  191. }
  192. }