[stringtemplate-interest] Generating big documents

Adam Bennett adamb at videx.com
Wed Dec 27 15:03:08 PST 2006


I think this gets me close, Nate.  I did not realize 
that StringTemplate treats iterators as multi-valued attributes.  Nice.  
I could create an Iterator to wrap a JDBC ResultSet.  One problem 
however: I think StringTemplate is evaluating the iterator all at once 
(as opposed to iteratively).  For instance, this code causes an 
OutOfMemoryError:

public class Play
{
  public static void main(String[] args)
  {
    StringTemplate template = new StringTemplate("$data:{$i$ == $it$\n}$");
    template.setAttribute("data", new InfiniteItr());
   
    try
    {
      template.write(new DummyWriter());
    }
    catch (java.io.IOException ex)
    {
      ex.printStackTrace();
    }
   }
 
  /**an infinite sequence of integers*/
  static class InfiniteItr implements Iterator<Integer>
  {
    private int i = 0;
    public boolean hasNext() {return true;}
    public Integer next(){return new Integer(i++); }
    public void remove(){}
  }
 
  /**a StringTemplateWriter that discards all output*/
  static public class DummyWriter
    implements StringTemplateWriter
  {
    public void popAnchorPoint() {}
    public String popIndentation() {return "";}
    public void pushAnchorPoint(){}
    public void pushIndentation(String string){}
    public void setLineWidth(int _int){}
    public int write(String string)
      throws IOException
    {
      return string.length();
    }
    public int write(String string, String string1)
      throws IOException
    {
      return string.length();
    }
    public int writeSeparator(String string)
      throws IOException
    {
      return string.length();
    }
    public int writeWrapSeparator(String string)
      throws IOException
    {
      return string.length();
    }
  } 
}

    ------------------------------------------------------------------------
    *From:* Nate [mailto:misc at n4te.com <mailto:misc at n4te.com>]
    *To:* Adam Bennett [mailto:adamb at videx.com <mailto:adamb at videx.com>]
    *Cc:* stringtemplate-interest at antlr.org
    <mailto:stringtemplate-interest at antlr.org>
    *Sent:* Wed, 27 Dec 2006 11:04:39 -0800
    *Subject:* Re: [stringtemplate-interest] Generating big documents

    You could try something like this...

    ArrayList list = new ArrayList();
    list.add("10");
    list.add("20");
    list.add("30");
    StringTemplate template = new StringTemplate("$data:{$i$ == $it$\n}$");
    template.setAttribute("data", list.iterator());
    System.out.println(template.toString());

    Outputs...

    1 == 10
    2 == 20
    3 == 30

    -Nate


    Adam Bennett wrote:
     > Our web application has the ability to generate reports as HTML
    or XML
     > documents. These documents are potentially very large (20MB with
    some
     > of our test data). Early on we had problems with the server running
     > out of memory while generating these because all the data was
    brought
     > into memory from the database. Our solution was to stream the
    results
     > from the database while generating the document.
     >
     > Can StringTemplate be used to generate such documents? I cannot see
     > how to avoid bringing all the data into memory because
    StringTemplate
     > requires all data to be passed as an attribute.
     >
    ------------------------------------------------------------------------
     >
     > _______________________________________________
     > stringtemplate-interest mailing list
     > stringtemplate-interest at antlr.org
    <mailto:stringtemplate-interest at antlr.org>
     > http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
     >

 
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20061227/41d38822/attachment.html 


More information about the stringtemplate-interest mailing list