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) 2014 Jeremy Long. All Rights Reserved.
17 */
18 package org.owasp.dependencycheck.reporting;
19
20 import java.io.UnsupportedEncodingException;
21 import java.net.URLEncoder;
22 import org.apache.commons.lang3.StringEscapeUtils;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27 * An extremely simple wrapper around various escape utils to perform URL and HTML encoding within the reports. This class was
28 * created to simplify the velocity configuration and avoid using the "built-in" escape tool.
29 *
30 * @author Jeremy Long
31 */
32 public class EscapeTool {
33
34 /**
35 * The logger.
36 */
37 private static final Logger LOGGER = LoggerFactory.getLogger(EscapeTool.class);
38
39 /**
40 * URL Encodes the provided text.
41 *
42 * @param text the text to encode
43 * @return the URL encoded text
44 */
45 public String url(String text) {
46 if (text == null || text.isEmpty()) {
47 return text;
48 }
49 try {
50 return URLEncoder.encode(text, "UTF-8");
51 } catch (UnsupportedEncodingException ex) {
52 LOGGER.warn("UTF-8 is not supported?");
53 LOGGER.info("", ex);
54 }
55 return "";
56 }
57
58 /**
59 * HTML Encodes the provided text.
60 *
61 * @param text the text to encode
62 * @return the HTML encoded text
63 */
64 public String html(String text) {
65 if (text == null || text.isEmpty()) {
66 return text;
67 }
68 return StringEscapeUtils.escapeHtml4(text);
69 }
70
71 /**
72 * XML Encodes the provided text.
73 *
74 * @param text the text to encode
75 * @return the XML encoded text
76 */
77 public String xml(String text) {
78 if (text == null || text.isEmpty()) {
79 return text;
80 }
81 return StringEscapeUtils.escapeXml11(text);
82 }
83 }