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 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 public class SuppressionErrorHandler implements ErrorHandler {
32
33 /**
34 * The logger.
35 */
36 private static final Logger LOGGER = LoggerFactory.getLogger(SuppressionErrorHandler.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 final StringBuilder sb = new StringBuilder();
47
48 if (ex.getSystemId() != null) {
49 sb.append("systemId=").append(ex.getSystemId()).append(", ");
50 }
51 if (ex.getPublicId() != null) {
52 sb.append("publicId=").append(ex.getPublicId()).append(", ");
53 }
54 if (ex.getLineNumber() > 0) {
55 sb.append("Line=").append(ex.getLineNumber());
56 }
57 if (ex.getColumnNumber() > 0) {
58 sb.append(", Column=").append(ex.getColumnNumber());
59 }
60 sb.append(": ").append(ex.getMessage());
61
62 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 LOGGER.debug("", ex);
74 }
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 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 throw new SAXException(getPrettyParseExceptionInfo(ex));
96 }
97 }