src/share/classes/com/sun/tools/javac/tree/DocPretty.java

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1455
75ab654b5cd5
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

jjg@1409 1 /*
jjg@1409 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
jjg@1409 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1409 4 *
jjg@1409 5 * This code is free software; you can redistribute it and/or modify it
jjg@1409 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1409 7 * published by the Free Software Foundation. Oracle designates this
jjg@1409 8 * particular file as subject to the "Classpath" exception as provided
jjg@1409 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@1409 10 *
jjg@1409 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1409 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1409 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1409 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1409 15 * accompanied this code).
jjg@1409 16 *
jjg@1409 17 * You should have received a copy of the GNU General Public License version
jjg@1409 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1409 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1409 20 *
jjg@1409 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1409 22 * or visit www.oracle.com if you need additional information or have any
jjg@1409 23 * questions.
jjg@1409 24 */
jjg@1409 25
jjg@1409 26 package com.sun.tools.javac.tree;
jjg@1409 27
jjg@1409 28 import java.io.Writer;
jjg@1409 29
jjg@1409 30 import com.sun.source.doctree.*;
jjg@1409 31 import com.sun.source.doctree.AttributeTree.ValueKind;
jjg@1409 32 import com.sun.tools.javac.util.Convert;
jjg@1409 33 import java.io.IOException;
jjg@1409 34 import java.util.List;
jjg@1409 35
jjg@1409 36 /**
jjg@1409 37 * Prints out a doc comment tree.
jjg@1409 38 *
jjg@1409 39 * <p><b>This is NOT part of any supported API.
jjg@1409 40 * If you write code that depends on this, you do so at your own risk.
jjg@1409 41 * This code and its internal interfaces are subject to change or
jjg@1409 42 * deletion without notice.</b>
jjg@1409 43 */
jjg@1409 44 public class DocPretty implements DocTreeVisitor<Void,Void> {
jjg@1409 45
jjg@1409 46 /**
jjg@1409 47 * The output stream on which trees are printed.
jjg@1409 48 */
jjg@1409 49 final Writer out;
jjg@1409 50
jjg@1409 51 /**
jjg@1409 52 * The left margin.
jjg@1409 53 */
jjg@1409 54 int lmargin = 0;
jjg@1409 55
jjg@1409 56 public DocPretty(Writer out) {
jjg@1409 57 this.out = out;
jjg@1409 58 }
jjg@1409 59
jjg@1409 60 /** Visitor method: print expression tree.
jjg@1409 61 */
jjg@1409 62 public void print(DocTree tree) throws IOException {
jjg@1409 63 try {
jjg@1409 64 if (tree == null)
jjg@1409 65 print("/*missing*/");
jjg@1409 66 else {
jjg@1409 67 tree.accept(this, null);
jjg@1409 68 }
jjg@1409 69 } catch (UncheckedIOException ex) {
jjg@1409 70 throw new IOException(ex.getMessage(), ex);
jjg@1409 71 }
jjg@1409 72 }
jjg@1409 73
jjg@1409 74 /**
jjg@1409 75 * Print string, replacing all non-ascii character with unicode escapes.
jjg@1409 76 */
jjg@1409 77 protected void print(Object s) throws IOException {
jjg@1409 78 out.write(Convert.escapeUnicode(s.toString()));
jjg@1409 79 }
jjg@1409 80
jjg@1409 81 /**
jjg@1409 82 * Print list.
jjg@1409 83 */
jjg@1455 84 public void print(List<? extends DocTree> list) throws IOException {
jjg@1409 85 for (DocTree t: list) {
jjg@1409 86 print(t);
jjg@1409 87 }
jjg@1409 88 }
jjg@1409 89
jjg@1409 90 /**
jjg@1409 91 * Print list., with separators
jjg@1409 92 */
jjg@1409 93 protected void print(List<? extends DocTree> list, String sep) throws IOException {
jjg@1409 94 if (list.isEmpty())
jjg@1409 95 return;
jjg@1409 96 boolean first = true;
jjg@1409 97 for (DocTree t: list) {
jjg@1409 98 if (!first)
jjg@1409 99 print(sep);
jjg@1409 100 print(t);
jjg@1409 101 first = false;
jjg@1409 102 }
jjg@1409 103 }
jjg@1409 104
jjg@1409 105 /** Print new line.
jjg@1409 106 */
jjg@1409 107 protected void println() throws IOException {
jjg@1409 108 out.write(lineSep);
jjg@1409 109 }
jjg@1409 110
jjg@1409 111 protected void printTagName(DocTree node) throws IOException {
jjg@1409 112 out.write("@");
jjg@1409 113 out.write(node.getKind().tagName);
jjg@1409 114 }
jjg@1409 115
jjg@1409 116 final String lineSep = System.getProperty("line.separator");
jjg@1409 117
jjg@1409 118 /**************************************************************************
jjg@1409 119 * Traversal methods
jjg@1409 120 *************************************************************************/
jjg@1409 121
jjg@1409 122 /** Exception to propagate IOException through visitXXX methods */
jjg@1409 123 private static class UncheckedIOException extends Error {
jjg@1409 124 static final long serialVersionUID = -4032692679158424751L;
jjg@1409 125 UncheckedIOException(IOException e) {
jjg@1409 126 super(e.getMessage(), e);
jjg@1409 127 }
jjg@1409 128 }
jjg@1409 129
jjg@1409 130
jjg@1409 131 public Void visitAttribute(AttributeTree node, Void p) {
jjg@1409 132 try {
jjg@1409 133 print(node.getName());
jjg@1409 134 String quote;
jjg@1409 135 switch (node.getValueKind()) {
jjg@1409 136 case EMPTY:
jjg@1409 137 quote = null;
jjg@1409 138 break;
jjg@1409 139 case UNQUOTED:
jjg@1409 140 quote = "";
jjg@1409 141 break;
jjg@1409 142 case SINGLE:
jjg@1409 143 quote = "'";
jjg@1409 144 break;
jjg@1409 145 case DOUBLE:
jjg@1409 146 quote = "\"";
jjg@1409 147 break;
jjg@1409 148 default:
jjg@1409 149 throw new AssertionError();
jjg@1409 150 }
jjg@1409 151 if (quote != null) {
jjg@1409 152 print("=" + quote);
jjg@1409 153 print(node.getValue());
jjg@1409 154 print(quote);
jjg@1409 155 }
jjg@1409 156 } catch (IOException e) {
jjg@1409 157 throw new UncheckedIOException(e);
jjg@1409 158 }
jjg@1409 159 return null;
jjg@1409 160 }
jjg@1409 161
jjg@1409 162 public Void visitAuthor(AuthorTree node, Void p) {
jjg@1409 163 try {
jjg@1409 164 printTagName(node);
jjg@1409 165 print(" ");
jjg@1409 166 print(node.getName());
jjg@1409 167 } catch (IOException e) {
jjg@1409 168 throw new UncheckedIOException(e);
jjg@1409 169 }
jjg@1409 170 return null;
jjg@1409 171 }
jjg@1409 172
jjg@1409 173 public Void visitComment(CommentTree node, Void p) {
jjg@1409 174 try {
jjg@1409 175 print(node.getBody());
jjg@1409 176 } catch (IOException e) {
jjg@1409 177 throw new UncheckedIOException(e);
jjg@1409 178 }
jjg@1409 179 return null;
jjg@1409 180 }
jjg@1409 181
jjg@1409 182 public Void visitDeprecated(DeprecatedTree node, Void p) {
jjg@1409 183 try {
jjg@1409 184 printTagName(node);
jjg@1409 185 if (!node.getBody().isEmpty()) {
jjg@1409 186 print(" ");
jjg@1409 187 print(node.getBody());
jjg@1409 188 }
jjg@1409 189 } catch (IOException e) {
jjg@1409 190 throw new UncheckedIOException(e);
jjg@1409 191 }
jjg@1409 192 return null;
jjg@1409 193 }
jjg@1409 194
jjg@1409 195 public Void visitDocComment(DocCommentTree node, Void p) {
jjg@1409 196 try {
jjg@1409 197 List<? extends DocTree> fs = node.getFirstSentence();
jjg@1409 198 List<? extends DocTree> b = node.getBody();
jjg@1409 199 List<? extends DocTree> t = node.getBlockTags();
jjg@1409 200 print(fs);
jjg@1409 201 if (!fs.isEmpty() && !b.isEmpty())
jjg@1409 202 print(" ");
jjg@1409 203 print(b);
jjg@1409 204 if ((!fs.isEmpty() || !b.isEmpty()) && !t.isEmpty())
jjg@1409 205 print("\n");
jjg@1409 206 print(t, "\n");
jjg@1409 207 } catch (IOException e) {
jjg@1409 208 throw new UncheckedIOException(e);
jjg@1409 209 }
jjg@1409 210 return null;
jjg@1409 211 }
jjg@1409 212
jjg@1409 213 public Void visitDocRoot(DocRootTree node, Void p) {
jjg@1409 214 try {
jjg@1409 215 print("{");
jjg@1409 216 printTagName(node);
jjg@1409 217 print("}");
jjg@1409 218 } catch (IOException e) {
jjg@1409 219 throw new UncheckedIOException(e);
jjg@1409 220 }
jjg@1409 221 return null;
jjg@1409 222 }
jjg@1409 223
jjg@1409 224 public Void visitEndElement(EndElementTree node, Void p) {
jjg@1409 225 try {
jjg@1409 226 print("</");
jjg@1409 227 print(node.getName());
jjg@1409 228 print(">");
jjg@1409 229 } catch (IOException e) {
jjg@1409 230 throw new UncheckedIOException(e);
jjg@1409 231 }
jjg@1409 232 return null;
jjg@1409 233 }
jjg@1409 234
jjg@1409 235 public Void visitEntity(EntityTree node, Void p) {
jjg@1409 236 try {
jjg@1409 237 print("&");
jjg@1409 238 print(node.getName());
jjg@1409 239 print(";");
jjg@1409 240 } catch (IOException e) {
jjg@1409 241 throw new UncheckedIOException(e);
jjg@1409 242 }
jjg@1409 243 return null;
jjg@1409 244 }
jjg@1409 245
jjg@1409 246 public Void visitErroneous(ErroneousTree node, Void p) {
jjg@1409 247 try {
jjg@1409 248 print(node.getBody());
jjg@1409 249 } catch (IOException e) {
jjg@1409 250 throw new UncheckedIOException(e);
jjg@1409 251 }
jjg@1409 252 return null;
jjg@1409 253 }
jjg@1409 254
jjg@1409 255 public Void visitIdentifier(IdentifierTree node, Void p) {
jjg@1409 256 try {
jjg@1409 257 print(node.getName());
jjg@1409 258 } catch (IOException e) {
jjg@1409 259 throw new UncheckedIOException(e);
jjg@1409 260 }
jjg@1409 261 return null;
jjg@1409 262 }
jjg@1409 263
jjg@1409 264 public Void visitInheritDoc(InheritDocTree node, Void p) {
jjg@1409 265 try {
jjg@1409 266 print("{");
jjg@1409 267 printTagName(node);
jjg@1409 268 print("}");
jjg@1409 269 } catch (IOException e) {
jjg@1409 270 throw new UncheckedIOException(e);
jjg@1409 271 }
jjg@1409 272 return null;
jjg@1409 273 }
jjg@1409 274
jjg@1409 275 public Void visitLink(LinkTree node, Void p) {
jjg@1409 276 try {
jjg@1409 277 print("{");
jjg@1409 278 printTagName(node);
jjg@1409 279 print(" ");
jjg@1409 280 print(node.getReference());
jjg@1409 281 if (!node.getLabel().isEmpty()) {
jjg@1409 282 print(" ");
jjg@1409 283 print(node.getLabel());
jjg@1409 284 }
jjg@1409 285 print("}");
jjg@1409 286 } catch (IOException e) {
jjg@1409 287 throw new UncheckedIOException(e);
jjg@1409 288 }
jjg@1409 289 return null;
jjg@1409 290 }
jjg@1409 291
jjg@1409 292 public Void visitLiteral(LiteralTree node, Void p) {
jjg@1409 293 try {
jjg@1409 294 print("{");
jjg@1409 295 printTagName(node);
jjg@1409 296 print(" ");
jjg@1409 297 print(node.getBody());
jjg@1409 298 print("}");
jjg@1409 299 } catch (IOException e) {
jjg@1409 300 throw new UncheckedIOException(e);
jjg@1409 301 }
jjg@1409 302 return null;
jjg@1409 303 }
jjg@1409 304
jjg@1409 305 public Void visitParam(ParamTree node, Void p) {
jjg@1409 306 try {
jjg@1409 307 printTagName(node);
jjg@1409 308 print(" ");
jjg@1409 309 if (node.isTypeParameter()) print("<");
jjg@1409 310 print(node.getName());
jjg@1409 311 if (node.isTypeParameter()) print(">");
jjg@1409 312 if (!node.getDescription().isEmpty()) {
jjg@1409 313 print(" ");
jjg@1409 314 print(node.getDescription());
jjg@1409 315 }
jjg@1409 316 } catch (IOException e) {
jjg@1409 317 throw new UncheckedIOException(e);
jjg@1409 318 }
jjg@1409 319 return null;
jjg@1409 320 }
jjg@1409 321
jjg@1409 322 public Void visitReference(ReferenceTree node, Void p) {
jjg@1409 323 try {
jjg@1409 324 print(node.getSignature());
jjg@1409 325 } catch (IOException e) {
jjg@1409 326 throw new UncheckedIOException(e);
jjg@1409 327 }
jjg@1409 328 return null;
jjg@1409 329 }
jjg@1409 330
jjg@1409 331 public Void visitReturn(ReturnTree node, Void p) {
jjg@1409 332 try {
jjg@1409 333 printTagName(node);
jjg@1409 334 print(" ");
jjg@1409 335 print(node.getDescription());
jjg@1409 336 } catch (IOException e) {
jjg@1409 337 throw new UncheckedIOException(e);
jjg@1409 338 }
jjg@1409 339 return null;
jjg@1409 340 }
jjg@1409 341
jjg@1409 342 public Void visitSee(SeeTree node, Void p) {
jjg@1409 343 try {
jjg@1409 344 printTagName(node);
jjg@1409 345 boolean first = true;
jjg@1409 346 boolean needSep = true;
jjg@1409 347 for (DocTree t: node.getReference()) {
jjg@1409 348 if (needSep) print(" ");
jjg@1409 349 needSep = (first && (t instanceof ReferenceTree));
jjg@1409 350 first = false;
jjg@1409 351 print(t);
jjg@1409 352 }
jjg@1409 353 } catch (IOException e) {
jjg@1409 354 throw new UncheckedIOException(e);
jjg@1409 355 }
jjg@1409 356 return null;
jjg@1409 357 }
jjg@1409 358
jjg@1409 359 public Void visitSerial(SerialTree node, Void p) {
jjg@1409 360 try {
jjg@1409 361 printTagName(node);
jjg@1409 362 if (!node.getDescription().isEmpty()) {
jjg@1409 363 print(" ");
jjg@1409 364 print(node.getDescription());
jjg@1409 365 }
jjg@1409 366 } catch (IOException e) {
jjg@1409 367 throw new UncheckedIOException(e);
jjg@1409 368 }
jjg@1409 369 return null;
jjg@1409 370 }
jjg@1409 371
jjg@1409 372 public Void visitSerialData(SerialDataTree node, Void p) {
jjg@1409 373 try {
jjg@1409 374 printTagName(node);
jjg@1409 375 if (!node.getDescription().isEmpty()) {
jjg@1409 376 print(" ");
jjg@1409 377 print(node.getDescription());
jjg@1409 378 }
jjg@1409 379 } catch (IOException e) {
jjg@1409 380 throw new UncheckedIOException(e);
jjg@1409 381 }
jjg@1409 382 return null;
jjg@1409 383 }
jjg@1409 384
jjg@1409 385 public Void visitSerialField(SerialFieldTree node, Void p) {
jjg@1409 386 try {
jjg@1409 387 printTagName(node);
jjg@1409 388 print(" ");
jjg@1409 389 print(node.getName());
jjg@1409 390 print(" ");
jjg@1409 391 print(node.getType());
jjg@1409 392 if (!node.getDescription().isEmpty()) {
jjg@1409 393 print(" ");
jjg@1409 394 print(node.getDescription());
jjg@1409 395 }
jjg@1409 396 } catch (IOException e) {
jjg@1409 397 throw new UncheckedIOException(e);
jjg@1409 398 }
jjg@1409 399 return null;
jjg@1409 400 }
jjg@1409 401
jjg@1409 402 public Void visitSince(SinceTree node, Void p) {
jjg@1409 403 try {
jjg@1409 404 printTagName(node);
jjg@1409 405 print(" ");
jjg@1409 406 print(node.getBody());
jjg@1409 407 } catch (IOException e) {
jjg@1409 408 throw new UncheckedIOException(e);
jjg@1409 409 }
jjg@1409 410 return null;
jjg@1409 411 }
jjg@1409 412
jjg@1409 413 public Void visitStartElement(StartElementTree node, Void p) {
jjg@1409 414 try {
jjg@1409 415 print("<");
jjg@1409 416 print(node.getName());
jjg@1409 417 List<? extends DocTree> attrs = node.getAttributes();
jjg@1409 418 if (!attrs.isEmpty()) {
jjg@1409 419 print(" ");
jjg@1409 420 print(attrs);
jjg@1409 421 DocTree last = node.getAttributes().get(attrs.size() - 1);
jjg@1409 422 if (node.isSelfClosing() && last instanceof AttributeTree
jjg@1409 423 && ((AttributeTree) last).getValueKind() == ValueKind.UNQUOTED)
jjg@1409 424 print(" ");
jjg@1409 425 }
jjg@1409 426 if (node.isSelfClosing())
jjg@1409 427 print("/");
jjg@1409 428 print(">");
jjg@1409 429 } catch (IOException e) {
jjg@1409 430 throw new UncheckedIOException(e);
jjg@1409 431 }
jjg@1409 432 return null;
jjg@1409 433 }
jjg@1409 434
jjg@1409 435 public Void visitText(TextTree node, Void p) {
jjg@1409 436 try {
jjg@1409 437 print(node.getBody());
jjg@1409 438 } catch (IOException e) {
jjg@1409 439 throw new UncheckedIOException(e);
jjg@1409 440 }
jjg@1409 441 return null;
jjg@1409 442 }
jjg@1409 443
jjg@1409 444 public Void visitThrows(ThrowsTree node, Void p) {
jjg@1409 445 try {
jjg@1409 446 printTagName(node);
jjg@1409 447 print(" ");
jjg@1409 448 print(node.getExceptionName());
jjg@1409 449 if (!node.getDescription().isEmpty()) {
jjg@1409 450 print(" ");
jjg@1409 451 print(node.getDescription());
jjg@1409 452 }
jjg@1409 453 } catch (IOException e) {
jjg@1409 454 throw new UncheckedIOException(e);
jjg@1409 455 }
jjg@1409 456 return null;
jjg@1409 457 }
jjg@1409 458
jjg@1409 459 public Void visitUnknownBlockTag(UnknownBlockTagTree node, Void p) {
jjg@1409 460 try {
jjg@1409 461 print("@");
jjg@1409 462 print(node.getTagName());
jjg@1409 463 print(" ");
jjg@1409 464 print(node.getContent());
jjg@1409 465 } catch (IOException e) {
jjg@1409 466 throw new UncheckedIOException(e);
jjg@1409 467 }
jjg@1409 468 return null;
jjg@1409 469 }
jjg@1409 470
jjg@1409 471 public Void visitUnknownInlineTag(UnknownInlineTagTree node, Void p) {
jjg@1409 472 try {
jjg@1409 473 print("{");
jjg@1409 474 print("@");
jjg@1409 475 print(node.getTagName());
jjg@1409 476 print(" ");
jjg@1409 477 print(node.getContent());
jjg@1409 478 print("}");
jjg@1409 479 } catch (IOException e) {
jjg@1409 480 throw new UncheckedIOException(e);
jjg@1409 481 }
jjg@1409 482 return null;
jjg@1409 483 }
jjg@1409 484
jjg@1409 485 public Void visitValue(ValueTree node, Void p) {
jjg@1409 486 try {
jjg@1409 487 print("{");
jjg@1409 488 printTagName(node);
jjg@1409 489 if (node.getReference() != null) {
jjg@1409 490 print(" ");
jjg@1409 491 print(node.getReference());
jjg@1409 492 }
jjg@1409 493 print("}");
jjg@1409 494 } catch (IOException e) {
jjg@1409 495 throw new UncheckedIOException(e);
jjg@1409 496 }
jjg@1409 497 return null;
jjg@1409 498 }
jjg@1409 499
jjg@1409 500 public Void visitVersion(VersionTree node, Void p) {
jjg@1409 501 try {
jjg@1409 502 printTagName(node);
jjg@1409 503 print(" ");
jjg@1409 504 print(node.getBody());
jjg@1409 505 } catch (IOException e) {
jjg@1409 506 throw new UncheckedIOException(e);
jjg@1409 507 }
jjg@1409 508 return null;
jjg@1409 509 }
jjg@1409 510
jjg@1409 511 public Void visitOther(DocTree node, Void p) {
jjg@1409 512 try {
jjg@1409 513 print("(UNKNOWN: " + node + ")");
jjg@1409 514 println();
jjg@1409 515 } catch (IOException e) {
jjg@1409 516 throw new UncheckedIOException(e);
jjg@1409 517 }
jjg@1409 518 return null;
jjg@1409 519 }
jjg@1409 520 }

mercurial