FileDocCategorySizeDatePackage
DefaultErrorHandler.javaAPI DocJava SE 6 API11662Tue Jun 10 00:23:10 BST 2008com.sun.org.apache.xml.internal.utils

DefaultErrorHandler

public class DefaultErrorHandler extends Object implements ErrorListener, ErrorHandler
Implement SAX error handler for default reporting.
xsl.usage
general

Fields Summary
PrintWriter
m_pw
boolean
m_throwExceptionOnError
if this flag is set to true, we will rethrow the exception on the error() and fatalError() methods. If it is false, the errors are reported to System.err.
Constructors Summary
public DefaultErrorHandler(PrintWriter pw)
Constructor DefaultErrorHandler


       
    
  
    m_pw = pw;
  
public DefaultErrorHandler(PrintStream pw)
Constructor DefaultErrorHandler

    m_pw = new PrintWriter(pw, true);
  
public DefaultErrorHandler()
Constructor DefaultErrorHandler

    this(true);
  
public DefaultErrorHandler(boolean throwExceptionOnError)
Constructor DefaultErrorHandler

    m_pw = new PrintWriter(System.err, true);
    m_throwExceptionOnError = throwExceptionOnError;
  
Methods Summary
public static voidensureLocationSet(javax.xml.transform.TransformerException exception)

    // SourceLocator locator = exception.getLocator();
    SourceLocator locator = null;
    Throwable cause = exception;
    
    // Try to find the locator closest to the cause.
    do
    {
      if(cause instanceof SAXParseException)
      {
        locator = new SAXSourceLocator((SAXParseException)cause);
      }
      else if (cause instanceof TransformerException)
      {
        SourceLocator causeLocator = ((TransformerException)cause).getLocator();
        if(null != causeLocator)
          locator = causeLocator;
      }
      
      if(cause instanceof TransformerException)
        cause = ((TransformerException)cause).getCause();
      else if(cause instanceof SAXException)
        cause = ((SAXException)cause).getException();
      else
        cause = null;
    }
    while(null != cause);
    
    exception.setLocator(locator);
  
public voiderror(org.xml.sax.SAXParseException exception)
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.

param
exception The error information encapsulated in a SAX parse exception.
throws
SAXException Any SAX exception, possibly wrapping another exception.

    //printLocation(exception);
    // m_pw.println(exception.getMessage());

    throw exception;
  
public voiderror(javax.xml.transform.TransformerException exception)
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.

param
exception The error information encapsulated in a SAX parse exception.
throws
javax.xml.transform.TransformerException Any SAX exception, possibly wrapping another exception.
see
javax.xml.transform.TransformerException

    // If the m_throwExceptionOnError flag is true, rethrow the exception.
    // Otherwise report the error to System.err.
    if (m_throwExceptionOnError)
      throw exception;
    else
    {
      printLocation(m_pw, exception);
      m_pw.println(exception.getMessage());
    }
  
public voidfatalError(javax.xml.transform.TransformerException exception)
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.

param
exception The error information encapsulated in a SAX parse exception.
throws
javax.xml.transform.TransformerException Any SAX exception, possibly wrapping another exception.
see
javax.xml.transform.TransformerException

    // If the m_throwExceptionOnError flag is true, rethrow the exception.
    // Otherwise report the error to System.err.
    if (m_throwExceptionOnError)
      throw exception;
    else
    {
      printLocation(m_pw, exception);
      m_pw.println(exception.getMessage());
    }
  
public voidfatalError(org.xml.sax.SAXParseException exception)
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.

param
exception The error information encapsulated in a SAX parse exception.
throws
SAXException Any SAX exception, possibly wrapping another exception.

    // printLocation(exception);
    // m_pw.println(exception.getMessage());

    throw exception;
  
public static voidprintLocation(java.io.PrintStream pw, javax.xml.transform.TransformerException exception)

    printLocation(new PrintWriter(pw), exception);
  
public static voidprintLocation(java.io.PrintStream pw, org.xml.sax.SAXParseException exception)

    printLocation(new PrintWriter(pw), exception);
  
public static voidprintLocation(java.io.PrintWriter pw, java.lang.Throwable exception)

    SourceLocator locator = null;
    Throwable cause = exception;
    
    // Try to find the locator closest to the cause.
    do
    {
      if(cause instanceof SAXParseException)
      {
        locator = new SAXSourceLocator((SAXParseException)cause);
      }
      else if (cause instanceof TransformerException)
      {
        SourceLocator causeLocator = ((TransformerException)cause).getLocator();
        if(null != causeLocator)
          locator = causeLocator;
      }
      if(cause instanceof TransformerException)
        cause = ((TransformerException)cause).getCause();
      else if(cause instanceof WrappedRuntimeException)
        cause = ((WrappedRuntimeException)cause).getException();
      else if(cause instanceof SAXException)
        cause = ((SAXException)cause).getException();
      else
        cause = null;
    }
    while(null != cause);
        
    if(null != locator)
    {
      // m_pw.println("Parser fatal error: "+exception.getMessage());
      String id = (null != locator.getPublicId() )
                  ? locator.getPublicId()
                    : (null != locator.getSystemId())
                      ? locator.getSystemId() : XMLMessages.createXMLMessage(XMLErrorResources.ER_SYSTEMID_UNKNOWN, null); //"SystemId Unknown";

      pw.print(id + "; " +XMLMessages.createXMLMessage("line", null) + locator.getLineNumber()
                         + "; " +XMLMessages.createXMLMessage("column", null) + locator.getColumnNumber()+"; ");
    }
    else
      pw.print("("+XMLMessages.createXMLMessage(XMLErrorResources.ER_LOCATION_UNKNOWN, null)+")");
  
public voidwarning(org.xml.sax.SAXParseException exception)
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.

param
exception The warning information encapsulated in a SAX parse exception.
throws
SAXException Any SAX exception, possibly wrapping another exception.

    printLocation(m_pw, exception);
    m_pw.println("Parser warning: " + exception.getMessage());
  
public voidwarning(javax.xml.transform.TransformerException exception)
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.

param
exception The warning information encapsulated in a SAX parse exception.
throws
javax.xml.transform.TransformerException Any SAX exception, possibly wrapping another exception.
see
javax.xml.transform.TransformerException

    printLocation(m_pw, exception);

    m_pw.println(exception.getMessage());