test/tools/javac/diags/RunExamples.java

Mon, 13 Dec 2010 15:11:00 -0800

author
mcimadamore
date
Mon, 13 Dec 2010 15:11:00 -0800
changeset 795
7b99f98b3035
parent 610
3640b60bd0f6
child 842
3da26790ccb7
permissions
-rw-r--r--

6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
Reviewed-by: jjg, darcy

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

mercurial