3

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?

Zibbobz
  • 725
  • 1
  • 15
  • 41
  • Can you do all the appending with a `StringBuilder` and *then* create the `StringReader` out of that? – arshajii Jul 24 '13 at 13:50
  • Maybe, but I seem to be having trouble importing StringBuilder into my code. Is it "Import java.lang.StringBuilder;"? It doesn't seem to work, so maybe we don't have support for it. – Zibbobz Jul 24 '13 at 14:15
  • You shouldn't need to import `StringBuilder` (or anything from `java.lang` for that matter). – arshajii Jul 24 '13 at 14:15
  • Odd...I'm trying to construct a StringBuilder object, but my developer (WAS 5) says "StringBuilder cannot be resolved or is not a type". StringBuilder bob = new StringBuilder(); – Zibbobz Jul 24 '13 at 14:21
  • I guess your using a Java version less than 5. You can try `StringBuffer` instead. – arshajii Jul 24 '13 at 14:21
  • Probably stability reasons. StringBuffer seems to work for holding the data, but I can't figure out how to get it into StringReader, or InputSource. – Zibbobz Jul 24 '13 at 14:25

1 Answers1

2

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.

arshajii
  • 127,459
  • 24
  • 238
  • 287