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