langtools/test/tools/javac/diags/RunExamples.java
author jjg
Fri, 27 Aug 2010 17:59:08 -0700
changeset 6581 f58f0ce45802
parent 6149 48de3564aa13
child 7643 a067a0cda531
permissions -rw-r--r--
6980707: Reduce use of IOException in JavaCompiler Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6149
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     1
/*
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     4
 *
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     8
 *
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    13
 * accompanied this code).
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    14
 *
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    18
 *
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    21
 * questions.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    22
 */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    23
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    24
/**
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    25
 * @test
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    26
 * @bug 6968063
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    27
 * @summary provide examples of code that generate diagnostics
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    28
 * @build Example HTMLWriter RunExamples
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    29
 * @run main RunExamples
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    30
 */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    31
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    32
import java.io.*;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    33
import java.text.SimpleDateFormat;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    34
import java.util.*;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    35
import java.util.regex.Matcher;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    36
import java.util.regex.Pattern;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    37
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    38
/**
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    39
 * Utility to run selected or all examples, writing results to
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    40
 * stdout, a plain text file or an HTML file. This program can be
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    41
 * run standalone, or as a jtreg test.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    42
 *
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    43
 * Options:
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    44
 *  -examples dir       directory of examples. Defaults to ${test.src}/examples
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    45
 *  -raw                run examples with -XDrawDiagnostics
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    46
 *  -showFiles          include text of source files in the output
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    47
 *  -verbose            verbose output
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    48
 *  -o file             write output to file: format will be HTML if
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    49
 *                      file has .html extension; otherwise it will be plain text.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    50
 *                      default is to stdout
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    51
 *  -title string       specify a title, only applies to HTML output
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    52
 */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    53
