APIdia Launch Partner
Top Description Fields Constructors Methods
com.sun.org.apache.xml.internal.utils

public Class ListingErrorHandler

extends Object
implements ErrorHandler, ErrorListener
Class Inheritance
All Implemented Interfaces
javax.xml.transform.ErrorListener, org.xml.sax.ErrorHandler
Imports
java.io.BufferedReader, .InputStream, .InputStreamReader, .PrintWriter, java.net.MalformedURLException, .URL, .URLConnection, javax.xml.transform.ErrorListener, .SourceLocator, .TransformerException, com.sun.org.apache.xml.internal.res.XMLErrorResources, .XMLMessages, org.xml.sax.ErrorHandler, .SAXException, .SAXParseException

Sample implementation of similar SAX ErrorHandler and JAXP ErrorListener.

This implementation is suitable for various use cases, and provides some basic configuration API's as well to control when we re-throw errors, etc.

Author
[email protected]

Field Summary

Modifier and TypeField and Description
protected PrintWriter
protected boolean
throwOnError

If we should throw exception on errors; default:true.

protected boolean
throwOnFatalError

If we should throw exception on fatalErrors; default:true.

protected boolean
throwOnWarning

If we should throw exception on warnings; default:false.

Constructor Summary

AccessConstructor and Description
public
ListingErrorHandler(PrintWriter pw)

Constructor ListingErrorHandler; user-supplied PrintWriter.

public
ListingErrorHandler()

Constructor ListingErrorHandler; uses System.err.

Method Summary

Modifier and TypeMethod and Description
public void
error(SAXParseException
The error information encapsulated in a SAX parse exception.
exception
)

Implements org.xml.sax.ErrorHandler.error.

Receive notification of a recoverable error.

public void
error(TransformerException
The error information encapsulated in a transformer exception.
exception
)

Implements javax.xml.transform.ErrorListener.error.

Receive notification of a recoverable error.

public void
fatalError(SAXParseException
The error information encapsulated in a SAX parse exception.
exception
)

Implements org.xml.sax.ErrorHandler.fatalError.

Receive notification of a non-recoverable error.

public void
fatalError(TransformerException
The error information encapsulated in a transformer exception.
exception
)

Implements javax.xml.transform.ErrorListener.fatalError.

Receive notification of a non-recoverable error.

protected static String
getSourceLine(String sourceUrl, int lineNum)

Return the specific source line that caused the exception, if possible to load it; allow exceptions to be thrown.

public boolean

Returns:

if we throw an exception on errors
getThrowOnError
()

User-settable behavior: when to re-throw exceptions.

public boolean

Returns:

if we throw an exception on fatalErrors
getThrowOnFatalError
()

User-settable behavior: when to re-throw exceptions.

public boolean

Returns:

if we throw an exception on warnings
getThrowOnWarning
()

User-settable behavior: when to re-throw exceptions.

public static void
logExceptionLocation(PrintWriter
PrintWriter to send output to
pw
,
Throwable
TransformerException or SAXParseException to log information about
exception
)

Print out location information about the exception.

public static void
logSourceLine(PrintWriter
PrintWriter to send output to
pw
,
SourceLocator
Xalan wrapper for either a JAXP or a SAX source location object
locator
)

Print out the specific source line that caused the exception, if possible to load it.

private static URL
newURL(String spec)

public void
setThrowOnError(boolean
if we should throw an exception on errors
b
)

User-settable behavior: when to re-throw exceptions.

public void
setThrowOnFatalError(boolean
if we should throw an exception on fatalErrors
b
)

User-settable behavior: when to re-throw exceptions.

public void
setThrowOnWarning(boolean
if we should throw an exception on warnings
b
)

User-settable behavior: when to re-throw exceptions.

public void
warning(SAXParseException
The warning information encapsulated in a SAX parse exception.
exception
)

Implements org.xml.sax.ErrorHandler.warning.

Receive notification of a warning.

public void
warning(TransformerException
The warning information encapsulated in a transformer exception.
exception
)

Implements javax.xml.transform.ErrorListener.warning.

Receive notification of a warning.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

m_pwback to summary
protected PrintWriter m_pw
throwOnErrorback to summary
protected boolean throwOnError

If we should throw exception on errors; default:true.

throwOnFatalErrorback to summary
protected boolean throwOnFatalError

If we should throw exception on fatalErrors; default:true.

throwOnWarningback to summary
protected boolean throwOnWarning

If we should throw exception on warnings; default:false.

Constructor Detail

ListingErrorHandlerback to summary
public ListingErrorHandler(PrintWriter pw)

Constructor ListingErrorHandler; user-supplied PrintWriter.

ListingErrorHandlerback to summary
public ListingErrorHandler()

Constructor ListingErrorHandler; uses System.err.

Method Detail

errorback to summary
public void error(SAXParseException exception) throws SAXException

Implements org.xml.sax.ErrorHandler.error.

Receive notification of a recoverable error.

This corresponds to the definition of "error" in section 1.2 of the W3C XML 1.0 Recommendation. For example, a validating parser would use this callback to report the violation of a validity constraint. The default behaviour is to take no action.

The SAX parser must continue to provide normal parsing events after invoking this method: it should still be possible for the application to process the document through to the end. If the application cannot do so, then the parser should report a fatal error even if the XML 1.0 recommendation does not require it to do so.

Filters may use this method to report other, non-XML errors as well.

Parameters
exception:SAXParseException

The error information encapsulated in a SAX parse exception.

Exceptions
SAXException:
Any SAX exception, possibly wrapping another exception; only if setThrowOnErroris true.
See Also
org.xml.sax.SAXParseException
errorback to summary
public void error(TransformerException exception) throws TransformerException

Implements javax.xml.transform.ErrorListener.error.

