Coverage Report - org.owasp.dependencycheck.xml.hints.HintErrorHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
HintErrorHandler
11%
2/17
0%
0/8
2.5
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
 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  
  * Copyright (c) 2016 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.xml.hints;
 19  
 
 20  
 import org.slf4j.Logger;
 21  
 import org.slf4j.LoggerFactory;
 22  
 import org.xml.sax.ErrorHandler;
 23  
 import org.xml.sax.SAXException;
 24  
 import org.xml.sax.SAXParseException;
 25  
 
 26  
 /**
 27  
  * An XML parsing error handler.
 28  
  *
 29  
  * @author Jeremy Long
 30  
  */
 31  5
 public class HintErrorHandler implements ErrorHandler {
 32  
 
 33  
     /**
 34  
      * The logger.
 35  
      */
 36  1
     private static final Logger LOGGER = LoggerFactory.getLogger(HintErrorHandler.class);
 37  
 
 38  
     /**
 39  
      * Builds a prettier exception message.
 40  
      *
 41  
      * @param ex the SAXParseException
 42  
      * @return an easier to read exception message
 43  
      */
 44  
     private String getPrettyParseExceptionInfo(SAXParseException ex) {
 45  
 
 46  0
         final StringBuilder sb = new StringBuilder();
 47  
 
 48  0
         if (ex.getSystemId() != null) {
 49  0
             sb.append("systemId=").append(ex.getSystemId()).append(", ");
 50  
         }
 51  0
         if (ex.getPublicId() != null) {
 52  0
             sb.append("publicId=").append(ex.getPublicId()).append(", ");
 53  
         }
 54  0
         if (ex.getLineNumber() > 0) {
 55  0
             sb.append("Line=").append(ex.getLineNumber());
 56  
         }
 57  0
         if (ex.getColumnNumber() > 0) {
 58  0
             sb.append(", Column=").append(ex.getColumnNumber());
 59  
         }
 60  0
         sb.append(": ").append(ex.getMessage());
 61  
 
 62  0
         return sb.toString();
 63  
     }
 64  
 
 65  
     /**
 66  
      * Logs warnings.
 67  
      *
 68  
      * @param ex the warning to log
 69  
      * @throws SAXException is never thrown
 70  
      */
 71  
     @Override
 72  
     public void warning(SAXParseException ex) throws SAXException {
 73  0
         LOGGER.debug("", ex);
 74  0
     }
 75  
 
 76  
     /**
 77  
      * Handles errors.
 78  
      *
 79  
      * @param ex the error to handle
 80  
      * @throws SAXException is always thrown
 81  
      */
 82  
     @Override
 83  
     public void error(SAXParseException ex) throws SAXException {
 84  0
         throw new SAXException(getPrettyParseExceptionInfo(ex));
 85  
     }
 86  
 
 87  
     /**
 88  
      * Handles fatal exceptions.
 89  
      *
 90  
      * @param ex a fatal exception
 91  
      * @throws SAXException is always
 92  
      */
 93  
     @Override
 94  
     public void fatalError(SAXParseException ex) throws SAXException {
 95  0
         throw new SAXException(getPrettyParseExceptionInfo(ex));
 96  
     }
 97  
 }