Exemplo n.º 1
0
        private void AddAnomaly(ProcessStepCompletionArgs datas, string message, bool isError)
        {
            datas.Anomalies.Add(new ProcessAnomaly()
            {
                IsError = isError,
                Message = message
            });
            if (isError)
            {
                datas.IsSuccess = false;
            }

            OnProcessStepAnomaly(datas.Anomalies.Last());
        }
Exemplo n.º 2
0
        private bool ValidateXMLFormat()
        {
            var startArgs = new ProcessStepStartArgs()
            {
                StartTime = DateTime.Now,
                Libelle   = TemplateValidatorResources.XmlValidation_Title,
            };
            var completeArgs = new ProcessStepCompletionArgs(startArgs)
            {
                IsSuccess = true
            };


            _processCompletionArgs.StepProcess.Add(completeArgs);
            OnProcessStepStart(startArgs);


            XmlSchemaSet sc = new XmlSchemaSet();

            sc.Add(null, _processStartArgs.SchemaUri);
            XmlReaderSettings settings = new XmlReaderSettings
            {
                ValidationType = ValidationType.Schema,
                Schemas        = sc
            };


            settings.ValidationEventHandler += (s, e) => AddAnomaly(completeArgs, e.Message, true);

            using (XmlReader reader = XmlReader.Create(_processCompletionArgs.XmlDocumentInformation.FileCompletePath, settings))
            {
                while (reader.Read())
                {
                    ;
                }
            }
            completeArgs.CompletionTime = DateTime.Now;
            OnProcessStepCompletion(completeArgs);
            return(completeArgs.IsSuccess);
        }
