I'm not understanding what happens to the StreamResult instance. I see that the Transformer object receives source and streamResult:
transformer.transform(source, streamResult);
this transforms source into streamResult? It seems odd that there's nothing returned from this operation yet streamResult now has, for lack of a better word, data. Is it xml data?
package helloWorldSaxon;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Properties;
import java.util.logging.Logger;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
public class App {
private static final Logger LOG = Logger.getLogger(App.class.getName());
private final Properties properties = new Properties();
public static void main(String[] args) throws TransformerException, TransformerConfigurationException, IOException, SAXException {
LOG.fine("starting..");
new App().identyTransformOnURL();
}
private void identyTransformOnURL() throws TransformerConfigurationException, TransformerException, IOException, SAXException {
properties.loadFromXML(App.class.getResourceAsStream("/saxon.xml"));
String url = properties.getProperty("url");
StringWriter writer = new StringWriter();
StreamResult streamResult = new StreamResult(writer);
TransformerFactory factory = TransformerFactory.newInstance();
XMLReader xmlReader = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser");
Source source = new SAXSource(xmlReader, new InputSource(url));
Transformer transformer = factory.newTransformer();
transformer.transform(source, streamResult);
String stringResult = writer.toString();
LOG.info(stringResult);
}
}
I don't understand the JavaDoc on this method:
Transform the XML Source to a Result.
Wouldn't it be more intuitive to return a result?