Coverage Report - org.owasp.dependencycheck.suppression.SuppressionErrorHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
SuppressionErrorHandler
6%
1/16
0%
0/8
2.5
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Dependency-check-core is free software: you can redistribute it and/or modify it
 5  
  * under the terms of the GNU General Public License as published by the Free
 6  
  * Software Foundation, either version 3 of the License, or (at your option) any
 7  
  * later version.
 8  
  *
 9  
  * Dependency-check-core is distributed in the hope that it will be useful, but
 10  
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 12  
  * details.
 13  
  *
 14  
  * You should have received a copy of the GNU General Public License along with
 15  
  * dependency-check-core. If not, see http://www.gnu.org/licenses/.
 16  
  *
 17  
  * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
 18  
  */
 19  
 package org.owasp.dependencycheck.suppression;
 20  
 
 21  
 import java.util.logging.Level;
 22  
 import java.util.logging.Logger;
 23  
 import org.xml.sax.ErrorHandler;
 24  
 import org.xml.sax.SAXException;
 25  
 import org.xml.sax.SAXParseException;
 26  
 
 27  
 /**
 28  
  * An XML parsing error handler.
 29  
  *
 30  
  * @author Jeremy Long (jeremy.long@owasp.org)
 31  
  */
 32  2
 public class SuppressionErrorHandler implements ErrorHandler {
 33  
 
 34  
     /**
 35  
      * Builds a prettier exception message.
 36  
      *
 37  
      * @param ex the SAXParseException
 38  
      * @return an easier to read exception message
 39  
      */
 40  
     private String getPrettyParseExceptionInfo(SAXParseException ex) {
 41  
 
 42  0
         final StringBuffer sb = new StringBuffer();
 43  
 
 44  0
         if (ex.getSystemId() != null) {
 45  0
             sb.append("systemId=").append(ex.getSystemId()).append(", ");
 46  
         }
 47  0
         if (ex.getPublicId() != null) {
 48  0
             sb.append("publicId=").append(ex.getPublicId()).append(", ");
 49  
         }
 50  0
         if (ex.getLineNumber() > 0) {
 51  0
             sb.append("Line=").append(ex.getLineNumber());
 52  
         }
 53  0
         if (ex.getColumnNumber() > 0) {
 54  0
             sb.append(", Column=").append(ex.getColumnNumber());
 55  
         }
 56  0
         sb.append(": ").append(ex.getMessage());
 57  
 
 58  0
         return sb.toString();
 59  
     }
 60  
 
 61  
     /**
 62  
      * Logs warnings.
 63  
      *
 64  
      * @param ex the warning to log
 65  
      * @throws SAXException is never thrown
 66  
      */
 67  
     @Override
 68  
     public void warning(SAXParseException ex) throws SAXException {
 69  0
         Logger.getLogger(SuppressionErrorHandler.class.getName()).log(Level.FINE, null, ex);
 70  0
     }
 71  
 
 72  
     /**
 73  
      * Handles errors.
 74  
      *
 75  
      * @param ex the error to handle
 76  
      * @throws SAXException is always thrown
 77  
      */
 78  
     @Override
 79  
     public void error(SAXParseException ex) throws SAXException {
 80  0
         throw new SAXException(getPrettyParseExceptionInfo(ex));
 81  
     }
 82  
 
 83  
     /**
 84  
      * Handles fatal exceptions.
 85  
      *
 86  
      * @param ex a fatal exception
 87  
      * @throws SAXException is always
 88  
      */
 89  
     @Override
 90  
     public void fatalError(SAXParseException ex) throws SAXException {
 91  0
         throw new SAXException(getPrettyParseExceptionInfo(ex));
 92  
     }
 93  
 }