Пример #1
0
        internal static void ValidateXslt(List <PackageFragmentValidationResult> validationResults, Func <XElement> elementProvider, string elementName, Func <XAttribute> xsltPathAttributeProvider, Func <string> xsltFilePathProvider, IZipFileSystem zipFileSystem, bool validateResultingConfigurationFile)
        {
            if (elementProvider() == null)
            {
                validationResults.AddFatal(
                    GetResourceString("ConfigurationTransformationPackageFragmentInstaller.MissingElement")
                    .FormatWith(elementName));
                return;
            }

            string xslFilePath = xsltFilePathProvider();

            if (xslFilePath == null)
            {
                validationResults.AddFatal(
                    GetResourceString("ConfigurationTransformationPackageFragmentInstaller.MissingAttribute")
                    .FormatWith(_xsltFilePathAttributeName), elementProvider());
                return;
            }

            if (zipFileSystem.ContainsFile(xslFilePath) == false)
            {
                validationResults.AddFatal(
                    GetResourceString("ConfigurationTransformationPackageFragmentInstaller.PathDoesNotExist")
                    .FormatWith(xslFilePath), xsltPathAttributeProvider());
                return;
            }

            if (!PathUtil.WritePermissionGranted(ConfigurationServices.FileConfigurationSourcePath))
            {
                validationResults.AddFatal(
                    GetResourceString("NotEnoughNtfsPermissions")
                    .FormatWith(ConfigurationServices.FileConfigurationSourcePath));
                return;
            }

            using (Stream xsltFileStream = zipFileSystem.GetFileStream(xslFilePath))
            {
                using (TextReader xsltTextReader = new C1StreamReader(xsltFileStream))
                {
                    XDocument xslt = null;

                    try
                    {
                        xslt = XDocument.Load(xsltTextReader);
                    }
                    catch (Exception ex)
                    {
                        validationResults.AddFatal(
                            GetResourceString("ConfigurationTransformationPackageFragmentInstaller.UnableToParsXslt")
                            .FormatWith(xslFilePath, ex.Message), xsltPathAttributeProvider());
                    }

                    if (xslt != null && validateResultingConfigurationFile)
                    {
                        try
                        {
                            ConfigurationServices.TransformConfiguration(xslt, true);
                        }
                        //catch (ConfigurationException ex)
                        //{
                        //    validationResults.AddFatal(
                        //        GetResourceString("ConfigurationTransformationPackageFragmentInstaller.XsltWillGeneratedInvalid")
                        //        .FormatWith(xsltFilePathProvider(), ex.Message), xsltPathAttributeProvider());
                        //}
                        catch (Exception ex)
                        {
                            validationResults.AddFatal(
                                GetResourceString("ConfigurationTransformationPackageFragmentInstaller.XsltWillGeneratedInvalid")
                                .FormatWith(xslFilePath, ex.Message), xsltPathAttributeProvider());
                        }
                    }
                }
            }
        }
        public static List <string> GetFiles(string attributeName, XElement configuration, IZipFileSystem zipFileSystem, out IList <PackageFragmentValidationResult> validationResult)
        {
            var files = new List <string>();

            validationResult = new List <PackageFragmentValidationResult>();

            var filesElements = configuration.Elements("Files").ToList();

            if (filesElements.Count() > 1)
            {
                validationResult.Add(
                    new PackageFragmentValidationResult(PackageFragmentValidationResultType.Fatal,
                                                        "You can only specify one Files element", configuration));
            }

            var filesElement = filesElements.SingleOrDefault();

            if (filesElement == null)
            {
                return(files);
            }

            var connectionStringNameAttr = filesElement.Attribute("connectionStringName");

            if (connectionStringNameAttr == null)
            {
                validationResult.Add(new PackageFragmentValidationResult(PackageFragmentValidationResultType.Fatal, "Missing connectionstring name attribute", filesElement));

                return(files);
            }

            var connectionStringSettings = ConfigurationManager.ConnectionStrings[connectionStringNameAttr.Value];

            if (connectionStringSettings == null)
            {
                validationResult.Add(new PackageFragmentValidationResult(PackageFragmentValidationResultType.Fatal, "C1 connectionstring missing"));

                return(files);
            }

            var elements = filesElement.Elements("File");

            foreach (var file in elements)
            {
                var pathAttribute = file.Attribute(attributeName);
                if (pathAttribute == null)
                {
                    validationResult.Add(new PackageFragmentValidationResult(PackageFragmentValidationResultType.Fatal, "Missing path attribute", file));

                    continue;
                }

                var path = pathAttribute.Value;
                if (!zipFileSystem.ContainsFile(path))
                {
                    validationResult.Add(new PackageFragmentValidationResult(PackageFragmentValidationResultType.Fatal, "File is missing", pathAttribute));

                    continue;
                }

                files.Add(path);
            }

            return(files);
        }
        internal static void ValidateXslt(List<PackageFragmentValidationResult> validationResults, Func<XElement> elementProvider, string elementName, Func<XAttribute> xsltPathAttributeProvider, Func<string> xsltFilePathProvider, IZipFileSystem zipFileSystem, bool validateResultingConfigurationFile)
        {
            if (elementProvider() == null)
            {
                validationResults.AddFatal(
                    GetResourceString("ConfigurationTransformationPackageFragmentInstaller.MissingElement")
                    .FormatWith(elementName));
                return;
            }

            string xslFilePath = xsltFilePathProvider();

            if (xslFilePath == null)
            {
                validationResults.AddFatal(
                    GetResourceString("ConfigurationTransformationPackageFragmentInstaller.MissingAttribute")
                    .FormatWith(_xsltFilePathAttributeName), elementProvider());
                return;
            }

            if (zipFileSystem.ContainsFile(xslFilePath) == false)
            {
                validationResults.AddFatal(
                    GetResourceString("ConfigurationTransformationPackageFragmentInstaller.PathDoesNotExist")
                    .FormatWith(xslFilePath), xsltPathAttributeProvider());
                return;
            }

            if(!PathUtil.WritePermissionGranted(ConfigurationServices.FileConfigurationSourcePath))
            {
                validationResults.AddFatal(
                    GetResourceString("NotEnoughNtfsPermissions")
                    .FormatWith(ConfigurationServices.FileConfigurationSourcePath));
                return;
            }

            using (Stream xsltFileStream = zipFileSystem.GetFileStream(xslFilePath))
            {
                using (TextReader xsltTextReader = new C1StreamReader(xsltFileStream))
                {
                    XDocument xslt = null;

                    try
                    {
                        xslt = XDocument.Load(xsltTextReader);
                    }
                    catch (Exception ex)
                    {
                        validationResults.AddFatal(
                            GetResourceString("ConfigurationTransformationPackageFragmentInstaller.UnableToParsXslt")
                            .FormatWith(xslFilePath, ex.Message), xsltPathAttributeProvider());
                    }

                    if (xslt != null && validateResultingConfigurationFile)
                    {
                        try
                        {
                            ConfigurationServices.TransformConfiguration(xslt, true);
                        }
                        //catch (ConfigurationException ex)
                        //{
                        //    validationResults.AddFatal(
                        //        GetResourceString("ConfigurationTransformationPackageFragmentInstaller.XsltWillGeneratedInvalid")
                        //        .FormatWith(xsltFilePathProvider(), ex.Message), xsltPathAttributeProvider());
                        //}
                        catch (Exception ex)
                        {
                            validationResults.AddFatal(
                                GetResourceString("ConfigurationTransformationPackageFragmentInstaller.XsltWillGeneratedInvalid")
                                .FormatWith(xslFilePath, ex.Message), xsltPathAttributeProvider());
                        }
                    }
                }
            }
        }