// Copyright (c) 2012, Outercurve Foundation. // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // - Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // // - Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // - Neither the name of the Outercurve Foundation nor the names of its // contributors may be used to endorse or promote products derived from this // software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. using System; using System.IO; using System.Collections.Generic; using System.Text; using WebsitePanel.Templates.AST; using System.Collections; namespace WebsitePanel.Templates { /// /// Template allows to process block of texts containing special directives such as variables, /// conditional statements and loops. /// public class Template { string data; Lexer lexer = null; Parser parser = null; List statements = null; TemplateContext context = new TemplateContext(); /// /// Initializes a new instance of Template with specified template text. /// /// Template text. public Template(string data) { if (data == null) throw new ArgumentNullException("data"); this.data = data; } /// /// Initializes a new instance of Template with specified StringReader containing template text. /// /// StringReader containing template text. public Template(StringReader reader) { if(reader == null) throw new ArgumentNullException("reader"); this.data = reader.ReadToEnd(); } /// /// Verifies template syntax and throws ParserException when error is found. /// /// Thrown when error is found. public void CheckSyntax() { // create lexer lexer = new Lexer(data); // create parser parser = new Parser(lexer); // parse template parser.Parse(); } /// /// Evaluates template and returns the result as a string. /// /// String containing evaluated template. public string Evaluate() { // create writer to hold result StringWriter writer = new StringWriter(); // evaluate Evaluate(writer); // return result return writer.ToString(); } /// /// Evaluates template and returns the result as a string. /// /// String containing evaluated template. public string Evaluate(Hashtable items) { // copy items from hashtable foreach (string keyName in items.Keys) { this[keyName] = items[keyName]; } // evaluate return Evaluate(); } /// /// Evaluates template to the StringWriter. /// /// StringWriter to write evaluation results. public void Evaluate(StringWriter writer) { if(writer == null) throw new ArgumentNullException("writer"); if (lexer == null) { // create lexer lexer = new Lexer(data); // create parser parser = new Parser(lexer); // parse template statements = parser.Parse(); } // index custom templates int i = 0; while (i < statements.Count) { TemplateStatement tmpStatement = statements[i] as TemplateStatement; if (tmpStatement != null) { context.Templates.Add(tmpStatement.Name, tmpStatement); statements.RemoveAt(i); continue; } i++; } // evaluate template statements foreach (AST.Statement stm in statements) { // eval next statement stm.Eval(context, writer); } } /// /// Gets or sets the value of template context variable. /// /// The name of the context variable. Variable names are case-insensitive. /// Returns the value of the context variable. public object this[string name] { get { return context.Variables.ContainsKey(name) ? context.Variables[name] : null; } set { context.Variables[name] = value; } } } }