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 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    */
HtmlDoc(PrintStream ps, DocString title, URI style)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
title(String format, Object... args)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
menu(DocString string)56   public void menu(DocString string) {
57     ps.format("<div class=\"menu\">%s</div>", string.html());
58   }
59 
60   @Override
section(String title)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
println(DocString string)68   public void println(DocString string) {
69     ps.print(string.html());
70     ps.println("<br />");
71   }
72 
73   @Override
big(DocString str)74   public void big(DocString str) {
75     ps.print("<h2>");
76     ps.print(str.html());
77     ps.println("</h2>");
78   }
79 
80   @Override
table(Column... columns)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       if (columns[i].visible) {
90         ps.format("<th>%s</th>", columns[i].heading.html());
91       }
92     }
93 
94     // Align the last header to the left so it's easier to see if the last
95     // column is very wide.
96     if (columns[columns.length - 1].visible) {
97       ps.format("<th align=\"left\">%s</th>", columns[columns.length - 1].heading.html());
98     }
99   }
100 
101   @Override
table(DocString description, List<Column> subcols, List<Column> cols)102   public void table(DocString description, List<Column> subcols, List<Column> cols) {
103     mCurrentTableColumns = new Column[subcols.size() + cols.size()];
104     int j = 0;
105     int visibleSubCols = 0;
106     for (Column col : subcols) {
107       if (col.visible) {
108         visibleSubCols++;
109       }
110       mCurrentTableColumns[j] = col;
111       j++;
112     }
113     for (Column col : cols) {
114       mCurrentTableColumns[j] = col;
115       j++;
116     }
117 
118     ps.println("<table>");
119     ps.format("<tr><th colspan=\"%d\">%s</th>", visibleSubCols, description.html());
120     for (int i = 0; i < cols.size() - 1; i++) {
121       if (cols.get(i).visible) {
122         ps.format("<th rowspan=\"2\">%s</th>", cols.get(i).heading.html());
123       }
124     }
125     if (!cols.isEmpty()) {
126       // Align the last column header to the left so it can still be seen if
127       // the last column is very wide.
128       Column col = cols.get(cols.size() - 1);
129       if (col.visible) {
130         ps.format("<th align=\"left\" rowspan=\"2\">%s</th>", col.heading.html());
131       }
132     }
133     ps.println("</tr>");
134 
135     ps.print("<tr>");
136     for (Column subcol : subcols) {
137       if (subcol.visible) {
138         ps.format("<th>%s</th>", subcol.heading.html());
139       }
140     }
141     ps.println("</tr>");
142   }
143 
144   @Override
row(DocString... values)145   public void row(DocString... values) {
146     if (mCurrentTableColumns == null) {
147       throw new IllegalStateException("table method must be called before row");
148     }
149 
150     if (mCurrentTableColumns.length != values.length) {
151       throw new IllegalArgumentException(String.format(
152           "Wrong number of row values. Expected %d, but got %d",
153           mCurrentTableColumns.length, values.length));
154     }
155 
156     ps.print("<tr>");
157     for (int i = 0; i < values.length; i++) {
158       if (mCurrentTableColumns[i].visible) {
159       ps.print("<td");
160         if (mCurrentTableColumns[i].align == Column.Align.RIGHT) {
161           ps.print(" align=\"right\"");
162         }
163         ps.format(">%s</td>", values[i].html());
164       }
165     }
166     ps.println("</tr>");
167   }
168 
169   @Override
descriptions()170   public void descriptions() {
171     ps.println("<table>");
172   }
173 
174   @Override
description(DocString key, DocString value)175   public void description(DocString key, DocString value) {
176     ps.format("<tr><th align=\"left\">%s:</th><td>%s</td></tr>", key.html(), value.html());
177   }
178 
179   @Override
end()180   public void end() {
181     ps.println("</table>");
182     mCurrentTableColumns = null;
183   }
184 
185   @Override
close()186   public void close() {
187     ps.println("</body>");
188     ps.println("</html>");
189     ps.close();
190   }
191 }
192