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

redora.util.DefaultErrorHandler Maven / Gradle / Ivy

/*
 * Copyright 2009-2010 Nanjing RedOrange ltd (http://www.red-orange.cn)
 *
 * 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 or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package redora.util;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.jetbrains.annotations.NotNull;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;

import java.util.logging.Logger;

import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;

/**
 * Schema validation error handle.
 *
 * @author Nanjing RedOrange (www.red-orange.cn)
 *
 */
public class DefaultErrorHandler implements ErrorHandler {
    private static final transient Logger l = Logger.getLogger("redora.util.DefaultErrorHandler");

    private final Element errors;

    public DefaultErrorHandler() {
        this.errors = DocumentHelper.createElement("errors");
    }

    /**
     * @return the errors
     */
    @NotNull
    public Element getErrors() {
        return errors;
    }

    /**
     * add exception.
     */
    protected void addException(@NotNull Element element, @NotNull SAXParseException exception) {
        element.addAttribute("column"
                , Integer.toString(exception.getColumnNumber()));
        element.addAttribute("line"
                , Integer.toString(exception.getLineNumber()));

        String publicId = exception.getPublicId();
        if (publicId != null && publicId.length() > 0) {
            element.addAttribute("publicId", publicId);
        }

        String systemId = exception.getSystemId();
        if (systemId != null && systemId.length() > 0) {
            element.addAttribute("systemId", systemId);
        }

        element.addText(exception.getMessage());
   }

    /**
     * (non-Javadoc).
     *
     * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
     */
    public void error(@NotNull SAXParseException exception) {
        addException(errors.addElement("errors"), exception);
        l.log(INFO, "XML file is invalid: {0}", errors.asXML());
    }

    /**
     * (non-Javadoc).
     *
     * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
     */
    public void fatalError(@NotNull SAXParseException exception) {
        addException(errors.addElement("fatalError"), exception);
        l.log(INFO, "XML file is invalid: {0}", errors.asXML());
    }

    /**
     * (non-Javadoc).
     *
     * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
     */
    public void warning(@NotNull SAXParseException exception) {
        addException(errors.addElement("warning"), exception);
        l.log(FINE, "XML file is invalid: {0}", errors.asXML());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy