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.util.CmsStringUtil;
031
032import org.dom4j.DocumentHelper;
033import org.dom4j.Element;
034import org.dom4j.util.XMLErrorHandler;
035import org.xml.sax.SAXParseException;
036
037/**
038 * Error hander for writing errors found during XML validation to the OpenCms log.<p>
039 *
040 * Exceptions caused by warnings are suppressed (but written to the log if level is set to WARN).<p>
041 *
042 * @since 6.0.0
043 */
044public class CmsXmlValidationErrorHandler extends XMLErrorHandler {
045
046    /** Stores the warnings that occur during a SAX parse. */
047    private Element m_warnings;
048
049    /**
050     * Constructor from superclass.<p>
051     */
052    public CmsXmlValidationErrorHandler() {
053
054        super();
055        m_warnings = DocumentHelper.createElement("warnings");
056    }
057
058    /**
059     * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
060     */
061    @Override
062    public void error(SAXParseException e) {
063
064        String message = e.getMessage();
065        if (CmsStringUtil.isNotEmpty(message)) {
066
067            if (message.startsWith("sch-props-correct.2")) {
068                // HACK: multiple schema includes cause errors in validation with Xerces 2
069                // the schema nevertheless is usable
070                // redirect this error to be a warning
071                warning(e);
072                return;
073            }
074        }
075
076        super.error(e);
077    }
078
079    /**
080     * Returns the warnings.<p>
081     *
082     * @return the warnings
083     */
084    public Element getWarnings() {
085
086        return m_warnings;
087    }
088
089    /**
090     * @see org.dom4j.util.XMLErrorHandler#warning(org.xml.sax.SAXParseException)
091     */
092    @Override
093    public void warning(SAXParseException e) {
094
095        Element element = m_warnings.addElement(WARNING_QNAME);
096        addException(element, e);
097    }
098}