using System; using System.Collections; using System.IO; using System.Xml; using System.Xml.Schema; using System.Reflection; namespace Microsoft.Fusion.ADF { public class ManifestGenerator { private Stream appManStream, subManStream, extraXmlStream; private Stream appManSchema, subManSchema; private XmlDocument extraXmlDoc; private MGParamParser mgPP; private string sourceDir; public void CloseStreams() { appManStream.Close(); subManStream.Close(); } public Stream AppManStream { get { return appManStream; } } public Stream SubManStream { get { return subManStream; } } private void GenerateManifests() { // Read in extra xml; make sure it's valid if(this.extraXmlStream == null) this.extraXmlDoc = null; else { try { this.extraXmlDoc = new XmlDocument(); this.extraXmlDoc.Load(this.extraXmlStream); } catch(XmlException xmle) { throw new XmlException("Problem with extra input XML", xmle); } } // Start doing work XmlTextReader manReader = null; XmlValidatingReader manValidatingReader = null; XmlSchemaCollection appXmlSchemaCollection = null, subXmlSchemaCollection = null; // Now scan manifest dependencies MGDepTracker mgDT = new MGDepTracker(); DirScanner.BeginScan((IFileOperator) mgDT, sourceDir); try { mgDT.VerifyDependencies(); } catch(MGDependencyException mgde) { throw mgde; } mgDT.CalculateSizes(); // Write application manifest XmlTextWriter appGen = new XmlTextWriter(appManStream, null); //XmlTextWriter appGen = new XmlTextWriter(Console.Out); appGen.Formatting = Formatting.Indented; appGen.WriteStartDocument(); appGen.WriteComment("Generated by mg v0.1 (managed)"); appGen.WriteStartElement("assembly"); appGen.WriteAttributeString("xmlns", "asm_namespace_v1", null, "urn:schemas-microsoft-com:asm.v1"); appGen.WriteAttributeString("manifestVersion", "1.0"); //MGTemplateWriter.XmlOutput(appGen, mgPR.AppKeyValPairs, mgPR.DescriptionStr); mgPP.WriteAppParams(appGen); mgDT.FileXmlOutput(appGen); appGen.WriteStartElement("dependency"); mgDT.AssmXmlOutput(appGen); //MGPlatformWriter.XmlOutput(appGen); mgPP.WritePlatParams(appGen); appGen.WriteEndElement(); // dependency appGen.WriteEndElement(); // assembly appGen.WriteEndDocument(); appGen.Flush(); appManStream.Position = 0; // Verify application manifest appXmlSchemaCollection = new XmlSchemaCollection(); appXmlSchemaCollection.Add(null , new XmlTextReader(appManSchema)); manReader = new XmlTextReader(appManStream); manValidatingReader = new XmlValidatingReader(manReader); manValidatingReader.Schemas.Add(appXmlSchemaCollection); manValidatingReader.ValidationType = ValidationType.Schema; XmlDocument appManifest = new XmlDocument(); try { appManifest.Load(manValidatingReader); } catch (XmlSchemaException xmlse) { // Validation error throw new XmlException("Error validating final application manifest; check parameters", xmlse); } appManStream.Close(); if(extraXmlDoc != null) { XmlNode extraCopy = appManifest.ImportNode(extraXmlDoc.DocumentElement, true); appManifest.DocumentElement.AppendChild(extraCopy); } appManStream = new BufferedStream(new MemoryStream()); appManifest.Save(appManStream); appManStream.Position = 0; // Write subscription manifest XmlTextWriter subGen = new XmlTextWriter(subManStream, null); subGen.Formatting = Formatting.Indented; subGen.WriteStartDocument(); subGen.WriteComment("Generated by mg v0.1 (managed)"); subGen.WriteStartElement("assembly"); subGen.WriteAttributeString("xmlns", "asm_namespace_v1", null, "urn:schemas-microsoft-com:asm.v1"); subGen.WriteAttributeString("manifestVersion", "1.0"); //MGSubManifestWriter.XmlOutput(subGen, mgPR.AppKeyValPairs, mgPR.SubKeyValPairs, mgPR.DescriptionStr); mgPP.WriteSubParams(subGen); subGen.WriteEndElement(); subGen.WriteEndDocument(); subGen.Flush(); subManStream.Position = 0; // Verify subscription manifest subXmlSchemaCollection = new XmlSchemaCollection(); subXmlSchemaCollection.Add(null , new XmlTextReader(subManSchema)); manReader = new XmlTextReader(subManStream); manValidatingReader = new XmlValidatingReader(manReader); manValidatingReader.Schemas.Add(subXmlSchemaCollection); manValidatingReader.ValidationType = ValidationType.Schema; try { while(manValidatingReader.Read()); } catch (XmlSchemaException xmlse) { // Validation error throw new XmlException("Error validating final subscription manifest; check parameters", xmlse); } subManStream.Position = 0; } public ManifestGenerator(string sourceDir, Stream paramStream, Stream extraXmlStream) { MGParamParser tempMGPP; // Check for null arguments if(sourceDir == null) throw new ArgumentNullException("Source directory (arg 1) is null."); if(paramStream == null) throw new ArgumentNullException("Parameter stream (arg 2) is null."); // Check that paths and files are valid if(!Directory.Exists(sourceDir)) throw new ArgumentException("Source directory " + sourceDir + " does not exist."); this.appManStream = new BufferedStream(new MemoryStream()); this.subManStream = new BufferedStream(new MemoryStream()); // Get the schemas from the assembly as resource streams Assembly thisAssm = Assembly.GetExecutingAssembly(); this.appManSchema = thisAssm.GetManifestResourceStream("appManSchema"); this.subManSchema = thisAssm.GetManifestResourceStream("subManSchema"); this.sourceDir = sourceDir; this.extraXmlStream = extraXmlStream; // Read param stream try { this.mgPP = new MGParamParser(paramStream); } catch(MGParseErrorException mgpee) { throw mgpee; } GenerateManifests(); } public ManifestGenerator(string sourceDir, MGParamParser mgPP, Stream extraXmlStream) { // Check for null arguments if(sourceDir == null) throw new ArgumentNullException("Source directory (arg 1) is null."); if(mgPP == null) throw new ArgumentNullException("Parameter object (arg 2) is null."); // Check that paths and files are valid if(!Directory.Exists(sourceDir)) throw new ArgumentException("Source directory " + sourceDir + " does not exist."); this.appManStream = new BufferedStream(new MemoryStream()); this.subManStream = new BufferedStream(new MemoryStream()); // Get the schemas from the assembly as resource streams Assembly thisAssm = Assembly.GetExecutingAssembly(); this.appManSchema = thisAssm.GetManifestResourceStream("appManSchema"); this.subManSchema = thisAssm.GetManifestResourceStream("subManSchema"); this.sourceDir = sourceDir; this.extraXmlStream = extraXmlStream; this.mgPP = mgPP; GenerateManifests(); } } }