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      public XmlErrorHandler() {
33      }
34  
35      public void warning(SAXParseException e) {
36          String parseMessage = assembleMessage("warning", e);
37          LOG.error(parseMessage);
38          throw new ParseException(parseMessage, e);
39      }
40  
41      public void error(SAXParseException e) {
42          String parseMessage = assembleMessage("error", e);
43          LOG.error(parseMessage);
44          throw new ParseException(parseMessage, e);
45      }
46  
47      public void fatalError(SAXParseException e) {
48          String parseMessage = assembleMessage("fatal error", e);
49          LOG.error(parseMessage);
50          throw new ParseException(parseMessage, e);
51      }
52  
53      private String assembleMessage(String messageType, SAXParseException e) {
54          StringBuffer message = new StringBuffer(messageType);
55          message.append(" Parsing error was encountered on line ");
56          message.append(e.getLineNumber());
57          message.append(", column ");
58          message.append(e.getColumnNumber());
59          message.append(": ");
60          message.append(e.getMessage());
61  
62          return message.toString();
63      }
64  }