/// <summary>
        /// Preprocess document for compilation.
        /// </summary>
        /// <param name="req">compile request</param>
        /// <param name="kentity">language for project</param>
        /// <param name="filePath">file path</param>
        /// <param name="document">document to compile</param>
        /// <param name="nfUXErrors">list of errors</param>
        /// <param name="rootUrl">root url</param>
        public static void PreprocessDocument(CompileResourceRequest req, KEntity kentity, string filePath, out HtmlDocument document, List <CompilerError> nfUXErrors, out string rootUrl)
        {
            document = GetDocumentObject();
            try
            {
                document.LoadHtml(req.FileContent);
            }
            catch { }

            rootUrl = "";

            /*
             * if (document.ParseErrors.Count() > 0)
             * {
             *  foreach (var error in document.ParseErrors.ToList())
             *  {
             *      nfUXErrors.Add(CompileResultHelper.GetCompileError(error.Reason, error.Line, error.LinePosition));
             *  }
             * }
             */
            var descendants = document.DocumentNode.Descendants();

            PrepCommentTagsForCompile(ref document, ref descendants);
            SetPageProperties(req, kentity, filePath, ref rootUrl, descendants);
        }
        /// <summary>
        /// Set properties like pageType, IsStatic, rootUrl, URLPattern.
        /// </summary>
        /// <param name="req">compile request</param>
        /// <param name="kentity">k language for project</param>
        /// <param name="filePath">file path</param>
        /// <param name="rootUrl">root url for project</param>
        /// <param name="descendants">descendants of the document to compile</param>
        private static void SetPageProperties(CompileResourceRequest req, KEntity kentity, string filePath, ref string rootUrl, IEnumerable <HtmlNode> descendants)
        {
            //If the page is set to static, don't validate against language
            if (descendants.Any(x => x.Attributes[LanguageAttributes.KPartial.GetDescription()] != null))
            {
                req.PageType = Models.Project.KitsunePageType.PARTIAL;
            }
            //If the page is partial then by default its dynamic
            if (req.PageType == Models.Project.KitsunePageType.PARTIAL)
            {
                req.IsStatic = false;
            }
            else
            {
                req.IsStatic = IsResourceStatic(req.FileContent, req.SourcePath);
            }

            if (!req.IsStatic)
            {
                //Generate valid class name from the page name
                req.ClassName = GenerateClassName(filePath);
                rootUrl       = string.Format("{0}.rootaliasurl.url", kentity?.EntityName?.ToLower() ?? Constants.KPayDefaultSchema.ToLower());
                if (string.IsNullOrEmpty(req.UrlPattern))
                {
                    req.UrlPattern = string.Format("[[{0}]]/{1}", rootUrl, filePath.ToLower());
                }
            }
        }
        public IActionResult UploadTemplate(string projectid, [FromBody] CompileResourceRequest req)
        {
            try
            {
                if (req == null)
                {
                    return(BadRequest("Request object can not be null"));
                }

                req.ProjectId = projectid;

                var projectDetailsRequestModel = new GetProjectDetailsRequestModel
                {
                    ProjectId        = req.ProjectId,
                    ExcludeResources = false,
                    UserEmail        = req.UserEmail
                };

                var compileResult = CompilerHelper.CompileProjectResource(req);

                var compilerService   = new CompilerService();
                var updatePageRequest = new CreateOrUpdateResourceRequestModel
                {
                    Errors        = null,
                    FileContent   = req.FileContent,
                    SourcePath    = req.SourcePath.Trim(),
                    ClassName     = req.ClassName,
                    ProjectId     = req.ProjectId,
                    UserEmail     = req.UserEmail,
                    UrlPattern    = req.UrlPattern,
                    IsStatic      = req.IsStatic,
                    IsDefault     = req.IsDefault,
                    PageType      = req.PageType,
                    KObject       = req.KObject,
                    ResourceType  = null,
                    Configuration = !string.IsNullOrEmpty(req.Configuration) ? JsonConvert.DeserializeObject <Dictionary <string, object> >(req.Configuration) : null,
                };

                var validationResult = updatePageRequest.Validate();
                if (validationResult.Any())
                {
                    return(BadRequest(validationResult));
                }

                if (MongoConnector.CreateOrUpdateResource(updatePageRequest))
                {
                    return(Ok(compileResult));
                }

                return(BadRequest());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
        public override void ProcessNode(CompileResourceRequest request, List <CompilerError> compileErrors, Dictionary <string, int> customVariables, string rootUrl, string filePath, Dictionary <string, string> classNameAlias, Dictionary <int, string> classNameAliasdepth, int depth, HtmlNode node, HtmlAttribute dynamicAttribute, List <MatchNode> objectNamesToValidate, DocumentValidator documentValidator)
        {
            List <string> objects = Parser.GetObjects(dynamicAttribute.Value);

            foreach (string obj in objects)
            {
                objectNamesToValidate.Add(new MatchNode {
                    Value = obj.Replace("[[", "").Replace("]]", "").ToLower(), Line = dynamicAttribute.Line, Column = dynamicAttribute.LinePosition
                });
            }
            dynamicAttribute.Value = "";
        }
Exemple #5
0
        private static void UpdateForPublish(ref HtmlDocument document,
                                             CompileResourceRequest request,
                                             KEntity kentity,
                                             List <CompilerError> compileErrors,
                                             Dictionary <string, int> customVariables,
                                             GetProjectDetailsResponseModel projectDetails,
                                             GetPartialPagesDetailsResponseModel partialPages = null,
                                             List <KLanguageModel> componentEntities          = null,
                                             bool isCSRFPresent = false
                                             )
        {
            var htmlString = document.DocumentNode.OuterHtml.ToString();

            htmlString = htmlString.Replace("itemscope=\"\"", "itemscope");
            if (!string.IsNullOrEmpty(htmlString))
            {
                if (isCSRFPresent)
                {
                    htmlString = Constants.CSRFRegularExpression.Replace(htmlString, Constants.CSRFReplacementToken);
                }

                var lines = htmlString.Split('\n');

                var descendants = document.DocumentNode.Descendants();
                var kdlOb       = descendants.Where(s => s.Attributes[LanguageAttributes.KDL.GetDescription()] != null);
                //store temporary original KDL value before replacement of _system object
                var tempKDL = string.Empty;
                if (kdlOb != null && kdlOb.Any())
                {
                    tempKDL = kdlOb.FirstOrDefault().GetAttributeValue(LanguageAttributes.KDL.GetDescription(), null);
                }

                for (int i = 0; i < lines.Length; i++)
                {
                    ReplaceCustomVariables(customVariables, lines, i);
                    InjectPartialView(request, kentity, projectDetails, partialPages, lines, i);
                    ReplaceComponentEntities(projectDetails, componentEntities, lines, i);
                    SetObject(compileErrors, kentity, projectDetails, lines, i, request.ClassName);
                }
                htmlString = string.Join("\n", lines);
                document   = GetDocumentObject();
                document.LoadHtml(htmlString);

                //restore back the original k-dl attribute
                if (!string.IsNullOrEmpty(tempKDL))
                {
                    document.DocumentNode.Descendants().First(x => x.Attributes["k-dl"] != null).Attributes["k-dl"].Value = tempKDL;
                }
                htmlString = document.DocumentNode.OuterHtml;
            }
        }
Exemple #6
0
 /// <summary>
 /// Process nodes with k-object tags.
 /// </summary>
 /// <param name="request">Incoming request</param>
 /// <param name="compileErrors">list of errors during compilation</param>
 /// <param name="kentity">project language</param>
 /// <param name="customVariables">output list to hold custom variables</param>
 /// <param name="rootUrl">root url</param>
 /// <param name="filePath">file path</param>
 /// <param name="classNameAlias">aliases used either with k-repeat or k-object</param>
 /// <param name="classNameAliasdepth">depth based storage of aliases</param>
 /// <param name="depth">depth of the node in DOM</param>
 /// <param name="dynamicAttribute">key-value pair of the k-tag</param>
 public override void ProcessNode(CompileResourceRequest request, List <CompilerError> compileErrors, Dictionary <string, int> customVariables, string rootUrl, string filePath, Dictionary <string, string> classNameAlias, Dictionary <int, string> classNameAliasdepth, int depth, HtmlNode node, HtmlAttribute dynamicAttribute, List <MatchNode> objectNamesToValidate, DocumentValidator documentValidator)
 {
     if (Kitsune.Helper.Constants.WidgetRegulerExpression.IsMatch(dynamicAttribute.Value))
     {
         var    property = Kitsune.Helper.Constants.WidgetRegulerExpression.Match(dynamicAttribute.Value).Groups[1].Value;
         KClass kClassOb;
         if (!IsSearchableProperty(property, out kClassOb, documentValidator))
         {
             compileErrors.Add(new CompilerError {
                 Message = "Invalid search property", LineNumber = node.Line, LinePosition = node.LinePosition
             });
         }
         request.PageType = Models.Project.KitsunePageType.SEARCH;
     }
 }
        public IActionResult UpdatePageDetails(string projectid, [FromBody] CompileResourceRequest req)
        {
            try
            {
                if (req == null)
                {
                    return(BadRequest("Request object can not be null"));
                }

                req.ProjectId = projectid;
                req.IsPublish = false;

                var compileResult = CompilerHelper.CompileProjectResource(req);

                var compilerService   = new CompilerService();
                var updatePageRequest = new CreateOrUpdateResourceRequestModel
                {
                    Errors       = null,
                    FileContent  = req.FileContent,
                    SourcePath   = req.SourcePath.Trim(),
                    ClassName    = req.ClassName,
                    ProjectId    = req.ProjectId,
                    UserEmail    = req.UserEmail,
                    UrlPattern   = req.UrlPattern,
                    IsStatic     = req.IsStatic,
                    IsDefault    = req.IsDefault,
                    PageType     = req.PageType,
                    KObject      = req.KObject,
                    ResourceType = null
                };

                var validationResult = updatePageRequest.Validate();
                if (validationResult.Any())
                {
                    return(BadRequest(validationResult));
                }
                if (MongoConnector.CreateOrUpdateResource(updatePageRequest))
                {
                    return(Ok(compileResult));
                }
                return(BadRequest());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
Exemple #8
0
        private static void HandleScriptInjection(HtmlDocument document,
                                                  CompileResourceRequest request,
                                                  KEntity kentity,
                                                  string rootUrl,
                                                  GetProjectDetailsResponseModel projectDetails, bool isCSRF)
        {
            HtmlNode body        = document.DocumentNode.SelectSingleNode("//body");
            var      descendants = document.DocumentNode.Descendants();
            var      kPayObject  = descendants.Where(s => s.Attributes[LanguageAttributes.KPayAmount.GetDescription()] != null);


            if (body != null)
            {
                if (kPayObject != null && kPayObject.Any())
                {
                    HtmlNode kPayScript = document.CreateElement("script");
                    kPayScript.SetAttributeValue("src", Constants.KPayJsLink);
                    kPayScript.SetAttributeValue("type", "text/javascript");
                    kPayScript.SetAttributeValue("charset", "utf-8");
                    kPayScript.SetAttributeValue("defer", "defer");
                    kPayScript.SetAttributeValue("async", "async");
                    body.AppendChild(kPayScript);
                }

                if (isCSRF)
                {
                    HtmlNode csrfScript = document.CreateElement("script");
                    csrfScript.SetAttributeValue("src", Constants.CSRFTokenJSLink);
                    csrfScript.SetAttributeValue("type", "text/javascript");
                    csrfScript.SetAttributeValue("charset", "utf-8");
                    csrfScript.SetAttributeValue("defer", "defer");
                    csrfScript.SetAttributeValue("async", "async");
                    body.AppendChild(csrfScript);
                }

                HtmlNode ThemeIdWidget = HtmlNode.CreateNode(string.Format("<span style='display: none;' id='GET-VALUE(THEME-ID)'>{0}</span>", request.ProjectId));
                HtmlNode WebsiteId     = HtmlNode.CreateNode("<span style='display: none;' id='GET-VALUE(WEBSITE-ID)'>[KITSUNE_WEBSITE_ID]</span>");
                body.AppendChild(ThemeIdWidget);
                body.AppendChild(WebsiteId);

                if (kentity.EntityName != Constants.KPayDefaultSchema)
                {
                    HtmlNode RootAlias = HtmlNode.CreateNode(string.Format("<a style='display: none;' href='[[{0}]]' id='GET-URL(HOME)'></a>", rootUrl));
                    body.AppendChild(RootAlias);
                }
            }
        }
Exemple #9
0
        private static void InjectPartialView(CompileResourceRequest request, KEntity kentity, GetProjectDetailsResponseModel projectDetails, GetPartialPagesDetailsResponseModel partialPages, string[] lines, int i)
        {
            var partialregex  = Constants.PartialPageRegularExpression;
            var partialresult = partialregex.Matches(lines[i]);

            if (partialPages != null && partialPages.Resources != null && partialPages.Resources.Any())
            {
                for (int mc = 0; mc < partialresult.Count; mc++)
                {
                    if (partialresult[mc].Groups.Count > 1)
                    {
                        var groupparam = partialresult[mc].Groups[1].Value?.Trim('(', ')').Trim('\'').ToLower();
                        if (!string.IsNullOrEmpty(groupparam))
                        {
                            var partialViewParams = groupparam.Split(',');
                            var viewName          = partialViewParams[0]?.Trim('\'');

                            if (projectDetails.Resources.Any(x => x.PageType == Models.Project.KitsunePageType.PARTIAL && x.SourcePath.ToLower() == viewName.ToLower()))
                            {
                                var htmlData = new HtmlDocument();
                                htmlData.LoadHtml(partialPages.Resources.FirstOrDefault(x => string.Compare(x.SourcePath, viewName, true) == 0).HtmlSourceString);
                                //Support partial page with Html body and without body tag
                                string partialbody       = htmlData.DocumentNode.SelectSingleNode("/html/body")?.InnerHtml ?? htmlData.DocumentNode.OuterHtml;
                                var    partialCompiledOb = KitsuneCompiler.CompileHTML(new CompileResourceRequest
                                {
                                    FileContent = partialbody,
                                    IsPublish   = true,
                                    SourcePath  = viewName,
                                    PageType    = Models.Project.KitsunePageType.PARTIAL,
                                    ProjectId   = request.ProjectId,
                                    UserEmail   = request.UserEmail
                                }, out htmlData, projectDetails, kentity, partialPages);
                                if (partialCompiledOb != null && partialCompiledOb.Success)
                                {
                                    lines[i] = lines[i].Replace(partialresult[mc].Value, partialCompiledOb.CompiledString);
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Process nodes with k-script tags.
        /// </summary>
        /// <param name="request">Incoming request</param>
        /// <param name="compileErrors">list of errors during compilation</param>
        /// <param name="kentity">project language</param>
        /// <param name="customVariables">output list to hold custom variables</param>
        /// <param name="rootUrl">root url</param>
        /// <param name="filePath">file path</param>
        /// <param name="classNameAlias">aliases used either with k-repeat or k-object</param>
        /// <param name="classNameAliasdepth">depth based storage of aliases</param>
        /// <param name="depth">depth of the node in DOM</param>
        /// <param name="dynamicAttribute">key-value pair of the k-tag</param>
        public override void ProcessNode(CompileResourceRequest request, List <CompilerError> compileErrors, Dictionary <string, int> customVariables, string rootUrl, string filePath, Dictionary <string, string> classNameAlias, Dictionary <int, string> classNameAliasdepth, int depth, HtmlNode node, HtmlAttribute dynamicAttribute, List <MatchNode> objectNamesToValidate, DocumentValidator documentValidator)
        {
            Regex  urlPattern = new Regex("http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
            string getApiURL  = node.Attributes["get-api"]?.Value;
            string postApiURL = node.Attributes["post-api"]?.Value;

            if (getApiURL == null && postApiURL == null)
            {
                compileErrors.Add(CompileResultHelper.GetCompileError(ErrorCodeConstants.KScriptNoApi, node.Line, node.LinePosition));
            }
            else if (getApiURL != null && postApiURL != null)
            {
                compileErrors.Add(CompileResultHelper.GetCompileError(ErrorCodeConstants.KScriptMultipleApi, node.Line, node.LinePosition));
            }
            else if (getApiURL != null && !urlPattern.IsMatch(getApiURL))
            {
                compileErrors.Add(CompileResultHelper.GetCompileError(ErrorCodeConstants.KScriptGetApiNoURL, node.Line, node.LinePosition));
            }
            else if (postApiURL != null && !urlPattern.IsMatch(postApiURL))
            {
                compileErrors.Add(CompileResultHelper.GetCompileError(ErrorCodeConstants.KScriptPostApiNoURL, node.Line, node.LinePosition));
            }
            else if (classNameAlias.ContainsKey("kresult"))
            {
                compileErrors.Add(CompileResultHelper.GetCompileError(ErrorCodeConstants.KScriptNested, node.Line, node.LinePosition));
            }
            else
            {
                classNameAlias.Add("kresult", "-1");

                if (classNameAliasdepth.ContainsKey(depth))
                {
                    string oldAliasDepth = classNameAliasdepth[depth];
                    classNameAliasdepth.Remove(depth);
                    classNameAliasdepth.Add(depth, classNameAliasdepth[depth] + ",kresult");
                }
                else
                {
                    classNameAliasdepth.Add(depth, "kresult");
                }
            }
        }
Exemple #11
0
        /// <summary>
        /// Update href, src etc paths for preview
        /// </summary>
        /// <param name="req">compiler request</param>
        /// <param name="document">document to update</param>
        private static void UpdatePathsForPreview(CompileResourceRequest req, HtmlDocument document)
        {
            if (req.IsPreview)
            {
                //Replace all the relative path with absolute path
                // TODO : Remove once identifier handle the relative path
                foreach (var elem in document.DocumentNode.Descendants().Where(x => x.Attributes["href"] != null || x.Attributes["src"] != null))
                {
                    var hrefAttr = elem.Attributes["href"];

                    if (hrefAttr != null && !string.IsNullOrEmpty(hrefAttr.Value) && !hrefAttr.Value.StartsWith("[[") && !hrefAttr.Value.ToLower().Trim().StartsWith("#") && !hrefAttr.Value.ToLower().Trim('/').StartsWith("http") && !hrefAttr.Value.ToLower().Trim('/').StartsWith("mailto:") && !hrefAttr.Value.ToLower().Trim('/').StartsWith("tel:") && !((hrefAttr.Value.ToLower().Trim('/').IndexOf(".html") > 0) || (hrefAttr.Value.ToLower().Trim('/').IndexOf(".htm") > 0) || (hrefAttr.Value.ToLower().Trim('/').IndexOf(".html.dl") > 0) || (hrefAttr.Value.ToLower().Trim('/').IndexOf(".php") > 0)))
                    {
                        hrefAttr.Value = string.Format(Kitsune.Helper.Constants.AssetsBasePath, req.ProjectId, hrefAttr.Value.Trim('.').Trim('/'));
                    }
                    var srcAttr = elem.Attributes["src"];
                    if (srcAttr != null && !string.IsNullOrEmpty(srcAttr.Value) && !srcAttr.Value.StartsWith("[[") && !srcAttr.Value.ToLower().Trim('/').StartsWith("http") && !((srcAttr.Value.ToLower().Trim('/').IndexOf(".html") > 0) || (srcAttr.Value.ToLower().Trim('/').IndexOf(".htm") > 0)))
                    {
                        srcAttr.Value = string.Format(Kitsune.Helper.Constants.AssetsBasePath, req.ProjectId, srcAttr.Value.Trim('.').Trim('/'));
                    }
                }
            }
        }
Exemple #12
0
        public override void ProcessNode(CompileResourceRequest request, List <CompilerError> compileErrors, Dictionary <string, int> customVariables, string rootUrl, string filePath, Dictionary <string, string> classNameAlias, Dictionary <int, string> classNameAliasdepth, int depth, HtmlNode node, HtmlAttribute dynamicAttribute, List <MatchNode> objectNamesToValidate, DocumentValidator documentValidator)
        {
            Node result = LexerGenerator.Parse(dynamicAttribute.Value.Trim('[', ']'));

            if (result?.Token?.Value == ACTIONS.Loop)
            {
                string referenceObject = result.Children[0].Children[0].Token.Value;
                string iterator        = result.Children[2].Children[0].Token.Value;
                //Fix issue with same iterator being used at same depth.
                if (classNameAlias.ContainsKey(iterator) && classNameAliasdepth.ContainsKey(depth) && classNameAliasdepth[depth].Contains(iterator))
                {
                    classNameAlias.Remove(iterator);
                    string oldAliasDepth = classNameAliasdepth[depth];
                    classNameAliasdepth.Remove(depth);
                    oldAliasDepth = oldAliasDepth.Replace(iterator, "").Replace(",,", ",");
                    classNameAliasdepth.Add(depth, oldAliasDepth);
                }
                if (!classNameAlias.ContainsKey(iterator))
                {
                    classNameAlias.Add(iterator, "");
                    if (classNameAliasdepth.ContainsKey(depth))
                    {
                        string oldAliasDepth = classNameAliasdepth[depth];
                        classNameAliasdepth.Remove(depth);
                        classNameAliasdepth.Add(depth, oldAliasDepth + ',' + iterator);
                    }
                    else
                    {
                        classNameAliasdepth.Add(depth, iterator);
                    }
                    objectNamesToValidate.Add(new MatchNode {
                        Value = referenceObject, Line = dynamicAttribute.Line, Column = dynamicAttribute.LinePosition
                    });
                    try
                    {
                        if (result?.Children[4]?.Token?.Value?.ToString() == "ViewProperty" && result?.Children[4]?.Children[1]?.Token?.Value?.ToLower() == "offset")
                        {
                            //Added list type in condition as there might update in the offset and reference object so we have to update the meta also
                            if (request.PageType == Models.Project.KitsunePageType.LIST || request.PageType == Models.Project.KitsunePageType.DEFAULT || request.PageType == Models.Project.KitsunePageType.DETAILS)
                            {
                                request.PageType = Models.Project.KitsunePageType.LIST;
                                request.Offset   = result.Children[6]?.Children[0]?.Token?.Value;
                                request.KObject  = referenceObject;
                            }
                        }
                    }
                    catch { }

                    string[] classHierarchyList = referenceObject.Split('.');
                    if (classHierarchyList[0].StartsWith("kresult", System.StringComparison.InvariantCultureIgnoreCase) || classHierarchyList[0].Equals("search", System.StringComparison.InvariantCultureIgnoreCase))
                    {
                        //pass
                    }
                    else if (classHierarchyList.Length >= 2)
                    {
                        KProperty kProperty = GetProperty(compileErrors, dynamicAttribute, documentValidator, referenceObject, classNameAlias);
                        if (kProperty == null)
                        {
                            compileErrors.Add(CompileResultHelper.GetCompileError(ErrorCodeConstants.KRepeatVariableNotArray, dynamicAttribute.Line, dynamicAttribute.LinePosition));
                        }
                        if (iterator.Split('.').Length > 1)
                        {
                            compileErrors.Add(CompileResultHelper.GetCompileError(ErrorCodeConstants.KRepeatInvalidIterator, dynamicAttribute.Line, dynamicAttribute.LinePosition));
                        }

                        KEntity entity = documentValidator?.GetKEntityFromEntityName(classHierarchyList[0]) ?? documentValidator?.GetKEntityFromEntityName(documentValidator.defaultEntity);
                        if (entity?.Classes?.Where(x => x.ClassType == KClassType.BaseClass && x.Name.ToLower() == iterator.ToLower()).FirstOrDefault() != null)
                        {
                            compileErrors.Add(CompileResultHelper.GetCompileError(ErrorCodeConstants.BaseClassAsKRepeatIterator, dynamicAttribute.Line, dynamicAttribute.LinePosition));
                        }
                    }
                }
                else
                {
                    compileErrors.Add(CompileResultHelper.GetCompileError(ErrorCodeConstants.KRepeatVariableAlreadyInUse, dynamicAttribute.Line, dynamicAttribute.LinePosition));
                }
            }
            else if (result?.Token?.Value == ACTIONS.InLoop)
            {
                string loopVariable    = result.Children[0].Children[0].Token.Value.ToLower();
                string referenceObject = result.Children[2].Children[0].Token.Value.ToLower() + $"[{GenerateVariableName(5)}]";
                objectNamesToValidate.Add(new MatchNode {
                    Value = referenceObject, Line = dynamicAttribute.Line, Column = dynamicAttribute.LinePosition
                });

                if (classNameAlias.ContainsKey(loopVariable))
                {
                    compileErrors.Add(CompileResultHelper.GetCompileError(ErrorCodeConstants.KRepeatVariableAlreadyInUse, dynamicAttribute.Line, dynamicAttribute.LinePosition));
                    return;
                }
                KEntity entity = documentValidator?.GetKEntityFromEntityName(loopVariable) ?? documentValidator?.GetKEntityFromEntityName(documentValidator.defaultEntity);
                if (entity.Classes.Where(x => x.ClassType == KClassType.BaseClass && x.Name.ToLower() == loopVariable).Any())
                {
                    compileErrors.Add(CompileResultHelper.GetCompileError(ErrorCodeConstants.BaseClassAsKRepeatVariable, dynamicAttribute.Line, dynamicAttribute.LinePosition));
                    return;
                }

                classNameAlias.Add(loopVariable, referenceObject);

                if (classNameAliasdepth.ContainsKey(depth))
                {
                    string oldAliasDepth = classNameAliasdepth[depth];
                    classNameAliasdepth.Remove(depth);
                    classNameAliasdepth.Add(depth, oldAliasDepth + ',' + loopVariable);
                }
                else
                {
                    classNameAliasdepth.Add(depth, loopVariable);
                }
            }
            else
            {
                compileErrors.Add(CompileResultHelper.GetCompileError(ErrorCodeConstants.InvalidKRepeatSyntax, dynamicAttribute.Line, dynamicAttribute.LinePosition));
            }
            dynamicAttribute.Value = "";
        }
Exemple #13
0
        public List <CompileResult> BuildProject(string userEmail, string projectId, bool _isDev, int currentCompilerVersion)
        {
            try
            {
                var response                = new List <CompileResult>();
                var api                     = new CompilerAPI();
                var oldPage                 = false;
                var changedKDLOrPartial     = false;
                var filesCompiledCount      = 0;
                var buildStatusUpdateResult = true;
                var dict                    = new Dictionary <string, int>();
                System.Diagnostics.Stopwatch compileStopWatch = new System.Diagnostics.Stopwatch();
                Log.Information("Compilation started");
                dict.Add("LINK", 0);
                //List<BuildError> BuildErrors;
                var userId         = new UserAPI().GetUserId(userEmail);
                var project        = api.GetProjectDetailsApi(userEmail, projectId);
                var forceBuild     = false;
                var skipValidation = false;


                if (project == null)
                {
                    response.Add(new CompileResult {
                        Success = false, ErrorMessages = new List <CompilerError>()
                        {
                            new CompilerError {
                                Message = $"Project with projectid \"{projectId}\" not found."
                            }
                        }
                    });
                    return(response);
                }
                else
                {
                    forceBuild = project.CompilerVersion < currentCompilerVersion;
                }

                if (project.Resources.Any(x => Kitsune.Helper.Constants.ConfigFileRegularExpression.IsMatch(x.SourcePath)))
                {
                    var settings = api.GetProjectResourceDetailsApi(userEmail, projectId, HttpUtility.UrlEncode("/kitsune-settings.json"));
                    if (!string.IsNullOrEmpty(settings.HtmlSourceString))
                    {
                        try
                        {
                            var settingsOb = JsonConvert.DeserializeObject <dynamic>(settings.HtmlSourceString);
                            if (settingsOb.GetType() == typeof(JObject))
                            {
                                var cleanBuild = ((JObject)settingsOb).SelectToken("build.cleanBuild");
                                if (cleanBuild != null && !string.IsNullOrEmpty((string)cleanBuild))
                                {
                                    if ("true" == ((string)cleanBuild).ToLower())
                                    {
                                        forceBuild = true;
                                    }
                                }
                                var syntaxValidation = ((JObject)settingsOb).SelectToken("build.syntax_validation");
                                if (syntaxValidation != null && !string.IsNullOrEmpty((string)syntaxValidation))
                                {
                                    if ("true" == ((string)syntaxValidation).ToLower())
                                    {
                                        skipValidation = false;
                                    }
                                    else
                                    {
                                        skipValidation = true;
                                    }
                                    Console.WriteLine($"skipValidation:{skipValidation}");
                                }
                            }
                        }
                        catch { }
                    }
                }

                project.UserEmail = userEmail;
                project.Resources = project.Resources.Where(x => x.IsStatic == false && x.ResourceType != Kitsune.Models.Project.ResourceType.APPLICATION).ToList();
                var staticFileCount = project.Resources.Count();
                dict.Add("TOTAL", staticFileCount);

                var languageEntity = new KEntity
                {
                    Classes = new List <KClass>()
                };
                if (!string.IsNullOrEmpty(project.SchemaId))
                {
                    languageEntity = new LanguageAPI().GetLanguage(project.SchemaId, userId);
                }
                languageEntity = new KLanguageBase().GetKitsuneLanguage(userEmail, projectId, project, languageEntity, userId);

                #region Handle Project Modules
                var componentEntities = UpdateModuleReference(project);
                #endregion

                var partialPages = api.GetPartialPagesDetailsApi(userEmail, projectId, null);
                GetAuditProjectAndResourcesDetailsResponseModel auditProject = new GetAuditProjectAndResourcesDetailsResponseModel();
                DateTime latestBuildDate = DateTime.MinValue;
                if (project.Version > 1 && !forceBuild)
                {
                    var latestProjectVersion = project.Version - 1;
                    auditProject = api.GetAuditProjectAndResourcesDetails(userEmail, projectId, latestProjectVersion);
                }
                var latestBuild = api.GetLastCompletedBuild(userEmail, projectId);
                if (latestBuild != null && latestBuild.CreatedOn != null)
                {
                    latestBuildDate = latestBuild.CreatedOn;
                }

                var ToBeUpdatedResources = new List <CreateOrUpdateResourceRequestModel>();
                var CompiledResources    = new Dictionary <string, CompileResult>();
                var UncompiledResources  = new List <CompileResourceRequest>();

                //put partial pages first
                var partialResources = project.Resources.FindAll(x => x.PageType == Kitsune.Models.Project.KitsunePageType.PARTIAL);
                if (partialResources.Any())
                {
                    foreach (var resource in partialResources)
                    {
                        project.Resources.Remove(resource);
                        project.Resources.Insert(0, resource);
                    }
                }
                foreach (var page in project.Resources)
                {
                    oldPage = false;
                    var resource = api.GetProjectResourceDetailsApi(userEmail, projectId, HttpUtility.UrlEncode(page.SourcePath));
                    // page.IsStatic = KitsuneCompiler.IsResourceStatic(resource.HtmlSourceString, page.SourcePath);
                    if (!page.IsStatic)
                    {
                        var compiled = false;
                        var document = new HtmlDocument();
                        Console.WriteLine("Page: " + page.SourcePath);
                        var req   = new CompileResourceRequest();
                        var outsd = new CompileResult();
                        if (project.Version > 1 && !forceBuild)
                        {
                            oldPage = auditProject.Resources.Any(x => x.SourcePath == page.SourcePath);
                        }
                        GetFileFromS3RequestModel resourceTreeRequest = new GetFileFromS3RequestModel
                        {
                            SourcePath = string.Format(CompilerConstants.TreeFileName, page.SourcePath),
                            ProjectId  = page.ProjectId,
                            BucketName = project.BucketNames.demo
                        };
                        var resourceTreePresent = api.GetFileFromS3(resourceTreeRequest);
                        if (!changedKDLOrPartial && project.Version > 1 && oldPage && page.UpdatedOn <= latestBuildDate && !string.IsNullOrEmpty(resourceTreePresent))
                        {
                            var samePage = auditProject.Resources.Find(x => x.SourcePath == page.SourcePath);
                            GetFileFromS3RequestModel FileRequest = new GetFileFromS3RequestModel
                            {
                                SourcePath = samePage.SourcePath,
                                ProjectId  = samePage.ProjectId,
                                BucketName = project.BucketNames.demo,
                                Compiled   = true,
                                Version    = samePage.Version
                            };
                            outsd = new CompileResult()
                            {
                                CompiledString = api.GetFileFromS3(FileRequest),
                                Success        = true
                            };
                            req.PageType   = samePage.PageType;
                            req.UrlPattern = samePage.UrlPattern;
                            req.SourcePath = samePage.SourcePath;
                            compiled       = false;
                        }
                        else
                        {
                            req = new CompileResourceRequest {
                                SourcePath = page.SourcePath, FileContent = resource.HtmlSourceString, UserEmail = project.UserEmail, ProjectId = projectId, IsDev = _isDev, IsPublish = true, IsStatic = page.IsStatic, IsDefault = page.IsDefault, ClassName = page.ClassName, UrlPattern = page.UrlPattern, KObject = page.KObject, PageType = page.PageType, ResourceType = page.ResourceType
                            };
                            //Compile config file

                            compileStopWatch.Reset();
                            compileStopWatch.Start();
                            if (skipValidation)
                            {
                                outsd = new CompileResult();
                                outsd.CompiledString = req.FileContent;
                                outsd.PageName       = req.SourcePath;
                                req.UrlPattern       = req.SourcePath;
                                page.UrlPattern      = req.SourcePath;
                                outsd.Success        = true;
                            }
                            else if (Kitsune.Helper.Constants.ConfigFileRegularExpression.IsMatch(req.SourcePath))
                            {
                                outsd = KitsuneCompiler.ValidateJsonConfig(new ValidateConfigRequestModel
                                {
                                    File = new ConfigFile {
                                        Content = req.FileContent
                                    },
                                    UserEmail = req.UserEmail
                                });
                                outsd.CompiledString = req.FileContent;
                                outsd.PageName       = req.SourcePath;
                                req.UrlPattern       = req.SourcePath;
                                page.UrlPattern      = req.SourcePath;
                            }
                            //Compile dynamic html file
                            else
                            {
                                Console.WriteLine($"Compiling : {req.SourcePath}");
                                outsd = KitsuneCompiler.CompileHTML(req, out document, project, languageEntity, partialPages, componentEntities: componentEntities);
                            }

                            if (!outsd.Success)
                            {
                                response.Add(outsd);
                            }
                            else
                            {
                                CompileLog(req.SourcePath, compileStopWatch.ElapsedMilliseconds.ToString());

                                // Console.WriteLine($"'{req.SourcePath}'-{compileStopWatch.ElapsedMilliseconds.ToString()}");
                                ResourceMetaInfo metaInfo = new ResourceMetaInfo();
                                try
                                {
                                    var tagsToIgnore = new List <string>();
                                    if (outsd.CustomVariables != null)
                                    {
                                        tagsToIgnore.AddRange(outsd.CustomVariables.Keys);
                                    }
                                    metaInfo           = ResourceMetaInfoGenerator.MetaInfo(req, outsd.CompiledString, project, languageEntity, tagsToIgnore);
                                    outsd.MetaClass    = metaInfo?.metaClass;
                                    outsd.MetaProperty = metaInfo?.Property;
                                }
                                catch (Exception ex)
                                {
                                    metaInfo = new ResourceMetaInfo
                                    {
                                        IsError = true,
                                        Message = string.Format("Excepiton occured while generating metainfo: {0}", ex.Message)
                                    };
                                }
                                CompiledResources.Add(string.Format("{0}:{1}", req.ProjectId, req.SourcePath), outsd);
                            }

                            if (!changedKDLOrPartial && oldPage)
                            {
                                var samePage = auditProject.Resources.Find(x => x.SourcePath == page.SourcePath);
                                if (samePage.UrlPattern != req.UrlPattern)
                                {
                                    changedKDLOrPartial = true;
                                }
                                if (samePage.PageType == Kitsune.Models.Project.KitsunePageType.PARTIAL)
                                {
                                    changedKDLOrPartial = true;
                                }
                            }
                            compiled = true;
                        }

                        //Replacing the aws path to the version controlled folder
                        //outsd.CompiledString = KitsuneCompiler.AddAuditAssetsPath(projectId, outsd.CompiledString, project.Version);


                        //Save compiled resource to kitsune resource collection
                        var updatePageRequest = new CreateOrUpdateResourceRequestModel
                        {
                            Errors          = null,
                            FileContent     = req.FileContent,
                            SourcePath      = req.SourcePath,
                            ClassName       = req.ClassName,
                            ProjectId       = req.ProjectId,
                            UserEmail       = req.UserEmail,
                            UrlPattern      = req.UrlPattern,
                            IsStatic        = req.IsStatic,
                            IsDefault       = req.IsDefault,
                            PageType        = req.PageType,
                            KObject         = req.KObject,
                            CustomVariables = outsd.CustomVariables,
                            Offset          = req.Offset
                        };



                        if (compiled)
                        {
                            ToBeUpdatedResources.Add(updatePageRequest);
                            filesCompiledCount += 1;

                            if (filesCompiledCount % 5 == 0)
                            {
                                try
                                {
                                    dict["LINK"]            = filesCompiledCount;
                                    buildStatusUpdateResult = api.UpdateBuildStatus(userEmail, new CreateOrUpdateKitsuneStatusRequestModel
                                    {
                                        ProjectId = projectId,
                                        Compiler  = dict,
                                        Stage     = BuildStatus.Compiled
                                                    //Error = BuildErrors
                                    });
                                }
                                catch (Exception ex)
                                {
                                }
                            }
                        }
                        else if (!compiled)
                        {
                            UncompiledResources.Add(new CompileResourceRequest
                            {
                                FileContent = resource.HtmlSourceString,
                                SourcePath  = page.SourcePath,
                                IsStatic    = page.IsStatic,
                                IsDefault   = page.IsDefault,
                                UrlPattern  = page.UrlPattern,
                                ClassName   = page.ClassName,
                            });
                        }
                    }
                }
                //Exception operations on uncompiled Pages
                if (!skipValidation && UncompiledResources.Any())
                {
                    if (changedKDLOrPartial)
                    {
                        foreach (var currpage in UncompiledResources)
                        {
                            var document = new HtmlDocument();
                            var req      = new CompileResourceRequest {
                                SourcePath = currpage.SourcePath, FileContent = currpage.FileContent, UserEmail = project.UserEmail, ProjectId = projectId, IsDev = _isDev, IsPublish = true, IsStatic = currpage.IsStatic, IsDefault = currpage.IsDefault, ClassName = currpage.ClassName, UrlPattern = currpage.UrlPattern
                            };

                            compileStopWatch.Reset();
                            compileStopWatch.Start();

                            Console.WriteLine($"Compiling UncompiledResources : {currpage.SourcePath}");

                            var outsd = KitsuneCompiler.CompileHTML(req, out document, project, languageEntity, partialPages, componentEntities: componentEntities);

                            if (!outsd.Success)
                            {
                                response.Add(outsd);
                            }
                            else
                            {
                                CompileLog(currpage.SourcePath, compileStopWatch.ElapsedMilliseconds.ToString());

                                ResourceMetaInfo metaInfo = new ResourceMetaInfo();
                                try
                                {
                                    var tagsToIgnore = new List <string>();
                                    if (outsd.CustomVariables != null)
                                    {
                                        tagsToIgnore.AddRange(outsd.CustomVariables.Keys);
                                    }
                                    metaInfo           = ResourceMetaInfoGenerator.MetaInfo(req, outsd.CompiledString, project, languageEntity, tagsToIgnore);
                                    outsd.MetaClass    = metaInfo?.metaClass;
                                    outsd.MetaProperty = metaInfo?.Property;
                                }
                                catch (Exception ex)
                                {
                                    metaInfo = new ResourceMetaInfo
                                    {
                                        IsError = true,
                                        Message = string.Format("Excepiton occured while generating metainfo: {0}", ex.Message)
                                    };
                                }
                                CompiledResources.Add(string.Format("{0}:{1}", req.ProjectId, req.SourcePath), outsd);
                            }
                            //outsd.CompiledString = KitsuneCompiler.AddAuditAssetsPath(projectId, outsd.CompiledString, project.Version);
                            ToBeUpdatedResources.Add(new CreateOrUpdateResourceRequestModel
                            {
                                Errors          = null,
                                FileContent     = req.FileContent,
                                SourcePath      = req.SourcePath,
                                ClassName       = req.ClassName,
                                ProjectId       = req.ProjectId,
                                UserEmail       = req.UserEmail,
                                UrlPattern      = req.UrlPattern,
                                IsStatic        = req.IsStatic,
                                IsDefault       = req.IsDefault,
                                PageType        = req.PageType,
                                KObject         = req.KObject,
                                Offset          = req.Offset,
                                CustomVariables = outsd.CustomVariables
                            });
                            filesCompiledCount += 1;
                            if (filesCompiledCount % 5 == 0)
                            {
                                try
                                {
                                    dict["LINK"]            = filesCompiledCount;
                                    buildStatusUpdateResult = api.UpdateBuildStatus(userEmail, new CreateOrUpdateKitsuneStatusRequestModel
                                    {
                                        ProjectId = projectId,
                                        Compiler  = dict,
                                        Stage     = BuildStatus.Compiled
                                                    //Error = BuildErrors
                                    });
                                }
                                catch (Exception ex)
                                {
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (var currpage in UncompiledResources)
                        {
                            //SubmittedResources.Add(new CreateOrUpdateResourceRequest
                            //{
                            //    Errors = null,
                            //    FileContent = currpage.HtmlSourceString,
                            //    SourcePath = currpage.SourcePath.Trim().ToUpper(),
                            //    ClassName = currpage.ClassName,
                            //    ProjectId = currpage.ProjectId,
                            //    UserEmail = currpage.UserEmail,
                            //    UrlPattern = currpage.UrlPattern,
                            //    IsStatic = currpage.IsStatic,
                            //    IsDefault = currpage.IsDefault,
                            //    PageType = currpage.PageType,
                            //    KObject = currpage.KObject
                            //});
                            filesCompiledCount += 1;
                            if (filesCompiledCount % 5 == 0)
                            {
                                try
                                {
                                    dict["LINK"]            = filesCompiledCount;
                                    buildStatusUpdateResult = api.UpdateBuildStatus(userEmail, new CreateOrUpdateKitsuneStatusRequestModel
                                    {
                                        ProjectId = projectId,
                                        Compiler  = dict,
                                        Stage     = BuildStatus.Compiled
                                                    //Error = BuildErrors
                                    });
                                }
                                catch (Exception ex)
                                {
                                }
                            }
                        }
                    }
                }

                try
                {
                    dict["LINK"]            = filesCompiledCount;
                    buildStatusUpdateResult = api.UpdateBuildStatus(userEmail, new CreateOrUpdateKitsuneStatusRequestModel
                    {
                        ProjectId = projectId,
                        Compiler  = dict,
                        Stage     = BuildStatus.Compiled
                                    //Error = BuildErrors
                    });
                }
                catch (Exception ex)
                {
                }
                if (!response.Any())
                {
                    List <UrlPatternRegexDetails> UrlPatternRegexList = new List <UrlPatternRegexDetails>();
                    foreach (var resource in project.Resources.Where(x => !string.IsNullOrEmpty(x.UrlPattern)))
                    {
                        var UrlPatternRegex = KitsuneCommonUtils.GenerateUrlPatternRegex(resource.UrlPattern.Substring(resource.UrlPattern.IndexOf("/") + 1), resource.SourcePath);
                        UrlPatternRegexList.Add(new UrlPatternRegexDetails
                        {
                            SourcePath      = resource.SourcePath,
                            UrlPattern      = resource.UrlPattern,
                            UrlPatternRegex = UrlPatternRegex
                        });
                    }
                    var UrlPatternRegexDistinct = UrlPatternRegexList.GroupBy(x => x.UrlPatternRegex).Select(x => new { Regex = x.Key, Count = x.Count() }).Where(x => x.Count > 1).ToList();
                    if (UrlPatternRegexDistinct != null && UrlPatternRegexDistinct.Any())
                    {
                        int regexMatchesCount = 1;
                        var message           = "Url pattern conflict : \n";
                        var pageName          = string.Empty;
                        foreach (var pattern in UrlPatternRegexDistinct)
                        {
                            var result = UrlPatternRegexList.FindAll(x => x.UrlPatternRegex == pattern.Regex);
                            pageName = result?.First()?.SourcePath;
                            var partMessage = string.Join(", ", result.Select(x => x.SourcePath).ToArray());
                            message           += regexMatchesCount.ToString() + ": " + partMessage + "\n";
                            regexMatchesCount += 1;
                        }
                        return(new List <CompileResult> {
                            new CompileResult {
                                ErrorMessages = new List <CompilerError> {
                                    new CompilerError {
                                        Message = message,
                                        LineNumber = 1,
                                        LinePosition = 1
                                    }
                                },
                                Success = false
                            }
                        });
                    }
                    List <ResourcePropertyPosition> AllResourcesMeta = new List <ResourcePropertyPosition>();
                    var metaClassCompletion = true;
                    foreach (var resource in ToBeUpdatedResources)
                    {
                        Console.WriteLine($"Uploading : {resource?.SourcePath}");
                        resource.UrlPatternRegex = UrlPatternRegexList.Find(x => x.SourcePath == resource.SourcePath).UrlPatternRegex;
                        var retryCount = 0;
                        do
                        {
                            var isUploaded = api.CreateOrUpdateResourceApi(resource);
                            if (!isUploaded)
                            {
                                Console.WriteLine($"Upload Failed.. retrying {retryCount}..");
                                retryCount++;
                                if (retryCount >= 3)
                                {
                                    return new List <CompileResult> {
                                               new CompileResult {
                                                   ErrorMessages = new List <CompilerError> {
                                                       new CompilerError {
                                                           Message = string.Format("Faild to save to DB for file : '{0}'", resource.SourcePath), LineNumber = 1, LinePosition = 1
                                                       }
                                                   }, Success = false
                                               }
                                    }
                                }
                                ;
                            }
                            else
                            {
                                retryCount = 0;
                            }
                        } while (retryCount != 0);

                        if (CompiledResources.ContainsKey(string.Format("{0}:{1}", resource.ProjectId, resource.SourcePath)))
                        {
                            var currentCompiledResource = CompiledResources[string.Format("{0}:{1}", resource.ProjectId, resource.SourcePath)];
                            SaveFileContentToS3RequestModel resourceContent = new SaveFileContentToS3RequestModel
                            {
                                ProjectId   = resource.ProjectId,
                                BucketName  = project.BucketNames.demo,
                                SourcePath  = resource.SourcePath,
                                FileContent = currentCompiledResource.CompiledString
                            };
                            var compiledStringResult = api.SaveFileContentToS3(resourceContent);
                            if (compiledStringResult == null)
                            {
                                return new List <CompileResult> {
                                           new CompileResult {
                                               ErrorMessages = new List <CompilerError> {
                                                   new CompilerError {
                                                       Message = string.Format("Faild to save to Demo Bucket for file : '{0}'", resource.SourcePath), LineNumber = 1, LinePosition = 1
                                                   }
                                               }, Success = false
                                           }
                                }
                            }
                            ;
                            //Save compiled (.kc) file
                            if (currentCompiledResource.KitsunePage != null)
                            {
                                var jsonSerializeSettings = new JsonSerializerSettings();

                                jsonSerializeSettings.TypeNameHandling = TypeNameHandling.Auto;
                                string output = JsonConvert.SerializeObject(currentCompiledResource.KitsunePage, Formatting.None, jsonSerializeSettings);

                                resourceContent = new SaveFileContentToS3RequestModel
                                {
                                    ProjectId   = resource.ProjectId,
                                    BucketName  = project.BucketNames.demo,
                                    SourcePath  = $"{resource.SourcePath}.kc",
                                    FileContent = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(output)),
                                    base64      = true
                                };
                                compiledStringResult = api.SaveFileContentToS3(resourceContent);
                                if (compiledStringResult == null)
                                {
                                    return new List <CompileResult> {
                                               new CompileResult {
                                                   ErrorMessages = new List <CompilerError> {
                                                       new CompilerError {
                                                           Message = string.Format("Faild to save to Demo Bucket for file : '{0}'", resource.SourcePath), LineNumber = 1, LinePosition = 1
                                                       }
                                                   }, Success = false
                                               }
                                    }
                                }
                                ;
                            }

                            try
                            {
                                if (Kitsune.Helper.Constants.DynamicFileExtensionRegularExpression.IsMatch(resource.SourcePath.ToLower()))
                                {
                                    if (currentCompiledResource.MetaClass == null)
                                    {
                                        metaClassCompletion = false;
                                        throw new Exception(string.Format("Meta Class is null for resource: {0}", resource.SourcePath));
                                    }
                                    var serializedMetaClass = MetaHelper.ProtoSerializer(CompiledResources[string.Format("{0}:{1}", resource.ProjectId, resource.SourcePath)].MetaClass);

                                    if (string.IsNullOrEmpty(serializedMetaClass))
                                    {
                                        throw new Exception(string.Format("Meta Class serialization failed for resource: {0}", resource.SourcePath));
                                    }
                                    //return new List<CompileResult> { new CompileResult { ErrorMessages = new List<CompilerError> { new CompilerError { Message = string.Format("Faild to serialize meta Class for : {0}", resource.SourcePath), LineNumber = 1, LinePosition = 1 } }, Success = false } };
                                    resourceContent = new SaveFileContentToS3RequestModel
                                    {
                                        ProjectId   = resource.ProjectId,
                                        BucketName  = project.BucketNames.demo,
                                        SourcePath  = string.Format(CompilerConstants.TreeFileName, resource.SourcePath),
                                        FileContent = serializedMetaClass,
                                        base64      = true
                                    };
                                    var metaClassResult = api.SaveFileContentToS3(resourceContent);
                                    if (metaClassResult == null)
                                    {
                                        throw new Exception(string.Format("Meta Class .k file not saved for resource: {0}", resource.SourcePath));
                                    }
                                    //return new List<CompileResult> { new CompileResult { ErrorMessages = new List<CompilerError> { new CompilerError { Message = string.Format("Faild to save meta class to Demo Bucket for : '{0}'", resource.SourcePath), LineNumber = 1, LinePosition = 1 } }, Success = false } };
                                    metaClassResult = null;

                                    AllResourcesMeta.Add(new ResourcePropertyPosition
                                    {
                                        SourcePath = resource.SourcePath,
                                        UrlPattern = resource.UrlPattern,
                                        Properties = CompiledResources[string.Format("{0}:{1}", resource.ProjectId, resource.SourcePath)].MetaClass
                                    });
                                }
                            }
                            catch (Exception ex)
                            {
                                //ToDo: Log MetaInfo Error
                            }
                        }
                    }
                    try
                    {
                        if (AllResourcesMeta.Any() && metaClassCompletion)
                        {
                            MetaGraphNode             metaGraphResult = new MetaGraphNode();
                            GetFileFromS3RequestModel GraphRequest    = new GetFileFromS3RequestModel
                            {
                                SourcePath = string.Format(CompilerConstants.GraphFileName, projectId),
                                ProjectId  = projectId,
                                BucketName = project.BucketNames.demo
                            };
                            var serializedMetaGraph = api.GetFileFromS3(GraphRequest);
                            var metaGraphBuilder    = new MetaGraphBuilder();
                            if (serializedMetaGraph != null)
                            {
                                var metaGraph = MetaHelper.ProtoDeserialize <MetaGraphNode>(serializedMetaGraph);
                                if (metaGraph == null)
                                {
                                    throw new Exception("Meta Graph Deserialization failed");
                                }
                                //return new List<CompileResult> { new CompileResult { ErrorMessages = new List<CompilerError> { new CompilerError { Message = "Failed to deserialize Reference Graph", LineNumber = 1, LinePosition = 1 } }, Success = false } };
                                metaGraphResult = metaGraphBuilder.CreateGraph(AllResourcesMeta, metaGraph);
                            }
                            else
                            {
                                metaGraphResult = metaGraphBuilder.CreateGraph(AllResourcesMeta);
                            }
                            if (metaGraphResult == null)
                            {
                                throw new Exception("Meta Graph creation failed");
                            }
                            //return new List<CompileResult> { new CompileResult { ErrorMessages = new List<CompilerError> { new CompilerError { Message = "Faild to create reference Graph", LineNumber = 1, LinePosition = 1 } }, Success = false } };
                            var newSerializedMetaGraph = MetaHelper.ProtoSerializer(metaGraphResult);
                            if (string.IsNullOrEmpty(newSerializedMetaGraph))
                            {
                                throw new Exception("Meta Graph serialization failed");
                            }
                            //return new List<CompileResult> { new CompileResult { ErrorMessages = new List<CompilerError> { new CompilerError { Message = string.Format("Faild to serialize meta Class for : {0}", projectId), LineNumber = 1, LinePosition = 1 } }, Success = false } };
                            SaveFileContentToS3RequestModel graphContent = new SaveFileContentToS3RequestModel
                            {
                                ProjectId   = projectId,
                                BucketName  = project.BucketNames.demo,
                                SourcePath  = string.Format(CompilerConstants.GraphFileName, projectId),
                                FileContent = newSerializedMetaGraph,
                                base64      = true
                            };
                            var graphSaveResult = api.SaveFileContentToS3(graphContent);
                            if (graphSaveResult == null)
                            {
                                throw new Exception("Meta Graph file not saved");
                            }
                            //return new List<CompileResult> { new CompileResult { ErrorMessages = new List<CompilerError> { new CompilerError { Message = "Faild to save Graph to Demo Bucket", LineNumber = 1, LinePosition = 1 } }, Success = false } };
                        }
                    }
                    catch (Exception ex)
                    {
                        //ToDo: Log MetaInfo Error
                    }

                    //Update the project version

                    if (project.CompilerVersion != currentCompilerVersion)
                    {
                        APIHelpers.UpdateProjectCompilerVersion(userEmail, projectId, currentCompilerVersion);
                    }
                }
                return(response);
            }
            catch (Exception ex)
            {
                Log.Error($"BuildProject : Exception : {ex.Message}, {ex.StackTrace}, {(ex.Data != null ? ex.Data.Count > 0 ? Newtonsoft.Json.JsonConvert.SerializeObject(ex.Data) : "" : "")}");
                return(new List <CompileResult> {
                    new CompileResult {
                        ErrorMessages = new List <CompilerError> {
                            new CompilerError {
                                Message = "BuildProject : Exception :  Something went wrong. Please try again.", LineNumber = 1, LinePosition = 1
                            }
                        }, Success = false
                    }
                });
            }
        }
Exemple #14
0
 public abstract void ProcessNode(CompileResourceRequest request, List <CompilerError> compileErrors, Dictionary <string, int> customVariables, string rootUrl, string filePath, Dictionary <string, string> classNameAlias, Dictionary <int, string> classNameAliasdepth, int depth, HtmlNode node, HtmlAttribute dynamicAttribute, List <MatchNode> objectNamesToValidate, DocumentValidator documentValidator);
Exemple #15
0
        public static CompileResult CompileHTML(CompileResourceRequest req, out HtmlDocument documentRef,
                                                GetProjectDetailsResponseModel projectDetails,
                                                KEntity kentity,
                                                GetPartialPagesDetailsResponseModel partialPages = null, bool subsequentCompilation = false, List <KLanguageModel> componentEntities = null) // partialPage is required only for the publish/preview
        {
            ///TODO : Handle the kitsune-settings.xml saperatly
            documentRef = GetDocumentObject();
            var filePath = req.SourcePath.Trim('/');

            if (!string.IsNullOrEmpty(req.FileContent))
            {
                HtmlDocument             document;
                List <CompilerError>     nfUXErrors      = new List <CompilerError>();
                Dictionary <string, int> customVariables = new Dictionary <string, int>();
                string rootUrl;

                DocumentPreprocessor.PreprocessDocument(req, kentity, filePath, out document, nfUXErrors, out rootUrl);
                documentRef = document;

                /*
                 * if (document.ParseErrors.Count() > 0)
                 * {
                 *  return CompileResultHelper.GetCompileResult(nfUXErrors, req.SourcePath, false);
                 * }
                 */
                req.UrlPattern = req.SourcePath; //assigning default urlpattern

                if (!req.IsStatic)
                {
                    if (kentity == null)
                    {
                        return(CompileResultHelper.GetErrorCompileResult("Schema not found for this project.", req.SourcePath));
                    }
                    else if (kentity.EntityName == null)
                    {
                        kentity.EntityName = Constants.KPayDefaultSchema;
                    }

                    HtmlDocument documentClone = GetDocumentObject();
                    documentClone.LoadHtml(document.DocumentNode.OuterHtml);
                    DocumentValidator validator = new DocumentValidator();
                    validator.ValidateDocument(documentClone, req, nfUXErrors, kentity, customVariables, rootUrl, filePath, projectDetails, componentEntities?.Select(x => x.Entity)?.ToList());
                    if (nfUXErrors.Count == 0)
                    {
                        if (req.IsPublish)
                        {
                            var csrfPresent = Constants.CSRFRegularExpression.IsMatch(document.DocumentNode.OuterHtml);

                            UpdateForPublish(ref document, req, kentity, nfUXErrors, customVariables, projectDetails, partialPages, componentEntities, csrfPresent);

                            if (req.PageType != Models.Project.KitsunePageType.PARTIAL)
                            {
                                HandleScriptInjection(document, req, kentity, rootUrl, projectDetails, csrfPresent);
                            }
                        }
                    }

                    /*
                     * KitsunePage myPage = (new NewRootNodeProcessor()).Process(document.DocumentNode.OuterHtml);
                     * var jsonSerializeSettings = new JsonSerializerSettings();
                     * jsonSerializeSettings.TypeNameHandling = TypeNameHandling.Objects;
                     * string output = JsonConvert.SerializeObject(myPage, Formatting.None, jsonSerializeSettings);
                     * //KitsunePage myNewPage = JsonConvert.DeserializeObject<KitsunePage>(output, jsonSerializeSettings);
                     */
                }
                UpdatePathsForPreview(req, document);
                if (nfUXErrors.Count > 0)
                {
                    return new CompileResult {
                               Success = false, ErrorMessages = nfUXErrors, PageName = req.SourcePath
                    }
                }
                ;
                else
                {
                    return new CompileResult {
                               Success = true, CompiledString = document.DocumentNode.OuterHtml.ToString(), ErrorMessages = nfUXErrors, PageName = req.SourcePath, CustomVariables = customVariables
                    }
                };
            }

            return(new CompileResult
            {
                Success = false,
                ErrorMessages = new List <CompilerError> {
                    new CompilerError {
                        Message = "Input should not be empty"
                    }
                },
                PageName = req.SourcePath,
            });
        }
        public IActionResult CompileKitsuneResource(string projectid, [FromQuery] string resourcename, [FromQuery] string user, [FromBody] CompileResourceRequest req)
        {
            try
            {
                req.UserEmail  = user;
                req.ProjectId  = projectid;
                req.SourcePath = resourcename;

                //Compile project dosent required partial pages
                var compileResult = CompilerHelper.CompileProjectResource(req);
                return(Ok(compileResult));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Exemple #17
0
        /// <summary>
        /// Process nodes with k-object tags.
        /// </summary>
        /// <param name="request">Incoming request</param>
        /// <param name="compileErrors">list of errors during compilation</param>
        /// <param name="kentity">project language</param>
        /// <param name="customVariables">output list to hold custom variables</param>
        /// <param name="rootUrl">root url</param>
        /// <param name="filePath">file path</param>
        /// <param name="classNameAlias">aliases used either with k-repeat or k-object</param>
        /// <param name="classNameAliasdepth">depth based storage of aliases</param>
        /// <param name="depth">depth of the node in DOM</param>
        /// <param name="dynamicAttribute">key-value pair of the k-tag</param>
        public override void ProcessNode(CompileResourceRequest request, List <CompilerError> compileErrors, Dictionary <string, int> customVariables, string rootUrl, string filePath, Dictionary <string, string> classNameAlias, Dictionary <int, string> classNameAliasdepth, int depth, HtmlNode node, HtmlAttribute dynamicAttribute, List <MatchNode> objectNamesToValidate, DocumentValidator documentValidator)
        {
            Node result = LexerGenerator.Parse(dynamicAttribute.Value.Trim('[', ']'));

            if (result?.Children?.Count == 3)
            {
                string referenceNames        = result.Token.Value == ACTIONS.KObject ? result.Children[0].Token.Value : result.Children[0].Children[0].Token.Value.ToLower();
                string referenceObject       = result.Children[2].Children[0].Token.Value.ToLower();
                var    referenceKObjectArray = referenceNames.Split(',');

                KEntity kentity = documentValidator.GetKEntityFromEntityName(documentValidator.defaultEntity);
                var     referenceObjectWithIterator = UpdateIterator(referenceObject, documentValidator);

                var tempRefObj = referenceObjectWithIterator.Trim();
                for (int i = referenceKObjectArray.Length - 1, j = 0; i >= 0; i--, j++)
                {
                    KEntity commEntity = documentValidator.GetKEntityFromEntityName(referenceKObjectArray[i]);
                    if (commEntity != null || (kentity != null && kentity.Classes.Where(x => x.Name?.ToLower() == referenceObject?.Split('.')?[0] && x.ClassType == KClassType.BaseClass).Count() == 0))
                    {
                        compileErrors.Add(CompileResultHelper.GetCompileError(ErrorCodeConstants.BaseClassAsKObjectVariable, dynamicAttribute.Line, dynamicAttribute.LinePosition));
                        return;
                    }
                    else
                    {
                        if (commEntity != null)
                        {
                            kentity = commEntity;
                        }
                        if (j > 0)
                        {
                            tempRefObj = tempRefObj.Substring(0, tempRefObj.LastIndexOf('.'));
                        }
                        KProperty property = GetProperty(compileErrors, dynamicAttribute, documentValidator, tempRefObj, classNameAlias);


                        if (property == null)
                        {
                            compileErrors.Add(CompileResultHelper.GetCompileError(ErrorCodeConstants.InvalidKObjectClass, dynamicAttribute.Line, dynamicAttribute.LinePosition));

                            return;
                        }
                        else if (property.Type == PropertyType.array)
                        {
                            classNameAlias.Add(referenceKObjectArray[i], tempRefObj);
                            objectNamesToValidate.Add(new MatchNode {
                                Value = tempRefObj, Line = dynamicAttribute.Line, Column = dynamicAttribute.LinePosition
                            });
                        }
                        else
                        {
                            classNameAlias.Add(referenceKObjectArray[i], tempRefObj);
                            objectNamesToValidate.Add(new MatchNode {
                                Value = tempRefObj, Line = dynamicAttribute.Line, Column = dynamicAttribute.LinePosition
                            });
                        }
                    }
                }
                request.PageType = Models.Project.KitsunePageType.DETAILS;
                var searchableProperty = IsSearchableProperty(referenceObject, out KClass kobjClass, documentValidator);

                if (referenceObject.StartsWith("webactions.") || kobjClass != null)
                {
                    request.KObject = referenceNames.Trim() + ":" + referenceObject;
                }
                else
                {
                    request.KObject = referenceNames.Trim();
                }
            }
            else
            {
                compileErrors.Add(CompileResultHelper.GetCompileError(ErrorCodeConstants.InvalidKObjectSyntax, dynamicAttribute.Line, dynamicAttribute.LinePosition));
            }
            dynamicAttribute.Value = "";
        }
        /// <summary>
        /// Validate k-tags and expressions in the document.
        /// </summary>
        /// <param name="document">Incoming document</param>
        /// <param name="request">Incoming request</param>
        /// <param name="compileErrors">list of errors during compile</param>
        /// <param name="kentity">project language</param>
        /// <param name="customVariables">custom variables found during compilation</param>
        /// <param name="rootUrl">root url</param>
        /// <param name="filePath">file path</param>
        public void ValidateDocument(HtmlDocument document, CompileResourceRequest request, List <CompilerError> compileErrors, KEntity kentity, Dictionary <string, int> customVariables, string rootUrl, string filePath, GetProjectDetailsResponseModel projectDetails, List <KEntity> componentsEntity = null)
        {
            Dictionary <string, bool>   objectNamesValidated  = new Dictionary <string, bool>();
            Dictionary <string, string> classNameAlias        = new Dictionary <string, string>();
            Dictionary <int, string>    classNameAliasLevel   = new Dictionary <int, string>();
            List <MatchNode>            objectNamesToValidate = new List <MatchNode>();

            entities.Add(kentity.EntityName, kentity);
            defaultEntity = kentity.EntityName;
            if (componentsEntity != null && componentsEntity.Any())
            {
                foreach (var comEntity in componentsEntity)
                {
                    if (!entities.Keys.Contains(comEntity.EntityName))
                    {
                        entities.Add(comEntity.EntityName, comEntity);
                    }
                }
            }

            var baseClassList = entities.SelectMany(x => x.Value?.Classes?.Where(y => y.ClassType == KClassType.BaseClass)?.ToList());

            HtmlNode      node = document.DocumentNode;
            List <string> dynamicTagDescriptors = GetDynamicTagDescriptors(typeof(LanguageAttributes));
            int           level = 0;

            while (node != null)
            {
                if (node.NodeType == HtmlNodeType.Element)
                {
                    var dynamicAttributes = node.Attributes.Where(x => dynamicTagDescriptors.Contains(x.Name.ToLower()));
                    for (int i = 0; i < dynamicAttributes.Count(); i++)
                    {
                        if (!string.IsNullOrEmpty(dynamicAttributes.ElementAt(i).Value))
                        {
                            Processor processor = ProcessorFactory.GetProcessor(dynamicAttributes.ElementAt(i).Name);
                            processor.ProcessNode(request, compileErrors, customVariables, rootUrl, filePath, classNameAlias, classNameAliasLevel, level, node, dynamicAttributes.ElementAt(i), objectNamesToValidate, this);
                        }
                        //k-partial k-norepeat attribute can be empty
                        else if (dynamicAttributes.ElementAt(i).Name?.ToLower() != LanguageAttributes.KPartial.GetDescription()?.ToLower() &&
                                 dynamicAttributes.ElementAt(i).Name?.ToLower() != LanguageAttributes.KNoRepeat.GetDescription()?.ToLower())
                        {
                            compileErrors.Add(CompileResultHelper.GetCompileError(string.Format(ErrorCodeConstants.InvalidKitsuneTagValue,
                                                                                                dynamicAttributes.ElementAt(i).Name,
                                                                                                dynamicAttributes.ElementAt(i).Line, dynamicAttributes.ElementAt(i).LinePosition)));
                        }
                    }
                    if (node.Name.Equals(LanguageAttributes.KScript.GetDescription(), StringComparison.InvariantCultureIgnoreCase))
                    {
                        Processor processor = ProcessorFactory.GetProcessor(node.Name.ToLower());
                        processor.ProcessNode(request, compileErrors, customVariables, rootUrl, filePath, classNameAlias, classNameAliasLevel, level, node, null, objectNamesToValidate, this);
                    }
                }

                if (node.HasChildNodes)
                {
                    node = node.FirstChild;
                    level++;
                }
                else
                {
                    ValidateExpressions(compileErrors, entities, objectNamesValidated, classNameAlias, baseClassList, node, objectNamesToValidate, customVariables, projectDetails);
                    if (node.NextSibling != null)
                    {
                        node = node.NextSibling;
                        ClearNameAliases(classNameAlias, classNameAliasLevel, level);
                    }
                    else
                    {
                        while (node.ParentNode != null && node.ParentNode.NextSibling == null)
                        {
                            node = node.ParentNode;
                            ValidateExpressions(compileErrors, entities, objectNamesValidated, classNameAlias, baseClassList, node, objectNamesToValidate, customVariables, projectDetails);
                            level--;
                            ClearNameAliases(classNameAlias, classNameAliasLevel, level);
                        }
                        node = node?.ParentNode;
                        if (node != null)
                        {
                            ValidateExpressions(compileErrors, entities, objectNamesValidated, classNameAlias, baseClassList, node, objectNamesToValidate, customVariables, projectDetails);
                        }
                        node = node?.NextSibling;
                        level--;
                        ClearNameAliases(classNameAlias, classNameAliasLevel, level);
                    }
                }
            }
        }
Exemple #19
0
        /// <summary>
        /// Process nodes with KDL tag.
        /// </summary>
        /// <param name="request">Incoming request</param>
        /// <param name="compileErrors">list of errors during compilation</param>
        /// <param name="kentity">project language</param>
        /// <param name="customVariables">output list to hold custom variables</param>
        /// <param name="rootUrl">root url</param>
        /// <param name="filePath">file path</param>
        /// <param name="classNameAlias">aliases used either with k-repeat or k-object</param>
        /// <param name="classNameAliasdepth">depth based storage of aliases</param>
        /// <param name="depth">depth of the node in DOM</param>
        /// <param name="dynamicAttribute">key-value pair of the k-tag</param>
        public override void ProcessNode(CompileResourceRequest request, List <CompilerError> compileErrors, Dictionary <string, int> customVariables, string rootUrl, string filePath, Dictionary <string, string> classNameAlias, Dictionary <int, string> classNameAliasdepth, int depth, HtmlNode node, HtmlAttribute dynamicAttribute, List <MatchNode> objectNamesToValidate, DocumentValidator documentValidator)
        {
            var pattern = dynamicAttribute.Value;

            if (!string.IsNullOrWhiteSpace(pattern))
            {
                pattern = pattern.Trim().Trim('/');
                if (pattern.IndexOf("currentpagenumber.urlencode()") < 0)
                {
                    pattern = pattern.Replace("currentpagenumber", "currentpagenumber.urlencode()");
                }

                KEntity kentity = documentValidator.GetKEntityFromEntityName(documentValidator.defaultEntity);

                #region Validate unique id exist for detailspage
                if (request.PageType == Models.Project.KitsunePageType.DETAILS)
                {
                    //added support for multiple kobjects, check for the last objetcts unique param
                    var kobjects   = request.KObject.Split(':')[0].Split(',');
                    var kobjParam  = kobjects[kobjects.Length - 1].ToLower();
                    var kidRegex   = Kitsune.Helper.Constants.GetKObjectIteratorRegex($"{kobjParam}._kid");
                    var _idRegex   = Kitsune.Helper.Constants.GetKObjectIteratorRegex($"{kobjParam}._id");
                    var indexRegex = Kitsune.Helper.Constants.GetKObjectIteratorRegex($"{kobjParam}.index");
                    var idRegex    = Kitsune.Helper.Constants.GetKObjectIteratorRegex($"{kobjParam}.id");
                    if (!(_idRegex.IsMatch(pattern.ToLower()) || kidRegex.IsMatch(pattern.ToLower()) || indexRegex.IsMatch(pattern.ToLower()) || idRegex.IsMatch(pattern.ToLower())))
                    {
                        compileErrors.Add(new CompilerError()
                        {
                            LineNumber   = dynamicAttribute.Line,
                            LinePosition = dynamicAttribute.LinePosition,
                            Message      = string.Format(ErrorCodeConstants.MissingUniqueIndexForDetailsPage, request.KObject.Split(':').Length > 1 ? request.KObject.Split(':')[1] : kobjParam)
                        });
                    }
                }
                #endregion

                var viewExist = KitsuneCompiler.ValidateView(ref pattern, kentity);
                if (viewExist != null)
                {
                    viewExist.LineNumber   = dynamicAttribute.Line;
                    viewExist.LinePosition = dynamicAttribute.LinePosition;
                    compileErrors.Add(viewExist);
                }
                var pagination = Kitsune.Helper.Constants.PaginationRegularExpression.Match(pattern);

                if (pagination != null && pagination.Success)
                {
                    var matches = Kitsune.Helper.Constants.FunctionParameterExpression.Match(pagination.Value);
                    if (matches != null && matches.Groups?.Count > 0)
                    {
                        request.UrlPattern = string.Format("[[{0}]]/{1}-[[{2}.currentpagenumber]]", rootUrl, matches.Groups[1].Value.Substring(matches.Groups[1].Value.IndexOf('.') + 1), request.ClassName);
                    }
                }
                else
                {
                    var expressions = Helper.HtmlHelper.GetExpressionFromElement(pattern, 0);
                    //Add urlencode
                    if (expressions != null && expressions.Any())
                    {
                        var tmp       = "";
                        var tmpValue  = "";
                        var groupCout = 0;
                        foreach (var exp in expressions)
                        {
                            groupCout++;
                            tmp      = exp.Value.Trim('[', ']');
                            tmpValue = exp.Value;
                            if (!tmp.ToLower().EndsWith(".urlencode()"))
                            {
                                pattern = pattern.Replace(tmpValue, exp.Value.Replace(tmp, $"{tmp}.urlencode()"));
                            }

                            var objects = Parser.GetObjects(tmp);
                            if (objects != null && objects.Any())
                            {
                                foreach (var custObj in objects)
                                {
                                    if (custObj.Split('.').Length == 1)
                                    {
                                        IList <KClass> allClasses = new List <KClass>();
                                        if (documentValidator != null && documentValidator.entities != null && documentValidator.entities.Keys.Any(x => x == custObj.Trim('[', ']').ToLower()))
                                        {
                                            allClasses = documentValidator.entities[custObj.Trim('[', ']').ToLower()].Classes;
                                        }
                                        if (!allClasses.Any(x => x.ClassType == KClassType.BaseClass && x.Name.ToLower() == custObj.Trim('[', ']')))
                                        {
                                            if (!customVariables.ContainsKey(custObj.Trim('[', ']')?.ToLower()))
                                            {
                                                customVariables.Add(custObj.Trim('[', ']')?.ToLower(), groupCout);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    request.UrlPattern = string.Format("[[{0}]]/{1}", rootUrl, pattern);
                    if (pattern != null && pattern.ToLower().IndexOf(@"search/") != -1)
                    {
                        request.PageType = Models.Project.KitsunePageType.SEARCH;
                    }
                }
                //req.UrlPattern = pattern.Replace(kdl, req.UrlPattern);
                dynamicAttribute.Value = request.UrlPattern;
            }
            else if (filePath.ToLower().EndsWith(".dl"))
            {
                compileErrors.Add(CompileResultHelper.GetCompileError(ErrorCodeConstants.NoDLTagInDLPage));
            }
            else if (string.IsNullOrEmpty(request.UrlPattern))
            {
                request.UrlPattern = string.Format("[[{0}]]/{1}", rootUrl, filePath.ToLower());
            }
        }
Exemple #20
0
        public static ResourceCompilationResult CompileProjectResource(CompileResourceRequest req,
                                                                       GetPartialPagesDetailsResponseModel partialPages = null)
        {
            //Source path should always start with / (to make sure its from root folder)
            if (!string.IsNullOrEmpty(req.SourcePath))
            {
                req.SourcePath = string.Format("/{0}", req.SourcePath.Trim('/'));
            }
            else
            {
                return new ResourceCompilationResult {
                           Success = false, ErrorMessages = new List <CompilerError> {
                               new CompilerError {
                                   LineNumber = 1, LinePosition = 1, Message = "Source path can not be empty."
                               }
                           }
                }
            };

            if (!string.IsNullOrEmpty(req.FileContent))
            {
                var document = new HtmlDocument();

                CompileResult compileResult = null;
                try
                {
                    //If the file is html extension then only compile
                    if (Kitsune.Helper.Constants.DynamicFileExtensionRegularExpression.IsMatch(req.SourcePath.ToLower()))
                    {
                        try
                        {
                            var byteStream = Convert.FromBase64String(req.FileContent);
                            req.FileContent = System.Text.Encoding.UTF8.GetString(byteStream);
                        }
                        catch
                        {
                            return(new ResourceCompilationResult {
                                Success = false, ErrorMessages = new List <CompilerError> {
                                    new CompilerError {
                                        Message = "Input string is not valid base64 string"
                                    }
                                }
                            });
                        }

                        var projectDetailsRequestModel = new GetProjectDetailsRequestModel
                        {
                            ProjectId        = req.ProjectId,
                            ExcludeResources = false,
                            UserEmail        = req.UserEmail
                        };
                        var projectDetails = MongoConnector.GetProjectDetails(projectDetailsRequestModel);
                        if (projectDetails == null)
                        {
                            return new ResourceCompilationResult
                                   {
                                       Success       = false,
                                       ErrorMessages = new List <CompilerError> {
                                           new CompilerError {
                                               Message = "Project not found."
                                           }
                                       }
                                   }
                        }
                        ;



                        var user = MongoConnector.GetUserIdFromUserEmail(new GetUserIdRequestModel
                        {
                            UserEmail = req.UserEmail
                        });
                        var languageEntity = MongoConnector.GetLanguageEntity(new GetLanguageEntityRequestModel
                        {
                            EntityId = projectDetails.SchemaId,
                        });
                        languageEntity = new KLanguageBase().GetKitsuneLanguage(req.UserEmail, req.ProjectId, projectDetails, languageEntity, user.Id);

                        #region Handle Project Components
                        var componentEntities = GetComponentReference(projectDetails.Components);
                        #endregion

                        //Compile only if the file type is supported
                        compileResult = KitsuneCompiler.CompileHTML(req, out document, projectDetails, languageEntity, partialPages, componentEntities: componentEntities);
                    }
                    //Validate the config file
                    else if (Helper.Constants.ConfigFileRegularExpression.IsMatch(req.SourcePath))
                    {
                        var byteStream = Convert.FromBase64String(req.FileContent);
                        req.FileContent = System.Text.Encoding.UTF8.GetString(byteStream);
                        compileResult   = KitsuneCompiler.ValidateJsonConfig(new ValidateConfigRequestModel
                        {
                            File = new ConfigFile {
                                Content = req.FileContent
                            },
                            UserEmail = req.UserEmail
                        });
                        req.UrlPattern = req.SourcePath;
                        req.IsStatic   = false;
                    }
                }
                catch (Exception ex)
                {
                }

                if (compileResult != null && compileResult.ErrorMessages?.Count() > 0)
                {
                    return(new ResourceCompilationResult {
                        Success = false, ErrorMessages = compileResult.ErrorMessages
                    });
                }
                else
                {
                    return new ResourceCompilationResult {
                               Success = true
                    }
                };
            }

            return(new ResourceCompilationResult {
                Success = false, ErrorMessages = new List <CompilerError> {
                    new CompilerError {
                        Message = "Input should not be empty"
                    }
                }
            });
        }
        public static ResourceMetaInfo MetaInfo(CompileResourceRequest req, string metadocument, GetProjectDetailsResponseModel projectDetails,
                                                KEntity kentity, List <string> tagsToIgnore)
        {
            try
            {
                if (tagsToIgnore == null)
                {
                    tagsToIgnore = new List <string>();
                }
                MetaInfoList = new List <SinglePositionProperty> {
                };
                metaList     = new List <Kitsune.Helper.MatchNode>();
                var documentRef = new HtmlDocument();
                documentRef = GetDocumentObject();
                if (!string.IsNullOrEmpty(metadocument))
                {
                    var document = GetDocumentObject();
                    try
                    {
                        document.LoadHtml(metadocument);
                    }
                    catch { }
                    documentRef = document;

                    var descendants = document.DocumentNode.Descendants();

                    if (!(req.IsStatic))
                    {
                        #region K-RepeatHandler
                        var repeatNodes = descendants.Where(x => x.Attributes[LanguageAttributes.KRepeat.GetDescription()] != null && !string.IsNullOrEmpty(x.Attributes[LanguageAttributes.KRepeat.GetDescription()].Value));
                        if (repeatNodes != null && repeatNodes.Any())
                        {
                            var           krepeatCount = 0;
                            var           kval         = "";
                            string        match;
                            List <string> repeatForEachParam;
                            foreach (var node in repeatNodes)
                            {
                                node.Attributes[LanguageAttributes.KRepeat.GetDescription()].Value = string.Concat("[", node.Attributes[LanguageAttributes.KRepeat.GetDescription()].Value.Trim(), "]");
                                kval  = node.Attributes[LanguageAttributes.KRepeat.GetDescription()].Value;
                                match = Regex.Match(kval, Kitsune.Helper.Constants.WidgetRegulerExpression.ToString())?.Value?.Trim('[', ']');
                                if (string.IsNullOrEmpty(match))
                                {
                                }
                                //nfUXErrors.Add(new CompilerError { Message = "Invalid k-repeat value", LineNumber = node.Line, LinePosition = node.LinePosition });
                                else
                                {
                                    //if (match.IndexOf(" in ") > 0)
                                    //{
                                    //    repeatForEachParam = match.Split(new string[] { " in " }, StringSplitOptions.None)?.ToList();
                                    //    ///TODO : check condition for all params
                                    //    if (repeatForEachParam.Count == 2)
                                    //    {
                                    //        var regexes = Kitsune.Helper.Constants.WidgetRegulerExpression;
                                    //        var regexmatchies = regexes.Matches(node.InnerHtml);
                                    //        var regex = new Regex(MakeRegexEligible(krepeatvar[krepeatCount].Trim()));
                                    //        krepeatCount++;
                                    //        if (regexmatchies != null && regexmatchies.Count > 0)
                                    //            for (int i = 0; i < regexmatchies.Count; i++)
                                    //            {
                                    //                node.InnerHtml = node.InnerHtml.Replace(regexmatchies[i].Value, regex.Replace(regexmatchies[i].Value, "[x]"));
                                    //            }

                                    //        foreach (var attr in node.Attributes.Where(x => x.Name.ToLower() != "k-repeat"))
                                    //        {
                                    //            regexmatchies = regexes.Matches(attr.Value);
                                    //            for (int i = 0; i < regexmatchies.Count; i++)
                                    //            {
                                    //                attr.Value = attr.Value.Replace(regexmatchies[i].Value, regex.Replace(regexmatchies[i].Value, "[x]"));
                                    //            }
                                    //        }
                                    //    }
                                    //}
                                    //else
                                    //{
                                    repeatForEachParam = match.Split(',').ToList();
                                    if (repeatForEachParam.Count == 3)
                                    {
                                        var repeatCount = repeatForEachParam[2].Split(':');
                                        tagsToIgnore.Add(repeatForEachParam[0].Trim());
                                        tagsToIgnore.Add(repeatForEachParam[1].Trim());
                                        if (repeatCount[0].Contains("offset"))
                                        {
                                            repeatCount[0] = "x";
                                        }
                                        var regexes       = Kitsune.Helper.Constants.WidgetRegulerExpression;
                                        var regexmatchies = regexes.Matches(node.InnerHtml);
                                        var regex         = new Regex(MakeRegexEligible(@"[\s*" + repeatForEachParam[1].Trim() + @"\s*]"));
                                        if (regexmatchies != null && regexmatchies.Count > 0)
                                        {
                                            for (int i = 0; i < regexmatchies.Count; i++)
                                            {
                                                if (repeatCount[1].Contains("length()"))
                                                {
                                                    node.InnerHtml = node.InnerHtml.Replace(regexmatchies[i].Value, regex.Replace(regexmatchies[i].Value, "[x]"));
                                                }
                                                else
                                                {
                                                    node.InnerHtml = node.InnerHtml.Replace(regexmatchies[i].Value, regex.Replace(regexmatchies[i].Value, "[" + repeatCount[0].Trim() + "x" + repeatCount[1].Trim() + "]"));
                                                }
                                            }
                                        }

                                        foreach (var attr in node.Attributes.Where(x => x.Name.ToLower() != "k-repeat"))
                                        {
                                            regexmatchies = regexes.Matches(attr.Value);
                                            for (int i = 0; i < regexmatchies.Count; i++)
                                            {
                                                attr.Value = attr.Value.Replace(regexmatchies[i].Value, regex.Replace(regexmatchies[i].Value, "$1[" + repeatCount[0].Trim() + "x" + repeatCount[1].Trim() + "]$3$4"));
                                            }
                                        }
                                    }

                                    //}
                                }
                            }
                            foreach (var node in repeatNodes)
                            {
                                node.Attributes[LanguageAttributes.KRepeat.GetDescription()].Value = node.Attributes[LanguageAttributes.KRepeat.GetDescription()].Value.Substring(1, node.Attributes[LanguageAttributes.KRepeat.GetDescription()].Value.Length - 2);
                            }
                            var newHtml = document.DocumentNode.OuterHtml;
                            document = GetDocumentObject();
                            document.LoadHtml(newHtml);
                            descendants = document.DocumentNode.Descendants();

                            #endregion
                        }


                        string[] NfWidgets = document.DocumentNode.OuterHtml.Split('\n');
                        ExtractProperty(NfWidgets, req.UserEmail, req.ProjectId, tagsToIgnore.Distinct().ToList());

                        var metaObject = new List <PropertyDetails>();

                        //MetaInfoListString.Sort();

                        List <MultiplePositionProperty> distinctInput = MetaInfoList.GroupBy(i => i.Property)
                                                                        .Select(g => new MultiplePositionProperty
                        {
                            Property  = g.Key,
                            Positions = g.Select(i => i.Position).ToList()
                        }).ToList();
                        distinctInput = distinctInput.GroupBy(i => i.Property)
                                        .Select(g => new MultiplePositionProperty
                        {
                            Property  = g.Key,
                            Positions = g.SelectMany(i => i.Positions).ToList()
                        }).ToList();

                        //# For writing unmerged properties to file in local
                        //List<string> MetaInfoListString = new List<string>();
                        //foreach (var metainfo in distinctInput)
                        //{
                        //    List<string> write = new List<string> { metainfo.Property };
                        //    foreach (var position in metainfo.Positions)
                        //    {
                        //        write.Add(position.Column.ToString());
                        //        write.Add(position.Line.ToString());
                        //    }
                        //    MetaInfoListString.Add(string.Join(",", write));
                        //}
                        //MetaInfoListString.Sort();
                        //string outputFile = @"D:\" + req.UserEmail + @"\" + req.SourcePath.Replace("/", "");
                        //System.IO.File.WriteAllLines(outputFile + @".csv", MetaInfoListString);

                        var metaInfoMergeList = distinctInput.Where(x => x.Property.Contains('[')).ToList();
                        distinctInput.RemoveAll(x => x.Property.Contains('['));
                        var mergedMetaInfoList = ksegregation.MergeProperty(metaInfoMergeList);
                        distinctInput.AddRange(mergedMetaInfoList);
                        distinctInput = distinctInput.OrderBy(x => x.Property).ToList();

                        //# For writing merged properties to file in local
                        //List<string> MetaInfoListString = new List<string>();
                        //foreach (var metainfo in distinctInput)
                        //{
                        //    List<string> write = new List<string> { metainfo.Property };
                        //    foreach (var position in metainfo.Positions)
                        //    {
                        //        write.Add(position.Column.ToString());
                        //        write.Add(position.Line.ToString());
                        //    }
                        //    MetaInfoListString.Add(string.Join(",", write));
                        //}
                        //string optimizedOutputFile = @"D:\optimized_" + req.UserEmail + @"\" + req.SourcePath.Replace("/", "");
                        //System.IO.File.WriteAllLines(optimizedOutputFile + @".csv", MetaInfoListString);


                        var metaClassBuilder = new MetaClassBuilder();
                        var metaClass        = metaClassBuilder.BuildMetaClass(kentity, distinctInput, req.SourcePath);

                        return(new ResourceMetaInfo
                        {
                            IsError = false,
                            Property = distinctInput,
                            metaClass = metaClass
                        });
                    }

                    return(new ResourceMetaInfo
                    {
                        IsError = true,
                        Message = "Input should not be Static"
                    });
                }
                return(new ResourceMetaInfo
                {
                    IsError = true,
                    Message = "Input should not be empty"
                });
            }
            catch (Exception ex)
            {
                //TODO: throw error
                //throw new Exception(string.Format("Excepiton occured while generating metainfo: {0}", ex.Message));
                return(new ResourceMetaInfo
                {
                    IsError = true,
                    Message = string.Format("Excepiton occured while generating metainfo: {0}", ex.Message)
                });
            }
        }