View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.kuali.ole.exception;
18  
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  
22  import org.xml.sax.ErrorHandler;
23  import org.xml.sax.SAXParseException;
24  
25  /**
26   * Defines exception-handling for the XML parses
27   */
28  public class XmlErrorHandler implements ErrorHandler {
29      // TODO: need to use SLF4j here.
30      private static Logger LOG = LoggerFactory.getLogger(XmlErrorHandler.class);
31      /**
32       *
33       * Default constructor.
34       */
35      public XmlErrorHandler() {
36      }
37  
38      /**
39       *  This method displays the warning message for SAXParseException
40       * @param e
41       */
42      public void warning(SAXParseException e) {
43          String parseMessage = assembleMessage("warning", e);
44          LOG.error(parseMessage);
45          throw new ParseException(parseMessage, e);
46      }
47  
48      /**
49       *  This method displays the SAXParseException error.
50       * @param e
51       */
52      public void error(SAXParseException e) {
53          String parseMessage = assembleMessage("error", e);
54          LOG.error(parseMessage);
55          throw new ParseException(parseMessage, e);
56      }
57  
58      /**
59       *  This method displays the SAXParseException fatalError.
60       * @param e
61       */
62      public void fatalError(SAXParseException e) {
63          String parseMessage = assembleMessage("fatal error", e);
64          LOG.error(parseMessage);
65          throw new ParseException(parseMessage, e);
66      }
67  
68      /**
69       * This method assemble the error message and error line numbers
70       * @param messageType
71       * @param e
72       * @return String
73       */
74      private String assembleMessage(String messageType, SAXParseException e) {
75          StringBuffer message = new StringBuffer(messageType);
76          message.append(" Parsing error was encountered on line ");
77          message.append(e.getLineNumber());
78          message.append(", column ");
79          message.append(e.getColumnNumber());
80          message.append(": ");
81          message.append(e.getMessage());
82  
83          return message.toString();
84      }
85  }