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