public void AddPackageResults(string culture, XmlValidationResults results) { if (!this.packageValidationResults.Contains(culture)) { this.packageValidationResults.Add(culture, results); } }
public Package(Product product, XmlNode node, XmlValidationResults validationResults, string name, string culture) { Product = product; Node = node; Name = name; Culture = culture; ValidationResults = validationResults; }
public Package(Product product, XmlNode node, XmlValidationResults validationResults, string name, string culture) { _product = product; _node = node; _name = name; _culture = culture; _validationResults = validationResults; }
public Package(Microsoft.Build.Tasks.Deployment.Bootstrapper.Product product, XmlNode node, XmlValidationResults validationResults, string name, string culture) { this.product = product; this.node = node; this.name = name; this.culture = culture; this.validationResults = validationResults; }
/// <summary> /// Adds the validation results of a package of the specified culture into the ProductValidationResults. /// </summary> /// <param name="culture">The culture of the XmlValidationResults to add.</param> /// <param name="results">The vaue of the results to add.</param> public void AddPackageResults(string culture, XmlValidationResults results) { if (!_packageValidationResults.Contains(culture)) { _packageValidationResults.Add(culture, results); } else { System.Diagnostics.Debug.Fail("Validation results have already been added for culture '{0}'", culture); } }
/// <summary> /// Adds the validation results of a package of the specified culture into the ProductValidationResults. /// </summary> /// <param name="culture">The culture of the XmlValidationResults to add.</param> /// <param name="results">The vaue of the results to add.</param> public void AddPackageResults(string culture, XmlValidationResults results) { if (!_packageValidationResults.ContainsKey(culture)) { _packageValidationResults.Add(culture, results); } else { System.Diagnostics.Debug.Fail("Validation results have already been added for culture '{0}'", culture); } }
private void ExploreDirectory(string strSubDirectory, XmlElement rootElement) { try { string packagePath = PackagePath; string strSubDirectoryFullPath = System.IO.Path.Combine(packagePath, strSubDirectory); // figure out our product file paths based on the directory full path string strBaseManifestFilename = System.IO.Path.Combine(strSubDirectoryFullPath, ROOT_MANIFEST_FILE); string strBaseManifestSchemaFileName = System.IO.Path.Combine(SchemaPath, MANIFEST_FILE_SCHEMA); ProductValidationResults productValidationResults = new ProductValidationResults(strBaseManifestFilename); // open the XmlDocument for this product.xml XmlDocument productDoc = LoadAndValidateXmlDocument(strBaseManifestFilename, false, strBaseManifestSchemaFileName, BOOTSTRAPPER_NAMESPACE, productValidationResults); if (productDoc != null) { bool packageAdded = false; XmlNode baseNode = productDoc.SelectSingleNode(BOOTSTRAPPER_PREFIX + ":Product", _xmlNamespaceManager); if (baseNode != null) { // Get the ProductCode attribute for this product XmlAttribute productCodeAttribute = (XmlAttribute)(baseNode.Attributes.GetNamedItem("ProductCode")); if (productCodeAttribute != null) { // now add it to our full document if it's not already present XmlNode productNode = rootElement.SelectSingleNode(BOOTSTRAPPER_PREFIX + ":Product[@ProductCode='" + productCodeAttribute.Value + "']", _xmlNamespaceManager); if (productNode == null) { productNode = CreateProductNode(baseNode); } else { productValidationResults = (ProductValidationResults)_validationResults[productCodeAttribute]; } // Fix-up the <PackageFiles> of the base node to include the SourcePath and TargetPath XmlNode packageFilesNode = baseNode.SelectSingleNode(BOOTSTRAPPER_PREFIX + ":PackageFiles", _xmlNamespaceManager); XmlNode checksNode = baseNode.SelectSingleNode(BOOTSTRAPPER_PREFIX + ":InstallChecks", _xmlNamespaceManager); XmlNode commandsNode = baseNode.SelectSingleNode(BOOTSTRAPPER_PREFIX + ":Commands", _xmlNamespaceManager); // if there was a packageFiles node, then add it in to our full document with the rest if (packageFilesNode != null) { UpdatePackageFileNodes(packageFilesNode, System.IO.Path.Combine(packagePath, strSubDirectory), strSubDirectory); ReplacePackageFileAttributes(checksNode, "PackageFile", packageFilesNode, "PackageFile", "OldName", "Name"); ReplacePackageFileAttributes(commandsNode, "PackageFile", packageFilesNode, "PackageFile", "OldName", "Name"); ReplacePackageFileAttributes(baseNode, EULA_ATTRIBUTE, packageFilesNode, "PackageFile", "OldName", "SourcePath"); } foreach (string strLanguageDirectory in Directory.GetDirectories(strSubDirectoryFullPath)) { // The base node would get destroyed as we build-up this new node. // Thus, we want to use a copy of the baseNode XmlElement baseElement = (XmlElement)(_document.ImportNode(baseNode, true)); string strLangManifestFilename = System.IO.Path.Combine(strLanguageDirectory, CHILD_MANIFEST_FILE); string strLangManifestSchemaFileName = System.IO.Path.Combine(SchemaPath, MANIFEST_FILE_SCHEMA); if (File.Exists(strLangManifestFilename)) { // Load Package.xml XmlValidationResults packageValidationResults = new XmlValidationResults(strLangManifestFilename); XmlDocument langDoc = LoadAndValidateXmlDocument(strLangManifestFilename, false, strLangManifestSchemaFileName, BOOTSTRAPPER_NAMESPACE, packageValidationResults); Debug.Assert(langDoc != null, "we couldn't load package.xml in '" + strLangManifestFilename + "'...?"); if (langDoc == null) continue; XmlNode langNode = langDoc.SelectSingleNode(BOOTSTRAPPER_PREFIX + ":Package", _xmlNamespaceManager); Debug.Assert(langNode != null, string.Format(CultureInfo.CurrentCulture, "Unable to find a package node in {0}", strLangManifestFilename)); if (langNode != null) { XmlElement langElement = (XmlElement)(_document.ImportNode(langNode, true)); XmlElement mergeElement = _document.CreateElement("Package", BOOTSTRAPPER_NAMESPACE); // Update the "PackageFiles" section to reflect this language subdirectory XmlNode packageFilesNodePackage = langElement.SelectSingleNode(BOOTSTRAPPER_PREFIX + ":PackageFiles", _xmlNamespaceManager); checksNode = langElement.SelectSingleNode(BOOTSTRAPPER_PREFIX + ":InstallChecks", _xmlNamespaceManager); commandsNode = langElement.SelectSingleNode(BOOTSTRAPPER_PREFIX + ":Commands", _xmlNamespaceManager); if (packageFilesNodePackage != null) { int nStartIndex = packagePath.Length; if ((strLanguageDirectory.ToCharArray())[nStartIndex] == System.IO.Path.DirectorySeparatorChar) nStartIndex++; UpdatePackageFileNodes(packageFilesNodePackage, strLanguageDirectory, strSubDirectory); ReplacePackageFileAttributes(checksNode, "PackageFile", packageFilesNodePackage, "PackageFile", "OldName", "Name"); ReplacePackageFileAttributes(commandsNode, "PackageFile", packageFilesNodePackage, "PackageFile", "OldName", "Name"); ReplacePackageFileAttributes(langElement, EULA_ATTRIBUTE, packageFilesNodePackage, "PackageFile", "OldName", "SourcePath"); } if (packageFilesNode != null) { ReplacePackageFileAttributes(checksNode, "PackageFile", packageFilesNode, "PackageFile", "OldName", "Name"); ReplacePackageFileAttributes(commandsNode, "PackageFile", packageFilesNode, "PackageFile", "OldName", "Name"); ReplacePackageFileAttributes(langElement, EULA_ATTRIBUTE, packageFilesNode, "PackageFile", "OldName", "SourcePath"); } // in general, we prefer the attributes of the language document over the // attributes of the base document. Copy attributes from the lang to the merged, // and then merge all unique elements into merge foreach (XmlAttribute attribute in langElement.Attributes) { mergeElement.Attributes.Append((XmlAttribute)(mergeElement.OwnerDocument.ImportNode(attribute, false))); } foreach (XmlAttribute attribute in baseElement.Attributes) { XmlAttribute convertedAttribute = (XmlAttribute)(mergeElement.OwnerDocument.ImportNode(attribute, false)); MergeAttribute(mergeElement, convertedAttribute); } // And append all of the nodes // There is a well-known set of nodes which may have inherit children // When merging these nodes, there may be subnodes taken from both the lang element and the base element. // There will never be multiple nodes with the same name in the same manifest // The function which performs this action is CombineElements(...) CombineElements(langElement, baseElement, "Commands", "PackageFile", mergeElement); CombineElements(langElement, baseElement, "InstallChecks", "Property", mergeElement); CombineElements(langElement, baseElement, "PackageFiles", "Name", mergeElement); CombineElements(langElement, baseElement, "Schedules", "Name", mergeElement); CombineElements(langElement, baseElement, "Strings", "Name", mergeElement); ReplaceStrings(mergeElement); CorrectPackageFiles(mergeElement); AppendNode(baseElement, "RelatedProducts", mergeElement); // Create a unique identifier for this package XmlAttribute cultureAttribute = (XmlAttribute)mergeElement.Attributes.GetNamedItem("Culture"); if (cultureAttribute != null && !String.IsNullOrEmpty(cultureAttribute.Value)) { string packageCode = productCodeAttribute.Value + "." + cultureAttribute.Value; AddAttribute(mergeElement, "PackageCode", packageCode); if (productValidationResults != null && packageValidationResults != null) { productValidationResults.AddPackageResults(cultureAttribute.Value, packageValidationResults); } // Only add this package if there is a culture apecified. productNode.AppendChild(mergeElement); packageAdded = true; } } } } if (packageAdded) { rootElement.AppendChild(productNode); if (!_validationResults.Contains(productCodeAttribute.Value)) { _validationResults.Add(productCodeAttribute.Value, productValidationResults); } else { Debug.WriteLine(String.Format(CultureInfo.CurrentCulture, "Validation results already added for Product Code '{0}'", productCodeAttribute)); } } } } } } catch (XmlException ex) { Debug.Fail(ex.Message); } catch (System.IO.IOException ex) { Debug.Fail(ex.Message); } catch (ArgumentException ex) { Debug.Fail(ex.Message); } }
private XmlDocument LoadAndValidateXmlDocument(string filePath, bool validateFilePresent, string schemaPath, string schemaNamespace, XmlValidationResults results) { XmlDocument xmlDocument = null; Debug.Assert(filePath != null, "null filePath?"); Debug.Assert(schemaPath != null, "null schemaPath?"); Debug.Assert(schemaNamespace != null, "null schemaNamespace?"); if ((filePath != null) && (schemaPath != null) && (schemaNamespace != null)) { // set up our validation logic by detecting the trace-switch enabled and whether or // not our files exist. bool validate = true; bool fileExists = File.Exists(filePath); bool schemaExists = File.Exists(schemaPath); // if we're being asked to validate but we can't find the schema file, then // output something useful to tell user that we can't find the schema. if (!schemaExists) { Debug.Fail("Could not locate schema '" + schemaPath + "', so no validation of '" + filePath + "' is possible."); validate = false; } // if we're being asked to validate but we can't find the data file, then // output something useful to tell user that we can't find the file and that we // can't do anything useful. if (validate && (!fileExists) && validateFilePresent) { Debug.Fail("Could not locate data file '" + filePath + "'."); validate = false; } if (fileExists) { XmlTextReader xmlTextReader = new XmlTextReader(filePath); xmlTextReader.DtdProcessing = DtdProcessing.Ignore; XmlReader xmlReader = xmlTextReader; if (validate) { #pragma warning disable 618 // Using XmlValidatingReader. TODO: We need to switch to using XmlReader.Create() with validation. var validatingReader = new XmlValidatingReader(xmlReader); #pragma warning restore 618 XmlReaderSettings xrSettings = new XmlReaderSettings(); xrSettings.DtdProcessing = DtdProcessing.Ignore; using (XmlReader xr = XmlReader.Create(schemaPath, xrSettings)) { try { // first, add our schema to the validating reader's collection of schemas var xmlSchema = validatingReader.Schemas.Add(null, xr); // if our schema namespace gets out of sync, // then all of our calls to SelectNodes and SelectSingleNode will fail Debug.Assert((xmlSchema != null) && string.Equals(schemaNamespace, xmlSchema.TargetNamespace, StringComparison.Ordinal), System.IO.Path.GetFileName(schemaPath) + " and BootstrapperBuilder.vb have mismatched namespaces, so the BootstrapperBuilder will fail to work."); // if we're supposed to be validating, then hook up our handler validatingReader.ValidationEventHandler += results.SchemaValidationEventHandler; // switch readers so the doc does the actual read over the validating // reader so we get validation events as we load the document xmlReader = validatingReader; } catch (XmlException ex) { if (validate) { Debug.Fail("Failed to load schema '" + schemaPath + "' due to the following exception:\r\n" + ex.Message); } validate = false; } catch (System.Xml.Schema.XmlSchemaException ex) { if (validate) { Debug.Fail("Failed to load schema '" + schemaPath + "' due to the following exception:\r\n" + ex.Message); } validate = false; } } } try { Debug.Assert(_document != null, "our document should have been created by now!"); xmlDocument = new XmlDocument(_document.NameTable); xmlDocument.Load(xmlReader); } catch (XmlException ex) { Debug.Fail("Failed to load document '" + filePath + "' due to the following exception:\r\n" + ex.Message); return null; } catch (System.Xml.Schema.XmlSchemaException ex) { Debug.Fail("Failed to load document '" + filePath + "' due to the following exception:\r\n" + ex.Message); return null; } finally { xmlReader.Close(); } // Note that the xml document's default namespace must match the schema namespace // or none of our SelectNodes/SelectSingleNode calls will succeed Debug.Assert(xmlDocument.DocumentElement != null && string.Equals(xmlDocument.DocumentElement.NamespaceURI, schemaNamespace, StringComparison.Ordinal), "'" + xmlDocument.DocumentElement.NamespaceURI + "' is not '" + schemaNamespace + "'..."); if ((xmlDocument.DocumentElement == null) || (!string.Equals(xmlDocument.DocumentElement.NamespaceURI, schemaNamespace, StringComparison.Ordinal))) { } } } return xmlDocument; }
private void ExploreDirectory(string strSubDirectory, XmlElement rootElement) { try { string packagePath = this.PackagePath; string str2 = System.IO.Path.Combine(packagePath, strSubDirectory); string filePath = System.IO.Path.Combine(str2, "product.xml"); string schemaPath = System.IO.Path.Combine(this.SchemaPath, "package.xsd"); ProductValidationResults results = new ProductValidationResults(filePath); XmlDocument document = this.LoadAndValidateXmlDocument(filePath, false, schemaPath, "http://schemas.microsoft.com/developer/2004/01/bootstrapper", results); if (document != null) { bool flag = false; XmlNode node = document.SelectSingleNode("bootstrapper:Product", this.xmlNamespaceManager); if (node != null) { XmlAttribute namedItem = (XmlAttribute) node.Attributes.GetNamedItem("ProductCode"); if (namedItem != null) { XmlNode newChild = rootElement.SelectSingleNode("bootstrapper:Product[@ProductCode='" + namedItem.Value + "']", this.xmlNamespaceManager); if (newChild == null) { newChild = this.CreateProductNode(node); } else { results = (ProductValidationResults) this.validationResults[namedItem]; } XmlNode packageFilesNode = node.SelectSingleNode("bootstrapper:PackageFiles", this.xmlNamespaceManager); XmlNode targetNodes = node.SelectSingleNode("bootstrapper:InstallChecks", this.xmlNamespaceManager); XmlNode node5 = node.SelectSingleNode("bootstrapper:Commands", this.xmlNamespaceManager); if (packageFilesNode != null) { this.UpdatePackageFileNodes(packageFilesNode, System.IO.Path.Combine(packagePath, strSubDirectory), strSubDirectory); this.ReplacePackageFileAttributes(targetNodes, "PackageFile", packageFilesNode, "PackageFile", "OldName", "Name"); this.ReplacePackageFileAttributes(node5, "PackageFile", packageFilesNode, "PackageFile", "OldName", "Name"); this.ReplacePackageFileAttributes(node, "LicenseAgreement", packageFilesNode, "PackageFile", "OldName", "SourcePath"); } foreach (string str5 in Directory.GetDirectories(str2)) { XmlElement baseElement = (XmlElement) this.document.ImportNode(node, true); string path = System.IO.Path.Combine(str5, "package.xml"); string str7 = System.IO.Path.Combine(this.SchemaPath, "package.xsd"); if (File.Exists(path)) { XmlValidationResults results2 = new XmlValidationResults(path); XmlDocument document2 = this.LoadAndValidateXmlDocument(path, false, str7, "http://schemas.microsoft.com/developer/2004/01/bootstrapper", results2); if (document2 != null) { XmlNode node6 = document2.SelectSingleNode("bootstrapper:Package", this.xmlNamespaceManager); if (node6 != null) { XmlElement element2 = (XmlElement) this.document.ImportNode(node6, true); XmlElement targetNode = this.document.CreateElement("Package", "http://schemas.microsoft.com/developer/2004/01/bootstrapper"); XmlNode node7 = element2.SelectSingleNode("bootstrapper:PackageFiles", this.xmlNamespaceManager); targetNodes = element2.SelectSingleNode("bootstrapper:InstallChecks", this.xmlNamespaceManager); node5 = element2.SelectSingleNode("bootstrapper:Commands", this.xmlNamespaceManager); if (node7 != null) { int length = packagePath.Length; if (str5.ToCharArray()[length] == System.IO.Path.DirectorySeparatorChar) { length++; } this.UpdatePackageFileNodes(node7, str5, strSubDirectory); this.ReplacePackageFileAttributes(targetNodes, "PackageFile", node7, "PackageFile", "OldName", "Name"); this.ReplacePackageFileAttributes(node5, "PackageFile", node7, "PackageFile", "OldName", "Name"); this.ReplacePackageFileAttributes(element2, "LicenseAgreement", node7, "PackageFile", "OldName", "SourcePath"); } if (packageFilesNode != null) { this.ReplacePackageFileAttributes(targetNodes, "PackageFile", packageFilesNode, "PackageFile", "OldName", "Name"); this.ReplacePackageFileAttributes(node5, "PackageFile", packageFilesNode, "PackageFile", "OldName", "Name"); this.ReplacePackageFileAttributes(element2, "LicenseAgreement", packageFilesNode, "PackageFile", "OldName", "SourcePath"); } foreach (XmlAttribute attribute2 in element2.Attributes) { targetNode.Attributes.Append((XmlAttribute) targetNode.OwnerDocument.ImportNode(attribute2, false)); } foreach (XmlAttribute attribute3 in baseElement.Attributes) { XmlAttribute attribute = (XmlAttribute) targetNode.OwnerDocument.ImportNode(attribute3, false); this.MergeAttribute(targetNode, attribute); } this.CombineElements(element2, baseElement, "Commands", "PackageFile", targetNode); this.CombineElements(element2, baseElement, "InstallChecks", "Property", targetNode); this.CombineElements(element2, baseElement, "PackageFiles", "Name", targetNode); this.CombineElements(element2, baseElement, "Schedules", "Name", targetNode); this.CombineElements(element2, baseElement, "Strings", "Name", targetNode); this.ReplaceStrings(targetNode); this.CorrectPackageFiles(targetNode); this.AppendNode(baseElement, "RelatedProducts", targetNode); XmlAttribute attribute5 = (XmlAttribute) targetNode.Attributes.GetNamedItem("Culture"); if ((attribute5 != null) && !string.IsNullOrEmpty(attribute5.Value)) { string attributeValue = namedItem.Value + "." + attribute5.Value; this.AddAttribute(targetNode, "PackageCode", attributeValue); if ((results != null) && (results2 != null)) { results.AddPackageResults(attribute5.Value, results2); } newChild.AppendChild(targetNode); flag = true; } } } } } if (flag) { rootElement.AppendChild(newChild); if (!this.validationResults.Contains(namedItem.Value)) { this.validationResults.Add(namedItem.Value, results); } } } } } } catch (XmlException) { } catch (IOException) { } catch (ArgumentException) { } }
private XmlDocument LoadAndValidateXmlDocument(string filePath, bool validateFilePresent, string schemaPath, string schemaNamespace, XmlValidationResults results) { XmlDocument document = null; if (((filePath != null) && (schemaPath != null)) && (schemaNamespace != null)) { bool flag = true; bool flag2 = File.Exists(filePath); if (!File.Exists(schemaPath)) { flag = false; } if ((flag && !flag2) && validateFilePresent) { flag = false; } if (!flag2) { return document; } XmlReader reader = new XmlTextReader(filePath); if (flag) { XmlValidatingReader reader2 = new XmlValidatingReader(reader); try { reader2.Schemas.Add(null, schemaPath); reader2.ValidationEventHandler += new ValidationEventHandler(results.SchemaValidationEventHandler); reader = reader2; } catch (XmlException) { flag = false; } catch (XmlSchemaException) { flag = false; } } try { document = new XmlDocument(this.document.NameTable); document.Load(reader); } catch (XmlException) { return null; } catch (XmlSchemaException) { return null; } finally { reader.Close(); } if (document.DocumentElement != null) { string.Equals(document.DocumentElement.NamespaceURI, schemaNamespace, StringComparison.Ordinal); } } return document; }