Пример #1
0
        private static void CheckSchemaVersion(XDocument document)
        {
#if !IS_CORECLR // CORECLR_TODO: XmlSchema
            // Get the metadata node and look for the schemaVersion attribute
            XElement metadata = GetMetadataElement(document);

            if (metadata != null)
            {
                // Yank this attribute since we don't want to have to put it in our xsd
                XAttribute schemaVersionAttribute = metadata.Attribute(SchemaVersionAttributeName);

                if (schemaVersionAttribute != null)
                {
                    schemaVersionAttribute.Remove();
                }

                // Get the package id from the metadata node
                string packageId = GetPackageId(metadata);

                // If the schema of the document doesn't match any of our known schemas
                if (!ManifestSchemaUtility.IsKnownSchema(document.Root.Name.Namespace.NamespaceName))
                {
                    throw new InvalidOperationException(
                              String.Format(CultureInfo.CurrentCulture,
                                            NuGetResources.IncompatibleSchema,
                                            packageId,
                                            typeof(Manifest).Assembly.GetName().Version));
                }
            }
#endif
        }
Пример #2
0
        private static void ValidateManifestSchema(XDocument document, string schemaNamespace)
        {
#if !IS_CORECLR // CORECLR_TODO: XmlSchema
            var schemaSet = ManifestSchemaUtility.GetManifestSchemaSet(schemaNamespace);

            document.Validate(schemaSet, (sender, e) =>
            {
                if (e.Severity == XmlSeverityType.Error)
                {
                    var message = e.Message;

                    // To make sure this error message is actionable, try to add the element name
                    // where the error is occurring.
                    var senderElement = sender as XElement;
                    if (senderElement != null)
                    {
                        message = string.Format(
                            CultureInfo.CurrentCulture,
                            Strings.InvalidNuspecElement,
                            message,
                            senderElement.Name.LocalName);
                    }

                    // Throw an exception if there is a validation error
                    throw new InvalidOperationException(message);
                }
            });
#endif
        }
Пример #3
0
        private static void ValidateManifestSchema(XDocument document, string schemaNamespace)
        {
#if !IS_CORECLR // CORECLR_TODO: XmlSchema
            var schemaSet = ManifestSchemaUtility.GetManifestSchemaSet(schemaNamespace);

            document.Validate(schemaSet, (sender, e) =>
            {
                if (e.Severity == XmlSeverityType.Error)
                {
                    // Throw an exception if there is a validation error
                    throw new InvalidOperationException(e.Message);
                }
            });
#endif
        }
Пример #4
0
        /// <summary>
        /// Saves the current manifest to the specified stream.
        /// </summary>
        /// <param name="stream">The target stream.</param>
        /// <param name="minimumManifestVersion">The minimum manifest version that this class must use when saving.</param>
        /// <param name="generateBackwardsCompatible">Write out a manifest that's consumable by legacy clients, by adding any necessary translations into legacy fields.</param>
        public void Save(Stream stream, int minimumManifestVersion, bool generateBackwardsCompatible)
        {
            Validate(this);

            var version         = Math.Max(minimumManifestVersion, ManifestVersionUtility.GetManifestVersion(Metadata));
            var schemaNamespace = (XNamespace)ManifestSchemaUtility.GetSchemaNamespace(version);

            new XDocument(
                new XElement(schemaNamespace + "package",
                             Metadata.ToXElement(schemaNamespace, generateBackwardsCompatible: generateBackwardsCompatible),
                             Files.Any() ?
                             new XElement(schemaNamespace + "files",
                                          Files.Select(file => new XElement(schemaNamespace + "file",
                                                                            new XAttribute("src", file.Source),
                                                                            file.Target != null ? new XAttribute("target", file.Target) : null,
                                                                            file.Exclude != null ? new XAttribute("exclude", file.Exclude) : null))) : null)).Save(stream);
        }