[stringtemplate-interest] Bug/Problem passing parameters into iteration

Mike Goodwin mike.goodwin at cantab.net
Tue Nov 2 16:55:31 PDT 2010


Hello List,

On the StringTemplate home page it says "Bug reports can be found and
filed via StringTemplate's JIRA", unfortunately it seems that account
registration is not possible, so I am not sure about the accuracy of
that statement. (For me it says contact the list of administrators,
but then the list of administrators it suggests is empty).

I have encountered what I believe to be a bug (source+output appended
below). For me the object Model has its toString() method called an
formed as part of the call to the template element. I cannot quite see
why this would be the case. Removing all references to indent make
this issue go away (but remove the indentation). I cannot see another
way that I would be able to pass arguments in to the iteration. (I can
work around it by using a field, and calculating it in the model, but
this is not ideal).

I am using StringTemplate 3.2.1.

regards,

Mike




----------------------- PokeStringTemplate.java ----------------------

package poke.stringtemplate;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.antlr.stringtemplate.language.DefaultTemplateLexer;

public class PokeStringTemplate {
	
	static public class Model{
		final String name;
		final List<Model> children = new ArrayList();;
		Model(Model parent, String name){
			if(parent!=null) parent.children.add(this);
			this.name = name;
		}
		public String getName(){ return name; }
		public List<Model> getChildren(){ return children; }
		@Override public String toString() {
			return "Do not want to see this!";
		}
	}

	
	static public void main(String[] args) throws IOException {
		
		Model a = new Model(null,"a");
		Model a_b = new Model(a,"b");
		Model a_c = new Model(a,"c");
		Model a_b_d = new Model(a_b,"d");
		Model a_b_e = new Model(a_b,"e");
		
		StringTemplate t = getTemplate("tree.stg", "tree");
		t.setAttribute("root", a);
		File out = new File("tree.html");
		stringToFile(t+"", out);
		
	}

	static private void stringToFile(String s, File f) throws IOException{
		FileWriter w = new FileWriter(f);
		w.append(s);
		w.close();
	}
	
    static private StringTemplateGroup getTemplateGroup(String name)
throws IOException{
    	URL url = PokeStringTemplate.class.getResource(name);
    	InputStream is = url.openStream();
    	return new StringTemplateGroup(new InputStreamReader(is),
DefaultTemplateLexer.class);
    	
    }

    static private StringTemplate getTemplate(String file, String
name) throws IOException{
    	return getTemplateGroup(file).getInstanceOf(name);
    }
}


------------------ tree.stg ---------------------------------

group xmldoc;

element(indent)::=<<
$indent$$it.name$<br>
$indent$$it.children:element(indent=indent+"&nbsp;&nbsp;")$
>>

elementTop()::=<<
$it.name$<br>
$it.children:element(indent="&nbsp;&nbsp;")$
>>

tree(root)::=<<
<html>
	<body>
		<code>
$root:elementTop()$
		</code>
	</body>
</html>
>>


------------------ output (copied from rendered html)
---------------------------------
a
  b
  Do not want to see this!  d
Do not want to see this!  Do not want to see this!  e
Do not want to see this!    c


More information about the stringtemplate-interest mailing list