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