public class RunExamples {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    54
    public static void main(String... args) throws Exception {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    55
        boolean jtreg = (System.getProperty("test.src") != null);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    56
        File tmpDir;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    57
        if (jtreg) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    58
            // use standard jtreg scratch directory: the current directory
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    59
            tmpDir = new File(System.getProperty("user.dir"));
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    60
        } else {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    61
            tmpDir = new File(System.getProperty("java.io.tmpdir"),
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    62
                    RunExamples.class.getName()
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    63
                    + (new SimpleDateFormat("yyMMddHHmmss")).format(new Date()));
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    64
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    65
        Example.setTempDir(tmpDir);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    66
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    67
        RunExamples r = new RunExamples();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    68
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    69
        try {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    70
            if (r.run(args))
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    71
                return;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    72
        } finally {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    73
            /* VERY IMPORTANT NOTE. In jtreg mode, tmpDir is set to the
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    74
             * jtreg scratch directory, which is the current directory.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    75
             * In case someone is faking jtreg mode, make sure to only
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    76
             * clean tmpDir when it is reasonable to do so.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    77
             */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    78
            if (tmpDir.isDirectory() &&
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    79
                    tmpDir.getName().startsWith(RunExamples.class.getName())) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    80
                if (clean(tmpDir))
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    81
                    tmpDir.delete();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    82
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    83
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    84
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    85
        if (jtreg)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    86
            throw new Exception(r.errors + " errors occurred");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    87
        else
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    88
            System.exit(1);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    89
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    90
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    91
    boolean run(String... args) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    92
        Set<String> selectedKeys = new TreeSet<String>();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    93
        Set<Example> selectedExamples = new TreeSet<Example>();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    94
        File testSrc = new File(System.getProperty("test.src", "."));
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    95
        File examplesDir = new File(testSrc, "examples");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    96
        File outFile = null;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    97
        boolean raw = false;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    98
        boolean showFiles = false;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
    99
        boolean verbose = false;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   100
        String title = null;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   101
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   102
        for (int i = 0; i < args.length; i++) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   103
            String arg = args[i];
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   104
            if (arg.equals("-k") && (i + 1) < args.length)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   105
                selectedKeys.add(args[++i]);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   106
            else if (arg.equals("-examples") && (i + 1) < args.length)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   107
                examplesDir = new File(args[++i]);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   108
            else if (arg.equals("-raw"))
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   109
                raw = true;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   110
            else if (arg.equals("-showFiles"))
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   111
                showFiles = true;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   112
            else if (arg.equals("-verbose"))
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   113
                verbose = true;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   114
            else if (arg.equals("-o") && (i + 1) < args.length)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   115
                outFile = new File(args[++i]);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   116
            else if (arg.equals("-title") && (i + 1) < args.length)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   117
                title = args[++i];
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   118
            else if (arg.startsWith("-")) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   119
                error("unknown option: " + arg);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   120
                return false;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   121
            } else {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   122
                while (i < args.length) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   123
                    File f = new File(examplesDir, args[i]);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   124
                    selectedExamples.add(new Example(f));
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   125
                    i++;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   126
                }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   127
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   128
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   129
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   130
        if (selectedKeys.size() > 0) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   131
            Set<Example> examples = getExamples(examplesDir);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   132
        nextKey:
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   133
            for (String k: selectedKeys) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   134
                for (Example e: examples) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   135
                    if (e.getDeclaredKeys().contains(k))
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   136
                        continue nextKey;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   137
                }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   138
                error("Key " + k + ": no examples found");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   139
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   140
        } else {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   141
            if (selectedExamples.size() == 0)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   142
                selectedExamples = getExamples(examplesDir);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   143
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   144
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   145
        try {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   146
            Runner r;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   147
            if (outFile == null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   148
                PrintWriter out = new PrintWriter(System.out);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   149
                r = new TextRunner(out, showFiles, raw, verbose);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   150
            } else if (outFile.getName().endsWith(".html"))
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   151
                r = new HTMLRunner(outFile, showFiles, raw, verbose, title);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   152
            else
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   153
                r = new TextRunner(outFile, showFiles, raw, verbose);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   154
            r.run(selectedExamples);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   155
            r.close();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   156
        } catch (IOException e) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   157
            error("Error writing output: " + e);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   158
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   159
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   160
        return (errors == 0);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   161
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   162
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   163
    /**
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   164
     * Get the complete set of examples to be checked.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   165
     */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   166
    Set<Example> getExamples(File examplesDir) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   167
        Set<Example> results = new TreeSet<Example>();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   168
        for (File f: examplesDir.listFiles()) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   169
            if (f.isDirectory() || f.isFile() && f.getName().endsWith(".java"))
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   170
                results.add(new Example(f));
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   171
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   172
        return results;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   173
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   174
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   175
    /**
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   176
     * Report an error.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   177
     */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   178
    void error(String msg) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   179
        System.err.println("Error: " + msg);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   180
        errors++;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   181
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   182
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   183
    int errors;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   184
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   185
    /**
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   186
     * Clean the contents of a directory.
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   187
     */
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   188
    static boolean clean(File dir) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   189
        boolean ok = true;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   190
        for (File f: dir.listFiles()) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   191
            if (f.isDirectory())
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   192
                ok &= clean(f);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   193
            ok &= f.delete();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   194
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   195
        return ok;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   196
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   197
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   198
    static abstract class Runner {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   199
        Runner(boolean showFiles, boolean raw, boolean verbose) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   200
            this.showFiles = showFiles;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   201
            this.raw = raw;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   202
            this.verbose = verbose;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   203
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   204
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   205
        void close() throws IOException { }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   206
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   207
        void run(Collection<Example> examples) throws IOException {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   208
            for (Example e: examples) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   209
                startExample(e);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   210
                if (showFiles) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   211
                    showFile(e, e.infoFile);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   212
                    Set<File> srcFiles = new TreeSet<File>(e.srcFiles);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   213
                    srcFiles.remove(e.infoFile);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   214
                    showFiles(e, srcFiles);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   215
                    showFiles(e, e.srcPathFiles);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   216
                    showFiles(e, e.procFiles);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   217
                    showFiles(e, e.supportFiles);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   218
                }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   219
                run(e);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   220
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   221
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   222
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   223
        void showFiles(Example e, Collection<File> files) throws IOException {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   224
            for (File f: files)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   225
                showFile(e, f);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   226
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   227
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   228
        abstract void startExample(Example e) throws IOException;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   229
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   230
        abstract void showFile(Example e, File f) throws IOException;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   231
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   232
        abstract void run(Example e) throws IOException;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   233
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   234
        protected String read(File f) throws IOException {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   235
            byte[] bytes = new byte[(int) f.length()];
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   236
            DataInputStream in = new DataInputStream(new FileInputStream(f));
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   237
            try {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   238
                in.readFully(bytes);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   239
            } finally {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   240
                in.close();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   241
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   242
            return new String(bytes);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   243
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   244
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   245
        protected Pattern copyrightHeaderPat =
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   246
                Pattern.compile("(?s)(/\\*.*?Copyright.*?\\*/\n)\\s*(.*)");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   247
        protected Pattern infoHeaderPat =
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   248
                Pattern.compile("(?s)((?://\\s*[a-z]+:[^\n]*\n)+)\\s*(.*)");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   249
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   250
        protected boolean showFiles;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   251
        protected boolean raw;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   252
        protected boolean verbose;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   253
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   254
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   255
    static class TextRunner extends Runner {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   256
        TextRunner(File file, boolean showFiles, boolean raw, boolean verbose)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   257
                throws IOException {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   258
            super(showFiles, raw, verbose);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   259
            this.file = file;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   260
            out = new PrintWriter(new FileWriter(file));
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   261
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   262
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   263
        TextRunner(PrintWriter out, boolean showFiles, boolean raw, boolean verbose)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   264
                throws IOException {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   265
            super(showFiles, raw, verbose);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   266
            this.out = out;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   267
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   268
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   269
        @Override
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   270
        void close() {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   271
            if (file != null)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   272
                out.close();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   273
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   274
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   275
        @Override
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   276
        void startExample(Example e) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   277
            out.println("----- " + e.getName() + " --------------------");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   278
            out.println();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   279
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   280
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   281
        @Override
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   282
        void showFile(Example e, File f) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   283
            out.println("--- " + f);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   284
            String text;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   285
            try {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   286
                text = read(f);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   287
            } catch (IOException ex) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   288
                text = "Error reading " + f + "; " + ex;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   289
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   290
            Matcher m = copyrightHeaderPat.matcher(text);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   291
            if (m.matches()) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   292
                out.println("(Copyright)");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   293
                writeLines(m.group(2));
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   294
            } else {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   295
                writeLines(text);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   296
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   297
            out.println();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   298
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   299
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   300
        @Override
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   301
        void run(Example e) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   302
            // only show Output: header if also showing files
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   303
            if (showFiles)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   304
                out.println("--- Output:");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   305
            e.run(out, raw, verbose);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   306
            out.println();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   307
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   308
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   309
        void writeLines(String text) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   310
            for (String line: text.split("\n"))
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   311
                out.println(line);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   312
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   313
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   314
        File file;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   315
        PrintWriter out;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   316
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   317
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   318
    static class HTMLRunner extends Runner {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   319
        HTMLRunner(File file, boolean showFiles, boolean raw, boolean verbose, String title)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   320
                throws IOException {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   321
            super(showFiles, raw, verbose);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   322
            this.file = file;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   323
            PrintWriter out = new PrintWriter(new FileWriter(file));
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   324
            html = new HTMLWriter(out);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   325
            html.startTag(HTMLWriter.HEAD);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   326
            if (title != null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   327
                html.startTag(HTMLWriter.TITLE);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   328
                html.write(title);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   329
                html.endTag(HTMLWriter.TITLE);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   330
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   331
            html.startTag(HTMLWriter.STYLE);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   332
            html.newLine();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   333
            html.writeLine("div.file { background-color:#e0ffe0; margin-left:30px; margin-right:30px;\n"
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   334
                    + "  padding: 3px; border: thin solid silver; }");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   335
            html.writeLine("p.file { white-space: pre-wrap; font-family:monospace; margin: 0; }");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   336
            html.writeLine("div.output { background-color:#e0e0ff; margin-left:30px; margin-right:30px;\n"
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   337
                    + "  padding: 3px; border: thin solid silver; }");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   338
            html.writeLine("p.output { white-space: pre-wrap; font-family:monospace; margin: 0; }");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   339
            html.writeLine("table.index { border: thin solid silver; }");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   340
            html.writeLine(".copyright { font-size: x-small }");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   341
            html.writeLine(".hidden { display:none }");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   342
            html.writeLine(".unhidden { display:block }");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   343
            html.writeLine(".odd { background-color: #e0e0e0 }");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   344
            html.writeLine(".even { background-color: white }");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   345
            html.endTag(HTMLWriter.STYLE);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   346
            html.startTag(HTMLWriter.SCRIPT);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   347
            html.writeAttr(HTMLWriter.TYPE, HTMLWriter.TEXT_JAVASCRIPT);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   348
            html.writeLine("\nfunction unhide(id) {\n"
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   349
                        + "  var item = document.getElementById(id);\n"
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   350
                        + "  if (item) {\n"
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   351
                        + "    item.className=(item.className=='hidden')?'unhidden':'hidden';\n"
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   352
                        + "  }\n"
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   353
                        + "}");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   354
            html.endTag(HTMLWriter.SCRIPT);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   355
            html.endTag(HTMLWriter.HEAD);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   356
            html.startTag(HTMLWriter.BODY);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   357
            if (title != null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   358
                html.startTag(TITLE_HEADER);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   359
                html.write(title);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   360
                html.endTag(TITLE_HEADER);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   361
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   362
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   363
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   364
        @Override
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   365
        void close() throws IOException {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   366
            html.endTag(HTMLWriter.BODY);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   367
            html.newLine();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   368
            html.flush();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   369
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   370
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   371
        @Override
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   372
        void run(Collection<Example> examples) throws IOException {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   373
            if (examples.size() > 1)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   374
                writeIndex(examples);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   375
            super.run(examples);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   376
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   377
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   378
        void writeIndex(Collection<Example> examples) throws IOException {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   379
            Map<String, Set<Example>> index = new TreeMap<String, Set<Example>>();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   380
            Set<String> initials = new HashSet<String>();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   381
            for (Example e: examples) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   382
                for (String k: e.getDeclaredKeys()) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   383
                    Set<Example> s = index.get(k);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   384
                    if (s == null)
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   385
                        index.put(k, s = new TreeSet<Example>());
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   386
                    s.add(e);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   387
                }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   388
                initials.add(e.getName().substring(0, 1).toUpperCase());
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   389
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   390
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   391
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   392
            if (INDEX_HEADER != null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   393
                html.startTag(INDEX_HEADER);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   394
                html.write("Index");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   395
                html.endTag(INDEX_HEADER);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   396
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   397
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   398
            html.startTag(HTMLWriter.P);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   399
            html.writeLine("Examples: ");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   400
            for (char initial = 'A'; initial <= 'Z'; initial++) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   401
                String s = String.valueOf(initial);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   402
                if (initials.contains(s)) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   403
                    html.writeLink("#" + s, s);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   404
                } else {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   405
                    html.write(s);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   406
                }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   407
                html.newLine();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   408
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   409
            html.endTag(HTMLWriter.P);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   410
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   411
            html.startTag(HTMLWriter.TABLE);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   412
            html.writeAttr(HTMLWriter.CLASS, "index");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   413
            html.newLine();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   414
            int row = 0;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   415
            for (Map.Entry<String, Set<Example>> entry: index.entrySet()) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   416
                html.startTag(HTMLWriter.TR);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   417
                html.writeAttr(HTMLWriter.CLASS,
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   418
                        (row++ % 2 == 0 ? "even" : "odd"));
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   419
                html.startTag(HTMLWriter.TD);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   420
                html.writeAttr("valign", "top");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   421
                html.write(entry.getKey());
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   422
                html.endTag(HTMLWriter.TD);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   423
                html.newLine();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   424
                html.startTag(HTMLWriter.TD);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   425
                html.writeAttr(HTMLWriter.ALIGN, "top");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   426
                String sep = "";
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   427
                for (Example e: entry.getValue()) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   428
                    html.write(sep);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   429
                    html.writeLink('#' + e.getName(), e.getName());
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   430
                    sep = ", ";
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   431
                }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   432
                html.endTag(HTMLWriter.TD);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   433
                html.endTag(HTMLWriter.TR);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   434
                html.newLine();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   435
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   436
            html.endTag(HTMLWriter.TABLE);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   437
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   438
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   439
        @Override
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   440
        void startExample(Example e) throws IOException {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   441
            String name = e.getName();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   442
            String initial = name.substring(0, 1).toUpperCase();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   443
            if (!initial.equals(currInitial)) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   444
                html.writeLinkDestination(initial, "");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   445
                currInitial = initial;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   446
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   447
            html.writeLinkDestination(name, "");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   448
            html.startTag(EXAMPLE_HEADER);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   449
            html.write(e.getName());
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   450
            html.endTag(EXAMPLE_HEADER);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   451
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   452
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   453
        @Override
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   454
        void showFile(Example e, File f) throws IOException {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   455
            String text;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   456
            try {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   457
                text = read(f);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   458
            } catch (IOException ex) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   459
                text = "Error reading " + f + ": " + ex;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   460
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   461
            if (!f.equals(e.file)) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   462
                html.startTag(FILE_HEADER);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   463
                html.write(e.file.toURI().relativize(f.toURI()).toString());
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   464
                html.endTag(FILE_HEADER);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   465
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   466
            html.startTag(HTMLWriter.DIV);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   467
            html.writeAttr(CLASS, FILE);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   468
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   469
            String legalHeader;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   470
            Matcher m1 = copyrightHeaderPat.matcher(text);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   471
            if (m1.matches()) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   472
                legalHeader = m1.group(1);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   473
                text = m1.group(2);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   474
            } else
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   475
                legalHeader = null;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   476
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   477
            String infoHeader;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   478
            Matcher m2 = infoHeaderPat.matcher(text);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   479
            if (m2.matches()) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   480
                infoHeader = m2.group(1);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   481
                text = m2.group(2);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   482
            } else
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   483
                infoHeader = null;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   484
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   485
            String legalId = null, infoId = null;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   486
            if (legalHeader != null || infoHeader != null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   487
                String sep = "";
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   488
                html.startTag(HTMLWriter.SPAN);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   489
                html.writeStyleAttr("float: right");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   490
                if (legalHeader != null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   491
                    legalId = nextId();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   492
                    html.startTag(HTMLWriter.A);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   493
                    html.writeAttr(HTMLWriter.HREF, "javascript:unhide('" + legalId + "');");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   494
                    //html.writeEntity("&copy;");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   495
                    html.write("Copyright");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   496
                    html.endTag(HTMLWriter.A);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   497
                    sep = ", ";
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   498
                }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   499
                if (infoHeader != null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   500
                    html.write(sep);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   501
                    infoId = nextId();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   502
                    html.startTag(HTMLWriter.A);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   503
                    html.writeAttr(HTMLWriter.HREF, "javascript:unhide('" + infoId + "');");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   504
                    html.write("Info");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   505
                    html.endTag(HTMLWriter.A);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   506
                    sep = ", ";
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   507
                }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   508
                html.endTag(HTMLWriter.SPAN);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   509
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   510
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   511
            html.startTag(HTMLWriter.P);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   512
            html.writeAttr(CLASS, FILE);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   513
            if (legalHeader != null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   514
                html.startTag(HTMLWriter.SPAN);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   515
                html.writeAttr(HTMLWriter.CLASS, "hidden");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   516
                html.writeAttr(HTMLWriter.ID, legalId);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   517
                html.write(legalHeader);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   518
                html.newLine();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   519
                html.endTag(HTMLWriter.SPAN);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   520
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   521
            if (infoHeader != null) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   522
                html.startTag(HTMLWriter.SPAN);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   523
                html.writeAttr(HTMLWriter.CLASS, "hidden");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   524
                html.writeAttr(HTMLWriter.ID, infoId);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   525
                html.write(infoHeader);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   526
                html.newLine();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   527
                html.endTag(HTMLWriter.SPAN);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   528
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   529
            html.write(text);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   530
            html.endTag(HTMLWriter.P);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   531
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   532
            html.endTag(HTMLWriter.DIV);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   533
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   534
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   535
        @Override
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   536
        void run(Example e) throws IOException {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   537
            StringWriter sw = new StringWriter();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   538
            PrintWriter pw = new PrintWriter(sw);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   539
            e.run(pw, raw, verbose);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   540
            pw.flush();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   541
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   542
            // only show Output: header if also showing files
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   543
            if (showFiles) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   544
                html.startTag(OUTPUT_HEADER);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   545
                html.write("Output:");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   546
                html.endTag(OUTPUT_HEADER);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   547
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   548
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   549
            html.startTag(HTMLWriter.DIV);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   550
            html.writeAttr(CLASS, OUTPUT);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   551
            html.startTag(HTMLWriter.P);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   552
            html.writeAttr(CLASS, OUTPUT);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   553
            String[] lines = sw.toString().split("\n");
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   554
            for (String line: lines) {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   555
                html.write(line);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   556
                html.newLine();
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   557
            }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   558
            html.endTag(HTMLWriter.P);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   559
            html.endTag(HTMLWriter.DIV);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   560
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   561
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   562
        String nextId() {
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   563
            return "id" + (nextId++);
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   564
        }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   565
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   566
        File file;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   567
        HTMLWriter html;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   568
        int nextId;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   569
        String currInitial = "";
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   570
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   571
        static final String TITLE_HEADER = HTMLWriter.H3;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   572
        static final String INDEX_HEADER = HTMLWriter.H4;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   573
        static final String EXAMPLE_HEADER = HTMLWriter.H4;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   574
        static final String FILE_HEADER = HTMLWriter.H5;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   575
        static final String OUTPUT_HEADER = HTMLWriter.H5;
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   576
        static final String CLASS = "class";
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   577
        static final String FILE = "file";
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   578
        static final String OUTPUT = "output";
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   579
    }
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   580
}
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   581
48de3564aa13 6968063: provide examples of code that generate diagnostics
jjg
parents:
diff changeset
   582