001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.xml;
029
030import org.opencms.main.CmsLog;
031import org.opencms.util.CmsStringUtil;
032
033import org.apache.commons.logging.Log;
034
035import org.xml.sax.ErrorHandler;
036import org.xml.sax.SAXException;
037import org.xml.sax.SAXParseException;
038
039/**
040 * Error hander for writing errors found during XML validation to the OpenCms log.<p>
041 *
042 * Exceptions caused by warnings are suppressed (but written to the log if level is set to WARN).<p>
043 *
044 * @since 6.0.0
045 */
046public class CmsXmlErrorHandler implements ErrorHandler {
047
048    /** The log object for this class. */
049    private static final Log LOG = CmsLog.getLog(CmsXmlErrorHandler.class);
050
051    /** The name of the resource that is parsed, for logging (optional). */
052    private String m_resourceName;
053
054    /**
055     * Creates an OpenCms XML error handler.<p>
056     */
057    public CmsXmlErrorHandler() {
058
059        this("");
060    }
061
062    /**
063     * Creates an OpenCms XML error handler with a resource name for error logging.<p>
064     *
065     * @param resourceName the name (path) of the XML resource that is handled, for logging
066     */
067    public CmsXmlErrorHandler(String resourceName) {
068
069        if (!CmsStringUtil.isEmptyOrWhitespaceOnly(resourceName)) {
070            m_resourceName = " " + resourceName;
071        } else {
072            m_resourceName = "";
073        }
074    }
075
076    /**
077     * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
078     */
079    public void error(SAXParseException exception) throws SAXException {
080
081        LOG.error(Messages.get().getBundle().key(Messages.LOG_PARSING_XML_RESOURCE_ERROR_1, m_resourceName), exception);
082        throw exception;
083    }
084
085    /**
086     * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
087     */
088    public void fatalError(SAXParseException exception) throws SAXException {
089
090        LOG.error(
091            Messages.get().getBundle().key(Messages.LOG_PARSING_XML_RESOURCE_FATAL_ERROR_1, m_resourceName),
092            exception);
093        throw exception;
094    }
095
096    /**
097     * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
098     */
099    public void warning(SAXParseException exception) {
100
101        if (LOG.isWarnEnabled()) {
102            LOG.error(
103                Messages.get().getBundle().key(Messages.LOG_PARSING_XML_RESOURCE_WARNING_1, m_resourceName),
104                exception);
105        }
106    }
107}