/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.ahat; import java.io.PrintStream; import java.net.URI; import java.util.List; /** * An Html implementation of Doc. */ public class HtmlDoc implements Doc { private PrintStream ps; private Column[] mCurrentTableColumns; /** * Create an HtmlDoc that writes to the given print stream. * @param title - The main page title. * @param style - A URI link to a stylesheet to link to. */ public HtmlDoc(PrintStream ps, DocString title, URI style) { this.ps = ps; ps.println(""); ps.println(""); ps.println(""); ps.format("%s\n", title.html()); ps.format("\n", style.toASCIIString()); ps.println(""); ps.println(""); } @Override public void title(String format, Object... args) { ps.print("

"); ps.print(DocString.text(String.format(format, args)).html()); ps.println("

"); } @Override public void menu(DocString string) { ps.format("
%s
", string.html()); } @Override public void section(String title) { ps.print("

"); ps.print(DocString.text(title).html()); ps.println(":

"); } @Override public void println(DocString string) { ps.print(string.html()); ps.println("
"); } @Override public void big(DocString str) { ps.print("

"); ps.print(str.html()); ps.println("

"); } @Override public void table(Column... columns) { if (columns.length == 0) { throw new IllegalArgumentException("No columns specified"); } mCurrentTableColumns = columns; ps.println(""); for (int i = 0; i < columns.length - 1; i++) { ps.format("", columns[i].heading.html()); } // Align the last header to the left so it's easier to see if the last // column is very wide. ps.format("", columns[columns.length - 1].heading.html()); } @Override public void table(DocString description, List subcols, List cols) { mCurrentTableColumns = new Column[subcols.size() + cols.size()]; int j = 0; for (Column col : subcols) { mCurrentTableColumns[j] = col; j++; } for (Column col : cols) { mCurrentTableColumns[j] = col; j++; } ps.println("
%s%s
"); ps.format("", subcols.size(), description.html()); for (int i = 0; i < cols.size() - 1; i++) { ps.format("", cols.get(i).heading.html()); } if (!cols.isEmpty()) { // Align the last column header to the left so it can still be seen if // the last column is very wide. ps.format("", cols.get(cols.size() - 1).heading.html()); } ps.println(""); ps.print(""); for (Column subcol : subcols) { ps.format("", subcol.heading.html()); } ps.println(""); } @Override public void row(DocString... values) { if (mCurrentTableColumns == null) { throw new IllegalStateException("table method must be called before row"); } if (mCurrentTableColumns.length != values.length) { throw new IllegalArgumentException(String.format( "Wrong number of row values. Expected %d, but got %d", mCurrentTableColumns.length, values.length)); } ps.print(""); for (int i = 0; i < values.length; i++) { ps.print("%s", values[i].html()); } ps.println(""); } @Override public void descriptions() { ps.println("
%s%s%s
%s
"); } @Override public void description(DocString key, DocString value) { ps.format("", key.html(), value.html()); } @Override public void end() { ps.println("
%s:%s
"); mCurrentTableColumns = null; } @Override public void close() { ps.println(""); ps.println(""); ps.close(); } }