001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.kuali.kfs.sys.exception;
018    
019    import org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    import org.xml.sax.ErrorHandler;
022    import org.xml.sax.SAXParseException;
023    
024    /**
025     * Defines exception-handling for the XML parses
026     */
027    public class XmlErrorHandler implements ErrorHandler {
028        private static Log LOG = LogFactory.getLog(XmlErrorHandler.class);
029    
030        public XmlErrorHandler() {
031        }
032    
033        public void warning(SAXParseException e) {
034            String parseMessage = assembleMessage("warning", e);
035            LOG.error(parseMessage);
036            throw new ParseException(parseMessage, e);
037        }
038    
039        public void error(SAXParseException e) {
040            String parseMessage = assembleMessage("error", e);
041            LOG.error(parseMessage);
042            throw new ParseException(parseMessage, e);
043        }
044    
045        public void fatalError(SAXParseException e) {
046            String parseMessage = assembleMessage("fatal error", e);
047            LOG.error(parseMessage);
048            throw new ParseException(parseMessage, e);
049        }
050    
051        private String assembleMessage(String messageType, SAXParseException e) {
052            StringBuffer message = new StringBuffer(messageType);
053            message.append(" Parsing error was encountered on line ");
054            message.append(e.getLineNumber());
055            message.append(", column ");
056            message.append(e.getColumnNumber());
057            message.append(": ");
058            message.append(e.getMessage());
059    
060            return message.toString();
061        }
062    }