Exemplo n.º 3
0
        private void CheckSharedFiles(XmlDocument doc, string rootPath, List <string> unusedFiles, ProcessStepCompletionArgs completeArgs)
        {
            string[] fileEntries;
            var      shareds = doc.DocumentElement.GetElementsByTagName("Shared");

            if (shareds.Count > 0)
            {
                // Les éléments partagés
                foreach (XmlNode t in shareds)
                {
                    var sharedKind = t.Attributes.GetNamedItem("kind").Value.ToLower();


                    if (t.ChildNodes.Cast <XmlNode>().Where(p => p.Name.Equals("Folder")).Count() == 1)
                    {
                        var folderNode = t.ChildNodes.Cast <XmlNode>().Where(p => p.Name.Equals("Folder")).FirstOrDefault();
                        var path       = folderNode.Attributes.GetNamedItem("path").Value;
                        var currDir    = Path.Combine(rootPath, path);
                        if (Directory.Exists(currDir))
                        {
                            fileEntries = Directory.GetFiles(currDir, "*", SearchOption.AllDirectories);
                        }
                        else
                        {
                            AddAnomaly(completeArgs, string.Format(TemplateValidatorResources.ContentValidation_DossierNonExistant, currDir), true);
                            continue;
                        }


                        for (int i = 0; i < fileEntries.Length; i++)
                        {
                            bool kindFound = false;
                            var  fileName  = GetFormatedName(fileEntries[i].ToLowerInvariant());

                            bool shouldNotUse = false;
                            switch (t.Attributes.GetNamedItem("kind").Value.ToLowerInvariant())
                            {
                            case "resources":
                            case "ressources":
                                kindFound = true;
                                break;

                            default:
                                if (!fileName.Contains(t.Attributes.GetNamedItem("kind").Value.ToLowerInvariant()))
                                {
                                    shouldNotUse = true;
                                }

                                break;
                            }

                            if (shouldNotUse)
                            {
                                continue;
                            }

                            switch (Path.GetExtension(fileName))
                            {
                            case ".html":
                                ReplaceRelativePath(fileEntries[i]);
                                if (sharedKind.Equals("html"))
                                {
                                    kindFound = true;
                                }
                                break;

                            case ".css":
                                ReplaceRelativePath(fileEntries[i]);
                                if (sharedKind.Equals("css"))
                                {
                                    kindFound = true;
                                }
                                break;

                            case ".js":
                                ReplaceRelativePath(fileEntries[i]);
                                if (sharedKind.Equals("js"))
                                {
                                    kindFound = true;
                                }
                                break;
                            }
                            if (kindFound)
                            {
                                unusedFiles.Remove(fileName);
                            }
                        }
                    }
                    else if (t.ChildNodes.Cast <XmlNode>().Where(p => p.Name.Equals("File")).Count() >= 1)
                    {
                        var fileNodes       = t.ChildNodes.Cast <XmlNode>().Where(p => p.Name.Equals("File")).ToList();
                        var dir             = Path.Combine(rootPath);
                        var filesDirFounded = false;

                        for (int i = 0; i < fileNodes.Count(); i++)
                        {
                            if (!filesDirFounded && File.Exists(Path.Combine(rootPath, fileNodes[i].Attributes.GetNamedItem("path").Value)))
                            {
                                fileEntries = Directory.GetFiles(rootPath, fileNodes[i].Attributes.GetNamedItem("path").Value);
                            }
                            else
                            {
                                AddAnomaly(completeArgs, string.Format(TemplateValidatorResources.ContentValidation_FichierNonExistant, fileNodes[i].Attributes.GetNamedItem("path").Value, sharedKind), true);
                                continue;
                            }

                            var  fileAttributeValue = fileNodes[i].Attributes.GetNamedItem("path").Value;
                            bool fileFound          = false;
                            for (int z = 0; z < fileEntries.Length; z++)
                            {
                                var fileName = GetFormatedName(fileEntries[z].ToLowerInvariant());

                                switch (t.Attributes.GetNamedItem("kind").Value.ToLowerInvariant())
                                {
                                case "resources":
                                case "ressources":
                                    fileFound = true;
                                    break;
                                }

                                switch (Path.GetExtension(fileName).ToLower())
                                {
                                case ".html":
                                    ReplaceRelativePath(fileEntries[z]);
                                    if (sharedKind.Equals("html"))
                                    {
                                        fileFound = true;
                                    }
                                    break;

                                case ".css":
                                    ReplaceRelativePath(fileEntries[z]);
                                    if (sharedKind.Equals("css"))
                                    {
                                        fileFound = true;
                                    }
                                    break;

                                case ".js":
                                    ReplaceRelativePath(fileEntries[z]);
                                    if (sharedKind.Equals("js"))
                                    {
                                        fileFound = true;
                                    }
                                    break;
                                }
                                if (fileFound)
                                {
                                    unusedFiles.Remove(fileName);
                                    break;
                                }
                            }
                            if (!fileFound)
                            {
                                AddAnomaly(completeArgs, string.Format(TemplateValidatorResources.ContentValidation_FichierNonExistant, fileNodes[i].Attributes.GetNamedItem("path").Value, sharedKind), true);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        private void CheckPublishSettings(XmlDocument doc, string rootPath, List <string> variationsCodes, List <string> unusedFiles, ProcessStepCompletionArgs completeArgs)
        {
            var publishSettings = doc.DocumentElement.GetElementsByTagName("PublishSettings");

            if (publishSettings.Count == 1)
            {
                // Les settings
                var settings = doc.DocumentElement.SelectNodes("//*[local-name()='Setting']");
                if (settings.Count > 0)
                {
                    foreach (XmlNode t in settings)
                    {
                        bool matchingVariableFound = false;
                        var  code = t.Attributes.GetNamedItem("code").Value;

                        for (int i = 0; i < _processCompletionArgs.XmlDocumentInformation.Variables.Count; i++)
                        {
                            var variable = _processCompletionArgs.XmlDocumentInformation.Variables[i];

                            if (code.Equals(variable.GetFormatedCode()))
                            {
                                var  value        = t.Attributes.GetNamedItem("value").Value;
                                bool isValueValid = true;
                                if (variable.Kind == VariableKind.Number)
                                {
                                    if (!int.TryParse(value, out int intR) && !float.TryParse(value, out float floatR))
                                    {
                                        isValueValid = false;
                                    }
                                }
                                else if (variable.Kind == VariableKind.InteractiveCatalogGuid)
                                {
                                    if (!Guid.TryParse(value, out Guid guidrR))
                                    {
                                        isValueValid = false;
                                    }
                                }
                                else if (variable.Kind == VariableKind.Date)
                                {
                                    if (!DateTime.TryParse(value, out DateTime datetimeR))
                                    {
                                        isValueValid = false;
                                    }
                                }

                                if (!isValueValid)
                                {
                                    AddAnomaly(completeArgs, string.Format(TemplateValidatorResources.ContentValidation_SettingTypeInvalide, value, variable.Kind), true);
                                }

                                matchingVariableFound = true;
                            }
                        }
                        if (!matchingVariableFound)
                        {
                            AddAnomaly(completeArgs, string.Format(TemplateValidatorResources.ContentValidation_SettingNoMatchWithVariable, code), true);
                        }
                    }
                }
                // variations
                var publishvariations = doc.DocumentElement.GetElementsByTagName("Variations");
                if (publishvariations.Count == 1)
                {
                    bool found            = false;
                    var  publishvariation = publishvariations[0];
                    foreach (XmlNode child in publishvariation.ChildNodes)
                    {
                        if (!child.Name.Equals("Variation"))
                        {
                            continue;
                        }
                        var code = child.Attributes.GetNamedItem("code").Value;

                        for (int i = 0; i < variationsCodes.Count; i++)
                        {
                            var variation = variationsCodes[i];

                            if (code.Equals(variation, StringComparison.InvariantCultureIgnoreCase))
                            {
                                found = true;
                                break;
                            }
                        }
                    }
                    if (!found)
                    {
                        AddAnomaly(completeArgs, TemplateValidatorResources.ContentValidation_DefaultVariationInvalide, true);
                    }
                }
                else
                {
                    AddAnomaly(completeArgs, TemplateValidatorResources.ContentValidation_DefaultVariationInvalide, true);
                }
            }
        }
Exemplo n.º 5
0
        private bool ValidateContent()
        {
            var startArgs = new ProcessStepStartArgs()
            {
                StartTime = DateTime.Now,
                Libelle   = TemplateValidatorResources.ContentValidation_Title,
            };
            var completeArgs = new ProcessStepCompletionArgs(startArgs)
            {
                IsSuccess = true
            };

            _processCompletionArgs.StepProcess.Add(completeArgs);

            OnProcessStepStart(startArgs);

            var rootPath = _processStartArgs.TemplateDirectoryPath;

            string[] fileEntries;
            //List<string> unusedFiles = Directory.GetFiles(rootPath, "*.*", SearchOption.AllDirectories).Where(x => x.Contains(temp_dir)).Select(x => x.ToLowerInvariant()).ToList();
            List <string> unusedFiles = Directory.GetFiles(rootPath, "*.*", SearchOption.AllDirectories).Select(x => x.ToLowerInvariant()).ToList();

            unusedFiles.Remove(_processCompletionArgs.XmlDocumentInformation.FileCompletePath.ToLowerInvariant()); // On remove le .xml racine.
            XmlDocument doc = _processCompletionArgs.XmlDocumentInformation.Document;

            var contentNode   = doc.DocumentElement.GetElementsByTagName("Content");
            var rootAttribute = contentNode[0].Attributes.GetNamedItem("root");

            if (!rootAttribute.Value.Equals("."))
            {
                rootPath = Path.Combine(_processStartArgs.TemplateDirectoryPath, rootAttribute.Value);
            }


            var           variationsElements = contentNode[0].ChildNodes;
            List <string> variationsCodes    = new List <string>();

            // Les variations et parts
            if (variationsElements.Count == 0)
            {
                AddAnomaly(completeArgs, TemplateValidatorResources.ContentValidation_AucuneVariation, true);
            }
            else
            {
                foreach (XmlNode n in variationsElements)
                {
                    if (!n.Name.Equals("Variation"))
                    {
                        continue;
                    }
                    var variationcode = n.Attributes.GetNamedItem("code").Value;
                    variationsCodes.Add(variationcode);

                    foreach (XmlNode t in n.ChildNodes)
                    {
                        var variationContentKind = t.Attributes.GetNamedItem("kind").Value;
                        if (!t.Name.Equals("VariationContent"))
                        {
                            continue;
                        }

                        if (t.ChildNodes.Cast <XmlNode>().Where(p => p.Name.Equals("Folder")).Count() == 1)
                        {
                            bool htmlFound  = false;
                            var  folderNode = t.ChildNodes.Cast <XmlNode>().Where(p => p.Name.Equals("Folder")).FirstOrDefault();
                            var  partPath   = folderNode.Attributes.GetNamedItem("path").Value;
                            var  currDir    = Path.Combine(rootPath, partPath);

                            if (Directory.Exists(currDir))
                            {
                                fileEntries = Directory.GetFiles(currDir, "*", SearchOption.AllDirectories);
                            }
                            else
                            {
                                AddAnomaly(completeArgs, string.Format(TemplateValidatorResources.ContentValidation_DossierNonExistant, currDir), true);
                                continue;
                            }

                            for (int i = 0; i < fileEntries.Length; i++)
                            {
                                var fileName = GetFormatedName(fileEntries[i].ToLowerInvariant());

                                bool shouldNotUse = false;
                                switch (t.Attributes.GetNamedItem("kind").Value.ToLowerInvariant())
                                {
                                case "resources":
                                case "ressources":
                                    break;

                                default:
                                    if (!fileName.Contains(t.Attributes.GetNamedItem("kind").Value.ToLowerInvariant()))
                                    {
                                        shouldNotUse = true;
                                    }

                                    break;
                                }

                                if (shouldNotUse)
                                {
                                    continue;
                                }

                                ReplaceRelativePath(fileEntries[i]);

                                unusedFiles.Remove(fileName);
                                switch (Path.GetExtension(fileName).ToLower())
                                {
                                case ".html":
                                    htmlFound = true;
                                    break;

                                case ".css":
                                    break;

                                case ".js":
                                    break;
                                }
                            }

                            if (!htmlFound)
                            {
                                AddAnomaly(completeArgs, string.Format(TemplateValidatorResources.ContentValidation_FichierHtmlNonExistant, variationContentKind), true);
                            }
                        }
                        else if (t.ChildNodes.Cast <XmlNode>().Where(p => p.Name.Equals("File")).Count() >= 1)
                        {
                            var fileNodes       = t.ChildNodes.Cast <XmlNode>().Where(p => p.Name.Equals("File")).ToList();
                            var dir             = Path.Combine(rootPath, variationcode);
                            var filesDirFounded = false;

                            for (int i = 0; i < fileNodes.Count(); i++)
                            {
                                if (!filesDirFounded && File.Exists(Path.Combine(rootPath, fileNodes[i].Attributes.GetNamedItem("path").Value)))
                                {
                                    fileEntries = Directory.GetFiles(rootPath, fileNodes[i].Attributes.GetNamedItem("path").Value);
                                }
                                else
                                {
                                    AddAnomaly(completeArgs, string.Format(TemplateValidatorResources.ContentValidation_FichierNonExistant, fileNodes[i].Attributes.GetNamedItem("path").Value, variationContentKind), true);
                                    continue;
                                }

                                var  fileAttributeValue = fileNodes[i].Attributes.GetNamedItem("path").Value;
                                bool fileFound          = false;
                                for (int z = 0; z < fileEntries.Length; z++)
                                {
                                    var fileName = GetFormatedName(fileEntries[z].ToLowerInvariant());

                                    ReplaceRelativePath(fileEntries[z]);

                                    switch (t.Attributes.GetNamedItem("kind").Value.ToLowerInvariant())
                                    {
                                    case "resources":
                                    case "ressources":
                                        fileFound = true;
                                        break;
                                    }

                                    unusedFiles.Remove(fileName);
                                    switch (Path.GetExtension(fileName).ToLower())
                                    {
                                    case ".html":
                                        fileFound = true;
                                        break;

                                    case ".css":
                                        fileFound = true;
                                        break;

                                    case ".js":
                                        fileFound = true;
                                        break;
                                    }
                                }
                                if (!fileFound)
                                {
                                    AddAnomaly(completeArgs, string.Format(TemplateValidatorResources.ContentValidation_FichierNonExistant, fileNodes[i].Attributes.GetNamedItem("path").Value, variationContentKind), true);
                                }
                            }
                        }
                    }
                }
            }



            CheckPublishSettings(doc, rootPath, variationsCodes, unusedFiles, completeArgs);
            CheckSharedFiles(doc, rootPath, unusedFiles, completeArgs);

            for (int i = 0; i < unusedFiles.Count; i++)
            {
                AddAnomaly(completeArgs, string.Format(TemplateValidatorResources.ContentValidation_FichierInutile, unusedFiles[i]), false);
            }


            _unusedFiles = unusedFiles.ToArray();
            completeArgs.CompletionTime = DateTime.Now;
            OnProcessStepCompletion(completeArgs);
            return(completeArgs.IsSuccess);
        }
Exemplo n.º 6
0
        private List <Variable> GetVariables(XmlDocument doc)
        {
            var startArgs = new ProcessStepStartArgs()
            {
                StartTime = DateTime.Now,
                Libelle   = TemplateValidatorResources.XmlRecuperation_Variables,
            };
            var completeArgs = new ProcessStepCompletionArgs(startArgs)
            {
                IsSuccess = true
            };

            OnProcessStepStart(startArgs);
            _processCompletionArgs.StepProcess.Add(completeArgs);


            // Récupération des variables
            List <Variable> variables = new List <Variable>();
            var             vars      = doc.DocumentElement.SelectNodes("//*[local-name()='Variable']");

            if (vars.Count > 0)
            {
                foreach (XmlNode variableNode in vars)
                {
                    var variableCode         = variableNode.Attributes.GetNamedItem("code").Value;
                    var variableLabel        = variableNode.Attributes.GetNamedItem("label").Value;
                    var variableKind         = variableNode.Attributes.GetNamedItem("kind").Value;
                    var variableDefaultValue = variableNode.Attributes.GetNamedItem("defaultValue");


                    if (!Enum.TryParse(variableKind, out VariableKind vk))
                    {
                        AddAnomaly(completeArgs, string.Format(TemplateValidatorResources.XmlRecuperation_TypeVariableInvalide, variableLabel), true);
                    }
                    Variable variable = new Variable(variableCode, variableLabel, vk, variableDefaultValue == null ? "" : variableDefaultValue.Value);


                    XmlNode currentNode = variableNode;
                    while (currentNode.ParentNode != null && !currentNode.Name.Equals("Settings"))
                    {
                        var parent = currentNode.ParentNode;
                        switch (parent.Name)
                        {
                        case "SettingsPage":
                            variable.SettingsPage = parent.Attributes.GetNamedItem("kind").Value;
                            break;

                        case "Group":
                            variable.Group = parent.Attributes.GetNamedItem("code").Value;
                            break;

                        case "Variation":
                            variable.Variation = parent.Attributes.GetNamedItem("code").Value;
                            break;
                        }
                        currentNode = currentNode.ParentNode;
                    }
                    if (!variables.Contains(variable))
                    {
                        variables.Add(variable);
                    }
                }
            }

            completeArgs.CompletionTime = DateTime.Now;
            OnProcessStepCompletion(completeArgs);
            return(variables);
        }
Exemplo n.º 7
0
 protected virtual void OnProcessStepCompletion(ProcessStepCompletionArgs e) => ProcessStepCompletion?.Invoke(this, e);
Exemplo n.º 8
0
        private XmlDocumentInformation GetXMLContent()
        {
            var startArgs = new ProcessStepStartArgs()
            {
                StartTime = DateTime.Now,
                Libelle   = TemplateValidatorResources.XmlRecuperation_Title,
            };
            var completeArgs = new ProcessStepCompletionArgs(startArgs)
            {
                IsSuccess = true
            };

            OnProcessStepStart(startArgs);
            _processCompletionArgs.StepProcess.Add(completeArgs);


            XmlDocumentInformation xdi = new XmlDocumentInformation();

            string[] rootFiles = Directory.GetFiles(_processStartArgs.TemplateDirectoryPath, "*.xml", SearchOption.TopDirectoryOnly);

            if (rootFiles.Length != 1)
            {
                if (rootFiles.Length > 1)
                {
                    AddAnomaly(completeArgs, TemplateValidatorResources.XmlValidation_TooManyXml, true);
                }
                else if (rootFiles.Length == 0)
                {
                    AddAnomaly(completeArgs, TemplateValidatorResources.XmlValidation_NoXml, true);
                }

                completeArgs.CompletionTime = DateTime.Now;
                OnProcessStepCompletion(completeArgs);
                return(xdi);
            }

            foreach (var r in rootFiles)
            {
                ReplaceEnvVar(r);
            }

            XmlDocument doc = new XmlDocument();

            foreach (string fileName in rootFiles)
            {
                var completePath = Path.Combine(_processStartArgs.TemplateDirectoryPath, Path.GetFileName(fileName));
                switch (Path.GetExtension(fileName).ToLowerInvariant())
                {
                case ".xml":
                    using (var entryStream = File.OpenRead(completePath))
                    {
                        try
                        {
                            doc.Load(entryStream);
                            xdi.FileCompletePath = completePath;
                            xdi.IsLoaded         = true;
                        }
                        catch (Exception e)
                        {
                            AddAnomaly(completeArgs, e.Message, true);
                            throw;
                        }
                    }
                    break;
                }
            }
            xdi.Document = doc;


            completeArgs.CompletionTime = DateTime.Now;
            OnProcessStepCompletion(completeArgs);
            return(xdi);
        }
Exemplo n.º 9
0
 public StepValidationResult(ProcessStepCompletionArgs psca)
 {
     Libelle   = psca.Libelle;
     IsSuccess = psca.IsSuccess;
     Anomalies = psca.Anomalies;
 }