1 /* 2 * Copyright (C) 2023 The Android Open Source Project 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 17 package com.android.ondevicepersonalization.services.display.velocity; 18 19 import org.owasp.encoder.Encode; 20 21 /** 22 * Tool containing methods passed to the velocity engine 23 * to be used during rendering within the template 24 */ 25 public class OnDevicePersonalizationVelocityTool { 26 27 /** 28 * Encode string for HTML context. 29 */ encodeHtml(String s)30 public static String encodeHtml(String s) { 31 return Encode.forHtml(s); 32 } 33 34 /** 35 * Encode string for URL context. 36 */ encodeUrl(String s)37 public static String encodeUrl(String s) { 38 return Encode.forUriComponent(s); 39 } 40 41 /** 42 * Encode string for JS context. 43 */ encodeJs(String s)44 public static String encodeJs(String s) { 45 return Encode.forJavaScript(s); 46 } 47 48 /** 49 * Encode string for CSS string context. 50 */ encodeCssString(String s)51 public static String encodeCssString(String s) { 52 return Encode.forCssString(s); 53 } 54 55 /** 56 * Encode string for CSS URL context. 57 */ encodeCssUrl(String s)58 public static String encodeCssUrl(String s) { 59 return Encode.forCssUrl(s); 60 } 61 } 62