All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sta.cts.MyErrorHandler Maven / Gradle / Ivy

/*
 * The Apache Software License, Version 1.1
 *
 *
 * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Xalan" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact [email protected].
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation and was
 * originally based on software copyright (c) 1999, Lotus
 * Development Corporation., http://www.lotus.com.  For more
 * information on the Apache Software Foundation, please see
 * .
 */

package com.sta.cts;

import java.io.PrintStream;
import java.io.PrintWriter;

import javax.xml.transform.ErrorListener;
import javax.xml.transform.SourceLocator;
import javax.xml.transform.TransformerException;

import org.apache.xml.res.XMLErrorResources;
import org.apache.xml.res.XMLMessages;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import org.apache.xml.utils.SAXSourceLocator;
import org.apache.xml.utils.WrappedRuntimeException;

/**
 * 

Name: MyErrorHandler

*

Description: Implement SAX error handler for default reporting. *

*

Copyright: Copyright (c) 20xx, 2016-2019, 2021

*

Company: >StA-Soft<

* @author StA * @version 1.0 */ public class MyErrorHandler implements ErrorHandler, ErrorListener { /** * Ein PrintWriter. */ private PrintWriter m_pw; /** * Constructor MyErrorHandler. * @param pw PrintWriter */ public MyErrorHandler(PrintWriter pw) { m_pw = pw; } /** * Constructor MyErrorHandler. * @param pw PrintStream */ public MyErrorHandler(PrintStream pw) { m_pw = new PrintWriter(pw, true); } /** * Constructor MyErrorHandler. */ public MyErrorHandler() { m_pw = new PrintWriter(System.err, true); } @Override public void warning(SAXParseException exception) throws SAXException { printLocation(m_pw, exception); m_pw.println("Parser warning: " + exception.getMessage()); } @Override public void error(SAXParseException exception) throws SAXException { // printLocation(exception); // m_pw.println(exception.getMessage()); throw exception; } @Override public void fatalError(SAXParseException exception) throws SAXException { // printLocation(exception); // m_pw.println(exception.getMessage()); throw exception; } @Override public void warning(TransformerException exception) throws TransformerException { printLocation(m_pw, exception); m_pw.println(exception.getMessage()); } @Override public void error(TransformerException exception) throws TransformerException { // printLocation(exception); // ensureLocationSet(exception); throw exception; } @Override public void fatalError(TransformerException exception) throws TransformerException { // printLocation(exception); // ensureLocationSet(exception); throw exception; } /** * Pr?fung. * @param exception Exception */ public static void ensureLocationSet(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 (causeLocator != null) { locator = causeLocator; } } if (cause instanceof TransformerException) { cause = cause.getCause(); } else if (cause instanceof SAXException) { cause = ((SAXException) cause).getException(); } else { cause = null; } } while (cause != null); exception.setLocator(locator); } /** * Location ausgeben. * @param ps Print-Stream * @param exception Exception */ public static void printLocation(PrintStream ps, TransformerException exception) { printLocation(new PrintWriter(ps), exception); } /** * Location ausgeben. * @param ps Print-Stream * @param exception Exception */ public static void printLocation(PrintStream ps, SAXParseException exception) { printLocation(new PrintWriter(ps), exception); } /** * Location ausgeben. * @param pw Print-Writer * @param exception Exception */ public static void printLocation(PrintWriter pw, 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 (causeLocator != null) { locator = causeLocator; } } if (cause instanceof TransformerException) { cause = cause.getCause(); } else if (cause instanceof WrappedRuntimeException) { cause = ((WrappedRuntimeException) cause).getException(); } else if (cause instanceof SAXException) { cause = ((SAXException) cause).getException(); } else { cause = null; } } while (cause != null); if (locator != null) { // m_pw.println("Parser fatal error: "+exception.getMessage()); String id = locator.getPublicId() != null ? locator.getPublicId() : locator.getSystemId() != null ? 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()+"; "); /* try { URL url = new URL(id); id = new File(url.getFile()).getName(); } catch (Exception ex) { } pw.print(" " + ConvTools.formatStringR(id + " (" + locator.getLineNumber() + "):", 30) + " "); */ } else { pw.print("(" + XMLMessages.createXMLMessage(XMLErrorResources.ER_LOCATION_UNKNOWN, null) + ")"); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy