///-------------------------------------------------------------------------------- /// <summary>This method gets the updated content for the template.</summary> /// /// <param name="parentModelContext">The parent model context from which to generate code.</param> /// /// <returns>A string representing the generated code for the template.</returns> ///-------------------------------------------------------------------------------- public string GetContent(IDomainEnterpriseObject parentModelContext) { try { ContentCodeBuilder.Clear(); if (IsWatchTemplate == false) { // clear parameters and variables if not passed in for temporary watch templates Parameters.Clear(); Variables.Clear(); } if (ContentAST == null && TemplateContent != String.Empty) { ParseContent(Solution.IsSampleMode); } if (ContentAST != null && ContentAST.ChildNodes.Count > 0) { ContentAST.InterpretNode(InterpreterTypeCode.Content, Solution, this, GetTemplateModelContext(parentModelContext), null); } ContentCode = ContentCodeBuilder.ToString() + MessageBuilder.ToString(); return(ContentCode); } catch (ApplicationAbortException) { throw; } catch (System.Exception ex) { HasErrors = true; Solution.ShowIssue(ex.Message + ex.StackTrace, DisplayValues.Exception_CodeGenerationTitle, Solution.IsSampleMode); } return(null); }
///-------------------------------------------------------------------------------- /// <summary>Interpret this node to produce content.</summary> /// /// <param name="solutionContext">The associated solution.</param> /// <param name="templateContext">The associated template.</param> /// <param name="modelContext">The associated model context.</param> /// <param name="appendToTemplateContext">Flag to append content.</param> /// <param name="parameters">Template parameters.</param> ///-------------------------------------------------------------------------------- public void GenerateContent(Solution solutionContext, ITemplate templateContext, IDomainEnterpriseObject modelContext, bool appendToTemplateContext, NameObjectCollection parameters) { ModelContextStack = null; PopCount = 0; if (modelContext is Project) { BusinessConfiguration.CurrentProject = modelContext as Project; } PushModelContext(modelContext); MessageBuilder.Clear(); ContentCodeBuilder.Clear(); Parameters.Clear(); Variables.Clear(); CurrentTabIndent = templateContext.CurrentTabIndent; if (ContentAST == null && TemplateContent != String.Empty) { ParseContent(Solution.IsSampleMode); } if (Solution.UseTemplateCache == true && Solution.IsSampleMode == false && String.IsNullOrEmpty(TemplateOutput) && !String.IsNullOrEmpty(TemplateContent) && modelContext != null && CachedContent[modelContext.ID.ToString()] != null) { // use cached content solutionContext.TemplatesExecuted++; solutionContext.CachedTemplatesExecuted++; ContentCodeBuilder.Append(CachedContent[modelContext.ID.ToString()] as String); } else { if (ContentAST != null) { ContentAST.InterpretNode(InterpreterTypeCode.Content, solutionContext, this, modelContext, parameters); } else { ContentCodeBuilder.Append("<" + TemplateName + ">"); } } if (Solution.UseTemplateCache == true && Solution.IsSampleMode == false && String.IsNullOrEmpty(TemplateOutput)) { CachedContent[modelContext.ID.ToString()] = null; // only cache smaller content that has no parameters or config settings if (Parameters.Count == 0 && HasRelativeSettings == false && ContentCodeBuilder.Length <= Solution.TemplateCacheMaxContentSize) { CachedContent[modelContext.ID.ToString()] = ContentCodeBuilder.ToString(); } } if (appendToTemplateContext == true) { templateContext.ContentCodeBuilder.Append(ContentCodeBuilder.ToString()); } ModelContextStack = null; PopCount = 0; }
///-------------------------------------------------------------------------------- /// <summary>This method generates the associated output path for the code template.</summary> /// /// <param name="parentModelContext">The parent model context from which to generate code.</param> /// /// <returns>A string representing the generated code for the template.</returns> ///-------------------------------------------------------------------------------- public string GenerateContentAndOutput(IDomainEnterpriseObject parentModelContext) { try { MessageBuilder.Clear(); ContentCodeBuilder.Clear(); Parameters.Clear(); Variables.Clear(); if (ContentAST == null && TemplateContent != String.Empty) { ParseContent(Solution.IsSampleMode); } OutputCodeBuilder.Clear(); Parameters.Clear(); Variables.Clear(); if (OutputAST == null && TemplateOutput != String.Empty) { ParseOutput(Solution.IsSampleMode); } IDomainEnterpriseObject context = GetTemplateModelContext(parentModelContext); if (ContentAST != null && ContentAST.ChildNodes.Count > 0) { ContentAST.InterpretNode(InterpreterTypeCode.Content, Solution, this, context, null); } if (OutputAST != null && OutputAST.ChildNodes.Count > 0) { OutputAST.InterpretNode(InterpreterTypeCode.Output, Solution, this, context, null); } ContentCode = ContentCodeBuilder.ToString(); OutputCode = OutputCodeBuilder.ToString() + MessageBuilder.ToString(); return(OutputCode); } catch (ApplicationAbortException) { throw; } catch (System.Exception ex) { HasErrors = true; Solution.ShowIssue(ex.Message + ex.StackTrace, DisplayValues.Exception_CodeGenerationTitle, Solution.IsSampleMode); } return(null); }
///-------------------------------------------------------------------------------- /// <summary>This method parses the template content into an abstract syntax tree.</summary> /// /// <returns>Parser error and other messages.</returns> ///-------------------------------------------------------------------------------- public bool ParseContent(bool showDialog = true) { try { ContentAST = null; if (String.IsNullOrEmpty(TemplateContent)) { HasErrors = true; Solution.ShowIssue(String.Format(DisplayValues.Exception_NoContent, TemplateName), DisplayValues.Exception_ParsingTitle, showDialog); return(false); } ParseTree tree = Solution.SpecTemplateContentParser.Parse(TemplateContent); if (tree.ParserMessages.Count == 0 && tree.Root.AstNode is TemplateNode) { ContentAST = tree.Root.AstNode as TemplateNode; if (ContentAST.LineNumber == 0 || TemplateContent.TrimStart().StartsWith("//") == true) { // TODO: this is a hack, investigate why parsing result line numbers are sometimes zero based and // sometimes one based. Need line numbers to be one based in line with text editors. ContentAST.ExecuteVisitor(new IncrementLineNumberVisitor()); } Solution.SpecTemplates[TemplateKey] = this; } else if (tree.ParserMessages.Count > 0) { HasErrors = true; StringBuilder messages = new StringBuilder(); messages.Append(DisplayValues.Exception_Parsing); messages.Append("\r\n"); messages.Append(DisplayValues.Exception_SolutionIntro); messages.Append(" "); messages.Append(Solution.SolutionName); messages.Append("\r\n"); messages.Append(DisplayValues.Exception_NodeIntro); messages.Append(" "); messages.Append(NodeName); messages.Append("\r\n"); messages.Append(DisplayValues.Exception_TemplateIntro); messages.Append(" "); messages.Append(TemplateName); messages.Append("\r\n"); messages.Append(DisplayValues.Exception_InnerMessageIntro); messages.Append("\r\n"); foreach (ParserMessage message in tree.ParserMessages) { messages.Append(String.Format(DisplayValues.Message_TemplateContentParseError, (message.Location.Line + 1).ToString(), (message.Location.Column + 1).ToString(), message.Message)); } Solution.ShowIssue(messages.ToString(), DisplayValues.Exception_ParsingTitle, showDialog); return(false); } return(true); } catch (ApplicationAbortException) { throw; } catch (System.Exception ex) { HasErrors = true; Solution.ShowIssue(ex.Message + ex.StackTrace, DisplayValues.Exception_ParsingTitle, showDialog); } return(false); }