Search code examples
javainputstreamstringreader

Append StringReader For Multiple Entries


I'm creating a program in Java that reads a string of XSL-FO code, populates the empty fields with app data, and adds it to a StringReader, to eventually be set as an InputSource for a web dispatcher.

I already have the code to find and populate the blank template, and now I need to loop through the template X number of times to create X instances of the same document, put together all as a single document.

Psuedocode:

StringReader reader = new StringReader();

for (Iterator i = Object.iterator(); i.hasNext();
{
Object o = (Object) i.next();
reader.append(populateObject(o);
}
InputSource isource = new InputSource(reader);

StringReader, however, doesn't have an append function, and probably isn't meant to have one either. So, how can I create an InputSource that will satisfy the need to have a full, accurate reference to my XML code, that can be read by an InputSource object?


Solution

  • You could try doing all the appending before-hand:

    StringBuilder sb = new StringBuilder();
    
    for (...)
        sb.append(populateObject(obj));
    
    StringReader reader = new StringReader(sb.toString());
    

    Use StringBuffer if you're using a Java version below 5.