1 /* 2 * Copyright (C) 2015 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.ahat; 18 19 import com.google.common.html.HtmlEscapers; 20 import java.net.URI; 21 import java.net.URISyntaxException; 22 23 /** 24 * A class representing a small string of document content consisting of text, 25 * links, images, etc. 26 */ 27 class DocString { 28 private StringBuilder mStringBuilder; 29 30 public DocString() { 31 mStringBuilder = new StringBuilder(); 32 } 33 34 /** 35 * Construct a new DocString, initialized with the given text. 36 */ 37 public static DocString text(String str) { 38 DocString doc = new DocString(); 39 return doc.append(str); 40 } 41 42 /** 43 * Construct a new DocString, initialized with the given formatted text. 44 */ 45 public static DocString format(String format, Object... args) { 46 DocString doc = new DocString(); 47 return doc.appendFormat(format, args); 48 } 49 50 /** 51 * Construct a new DocString, initialized with the given link. 52 */ 53 public static DocString link(URI uri, DocString content) { 54 DocString doc = new DocString(); 55 return doc.appendLink(uri, content); 56 57 } 58 59 /** 60 * Construct a new DocString initialized with the given image. 61 */ 62 public static DocString image(URI uri, String alt) { 63 return (new DocString()).appendImage(uri, alt); 64 } 65 66 /** 67 * Append literal text to the given doc string. 68 * Returns this object. 69 */ 70 public DocString append(String text) { 71 mStringBuilder.append(HtmlEscapers.htmlEscaper().escape(text)); 72 return this; 73 } 74 75 /** 76 * Append formatted text to the given doc string. 77 * Returns this object. 78 */ 79 public DocString appendFormat(String format, Object... args) { 80 append(String.format(format, args)); 81 return this; 82 } 83 84 public DocString append(DocString str) { 85 mStringBuilder.append(str.html()); 86 return this; 87 } 88 89 public DocString appendLink(URI uri, DocString content) { 90 mStringBuilder.append("<a href=\""); 91 mStringBuilder.append(uri.toASCIIString()); 92 mStringBuilder.append("\">"); 93 mStringBuilder.append(content.html()); 94 mStringBuilder.append("</a>"); 95 return this; 96 } 97 98 public DocString appendImage(URI uri, String alt) { 99 mStringBuilder.append("<img alt=\""); 100 mStringBuilder.append(HtmlEscapers.htmlEscaper().escape(alt)); 101 mStringBuilder.append("\" src=\""); 102 mStringBuilder.append(uri.toASCIIString()); 103 mStringBuilder.append("\" />"); 104 return this; 105 } 106 107 public DocString appendThumbnail(URI uri, String alt) { 108 mStringBuilder.append("<img height=\"16\" alt=\""); 109 mStringBuilder.append(HtmlEscapers.htmlEscaper().escape(alt)); 110 mStringBuilder.append("\" src=\""); 111 mStringBuilder.append(uri.toASCIIString()); 112 mStringBuilder.append("\" />"); 113 return this; 114 } 115 116 /** 117 * Convenience function for constructing a URI from a string with a uri 118 * known to be valid. 119 */ 120 public static URI uri(String uriString) { 121 try { 122 return new URI(uriString); 123 } catch (URISyntaxException e) { 124 throw new IllegalStateException("Known good uri has syntax error: " + uriString, e); 125 } 126 } 127 128 /** 129 * Convenience function for constructing a URI from a formatted string with 130 * a uri known to be valid. 131 */ 132 public static URI formattedUri(String format, Object... args) { 133 return uri(String.format(format, args)); 134 } 135 136 /** 137 * Render the DocString as html. 138 */ 139 public String html() { 140 return mStringBuilder.toString(); 141 } 142 } 143