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 java.io.PrintStream; 20 import java.net.URI; 21 import java.util.List; 22 23 /** 24 * An Html implementation of Doc. 25 */ 26 public class HtmlDoc implements Doc { 27 private PrintStream ps; 28 private Column[] mCurrentTableColumns; 29 30 /** 31 * Create an HtmlDoc that writes to the given print stream. 32 * @param title - The main page title. 33 * @param style - A URI link to a stylesheet to link to. 34 */ 35 public HtmlDoc(PrintStream ps, DocString title, URI style) { 36 this.ps = ps; 37 38 ps.println("<!DOCTYPE html>"); 39 ps.println("<html>"); 40 ps.println("<head>"); 41 ps.format("<title>%s</title>\n", title.html()); 42 ps.format("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">\n", 43 style.toASCIIString()); 44 ps.println("</head>"); 45 ps.println("<body>"); 46 } 47 48 @Override 49 public void title(String format, Object... args) { 50 ps.print("<h1>"); 51 ps.print(DocString.text(String.format(format, args)).html()); 52 ps.println("</h1>"); 53 } 54 55 @Override 56 public void menu(DocString string) { 57 ps.format("<div class=\"menu\">%s</div>", string.html()); 58 } 59 60 @Override 61 public void section(String title) { 62 ps.print("<h2>"); 63 ps.print(DocString.text(title).html()); 64 ps.println(":</h2>"); 65 } 66 67 @Override 68 public void println(DocString string) { 69 ps.print(string.html()); 70 ps.println("<br />"); 71 } 72 73 @Override 74 public void big(DocString str) { 75 ps.print("<h2>"); 76 ps.print(str.html()); 77 ps.println("</h2>"); 78 } 79 80 @Override 81 public void table(Column... columns) { 82 if (columns.length == 0) { 83 throw new IllegalArgumentException("No columns specified"); 84 } 85 86 mCurrentTableColumns = columns; 87 ps.println("<table>"); 88 for (int i = 0; i < columns.length - 1; i++) { 89 ps.format("<th>%s</th>", columns[i].heading.html()); 90 } 91 92 // Align the last header to the left so it's easier to see if the last 93 // column is very wide. 94 ps.format("<th align=\"left\">%s</th>", columns[columns.length - 1].heading.html()); 95 } 96 97 @Override 98 public void table(DocString description, List<Column> subcols, List<Column> cols) { 99 mCurrentTableColumns = new Column[subcols.size() + cols.size()]; 100 int j = 0; 101 for (Column col : subcols) { 102 mCurrentTableColumns[j] = col; 103 j++; 104 } 105 for (Column col : cols) { 106 mCurrentTableColumns[j] = col; 107 j++; 108 } 109 110 ps.println("<table>"); 111 ps.format("<tr><th colspan=\"%d\">%s</th>", subcols.size(), description.html()); 112 for (int i = 0; i < cols.size() - 1; i++) { 113 ps.format("<th rowspan=\"2\">%s</th>", cols.get(i).heading.html()); 114 } 115 if (!cols.isEmpty()) { 116 // Align the last column header to the left so it can still be seen if 117 // the last column is very wide. 118 ps.format("<th align=\"left\" rowspan=\"2\">%s</th>", 119 cols.get(cols.size() - 1).heading.html()); 120 } 121 ps.println("</tr>"); 122 123 ps.print("<tr>"); 124 for (Column subcol : subcols) { 125 ps.format("<th>%s</th>", subcol.heading.html()); 126 } 127 ps.println("</tr>"); 128 } 129 130 @Override 131 public void row(DocString... values) { 132 if (mCurrentTableColumns == null) { 133 throw new IllegalStateException("table method must be called before row"); 134 } 135 136 if (mCurrentTableColumns.length != values.length) { 137 throw new IllegalArgumentException(String.format( 138 "Wrong number of row values. Expected %d, but got %d", 139 mCurrentTableColumns.length, values.length)); 140 } 141 142 ps.print("<tr>"); 143 for (int i = 0; i < values.length; i++) { 144 ps.print("<td"); 145 if (mCurrentTableColumns[i].align == Column.Align.RIGHT) { 146 ps.print(" align=\"right\""); 147 } 148 ps.format(">%s</td>", values[i].html()); 149 } 150 ps.println("</tr>"); 151 } 152 153 @Override 154 public void descriptions() { 155 ps.println("<table>"); 156 } 157 158 @Override 159 public void description(DocString key, DocString value) { 160 ps.format("<tr><th align=\"left\">%s:</th><td>%s</td></tr>", key.html(), value.html()); 161 } 162 163 @Override 164 public void end() { 165 ps.println("</table>"); 166 mCurrentTableColumns = null; 167 } 168 169 @Override 170 public void close() { 171 ps.println("</body>"); 172 ps.println("</html>"); 173 ps.close(); 174 } 175 } 176