Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 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 | * Format arguments are supported. |
| 37 | */ |
| 38 | public static DocString text(String format, Object... args) { |
| 39 | DocString doc = new DocString(); |
| 40 | return doc.append(format, args); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Construct a new DocString, initialized with the given link. |
| 45 | */ |
| 46 | public static DocString link(URI uri, DocString content) { |
| 47 | DocString doc = new DocString(); |
| 48 | return doc.appendLink(uri, content); |
| 49 | |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Construct a new DocString initialized with the given image. |
| 54 | */ |
| 55 | public static DocString image(URI uri, String alt) { |
| 56 | return (new DocString()).appendImage(uri, alt); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Append literal text to the given doc string. |
| 61 | * Format arguments are supported. |
| 62 | * Returns this object. |
| 63 | */ |
| 64 | public DocString append(String format, Object... args) { |
| 65 | String text = String.format(format, args); |
| 66 | mStringBuilder.append(HtmlEscapers.htmlEscaper().escape(text)); |
| 67 | return this; |
| 68 | } |
| 69 | |
| 70 | public DocString append(DocString str) { |
| 71 | mStringBuilder.append(str.html()); |
| 72 | return this; |
| 73 | } |
| 74 | |
| 75 | public DocString appendLink(URI uri, DocString content) { |
| 76 | mStringBuilder.append("<a href=\""); |
| 77 | mStringBuilder.append(uri.toASCIIString()); |
| 78 | mStringBuilder.append("\">"); |
| 79 | mStringBuilder.append(content.html()); |
| 80 | mStringBuilder.append("</a>"); |
| 81 | return this; |
| 82 | } |
| 83 | |
| 84 | public DocString appendImage(URI uri, String alt) { |
| 85 | mStringBuilder.append("<img alt=\""); |
| 86 | mStringBuilder.append(HtmlEscapers.htmlEscaper().escape(alt)); |
| 87 | mStringBuilder.append("\" src=\""); |
| 88 | mStringBuilder.append(uri.toASCIIString()); |
| 89 | mStringBuilder.append("\" />"); |
| 90 | return this; |
| 91 | } |
| 92 | |
| 93 | public DocString appendThumbnail(URI uri, String alt) { |
| 94 | mStringBuilder.append("<img height=\"16\" alt=\""); |
| 95 | mStringBuilder.append(HtmlEscapers.htmlEscaper().escape(alt)); |
| 96 | mStringBuilder.append("\" src=\""); |
| 97 | mStringBuilder.append(uri.toASCIIString()); |
| 98 | mStringBuilder.append("\" />"); |
| 99 | return this; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Convenience function for constructing a URI from a string with a uri |
| 104 | * known to be valid. Format arguments are supported. |
| 105 | */ |
| 106 | public static URI uri(String format, Object... args) { |
| 107 | String uriString = String.format(format, args); |
| 108 | try { |
| 109 | return new URI(uriString); |
| 110 | } catch (URISyntaxException e) { |
| 111 | throw new IllegalStateException("Known good uri has syntax error: " + uriString, e); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Render the DocString as html. |
| 117 | */ |
| 118 | public String html() { |
| 119 | return mStringBuilder.toString(); |
| 120 | } |
| 121 | } |