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.cts.verifier.libs.ui; 18 19 import android.text.Html; 20 21 /** 22 * Utility class for building well-formatted HTML. 23 */ 24 public class HtmlFormatter { 25 private StringBuilder mSB = new StringBuilder(); 26 27 /** 28 * Clear any accumulated text 29 * @return this HtmlFormatter to allow for cascading calls. 30 */ clear()31 public HtmlFormatter clear() { 32 mSB = new StringBuilder(); 33 return this; 34 } 35 36 /** 37 * Starts the HTML document block 38 * @return this HtmlFormatter to allow for cascading calls. 39 */ openDocument()40 public HtmlFormatter openDocument() { 41 mSB.append("<!DOCTYPE html>\n<html lang=\"en-US\">\n<body>\n"); 42 return this; 43 } 44 45 /** 46 * Closes the HTML document block 47 * @return this HtmlFormatter to allow for cascading calls. 48 */ closeDocument()49 public HtmlFormatter closeDocument() { 50 mSB.append("</body>\n</html>"); 51 return this; 52 } 53 54 /** 55 * Starts the HTML Heading block 56 * @param level The desired heading level. Should be between 1 and 6 inclusive. 57 * @return this HtmlFormatter to allow for cascading calls. 58 */ openHeading(int level)59 public HtmlFormatter openHeading(int level) { 60 mSB.append("<h" + level + ">"); 61 return this; 62 } 63 64 /** 65 * Ends the HTML Heading block 66 * @param level The heading level associated with the corresponding openHeading() call. 67 * @return this HtmlFormatter to allow for cascading calls. 68 */ closeHeading(int level)69 public HtmlFormatter closeHeading(int level) { 70 mSB.append("</h" + level + ">"); 71 return this; 72 } 73 74 /** 75 * Opens an HTML paragraph block. 76 * @return this HtmlFormatter to allow for cascading calls. 77 */ openParagraph()78 public HtmlFormatter openParagraph() { 79 mSB.append("<p>"); 80 return this; 81 } 82 83 /** 84 * Closes an HTML paragraph block. 85 * @return this HtmlFormatter to allow for cascading calls. 86 */ closeParagraph()87 public HtmlFormatter closeParagraph() { 88 mSB.append("</p>\n"); 89 return this; 90 } 91 92 /** 93 * Opens an HTML bold block. 94 * @return this HtmlFormatter to allow for cascading calls. 95 */ openBold()96 public HtmlFormatter openBold() { 97 mSB.append("<b>"); 98 return this; 99 } 100 101 /** 102 * Closes an HTML bold block. 103 * @return this HtmlFormatter to allow for cascading calls. 104 */ closeBold()105 public HtmlFormatter closeBold() { 106 mSB.append("</b>"); 107 return this; 108 } 109 110 /** 111 * Opens an HTML italic block. 112 * @return this HtmlFormatter to allow for cascading calls. 113 */ openItalic()114 public HtmlFormatter openItalic() { 115 mSB.append("<i>"); 116 return this; 117 } 118 119 /** 120 * Closes an HTML italic block. 121 * @return this HtmlFormatter to allow for cascading calls. 122 */ closeItalic()123 public HtmlFormatter closeItalic() { 124 mSB.append("</i>"); 125 return this; 126 } 127 128 /** 129 * Inserts a 'break' in the HTML 130 * @return this HtmlFormatter to allow for cascading calls. 131 */ appendBreak()132 public HtmlFormatter appendBreak() { 133 mSB.append("<br>\n"); 134 return this; 135 } 136 137 /** 138 * Opens a text color block 139 * @param color The desired color, i.e. "red", "blue"... 140 * @return this HtmlFormatter to allow for cascading calls. 141 */ openTextColor(String color)142 public HtmlFormatter openTextColor(String color) { 143 mSB.append("<font color=\"" + color + "\">"); 144 return this; 145 } 146 147 /** 148 * Closes a color block 149 * @return this HtmlFormatter to allow for cascading calls. 150 */ closeTextColor()151 public HtmlFormatter closeTextColor() { 152 mSB.append("</font>"); 153 return this; 154 } 155 156 /** 157 * Starts a bullets list. 158 * @return This HtmlFormatter to allow for cascading calls. 159 */ openBulletList()160 public HtmlFormatter openBulletList() { 161 mSB.append("<ul>"); 162 return this; 163 } 164 165 /** 166 * Ends a bullets list. 167 * @return This HtmlFormatter to allow for cascading calls. 168 */ closeBulletList()169 public HtmlFormatter closeBulletList() { 170 mSB.append("</ul>"); 171 return this; 172 } 173 174 /** 175 * Opens a list item in an enclosing bulleted list. 176 * @return This HtmlFormatter to allow for cascading calls. 177 */ openListItem()178 public HtmlFormatter openListItem() { 179 mSB.append("<li>"); 180 return this; 181 } 182 183 /** 184 * Closes a list item in an enclosing bulleted list. 185 * @return This HtmlFormatter to allow for cascading calls. 186 */ closeListItem()187 public HtmlFormatter closeListItem() { 188 mSB.append("</li>"); 189 return this; 190 } 191 192 /** 193 * Appends a link tag with the specified link target URL 194 * @param url The url for the link. 195 * @return This HtmlFormatter to allow for cascading calls. 196 */ openLink(String url)197 public HtmlFormatter openLink(String url) { 198 mSB.append("<a href=\"" + url + "\">"); 199 return this; 200 } 201 202 /** 203 * Closes a link tag. 204 * @return This HtmlFormatter to allow for cascading calls. 205 */ closeLink()206 public HtmlFormatter closeLink() { 207 mSB.append("</a>"); 208 return this; 209 } 210 211 /** 212 * Appends the specified text to the HTML stream. 213 * @return this HtmlFormatter to allow for cascading calls. 214 */ appendText(String text)215 public HtmlFormatter appendText(String text) { 216 mSB.append(Html.escapeHtml(text)); 217 return this; 218 } 219 220 /** 221 * 222 * @return this HtmlFormatter to allow for cascading calls. 223 */ 224 @Override toString()225 public String toString() { 226 return mSB.toString(); 227 } 228 } 229