![]() |
futureLAB FriendlyTransformerException |
This is a small convenience class for Java XML/XSL developers.
The problem it tries to solve is this: Often when you are applying XSL stylesheets in your program using the JAXP API, you might encounter XML syntax errors in either the XML source or in the stylesheet source, or XSL syntax errors in the stylesheet.
In either case, you will get a Transformer exception which will contain an error message that's not too useful in most cases. The detailed error message, along with the exact line and column numbers, is usually wrapped inside the exception. This chain of enclosed exceptions can be of varying depth, and the final exception can be of different type, like SAXParseException or TransformerException, so you cannot access the location information in a uniform way.
This class is usually used in a catch {} block. It takes the caught exception as constructor argument and does all the messy work of getting at the useful information.
Make sure that you have the .jar file in your classpath and that you are importing the class or parent package in your source files.
Then catch any TransformerExceptions like this:
import ch.futurelab.xml.*;
...
try {
transformer.transform(source, result);
} catch (TransformerException te) {
throw new FriendlyTransformerException(te);
}
|
This will give you the long version of the error message which looks like this:
Unable to process because your input is invalid: Location: file:///data/www/mywebsite.com/mystylesheet.xsl Line number: 10 Column number: 76 Error message: xsl:output is not allowed in this position in the stylesheet! |
Without a FriendlyTransformerException, it would look like this:
javax.xml.transform.TransformerException: xsl:output is not allowed in this position in the stylesheet! |
Here's an example that uses the getShortMessage() method to get the compact error message string and prints it to the standard error channel:
try {
StreamSource ss = new StreamSource("file:///data/www/mywebsite.com/mystylesheet.xsl");
this.transformer = TransformerFactory.newInstance().newTransformer(ss);
} catch (TransformerConfigurationException tce) {
FriendlyTransformerException fte = new FriendlyTransformerException(tce);
System.err.println(fte.getShortMessage());
throw fte;
}
|
This is what ends up on standard error:
11:8@file:///data/www/mywebsite.com/mystylesheet.xsl: The entity name must immediately follow the '&' in the entity reference. |
Line and column numbers are separated by a : in this format. After the @ sign is the source where the error occured (if available, you won't see this or the line/column numbers with a DOMSource for example).
http://www.futurelab.ch/software/fte/FriendlyTransformerException-1.0.jar
Source code available freely upon request.
Written by Marc Liyanage. You can contact me at mliyanage@futurelab.ch