/** * Copyright 1999-2009 The Pegadi Team * * 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 org.pegadi.xml; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler; public class PegadiErrorHandler extends DefaultHandler { Logger log = LoggerFactory.getLogger(getClass()); private SAXParseException saxe; private boolean validationError = false; /* (non-Javadoc) * @see org.xml.sax.helpers.DefaultHandler#error(org.xml.sax.SAXParseException) */ public void error(SAXParseException arg0) throws SAXException { log.error("Error validating XML."); printInfo(arg0); this.saxe = arg0; setValidationError(true); } /* (non-Javadoc) * @see org.xml.sax.helpers.DefaultHandler#fatalError(org.xml.sax.SAXParseException) */ public void fatalError(SAXParseException arg0) throws SAXException { log.error("Fatal Error validating XML"); printInfo(arg0); this.saxe = arg0; setValidationError(true); } /* (non-Javadoc) * @see org.xml.sax.helpers.DefaultHandler#warning(org.xml.sax.SAXParseException) */ public void warning(SAXParseException arg0) throws SAXException { log.warn("Warning validating XML"); printInfo(arg0); } /* (non-Javadoc) * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes) */ public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException { log.debug("Start element: " + arg0); super.startElement(arg0, arg1, arg2, arg3); } /** * @return Returns the validationError. */ public boolean isValidationError() { return validationError; } /** * @param validationError The validationError to set. */ public void setValidationError(boolean validationError) { this.validationError = validationError; } private void printInfo(SAXParseException e) { //log.debug(" Public ID: "+e.getPublicId()); //log.debug(" System ID: "+e.getSystemId()); log.debug(" Line number: "+e.getLineNumber()); log.debug(" Column number: "+e.getColumnNumber()); log.debug(" Message: "+e.getMessage()); //boolean environmentOK = (new EnvironmentCheck()).checkEnvironment (new PrintWriter(System.out)); } }