Thu, 03 Feb 2011 09:36:28 +0000
5017953: spurious cascaded diagnostics when name not found
Summary: when an operator is applied to one or more erroneous operands, spurious diagnostics are generated
Reviewed-by: jjg
jjg@610 | 1 | /* |
jjg@842 | 2 | * Copyright (c) 2010, 2011, 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@842 | 28 | * @build ArgTypeCompilerFactory 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@842 | 100 | boolean argTypes = false; |
jjg@610 | 101 | String title = null; |
jjg@610 | 102 | |
jjg@610 | 103 | for (int i = 0; i < args.length; i++) { |
jjg@610 | 104 | String arg = args[i]; |
jjg@610 | 105 | if (arg.equals("-k") && (i + 1) < args.length) |
jjg@610 | 106 | selectedKeys.add(args[++i]); |
jjg@610 | 107 | else if (arg.equals("-examples") && (i + 1) < args.length) |
jjg@610 | 108 | examplesDir = new File(args[++i]); |
jjg@610 | 109 | else if (arg.equals("-raw")) |
jjg@610 | 110 | raw = true; |
jjg@610 | 111 | else if (arg.equals("-showFiles")) |
jjg@610 | 112 | showFiles = true; |
jjg@610 | 113 | else if (arg.equals("-verbose")) |
jjg@610 | 114 | verbose = true; |
jjg@610 | 115 | else if (arg.equals("-o") && (i + 1) < args.length) |
jjg@610 | 116 | outFile = new File(args[++i]); |
jjg@610 | 117 | else if (arg.equals("-title") && (i + 1) < args.length) |
jjg@610 | 118 | title = args[++i]; |
jjg@842 | 119 | else if (arg.equals("-argtypes")) |
jjg@842 | 120 | argTypes = true; |
jjg@610 | 121 | else if (arg.startsWith("-")) { |
jjg@610 | 122 | error("unknown option: " + arg); |
jjg@610 | 123 | return false; |
jjg@610 | 124 | } else { |
jjg@610 | 125 | while (i < args.length) { |
jjg@610 | 126 | File f = new File(examplesDir, args[i]); |
jjg@610 | 127 | selectedExamples.add(new Example(f)); |
jjg@610 | 128 | i++; |
jjg@610 | 129 | } |
jjg@610 | 130 | } |
jjg@610 | 131 | } |
jjg@610 | 132 | |
jjg@842 | 133 | // special mode to show message keys and the types of the args that |
jjg@842 | 134 | // are used. |
jjg@842 | 135 | if (argTypes) |
jjg@842 | 136 | Example.Compiler.factory = new ArgTypeCompilerFactory(); |
jjg@842 | 137 | |
jjg@610 | 138 | if (selectedKeys.size() > 0) { |
jjg@610 | 139 | Set<Example> examples = getExamples(examplesDir); |
jjg@610 | 140 | nextKey: |
jjg@610 | 141 | for (String k: selectedKeys) { |
jjg@610 | 142 | for (Example e: examples) { |
jjg@610 | 143 | if (e.getDeclaredKeys().contains(k)) |
jjg@610 | 144 | continue nextKey; |
jjg@610 | 145 | } |
jjg@610 | 146 | error("Key " + k + ": no examples found"); |
jjg@610 | 147 | } |
jjg@610 | 148 | } else { |
jjg@842 | 149 | if (selectedExamples.isEmpty()) |
jjg@610 | 150 | selectedExamples = getExamples(examplesDir); |
jjg@610 | 151 | } |
jjg@610 | 152 | |
jjg@610 | 153 | try { |
jjg@610 | 154 | Runner r; |
jjg@610 | 155 | if (outFile == null) { |
jjg@610 | 156 | PrintWriter out = new PrintWriter(System.out); |
jjg@610 | 157 | r = new TextRunner(out, showFiles, raw, verbose); |
jjg@610 | 158 | } else if (outFile.getName().endsWith(".html")) |
jjg@610 | 159 | r = new HTMLRunner(outFile, showFiles, raw, verbose, title); |
jjg@610 | 160 | else |
jjg@610 | 161 | r = new TextRunner(outFile, showFiles, raw, verbose); |
jjg@610 | 162 | r.run(selectedExamples); |
jjg@610 | 163 | r.close(); |
jjg@610 | 164 | } catch (IOException e) { |
jjg@610 | 165 | error("Error writing output: " + e); |
jjg@610 | 166 | } |
jjg@610 | 167 | |
jjg@610 | 168 | return (errors == 0); |
jjg@610 | 169 | } |
jjg@610 | 170 | |
jjg@610 | 171 | /** |
jjg@610 | 172 | * Get the complete set of examples to be checked. |
jjg@610 | 173 | */ |
jjg@610 | 174 | Set<Example> getExamples(File examplesDir) { |
jjg@610 | 175 | Set<Example> results = new TreeSet<Example>(); |
jjg@610 | 176 | for (File f: examplesDir.listFiles()) { |
mcimadamore@795 | 177 | if (isValidExample(f)) |
jjg@610 | 178 | results.add(new Example(f)); |
jjg@610 | 179 | } |
jjg@610 | 180 | return results; |
jjg@610 | 181 | } |
jjg@610 | 182 | |
mcimadamore@795 | 183 | boolean isValidExample(File f) { |
mcimadamore@795 | 184 | return (f.isDirectory() && (!jtreg || f.list().length > 0)) || |
mcimadamore@795 | 185 | (f.isFile() && f.getName().endsWith(".java")); |
mcimadamore@795 | 186 | } |
mcimadamore@795 | 187 | |
jjg@610 | 188 | /** |
jjg@610 | 189 | * Report an error. |
jjg@610 | 190 | */ |
jjg@610 | 191 | void error(String msg) { |
jjg@610 | 192 | System.err.println("Error: " + msg); |
jjg@610 | 193 | errors++; |
jjg@610 | 194 | } |
jjg@610 | 195 | |
mcimadamore@795 | 196 | static boolean jtreg; |
mcimadamore@795 | 197 | |
jjg@610 | 198 | int errors; |
jjg@610 | 199 | |
jjg@610 | 200 | /** |
jjg@610 | 201 | * Clean the contents of a directory. |
jjg@610 | 202 | */ |
jjg@610 | 203 | static boolean clean(File dir) { |
jjg@610 | 204 | boolean ok = true; |
jjg@610 | 205 | for (File f: dir.listFiles()) { |
jjg@610 | 206 | if (f.isDirectory()) |
jjg@610 | 207 | ok &= clean(f); |
jjg@610 | 208 | ok &= f.delete(); |
jjg@610 | 209 | } |
jjg@610 | 210 | return ok; |
jjg@610 | 211 | } |
jjg@610 | 212 | |
jjg@610 | 213 | static abstract class Runner { |
jjg@610 | 214 | Runner(boolean showFiles, boolean raw, boolean verbose) { |
jjg@610 | 215 | this.showFiles = showFiles; |
jjg@610 | 216 | this.raw = raw; |
jjg@610 | 217 | this.verbose = verbose; |
jjg@610 | 218 | } |
jjg@610 | 219 | |
jjg@610 | 220 | void close() throws IOException { } |
jjg@610 | 221 | |
jjg@610 | 222 | void run(Collection<Example> examples) throws IOException { |
jjg@610 | 223 | for (Example e: examples) { |
jjg@610 | 224 | startExample(e); |
jjg@610 | 225 | if (showFiles) { |
jjg@610 | 226 | showFile(e, e.infoFile); |
jjg@610 | 227 | Set<File> srcFiles = new TreeSet<File>(e.srcFiles); |
jjg@610 | 228 | srcFiles.remove(e.infoFile); |
jjg@610 | 229 | showFiles(e, srcFiles); |
jjg@610 | 230 | showFiles(e, e.srcPathFiles); |
jjg@610 | 231 | showFiles(e, e.procFiles); |
jjg@610 | 232 | showFiles(e, e.supportFiles); |
jjg@610 | 233 | } |
jjg@610 | 234 | run(e); |
jjg@610 | 235 | } |
jjg@610 | 236 | } |
jjg@610 | 237 | |
jjg@610 | 238 | void showFiles(Example e, Collection<File> files) throws IOException { |
jjg@610 | 239 | for (File f: files) |
jjg@610 | 240 | showFile(e, f); |
jjg@610 | 241 | } |
jjg@610 | 242 | |
jjg@610 | 243 | abstract void startExample(Example e) throws IOException; |
jjg@610 | 244 | |
jjg@610 | 245 | abstract void showFile(Example e, File f) throws IOException; |
jjg@610 | 246 | |
jjg@610 | 247 | abstract void run(Example e) throws IOException; |
jjg@610 | 248 | |
jjg@610 | 249 | protected String read(File f) throws IOException { |
jjg@610 | 250 | byte[] bytes = new byte[(int) f.length()]; |
jjg@610 | 251 | DataInputStream in = new DataInputStream(new FileInputStream(f)); |
jjg@610 | 252 | try { |
jjg@610 | 253 | in.readFully(bytes); |
jjg@610 | 254 | } finally { |
jjg@610 | 255 | in.close(); |
jjg@610 | 256 | } |
jjg@610 | 257 | return new String(bytes); |
jjg@610 | 258 | } |
jjg@610 | 259 | |
jjg@610 | 260 | protected Pattern copyrightHeaderPat = |
jjg@610 | 261 | Pattern.compile("(?s)(/\\*.*?Copyright.*?\\*/\n)\\s*(.*)"); |
jjg@610 | 262 | protected Pattern infoHeaderPat = |
jjg@610 | 263 | Pattern.compile("(?s)((?://\\s*[a-z]+:[^\n]*\n)+)\\s*(.*)"); |
jjg@610 | 264 | |
jjg@610 | 265 | protected boolean showFiles; |
jjg@610 | 266 | protected boolean raw; |
jjg@610 | 267 | protected boolean verbose; |
jjg@610 | 268 | } |
jjg@610 | 269 | |
jjg@610 | 270 | static class TextRunner extends Runner { |
jjg@610 | 271 | TextRunner(File file, boolean showFiles, boolean raw, boolean verbose) |
jjg@610 | 272 | throws IOException { |
jjg@610 | 273 | super(showFiles, raw, verbose); |
jjg@610 | 274 | this.file = file; |
jjg@610 | 275 | out = new PrintWriter(new FileWriter(file)); |
jjg@610 | 276 | } |
jjg@610 | 277 | |
jjg@610 | 278 | TextRunner(PrintWriter out, boolean showFiles, boolean raw, boolean verbose) |
jjg@610 | 279 | throws IOException { |
jjg@610 | 280 | super(showFiles, raw, verbose); |
jjg@610 | 281 | this.out = out; |
jjg@610 | 282 | } |
jjg@610 | 283 | |
jjg@610 | 284 | @Override |
jjg@610 | 285 | void close() { |
jjg@610 | 286 | if (file != null) |
jjg@610 | 287 | out.close(); |
jjg@610 | 288 | } |
jjg@610 | 289 | |
jjg@610 | 290 | @Override |
jjg@610 | 291 | void startExample(Example e) { |
jjg@610 | 292 | out.println("----- " + e.getName() + " --------------------"); |
jjg@610 | 293 | out.println(); |
jjg@610 | 294 | } |
jjg@610 | 295 | |
jjg@610 | 296 | @Override |
jjg@610 | 297 | void showFile(Example e, File f) { |
jjg@610 | 298 | out.println("--- " + f); |
jjg@610 | 299 | String text; |
jjg@610 | 300 | try { |
jjg@610 | 301 | text = read(f); |
jjg@610 | 302 | } catch (IOException ex) { |
jjg@610 | 303 | text = "Error reading " + f + "; " + ex; |
jjg@610 | 304 | } |
jjg@610 | 305 | Matcher m = copyrightHeaderPat.matcher(text); |
jjg@610 | 306 | if (m.matches()) { |
jjg@610 | 307 | out.println("(Copyright)"); |
jjg@610 | 308 | writeLines(m.group(2)); |
jjg@610 | 309 | } else { |
jjg@610 | 310 | writeLines(text); |
jjg@610 | 311 | } |
jjg@610 | 312 | out.println(); |
jjg@610 | 313 | } |
jjg@610 | 314 | |
jjg@610 | 315 | @Override |
jjg@610 | 316 | void run(Example e) { |
jjg@610 | 317 | // only show Output: header if also showing files |
jjg@610 | 318 | if (showFiles) |
jjg@610 | 319 | out.println("--- Output:"); |
jjg@610 | 320 | e.run(out, raw, verbose); |
jjg@610 | 321 | out.println(); |
jjg@610 | 322 | } |
jjg@610 | 323 | |
jjg@610 | 324 | void writeLines(String text) { |
jjg@610 | 325 | for (String line: text.split("\n")) |
jjg@610 | 326 | out.println(line); |
jjg@610 | 327 | } |
jjg@610 | 328 | |
jjg@610 | 329 | File file; |
jjg@610 | 330 | PrintWriter out; |
jjg@610 | 331 | } |
jjg@610 | 332 | |
jjg@610 | 333 | static class HTMLRunner extends Runner { |
jjg@610 | 334 | HTMLRunner(File file, boolean showFiles, boolean raw, boolean verbose, String title) |
jjg@610 | 335 | throws IOException { |
jjg@610 | 336 | super(showFiles, raw, verbose); |
jjg@610 | 337 | this.file = file; |
jjg@610 | 338 | PrintWriter out = new PrintWriter(new FileWriter(file)); |
jjg@610 | 339 | html = new HTMLWriter(out); |
jjg@610 | 340 | html.startTag(HTMLWriter.HEAD); |
jjg@610 | 341 | if (title != null) { |
jjg@610 | 342 | html.startTag(HTMLWriter.TITLE); |
jjg@610 | 343 | html.write(title); |
jjg@610 | 344 | html.endTag(HTMLWriter.TITLE); |
jjg@610 | 345 | } |
jjg@610 | 346 | html.startTag(HTMLWriter.STYLE); |
jjg@610 | 347 | html.newLine(); |
jjg@610 | 348 | html.writeLine("div.file { background-color:#e0ffe0; margin-left:30px; margin-right:30px;\n" |
jjg@610 | 349 | + " padding: 3px; border: thin solid silver; }"); |
jjg@610 | 350 | html.writeLine("p.file { white-space: pre-wrap; font-family:monospace; margin: 0; }"); |
jjg@610 | 351 | html.writeLine("div.output { background-color:#e0e0ff; margin-left:30px; margin-right:30px;\n" |
jjg@610 | 352 | + " padding: 3px; border: thin solid silver; }"); |
jjg@610 | 353 | html.writeLine("p.output { white-space: pre-wrap; font-family:monospace; margin: 0; }"); |
jjg@610 | 354 | html.writeLine("table.index { border: thin solid silver; }"); |
jjg@610 | 355 | html.writeLine(".copyright { font-size: x-small }"); |
jjg@610 | 356 | html.writeLine(".hidden { display:none }"); |
jjg@610 | 357 | html.writeLine(".unhidden { display:block }"); |
jjg@610 | 358 | html.writeLine(".odd { background-color: #e0e0e0 }"); |
jjg@610 | 359 | html.writeLine(".even { background-color: white }"); |
jjg@610 | 360 | html.endTag(HTMLWriter.STYLE); |
jjg@610 | 361 | html.startTag(HTMLWriter.SCRIPT); |
jjg@610 | 362 | html.writeAttr(HTMLWriter.TYPE, HTMLWriter.TEXT_JAVASCRIPT); |
jjg@610 | 363 | html.writeLine("\nfunction unhide(id) {\n" |
jjg@610 | 364 | + " var item = document.getElementById(id);\n" |
jjg@610 | 365 | + " if (item) {\n" |
jjg@610 | 366 | + " item.className=(item.className=='hidden')?'unhidden':'hidden';\n" |
jjg@610 | 367 | + " }\n" |
jjg@610 | 368 | + "}"); |
jjg@610 | 369 | html.endTag(HTMLWriter.SCRIPT); |
jjg@610 | 370 | html.endTag(HTMLWriter.HEAD); |
jjg@610 | 371 | html.startTag(HTMLWriter.BODY); |
jjg@610 | 372 | if (title != null) { |
jjg@610 | 373 | html.startTag(TITLE_HEADER); |
jjg@610 | 374 | html.write(title); |
jjg@610 | 375 | html.endTag(TITLE_HEADER); |
jjg@610 | 376 | } |
jjg@610 | 377 | } |
jjg@610 | 378 | |
jjg@610 | 379 | @Override |
jjg@610 | 380 | void close() throws IOException { |
jjg@610 | 381 | html.endTag(HTMLWriter.BODY); |
jjg@610 | 382 | html.newLine(); |
jjg@610 | 383 | html.flush(); |
jjg@610 | 384 | } |
jjg@610 | 385 | |
jjg@610 | 386 | @Override |
jjg@610 | 387 | void run(Collection<Example> examples) throws IOException { |
jjg@610 | 388 | if (examples.size() > 1) |
jjg@610 | 389 | writeIndex(examples); |
jjg@610 | 390 | super.run(examples); |
jjg@610 | 391 | } |
jjg@610 | 392 | |
jjg@610 | 393 | void writeIndex(Collection<Example> examples) throws IOException { |
jjg@610 | 394 | Map<String, Set<Example>> index = new TreeMap<String, Set<Example>>(); |
jjg@610 | 395 | Set<String> initials = new HashSet<String>(); |
jjg@610 | 396 | for (Example e: examples) { |
jjg@610 | 397 | for (String k: e.getDeclaredKeys()) { |
jjg@610 | 398 | Set<Example> s = index.get(k); |
jjg@610 | 399 | if (s == null) |
jjg@610 | 400 | index.put(k, s = new TreeSet<Example>()); |
jjg@610 | 401 | s.add(e); |
jjg@610 | 402 | } |
jjg@610 | 403 | initials.add(e.getName().substring(0, 1).toUpperCase()); |
jjg@610 | 404 | } |
jjg@610 | 405 | |
jjg@610 | 406 | |
jjg@610 | 407 | if (INDEX_HEADER != null) { |
jjg@610 | 408 | html.startTag(INDEX_HEADER); |
jjg@610 | 409 | html.write("Index"); |
jjg@610 | 410 | html.endTag(INDEX_HEADER); |
jjg@610 | 411 | } |
jjg@610 | 412 | |
jjg@610 | 413 | html.startTag(HTMLWriter.P); |
jjg@610 | 414 | html.writeLine("Examples: "); |
jjg@610 | 415 | for (char initial = 'A'; initial <= 'Z'; initial++) { |
jjg@610 | 416 | String s = String.valueOf(initial); |
jjg@610 | 417 | if (initials.contains(s)) { |
jjg@610 | 418 | html.writeLink("#" + s, s); |
jjg@610 | 419 | } else { |
jjg@610 | 420 | html.write(s); |
jjg@610 | 421 | } |
jjg@610 | 422 | html.newLine(); |
jjg@610 | 423 | } |
jjg@610 | 424 | html.endTag(HTMLWriter.P); |
jjg@610 | 425 | |
jjg@610 | 426 | html.startTag(HTMLWriter.TABLE); |
jjg@610 | 427 | html.writeAttr(HTMLWriter.CLASS, "index"); |
jjg@610 | 428 | html.newLine(); |
jjg@610 | 429 | int row = 0; |
jjg@610 | 430 | for (Map.Entry<String, Set<Example>> entry: index.entrySet()) { |
jjg@610 | 431 | html.startTag(HTMLWriter.TR); |
jjg@610 | 432 | html.writeAttr(HTMLWriter.CLASS, |
jjg@610 | 433 | (row++ % 2 == 0 ? "even" : "odd")); |
jjg@610 | 434 | html.startTag(HTMLWriter.TD); |
jjg@610 | 435 | html.writeAttr("valign", "top"); |
jjg@610 | 436 | html.write(entry.getKey()); |
jjg@610 | 437 | html.endTag(HTMLWriter.TD); |
jjg@610 | 438 | html.newLine(); |
jjg@610 | 439 | html.startTag(HTMLWriter.TD); |
jjg@610 | 440 | html.writeAttr(HTMLWriter.ALIGN, "top"); |
jjg@610 | 441 | String sep = ""; |
jjg@610 | 442 | for (Example e: entry.getValue()) { |
jjg@610 | 443 | html.write(sep); |
jjg@610 | 444 | html.writeLink('#' + e.getName(), e.getName()); |
jjg@610 | 445 | sep = ", "; |
jjg@610 | 446 | } |
jjg@610 | 447 | html.endTag(HTMLWriter.TD); |
jjg@610 | 448 | html.endTag(HTMLWriter.TR); |
jjg@610 | 449 | html.newLine(); |
jjg@610 | 450 | } |
jjg@610 | 451 | html.endTag(HTMLWriter.TABLE); |
jjg@610 | 452 | } |
jjg@610 | 453 | |
jjg@610 | 454 | @Override |
jjg@610 | 455 | void startExample(Example e) throws IOException { |
jjg@610 | 456 | String name = e.getName(); |
jjg@610 | 457 | String initial = name.substring(0, 1).toUpperCase(); |
jjg@610 | 458 | if (!initial.equals(currInitial)) { |
jjg@610 | 459 | html.writeLinkDestination(initial, ""); |
jjg@610 | 460 | currInitial = initial; |
jjg@610 | 461 | } |
jjg@610 | 462 | html.writeLinkDestination(name, ""); |
jjg@610 | 463 | html.startTag(EXAMPLE_HEADER); |
jjg@610 | 464 | html.write(e.getName()); |
jjg@610 | 465 | html.endTag(EXAMPLE_HEADER); |
jjg@610 | 466 | } |
jjg@610 | 467 | |
jjg@610 | 468 | @Override |
jjg@610 | 469 | void showFile(Example e, File f) throws IOException { |
jjg@610 | 470 | String text; |
jjg@610 | 471 | try { |
jjg@610 | 472 | text = read(f); |
jjg@610 | 473 | } catch (IOException ex) { |
jjg@610 | 474 | text = "Error reading " + f + ": " + ex; |
jjg@610 | 475 | } |
jjg@610 | 476 | if (!f.equals(e.file)) { |
jjg@610 | 477 | html.startTag(FILE_HEADER); |
jjg@610 | 478 | html.write(e.file.toURI().relativize(f.toURI()).toString()); |
jjg@610 | 479 | html.endTag(FILE_HEADER); |
jjg@610 | 480 | } |
jjg@610 | 481 | html.startTag(HTMLWriter.DIV); |
jjg@610 | 482 | html.writeAttr(CLASS, FILE); |
jjg@610 | 483 | |
jjg@610 | 484 | String legalHeader; |
jjg@610 | 485 | Matcher m1 = copyrightHeaderPat.matcher(text); |
jjg@610 | 486 | if (m1.matches()) { |
jjg@610 | 487 | legalHeader = m1.group(1); |
jjg@610 | 488 | text = m1.group(2); |
jjg@610 | 489 | } else |
jjg@610 | 490 | legalHeader = null; |
jjg@610 | 491 | |
jjg@610 | 492 | String infoHeader; |
jjg@610 | 493 | Matcher m2 = infoHeaderPat.matcher(text); |
jjg@610 | 494 | if (m2.matches()) { |
jjg@610 | 495 | infoHeader = m2.group(1); |
jjg@610 | 496 | text = m2.group(2); |
jjg@610 | 497 | } else |
jjg@610 | 498 | infoHeader = null; |
jjg@610 | 499 | |
jjg@610 | 500 | String legalId = null, infoId = null; |
jjg@610 | 501 | if (legalHeader != null || infoHeader != null) { |
jjg@610 | 502 | String sep = ""; |
jjg@610 | 503 | html.startTag(HTMLWriter.SPAN); |
jjg@610 | 504 | html.writeStyleAttr("float: right"); |
jjg@610 | 505 | if (legalHeader != null) { |
jjg@610 | 506 | legalId = nextId(); |
jjg@610 | 507 | html.startTag(HTMLWriter.A); |
jjg@610 | 508 | html.writeAttr(HTMLWriter.HREF, "javascript:unhide('" + legalId + "');"); |
jjg@610 | 509 | //html.writeEntity("©"); |
jjg@610 | 510 | html.write("Copyright"); |
jjg@610 | 511 | html.endTag(HTMLWriter.A); |
jjg@610 | 512 | sep = ", "; |
jjg@610 | 513 | } |
jjg@610 | 514 | if (infoHeader != null) { |
jjg@610 | 515 | html.write(sep); |
jjg@610 | 516 | infoId = nextId(); |
jjg@610 | 517 | html.startTag(HTMLWriter.A); |
jjg@610 | 518 | html.writeAttr(HTMLWriter.HREF, "javascript:unhide('" + infoId + "');"); |
jjg@610 | 519 | html.write("Info"); |
jjg@610 | 520 | html.endTag(HTMLWriter.A); |
jjg@610 | 521 | sep = ", "; |
jjg@610 | 522 | } |
jjg@610 | 523 | html.endTag(HTMLWriter.SPAN); |
jjg@610 | 524 | } |
jjg@610 | 525 | |
jjg@610 | 526 | html.startTag(HTMLWriter.P); |
jjg@610 | 527 | html.writeAttr(CLASS, FILE); |
jjg@610 | 528 | if (legalHeader != null) { |
jjg@610 | 529 | html.startTag(HTMLWriter.SPAN); |
jjg@610 | 530 | html.writeAttr(HTMLWriter.CLASS, "hidden"); |
jjg@610 | 531 | html.writeAttr(HTMLWriter.ID, legalId); |
jjg@610 | 532 | html.write(legalHeader); |
jjg@610 | 533 | html.newLine(); |
jjg@610 | 534 | html.endTag(HTMLWriter.SPAN); |
jjg@610 | 535 | } |
jjg@610 | 536 | if (infoHeader != null) { |
jjg@610 | 537 | html.startTag(HTMLWriter.SPAN); |
jjg@610 | 538 | html.writeAttr(HTMLWriter.CLASS, "hidden"); |
jjg@610 | 539 | html.writeAttr(HTMLWriter.ID, infoId); |
jjg@610 | 540 | html.write(infoHeader); |
jjg@610 | 541 | html.newLine(); |
jjg@610 | 542 | html.endTag(HTMLWriter.SPAN); |
jjg@610 | 543 | } |
jjg@610 | 544 | html.write(text); |
jjg@610 | 545 | html.endTag(HTMLWriter.P); |
jjg@610 | 546 | |
jjg@610 | 547 | html.endTag(HTMLWriter.DIV); |
jjg@610 | 548 | } |
jjg@610 | 549 | |
jjg@610 | 550 | @Override |
jjg@610 | 551 | void run(Example e) throws IOException { |
jjg@610 | 552 | StringWriter sw = new StringWriter(); |
jjg@610 | 553 | PrintWriter pw = new PrintWriter(sw); |
jjg@610 | 554 | e.run(pw, raw, verbose); |
jjg@610 | 555 | pw.flush(); |
jjg@610 | 556 | |
jjg@610 | 557 | // only show Output: header if also showing files |
jjg@610 | 558 | if (showFiles) { |
jjg@610 | 559 | html.startTag(OUTPUT_HEADER); |
jjg@610 | 560 | html.write("Output:"); |
jjg@610 | 561 | html.endTag(OUTPUT_HEADER); |
jjg@610 | 562 | } |
jjg@610 | 563 | |
jjg@610 | 564 | html.startTag(HTMLWriter.DIV); |
jjg@610 | 565 | html.writeAttr(CLASS, OUTPUT); |
jjg@610 | 566 | html.startTag(HTMLWriter.P); |
jjg@610 | 567 | html.writeAttr(CLASS, OUTPUT); |
jjg@610 | 568 | String[] lines = sw.toString().split("\n"); |
jjg@610 | 569 | for (String line: lines) { |
jjg@610 | 570 | html.write(line); |
jjg@610 | 571 | html.newLine(); |
jjg@610 | 572 | } |
jjg@610 | 573 | html.endTag(HTMLWriter.P); |
jjg@610 | 574 | html.endTag(HTMLWriter.DIV); |
jjg@610 | 575 | } |
jjg@610 | 576 | |
jjg@610 | 577 | String nextId() { |
jjg@610 | 578 | return "id" + (nextId++); |
jjg@610 | 579 | } |
jjg@610 | 580 | |
jjg@610 | 581 | File file; |
jjg@610 | 582 | HTMLWriter html; |
jjg@610 | 583 | int nextId; |
jjg@610 | 584 | String currInitial = ""; |
jjg@610 | 585 | |
jjg@610 | 586 | static final String TITLE_HEADER = HTMLWriter.H3; |
jjg@610 | 587 | static final String INDEX_HEADER = HTMLWriter.H4; |
jjg@610 | 588 | static final String EXAMPLE_HEADER = HTMLWriter.H4; |
jjg@610 | 589 | static final String FILE_HEADER = HTMLWriter.H5; |
jjg@610 | 590 | static final String OUTPUT_HEADER = HTMLWriter.H5; |
jjg@610 | 591 | static final String CLASS = "class"; |
jjg@610 | 592 | static final String FILE = "file"; |
jjg@610 | 593 | static final String OUTPUT = "output"; |
jjg@610 | 594 | } |
jjg@610 | 595 | } |
jjg@610 | 596 | |
jjg@610 | 597 |