| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SuppressionErrorHandler |
|
| 2.5;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.suppression; | |
| 19 | ||
| 20 | import java.util.logging.Level; | |
| 21 | import java.util.logging.Logger; | |
| 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 <jeremy.long@owasp.org> | |
| 30 | */ | |
| 31 | 4 | public class SuppressionErrorHandler implements ErrorHandler { |
| 32 | ||
| 33 | /** | |
| 34 | * Builds a prettier exception message. | |
| 35 | * | |
| 36 | * @param ex the SAXParseException | |
| 37 | * @return an easier to read exception message | |
| 38 | */ | |
| 39 | private String getPrettyParseExceptionInfo(SAXParseException ex) { | |
| 40 | ||
| 41 | 0 | final StringBuilder sb = new StringBuilder(); |
| 42 | ||
| 43 | 0 | if (ex.getSystemId() != null) { |
| 44 | 0 | sb.append("systemId=").append(ex.getSystemId()).append(", "); |
| 45 | } | |
| 46 | 0 | if (ex.getPublicId() != null) { |
| 47 | 0 | sb.append("publicId=").append(ex.getPublicId()).append(", "); |
| 48 | } | |
| 49 | 0 | if (ex.getLineNumber() > 0) { |
| 50 | 0 | sb.append("Line=").append(ex.getLineNumber()); |
| 51 | } | |
| 52 | 0 | if (ex.getColumnNumber() > 0) { |
| 53 | 0 | sb.append(", Column=").append(ex.getColumnNumber()); |
| 54 | } | |
| 55 | 0 | sb.append(": ").append(ex.getMessage()); |
| 56 | ||
| 57 | 0 | return sb.toString(); |
| 58 | } | |
| 59 | ||
| 60 | /** | |
| 61 | * Logs warnings. | |
| 62 | * | |
| 63 | * @param ex the warning to log | |
| 64 | * @throws SAXException is never thrown | |
| 65 | */ | |
| 66 | @Override | |
| 67 | public void warning(SAXParseException ex) throws SAXException { | |
| 68 | 0 | Logger.getLogger(SuppressionErrorHandler.class.getName()).log(Level.FINE, null, ex); |
| 69 | 0 | } |
| 70 | ||
| 71 | /** | |
| 72 | * Handles errors. | |
| 73 | * | |
| 74 | * @param ex the error to handle | |
| 75 | * @throws SAXException is always thrown | |
| 76 | */ | |
| 77 | @Override | |
| 78 | public void error(SAXParseException ex) throws SAXException { | |
| 79 | 0 | throw new SAXException(getPrettyParseExceptionInfo(ex)); |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * Handles fatal exceptions. | |
| 84 | * | |
| 85 | * @param ex a fatal exception | |
| 86 | * @throws SAXException is always | |
| 87 | */ | |
| 88 | @Override | |
| 89 | public void fatalError(SAXParseException ex) throws SAXException { | |
| 90 | 0 | throw new SAXException(getPrettyParseExceptionInfo(ex)); |
| 91 | } | |
| 92 | } |