blob: 19666dea8ca637e735dd2599dee84165ad78f87c [file] [log] [blame]
Richard Uhlerb730b782015-07-15 16:01:58 -07001/*
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
17package com.android.ahat;
18
19import com.google.common.html.HtmlEscapers;
20import java.net.URI;
21import java.net.URISyntaxException;
22
23/**
24 * A class representing a small string of document content consisting of text,
25 * links, images, etc.
26 */
27class 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.
Richard Uhlerb730b782015-07-15 16:01:58 -070036 */
Richard Uhlerc21e4e62015-08-31 16:16:14 -070037 public static DocString text(String str) {
Richard Uhlerb730b782015-07-15 16:01:58 -070038 DocString doc = new DocString();
Richard Uhlerc21e4e62015-08-31 16:16:14 -070039 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);
Richard Uhlerb730b782015-07-15 16:01:58 -070048 }
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.
Richard Uhlerb730b782015-07-15 16:01:58 -070068 * Returns this object.
69 */
Richard Uhlerc21e4e62015-08-31 16:16:14 -070070 public DocString append(String text) {
Richard Uhlerb730b782015-07-15 16:01:58 -070071 mStringBuilder.append(HtmlEscapers.htmlEscaper().escape(text));
72 return this;
73 }
74
Richard Uhlerc21e4e62015-08-31 16:16:14 -070075 /**
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
Richard Uhlerb730b782015-07-15 16:01:58 -070084 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
Richard Uhlerc21e4e62015-08-31 16:16:14 -0700118 * known to be valid.
Richard Uhlerb730b782015-07-15 16:01:58 -0700119 */
Richard Uhlerc21e4e62015-08-31 16:16:14 -0700120 public static URI uri(String uriString) {
Richard Uhlerb730b782015-07-15 16:01:58 -0700121 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 /**
Richard Uhlerc21e4e62015-08-31 16:16:14 -0700129 * 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 /**
Richard Uhlerb730b782015-07-15 16:01:58 -0700137 * Render the DocString as html.
138 */
139 public String html() {
140 return mStringBuilder.toString();
141 }
142}