Receive notification of a recoverable error.

The transformer must continue to try and provide normal transformation after invoking this method. It should still be possible for the application to process the document through to the end if no other errors are encountered.

Parameters
exception:TransformerException

The error information encapsulated in a transformer exception.

Exceptions
TransformerException:
only if setThrowOnError is true.
See Also
javax.xml.transform.TransformerException
fatalErrorback to summary
public void fatalError(SAXParseException exception) throws SAXException

Implements org.xml.sax.ErrorHandler.fatalError.

Receive notification of a non-recoverable error.

This corresponds to the definition of "fatal error" in section 1.2 of the W3C XML 1.0 Recommendation. For example, a parser would use this callback to report the violation of a well-formedness constraint.

The application must assume that the document is unusable after the parser has invoked this method, and should continue (if at all) only for the sake of collecting addition error messages: in fact, SAX parsers are free to stop reporting any other events once this method has been invoked.

Parameters
exception:SAXParseException

The error information encapsulated in a SAX parse exception.

Exceptions
SAXException:
Any SAX exception, possibly wrapping another exception; only if setThrowOnFatalError is true.
See Also
org.xml.sax.SAXParseException
fatalErrorback to summary
public void fatalError(TransformerException exception) throws TransformerException

Implements javax.xml.transform.ErrorListener.fatalError.

Receive notification of a non-recoverable error.

The transformer must continue to try and provide normal transformation after invoking this method. It should still be possible for the application to process the document through to the end if no other errors are encountered, but there is no guarantee that the output will be useable.

Parameters
exception:TransformerException

The error information encapsulated in a transformer exception.

Exceptions
TransformerException:
only if setThrowOnError is true.
See Also
javax.xml.transform.TransformerException
getSourceLineback to summary
protected static String getSourceLine(String sourceUrl, int lineNum) throws Exception

Return the specific source line that caused the exception, if possible to load it; allow exceptions to be thrown.

Author
[email protected]
getThrowOnErrorback to summary
public boolean getThrowOnError()

User-settable behavior: when to re-throw exceptions.

Returns:boolean

if we throw an exception on errors

getThrowOnFatalErrorback to summary
public boolean getThrowOnFatalError()

User-settable behavior: when to re-throw exceptions.

Returns:boolean

if we throw an exception on fatalErrors

getThrowOnWarningback to summary
public boolean getThrowOnWarning()

User-settable behavior: when to re-throw exceptions.

Returns:boolean

if we throw an exception on warnings

logExceptionLocationback to summary
public static void logExceptionLocation(PrintWriter pw, Throwable exception)

Print out location information about the exception. Cribbed from DefaultErrorHandler.printLocation()

Parameters
pw:PrintWriter

PrintWriter to send output to

exception:Throwable

TransformerException or SAXParseException to log information about

logSourceLineback to summary
public static void logSourceLine(PrintWriter pw, SourceLocator locator)

Print out the specific source line that caused the exception, if possible to load it.

Parameters
pw:PrintWriter

PrintWriter to send output to

locator:SourceLocator

Xalan wrapper for either a JAXP or a SAX source location object

newURLback to summary
private static URL newURL(String spec) throws MalformedURLException
Annotations
@SuppressWarnings:deprecation
setThrowOnErrorback to summary
public void setThrowOnError(boolean b)

User-settable behavior: when to re-throw exceptions.

This allows per-instance configuration of ListingErrorHandlers. You can ask us to either throw an exception when we're called for various warning / error / fatalErrors, or simply log them and continue.

Note that the behavior of many parsers/transformers after an error is not necessarily defined!

Parameters
b:boolean

if we should throw an exception on errors

setThrowOnFatalErrorback to summary
public void setThrowOnFatalError(boolean b)

User-settable behavior: when to re-throw exceptions.

This allows per-instance configuration of ListingErrorHandlers. You can ask us to either throw an exception when we're called for various warning / error / fatalErrors, or simply log them and continue.

Note that the behavior of many parsers/transformers after a fatalError is not necessarily defined, most products will probably barf if you continue.

Parameters
b:boolean

if we should throw an exception on fatalErrors

setThrowOnWarningback to summary
public void setThrowOnWarning(boolean b)

User-settable behavior: when to re-throw exceptions.

This allows per-instance configuration of ListingErrorHandlers. You can ask us to either throw an exception when we're called for various warning / error / fatalErrors, or simply log them and continue.

Parameters
b:boolean

if we should throw an exception on warnings

warningback to summary
public void warning(SAXParseException exception) throws SAXException

Implements org.xml.sax.ErrorHandler.warning.

Receive notification of a warning.

SAX parsers will use this method to report conditions that are not errors or fatal errors as defined by the XML 1.0 recommendation. The default behaviour is to take no action.

The SAX parser must continue to provide normal parsing events after invoking this method: it should still be possible for the application to process the document through to the end.

Filters may use this method to report other, non-XML warnings as well.

Parameters
exception:SAXParseException

The warning information encapsulated in a SAX parse exception.

Exceptions
SAXException:
Any SAX exception, possibly wrapping another exception; only if setThrowOnWarning is true.
See Also
org.xml.sax.SAXParseException
warningback to summary
public void warning(TransformerException exception) throws TransformerException

Implements javax.xml.transform.ErrorListener.warning.

Receive notification of a warning.

javax.xml.transform.Transformer can use this method to report conditions that are not errors or fatal errors. The default behaviour is to take no action.

After invoking this method, the Transformer must continue with the transformation. It should still be possible for the application to process the document through to the end.

Parameters
exception:TransformerException

The warning information encapsulated in a transformer exception.

Exceptions
TransformerException:
only if setThrowOnWarning is true.
See Also
javax.xml.transform.TransformerException