Ejemplo n.º 1
0
        /// <summary>
        /// 把实体的数据融合到模板中,并返回融合结果,会保留数据的空格,换行,减少空间占用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">实体类</param>
        /// <param name="templateNames">模板名称,可以有多级,多级用“.”分隔,对于小模板只需提供节点名称就可以了</param>
        /// <returns></returns>
        public string EntityToStr <T>(T entity, string templateName)
        {
            StringTemplate stringTemplate = new StringTemplate(_cache[templateName]);

            if (_attrRendererMap.ContainsKey(templateName))
            {
                foreach (KeyValuePair <Type, IAttributeRenderer> keyVal in _attrRendererMap[templateName])
                {
                    stringTemplate.RegisterRenderer(keyVal.Key, keyVal.Value);
                }
            }
            stringTemplate.SetAttribute("entity", entity);
            return(stringTemplate.ToString().Trim());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 把实体的数据融合到模板中,并返回融合结果,会把数据整合为一行,减少空间占用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">实体类</param>
        /// <param name="templateNames">模板名称,可以有多级,多级用“.”分隔,对于小模板只需提供节点名称就可以了</param>
        /// <returns></returns>
        public string EntityToRow <T>(T entity, string templateName)
        {
            StringBuilder templateSb = new StringBuilder();

            foreach (string row in _cache[templateName].Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
            {
                templateSb.Append(row);
            }
            StringTemplate stringTemplate = new StringTemplate(templateSb.ToString());

            if (_attrRendererMap.ContainsKey(templateName))
            {
                foreach (KeyValuePair <Type, IAttributeRenderer> keyVal in _attrRendererMap[templateName])
                {
                    stringTemplate.RegisterRenderer(keyVal.Key, keyVal.Value);
                }
            }
            stringTemplate.SetAttribute("entity", entity);
            return(stringTemplate.ToString().Trim());
        }
Ejemplo n.º 3
0
 public void TestRendererWithFormatAndSeparatorAndNull()
 {
     StringTemplate st = new StringTemplate(
             "The names: <names; separator=\" and \", null=\"n/a\", format=\"upper\">",
             typeof( AngleBracketTemplateLexer ) );
     IList names = new List<object>();
     names.Add( "ter" );
     names.Add( null );
     names.Add( "sriram" );
     st.SetAttribute( "names", names );
     st.RegisterRenderer( typeof( string ), new StringRenderer() );
     string expecting = "The names: TER and N/A and SRIRAM";
     string result = st.ToString();
     Assert.AreEqual( expecting, result );
 }
Ejemplo n.º 4
0
 public void TestRendererWithFormatAndSeparator()
 {
     StringTemplate st = new StringTemplate(
             "The names: <names; separator=\" and \", format=\"upper\">",
             typeof( AngleBracketTemplateLexer ) );
     st.SetAttribute( "names", "ter" );
     st.SetAttribute( "names", "tom" );
     st.SetAttribute( "names", "sriram" );
     st.RegisterRenderer( typeof( string ), new StringRenderer() );
     string expecting = "The names: TER and TOM and SRIRAM";
     string result = st.ToString();
     Assert.AreEqual( expecting, result );
 }
Ejemplo n.º 5
0
 public void TestRendererWithFormat()
 {
     StringTemplate st = new StringTemplate(
             "date: <created; format=\"yyyy.MM.dd\">",
             typeof( AngleBracketTemplateLexer ) );
     st.SetAttribute( "created",
                     new DateTime( 2005, 07, 05 ) );
     st.RegisterRenderer( typeof( DateTime ), new DateRenderer3() );
     string expecting = "date: 2005.07.05";
     string result = st.ToString();
     Assert.AreEqual( expecting, result );
 }
Ejemplo n.º 6
0
 public void TestEmbeddedRendererSeesEnclosing()
 {
     // st is embedded in outer; set renderer on outer, st should
     // still see it.
     StringTemplate outer = new StringTemplate(
             "X: <x>",
             typeof( AngleBracketTemplateLexer ) );
     StringTemplate st = new StringTemplate(
             "date: <created>",
             typeof( AngleBracketTemplateLexer ) );
     st.SetAttribute( "created",
                     new DateTime( 2005, 07, 05 ) );
     outer.SetAttribute( "x", st );
     outer.RegisterRenderer( typeof( DateTime ), new DateRenderer() );
     string expecting = "X: date: 2005.07.05";
     string result = outer.ToString();
     Assert.AreEqual( expecting, result );
 }