Exemple #1
0
        private static void CheckSchemaVersion(XDocument document)
        {
#if NET45 // 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
        }
Exemple #2
0
        public void Save(Stream stream, bool validate, int minimumManifestVersion)
        {
            if (validate)
            {
                // Validate before saving
                Validate(this);
            }

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

            // Define the namespaces to use when serializing
            var ns = new XmlSerializerNamespaces();

            ns.Add("", schemaNamespace);

            // Need to force the namespace here again as the default in order to get the XML output clean
            var serializer = new XmlSerializer(typeof(Manifest), schemaNamespace);

            using (var xmlWriter = new XmlTextWriter(stream, Encoding.UTF8))
            {
                xmlWriter.Indentation = 4;
                xmlWriter.Formatting  = Formatting.Indented;
                serializer.Serialize(xmlWriter, this, ns);
            }
        }
Exemple #3
0
        public void Save(Stream stream, bool validate, int minimumManifestVersion)
        {
            int version         = Math.Max(minimumManifestVersion, ManifestVersionUtility.GetManifestVersion(Metadata));
            var schemaNamespace = (XNamespace)ManifestSchemaUtility.GetSchemaNamespace(version);

            new XDocument(
                new XElement(schemaNamespace + "package",
                             Metadata.ToXElement(schemaNamespace))).Save(stream);
        }
Exemple #4
0
        private static void ValidateManifestSchema(XDocument document, string schemaNamespace)
        {
            XmlSchemaSet manifestSchemaSet = ManifestSchemaUtility.GetManifestSchemaSet(schemaNamespace);

            document.Validate(manifestSchemaSet, delegate(object sender, ValidationEventArgs e) {
                if (e.Severity == XmlSeverityType.Error)
                {
                    throw new InvalidOperationException(e.Message);
                }
            });
        }
Exemple #5
0
        public void Save(Stream stream, bool validate, int minimumManifestVersion)
        {
            if (validate)
            {
                Validate(this);
            }
            string schemaNamespace             = ManifestSchemaUtility.GetSchemaNamespace(Math.Max(minimumManifestVersion, ManifestVersionUtility.GetManifestVersion(this.Metadata)));
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();

            namespaces.Add("", schemaNamespace);
            new XmlSerializer(typeof(Manifest), schemaNamespace).Serialize(stream, this, namespaces);
        }
Exemple #6
0
        private static void ValidateManifestSchema(XDocument document, string schemaNamespace)
        {
            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);
                }
            });
        }
Exemple #7
0
        public void Save(Stream stream, bool validate, int minimumManifestVersion)
        {
            int    version         = Math.Max(minimumManifestVersion, ManifestVersionUtility.GetManifestVersion(Metadata));
            string schemaNamespace = ManifestSchemaUtility.GetSchemaNamespace(version);

            // Define the namespaces to use when serializing
            var ns = new XmlSerializerNamespaces();

            ns.Add("", schemaNamespace);

            // Need to force the namespace here again as the default in order to get the XML output clean
            var serializer = new XmlSerializer(typeof(Manifest), schemaNamespace);

            serializer.Serialize(stream, this, ns);
        }
Exemple #8
0
        public void Save(Stream stream, bool validate, int minimumManifestVersion)
        {
            int version         = Math.Max(minimumManifestVersion, ManifestVersionUtility.GetManifestVersion(Metadata));
            var schemaNamespace = (XNamespace)ManifestSchemaUtility.GetSchemaNamespace(version);

            new XDocument(
                new XElement(schemaNamespace + "package",
                             Metadata.ToXElement(schemaNamespace),
                             Files.Any() ?
                             new XElement(schemaNamespace + "files",
                                          Files.Select(file => new XElement(schemaNamespace + "file",
                                                                            new XAttribute("src", file.Source),
                                                                            new XAttribute("target", file.Target),
                                                                            new XAttribute("exclude", file.Exclude)))) : null)).Save(stream);
        }
Exemple #9
0
        public void Save(Stream stream, bool validate, int minimumManifestVersion)
        {
            int version         = Math.Max(minimumManifestVersion, ManifestVersionUtility.GetManifestVersion(Metadata));
            var schemaNamespace = (XNamespace)ManifestSchemaUtility.GetSchemaNamespace(version);

            var document = new XDocument(
                new XElement(schemaNamespace + "package",
                             Metadata.ToXElement(schemaNamespace)));

            var fileElement = Files.ToXElement(schemaNamespace);

            if (fileElement != null)
            {
                document.Root.Add(fileElement);
            }

            document.Save(stream);
        }
Exemple #10
0
        private static void CheckSchemaVersion(XDocument document)
        {
            XElement metadataElement = GetMetadataElement(document);

            if (metadataElement != null)
            {
                XAttribute attribute = metadataElement.Attribute("schemaVersion");
                if (attribute != null)
                {
                    attribute.Remove();
                }
                string packageId = GetPackageId(metadataElement);
                if (!ManifestSchemaUtility.IsKnownSchema(document.Root.Name.Namespace.NamespaceName))
                {
                    object[] args = new object[] { packageId, typeof(Manifest).Assembly.GetName().Version };
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, NuGetResources.IncompatibleSchema, args));
                }
            }
        }
Exemple #11
0
        private static void ValidateManifestSchema(XDocument document, string schemaNamespace)
        {
            CheckSchemaVersion(document);

            // Create the schema set
            var schemaSet = new XmlSchemaSet();

            using (TextReader reader = ManifestSchemaUtility.GetSchemaReader(schemaNamespace))
            {
                schemaSet.Add(schemaNamespace, XmlReader.Create(reader));
            }

            // Validate the document
            document.Validate(schemaSet, (sender, e) =>
            {
                if (e.Severity == XmlSeverityType.Error)
                {
                    // Throw an exception if there is a validation error
                    throw new InvalidOperationException(e.Message);
                }
            });
        }