19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one
39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * or more contributor license agreements. See the NOTICE file
49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed with this work for additional information
59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * regarding copyright ownership. The ASF licenses this file
69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * to you under the Apache License, Version 2.0 (the  "License");
79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * you may not use this file except in compliance with the License.
89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * You may obtain a copy of the License at
99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *     http://www.apache.org/licenses/LICENSE-2.0
119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Unless required by applicable law or agreed to in writing, software
139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * See the License for the specific language governing permissions and
169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * limitations under the License.
179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * $Id: DefaultErrorHandler.java 524806 2007-04-02 15:51:39Z zongaro $
209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpackage org.apache.xml.utils;
229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport java.io.PrintStream;
249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport java.io.PrintWriter;
259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport javax.xml.transform.ErrorListener;
279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport javax.xml.transform.SourceLocator;
289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport javax.xml.transform.TransformerException;
299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.res.XMLErrorResources;
319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.res.XMLMessages;
329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.xml.sax.ErrorHandler;
349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.xml.sax.SAXException;
359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.xml.sax.SAXParseException;
369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/**
399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Implement SAX error handler for default reporting.
409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @xsl.usage general
419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpublic class DefaultErrorHandler implements ErrorHandler, ErrorListener
439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson{
449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  PrintWriter m_pw;
459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * if this flag is set to true, we will rethrow the exception on
489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * the error() and fatalError() methods. If it is false, the errors
499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * are reported to System.err.
509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  boolean m_throwExceptionOnError = true;
529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Constructor DefaultErrorHandler
559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public DefaultErrorHandler(PrintWriter pw)
579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    m_pw = pw;
599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Constructor DefaultErrorHandler
639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public DefaultErrorHandler(PrintStream pw)
659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    m_pw = new PrintWriter(pw, true);
679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Constructor DefaultErrorHandler
719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public DefaultErrorHandler()
739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    this(true);
759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Constructor DefaultErrorHandler
799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public DefaultErrorHandler(boolean throwExceptionOnError)
819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // Defer creation of a PrintWriter until it's actually needed
839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    m_throwExceptionOnError = throwExceptionOnError;
849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Retrieve <code>java.io.PrintWriter</code> to which errors are being
889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * directed.
899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @return The <code>PrintWriter</code> installed via the constructor
909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *         or the default <code>PrintWriter</code>
919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public PrintWriter getErrorWriter() {
939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // Defer creating the java.io.PrintWriter until an error needs to be
949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // reported.
959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if (m_pw == null) {
969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      m_pw = new PrintWriter(System.err, true);
979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    return m_pw;
999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Receive notification of a warning.
1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * <p>SAX parsers will use this method to report conditions that
1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * are not errors or fatal errors as defined by the XML 1.0
1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * recommendation.  The default behaviour is to take no action.</p>
1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * <p>The SAX parser must continue to provide normal parsing events
1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * after invoking this method: it should still be possible for the
1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * application to process the document through to the end.</p>
1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param exception The warning information encapsulated in a
1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *                  SAX parse exception.
1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @throws SAXException Any SAX exception, possibly
1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *            wrapping another exception.
1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public void warning(SAXParseException exception) throws SAXException
1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    PrintWriter pw = getErrorWriter();
1209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    printLocation(pw, exception);
1229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    pw.println("Parser warning: " + exception.getMessage());
1239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
1269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Receive notification of a recoverable error.
1279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * <p>This corresponds to the definition of "error" in section 1.2
1299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * of the W3C XML 1.0 Recommendation.  For example, a validating
1309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * parser would use this callback to report the violation of a
1319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * validity constraint.  The default behaviour is to take no
1329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * action.</p>
1339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * <p>The SAX parser must continue to provide normal parsing events
1359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * after invoking this method: it should still be possible for the
1369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * application to process the document through to the end.  If the
1379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * application cannot do so, then the parser should report a fatal
1389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * error even if the XML 1.0 recommendation does not require it to
1399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * do so.</p>
1409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param exception The error information encapsulated in a
1429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *                  SAX parse exception.
1439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @throws SAXException Any SAX exception, possibly
1449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *            wrapping another exception.
1459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
1469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public void error(SAXParseException exception) throws SAXException
1479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
1489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    //printLocation(exception);
1499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // getErrorWriter().println(exception.getMessage());
1509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    throw exception;
1529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
1559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Receive notification of a non-recoverable error.
1569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * <p>This corresponds to the definition of "fatal error" in
1589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * section 1.2 of the W3C XML 1.0 Recommendation.  For example, a
1599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * parser would use this callback to report the violation of a
1609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * well-formedness constraint.</p>
1619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * <p>The application must assume that the document is unusable
1639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * after the parser has invoked this method, and should continue
1649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * (if at all) only for the sake of collecting addition error
1659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * messages: in fact, SAX parsers are free to stop reporting any
1669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * other events once this method has been invoked.</p>
1679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param exception The error information encapsulated in a
1699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *                  SAX parse exception.
1709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @throws SAXException Any SAX exception, possibly
1719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *            wrapping another exception.
1729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
1739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public void fatalError(SAXParseException exception) throws SAXException
1749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
1759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // printLocation(exception);
1769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // getErrorWriter().println(exception.getMessage());
1779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    throw exception;
1799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
1829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Receive notification of a warning.
1839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * <p>SAX parsers will use this method to report conditions that
1859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * are not errors or fatal errors as defined by the XML 1.0
1869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * recommendation.  The default behaviour is to take no action.</p>
1879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * <p>The SAX parser must continue to provide normal parsing events
1899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * after invoking this method: it should still be possible for the
1909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * application to process the document through to the end.</p>
1919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param exception The warning information encapsulated in a
1939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *                  SAX parse exception.
1949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @throws javax.xml.transform.TransformerException Any SAX exception, possibly
1959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *            wrapping another exception.
1969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @see javax.xml.transform.TransformerException
1979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
1989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public void warning(TransformerException exception) throws TransformerException
1999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
2009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    PrintWriter pw = getErrorWriter();
2019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    printLocation(pw, exception);
2039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    pw.println(exception.getMessage());
2049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
2059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
2079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Receive notification of a recoverable error.
2089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
2099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * <p>This corresponds to the definition of "error" in section 1.2
2109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * of the W3C XML 1.0 Recommendation.  For example, a validating
2119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * parser would use this callback to report the violation of a
2129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * validity constraint.  The default behaviour is to take no
2139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * action.</p>
2149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
2159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * <p>The SAX parser must continue to provide normal parsing events
2169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * after invoking this method: it should still be possible for the
2179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * application to process the document through to the end.  If the
2189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * application cannot do so, then the parser should report a fatal
2199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * error even if the XML 1.0 recommendation does not require it to
2209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * do so.</p>
2219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
2229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param exception The error information encapsulated in a
2239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *                  SAX parse exception.
2249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @throws javax.xml.transform.TransformerException Any SAX exception, possibly
2259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *            wrapping another exception.
2269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @see javax.xml.transform.TransformerException
2279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
2289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public void error(TransformerException exception) throws TransformerException
2299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
2309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // If the m_throwExceptionOnError flag is true, rethrow the exception.
2319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // Otherwise report the error to System.err.
2329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if (m_throwExceptionOnError)
2339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      throw exception;
2349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    else
2359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
2369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      PrintWriter pw = getErrorWriter();
2379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      printLocation(pw, exception);
2399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      pw.println(exception.getMessage());
2409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
2419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
2429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
2449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Receive notification of a non-recoverable error.
2459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
2469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * <p>This corresponds to the definition of "fatal error" in
2479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * section 1.2 of the W3C XML 1.0 Recommendation.  For example, a
2489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * parser would use this callback to report the violation of a
2499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * well-formedness constraint.</p>
2509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
2519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * <p>The application must assume that the document is unusable
2529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * after the parser has invoked this method, and should continue
2539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * (if at all) only for the sake of collecting addition error
2549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * messages: in fact, SAX parsers are free to stop reporting any
2559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * other events once this method has been invoked.</p>
2569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
2579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param exception The error information encapsulated in a
2589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *                  SAX parse exception.
2599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @throws javax.xml.transform.TransformerException Any SAX exception, possibly
2609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *            wrapping another exception.
2619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @see javax.xml.transform.TransformerException
2629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
2639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public void fatalError(TransformerException exception) throws TransformerException
2649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
2659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // If the m_throwExceptionOnError flag is true, rethrow the exception.
2669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // Otherwise report the error to System.err.
2679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if (m_throwExceptionOnError)
2689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      throw exception;
2699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    else
2709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
2719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      PrintWriter pw = getErrorWriter();
2729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      printLocation(pw, exception);
2749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      pw.println(exception.getMessage());
2759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
2769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
2779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public static void ensureLocationSet(TransformerException exception)
2799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
2809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // SourceLocator locator = exception.getLocator();
2819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    SourceLocator locator = null;
2829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    Throwable cause = exception;
2839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // Try to find the locator closest to the cause.
2859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    do
2869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
2879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      if(cause instanceof SAXParseException)
2889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      {
2899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        locator = new SAXSourceLocator((SAXParseException)cause);
2909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      }
2919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      else if (cause instanceof TransformerException)
2929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      {
2939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        SourceLocator causeLocator = ((TransformerException)cause).getLocator();
2949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        if(null != causeLocator)
2959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          locator = causeLocator;
2969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      }
2979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      if(cause instanceof TransformerException)
2999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        cause = ((TransformerException)cause).getCause();
3009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      else if(cause instanceof SAXException)
3019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        cause = ((SAXException)cause).getException();
3029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      else
3039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        cause = null;
3049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
3059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    while(null != cause);
3069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    exception.setLocator(locator);
3089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
3099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public static void printLocation(PrintStream pw, TransformerException exception)
3119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
3129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    printLocation(new PrintWriter(pw), exception);
3139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
3149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public static void printLocation(java.io.PrintStream pw, org.xml.sax.SAXParseException exception)
3169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
3179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    printLocation(new PrintWriter(pw), exception);
3189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
3199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public static void printLocation(PrintWriter pw, Throwable exception)
3219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
3229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    SourceLocator locator = null;
3239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    Throwable cause = exception;
3249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // Try to find the locator closest to the cause.
3269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    do
3279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
3289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      if(cause instanceof SAXParseException)
3299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      {
3309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        locator = new SAXSourceLocator((SAXParseException)cause);
3319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      }
3329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      else if (cause instanceof TransformerException)
3339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      {
3349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        SourceLocator causeLocator = ((TransformerException)cause).getLocator();
3359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        if(null != causeLocator)
3369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          locator = causeLocator;
3379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      }
3389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      if(cause instanceof TransformerException)
3399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        cause = ((TransformerException)cause).getCause();
3409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      else if(cause instanceof WrappedRuntimeException)
3419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        cause = ((WrappedRuntimeException)cause).getException();
3429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      else if(cause instanceof SAXException)
3439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        cause = ((SAXException)cause).getException();
3449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      else
3459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        cause = null;
3469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
3479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    while(null != cause);
3489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if(null != locator)
3509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
3519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      // getErrorWriter().println("Parser fatal error: "+exception.getMessage());
3529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      String id = (null != locator.getPublicId() )
3539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                  ? locator.getPublicId()
3549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    : (null != locator.getSystemId())
3559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                      ? locator.getSystemId() : XMLMessages.createXMLMessage(XMLErrorResources.ER_SYSTEMID_UNKNOWN, null); //"SystemId Unknown";
3569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      pw.print(id + "; " +XMLMessages.createXMLMessage("line", null) + locator.getLineNumber()
3589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                         + "; " +XMLMessages.createXMLMessage("column", null) + locator.getColumnNumber()+"; ");
3599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
3609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    else
3619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      pw.print("("+XMLMessages.createXMLMessage(XMLErrorResources.ER_LOCATION_UNKNOWN, null)+")");
3629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
3639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
364