The assignment my students have to complete has functionality that involves outputing an XML Document into a file. So nice of the DOM folks to NOT have that functionality in the Document object, but good enough, we have the Transformer object which can do just that.
Using the Transformer object’s transform() method that allows you to send the result of a transformation into an output stream – a file.
The students were asked to save the XML to a file in the home folder of the person grading the assignment – a property accessible by using the Java method:
System.getProperty("user.home")
In Windows, the property is mapped to:
[DRIVE]:\Documents and Settings\[USER NAME]
“Documents and Settings” have spaces between the words ‘Documents’, ‘and’ and ‘Settings’. That, is, an, issue, if, you, are, using JBoss on Windows – why?
Let’s go back to the Transformer: we are supposed to be able to use this code…
File file = new File([PATH WITH SPACES]);
Result result = new StreamResult(file);
transformer.transform([source xml], result);
On JBoss something this innocuous will produce a FileNotFoundException
, saying it cannot find the file at [DRIVE]:\Documents%20and%20Settings\[USER NAME]
– the file location is escaped as if the file location was a URL. This is a problem and appaerently there is/was an open bug on the JBoss JIRA server. JBoss 4.0.1sp1 still does not have it resolved and the issue is apparently rooted in the FileURLConnection
that handles files in JBoss. Sorta uncool, eh?
Solution:
Instead of going the file route directly, how about we use a stream instread:
PrintWriter outStream = new PrintWriter(new FileOutputStream([FILE_LOCATION]));
StreamResult fileResult = new StreamResult(outStream);
transformer.transform(source, fileResult);
Hope this helps!