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

patterntesting.runtime.xml.SAXHelper Maven / Gradle / Ivy

Go to download

PatternTesting Runtime (patterntesting-rt) is the runtime component for the PatternTesting framework. It provides the annotations and base classes for the PatternTesting testing framework (e.g. patterntesting-check, patterntesting-concurrent or patterntesting-exception) but can be also used standalone for classpath monitoring or profiling. It uses AOP and AspectJ to perform this feat.

The newest version!
/*
 * $Id: SAXHelper.java,v 1.4 2009/12/19 22:34:09 oboehm Exp $
 *
 * Copyright (c) 2008 by Oliver Boehm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * (c)reated 17.12.2008 by oliver ([email protected])
 */
package patterntesting.runtime.xml;

import java.io.*;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;
import org.xml.sax.SAXException;

/**
 * The class SAXHelper was introduced to be able to provide better
 * SAXExceptions.
 *
 * @author oliver
 * @since 17.12.2008
 * @version $Revision: 1.4 $
 */
public class SAXHelper {

    private static final Log log = LogFactoryImpl.getLog(SAXHelper.class);

    /** The Constant DRIVER_PROPERTY. */
    public static final String DRIVER_PROPERTY = "org.xml.sax.driver";

    /** The Constant DRIVER_RESOURCE. */
    public static final String DRIVER_RESOURCE = "META-INF/services/" + DRIVER_PROPERTY;

    /**
     * Better sax exception.
     *
     * @param saxe the saxe
     *
     * @return the sAX exception
     */
    public static SAXException betterSAXException(SAXException saxe) {
        String msg = saxe.getMessage();
        String className = parseMessage(msg);
        if (className == null) {
            if (log.isTraceEnabled()) {
                log.trace("unknown message '" + msg + "' - returning " + saxe
                        + "...");
            }
            return saxe;
        }
        if (isDriverPropertySetWith(className)) {
            if (log.isTraceEnabled()) {
                log.trace("conflict with property " + DRIVER_PROPERTY + "="
                        + className + " detected");
            }
            msg = msg + " - check property \"" + DRIVER_PROPERTY + "\"";
            return betterSAXException(saxe, msg);
        } else if (isDriverResourceSetWith(className)) {
            if (log.isTraceEnabled()) {
                log.trace("conflict with resource " + DRIVER_RESOURCE + "="
                        + className + " detected");
            }
            msg = msg + " - check system resource \"" + DRIVER_RESOURCE + "\"";
            return betterSAXException(saxe, msg);
        } else {
            return saxe;
        }
    }

    /**
     * Better sax exception.
     *
     * @param saxe the saxe
     * @param newMessage the new message
     *
     * @return the sAX exception
     */
    public static SAXException betterSAXException(SAXException saxe, String newMessage) {
        Exception cause = saxe.getException();
        SAXException betterException = new SAXException(newMessage, cause);
        betterException.setStackTrace(saxe.getStackTrace());
        return betterException;
    }

    private static String parseMessage(String msg) {
        String prefix = "SAX2 driver class ";
        if (msg.startsWith(prefix)) {
            return StringUtils.substringBetween(msg, prefix, " ");
        } else {
            return null;
        }
    }

    private static boolean isDriverPropertySetWith(String className) {
        String prop = System.getProperty(DRIVER_PROPERTY);
        return className.equals(prop);
    }

    private static boolean isDriverResourceSetWith(String className) {
        InputStream in = ClassLoader.getSystemResourceAsStream(DRIVER_RESOURCE);
        if (in == null) {
            return false;
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    in, "UTF8"));
            String driver = reader.readLine();
            if (StringUtils.isBlank(driver)) {
                log.warn("no classname found in " + DRIVER_RESOURCE);
                return false;
            }
            return driver.equals(className);
        } catch (IOException ioe) {
            log.warn("can't read " + DRIVER_RESOURCE + " (" + ioe + ")");
            return false;
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

}

/**
 * $Log: SAXHelper.java,v $
 * Revision 1.4  2009/12/19 22:34:09  oboehm
 * trailing spaces removed
 *
 * Revision 1.3  2009/09/25 14:49:44  oboehm
 * javadocs completed with the help of JAutodoc
 *
 * Revision 1.2  2008/12/18 08:17:27  oboehm
 * checking now also the driver name as system resource
 *
 * Revision 1.1  2008/12/17 22:09:23  oboehm
 * beginning XML/SAX support
 *
 * $Source: /cvsroot/patterntesting/PatternTesting08/patterntesting-rt/src/main/java/patterntesting/runtime/xml/SAXHelper.java,v $
 */




© 2015 - 2024 Weber Informatics LLC | Privacy Policy