From 222826af9565922d27a44f3bbd0243fc1bcacd8e Mon Sep 17 00:00:00 2001 From: Jeremy Long Date: Wed, 2 Apr 2014 06:54:25 -0400 Subject: [PATCH] added to simplify velocity templates Former-commit-id: 2bb350d765993782f38d90d235bebb0e9e7d51a7 --- .../dependencycheck/reporting/EscapeTool.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 dependency-check-core/src/main/java/org/owasp/dependencycheck/reporting/EscapeTool.java diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/reporting/EscapeTool.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/reporting/EscapeTool.java new file mode 100644 index 000000000..11e112faf --- /dev/null +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/reporting/EscapeTool.java @@ -0,0 +1,67 @@ +/* + * Copyright 2014 OWASP. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.owasp.dependencycheck.reporting; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.apache.commons.lang.StringEscapeUtils; + +/** + * An extremely simple wrapper around various escape utils to perform URL and HTML encoding within the reports. This + * class was created to simplify the velocity configuration and avoid using the "built-in" escape tool. + * + * @author Jeremy Long + */ +public class EscapeTool { + + /** + * URL Encodes the provided text. + * + * @param text the text to encode + * @return the URL encoded text + */ + public String url(String text) { + try { + return URLEncoder.encode(text, "UTF-8"); + } catch (UnsupportedEncodingException ex) { + Logger.getLogger(EscapeTool.class.getName()).log(Level.WARNING, "UTF-8 is not supported?"); + Logger.getLogger(EscapeTool.class.getName()).log(Level.INFO, null, ex); + } + return ""; + } + + /** + * HTML Encodes the provided text. + * + * @param text the text to encode + * @return the HTML encoded text + */ + public String html(String text) { + return StringEscapeUtils.escapeHtml(text); + } + + /** + * XML Encodes the provided text. + * + * @param text the text to encode + * @return the XML encoded text + */ + public String xml(String text) { + return StringEscapeUtils.escapeXml(text); + } +}