src/share/classes/com/sun/tools/doclint/Checker.java

Tue, 19 Mar 2013 19:16:59 -0700

author
jjg
date
Tue, 19 Mar 2013 19:16:59 -0700
changeset 1650
74d7f9bcac93
parent 1552
153d20d0cac5
child 1668
991f11e13598
permissions
-rw-r--r--

8010317: DocLint incorrectly reports some <pre> tags as empty
Reviewed-by: darcy

jjg@1455 1 /*
jjg@1495 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1455 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1455 4 *
jjg@1455 5 * This code is free software; you can redistribute it and/or modify it
jjg@1455 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1455 7 * published by the Free Software Foundation. Oracle designates this
jjg@1455 8 * particular file as subject to the "Classpath" exception as provided
jjg@1455 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@1455 10 *
jjg@1455 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1455 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1455 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1455 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1455 15 * accompanied this code).
jjg@1455 16 *
jjg@1455 17 * You should have received a copy of the GNU General Public License version
jjg@1455 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1455 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1455 20 *
jjg@1455 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1455 22 * or visit www.oracle.com if you need additional information or have any
jjg@1455 23 * questions.
jjg@1455 24 */
jjg@1455 25
jjg@1455 26 package com.sun.tools.doclint;
jjg@1455 27
jjg@1650 28 import java.io.IOException;
jjg@1650 29 import java.io.StringWriter;
jjg@1455 30 import java.net.URI;
jjg@1650 31 import java.net.URISyntaxException;
jjg@1455 32 import java.util.Deque;
jjg@1455 33 import java.util.EnumSet;
jjg@1455 34 import java.util.HashSet;
jjg@1455 35 import java.util.LinkedList;
jjg@1455 36 import java.util.List;
jjg@1455 37 import java.util.Set;
jjg@1650 38 import java.util.regex.Matcher;
jjg@1650 39 import java.util.regex.Pattern;
jjg@1455 40
jjg@1455 41 import javax.lang.model.element.Element;
jjg@1455 42 import javax.lang.model.element.ElementKind;
jjg@1455 43 import javax.lang.model.element.ExecutableElement;
jjg@1455 44 import javax.lang.model.element.Name;
jjg@1455 45 import javax.lang.model.element.TypeElement;
jjg@1455 46 import javax.lang.model.type.TypeKind;
jjg@1455 47 import javax.lang.model.type.TypeMirror;
jjg@1455 48 import javax.tools.Diagnostic.Kind;
jjg@1455 49
jjg@1455 50 import com.sun.source.doctree.AttributeTree;
jjg@1455 51 import com.sun.source.doctree.AuthorTree;
jjg@1455 52 import com.sun.source.doctree.DocCommentTree;
jjg@1650 53 import com.sun.source.doctree.DocRootTree;
jjg@1455 54 import com.sun.source.doctree.DocTree;
jjg@1455 55 import com.sun.source.doctree.EndElementTree;
jjg@1455 56 import com.sun.source.doctree.EntityTree;
jjg@1455 57 import com.sun.source.doctree.ErroneousTree;
jjg@1455 58 import com.sun.source.doctree.IdentifierTree;
jjg@1455 59 import com.sun.source.doctree.InheritDocTree;
jjg@1650 60 import com.sun.source.doctree.LinkTree;
jjg@1650 61 import com.sun.source.doctree.LiteralTree;
jjg@1455 62 import com.sun.source.doctree.ParamTree;
jjg@1455 63 import com.sun.source.doctree.ReferenceTree;
jjg@1455 64 import com.sun.source.doctree.ReturnTree;
jjg@1455 65 import com.sun.source.doctree.SerialDataTree;
jjg@1455 66 import com.sun.source.doctree.SerialFieldTree;
jjg@1455 67 import com.sun.source.doctree.SinceTree;
jjg@1455 68 import com.sun.source.doctree.StartElementTree;
jjg@1455 69 import com.sun.source.doctree.TextTree;
jjg@1455 70 import com.sun.source.doctree.ThrowsTree;
jjg@1650 71 import com.sun.source.doctree.ValueTree;
jjg@1455 72 import com.sun.source.doctree.VersionTree;
jjg@1455 73 import com.sun.source.util.DocTreeScanner;
jjg@1455 74 import com.sun.source.util.TreePath;
jjg@1455 75 import com.sun.tools.doclint.HtmlTag.AttrKind;
jjg@1650 76 import com.sun.tools.javac.tree.DocPretty;
jjg@1455 77 import static com.sun.tools.doclint.Messages.Group.*;
jjg@1455 78
jjg@1455 79
jjg@1455 80 /**
jjg@1455 81 * Validate a doc comment.
jjg@1455 82 *
jjg@1455 83 * <p><b>This is NOT part of any supported API.
jjg@1455 84 * If you write code that depends on this, you do so at your own
jjg@1455 85 * risk. This code and its internal interfaces are subject to change
jjg@1455 86 * or deletion without notice.</b></p>
jjg@1455 87 */
jjg@1455 88 public class Checker extends DocTreeScanner<Void, Void> {
jjg@1455 89 final Env env;
jjg@1455 90
jjg@1455 91 Set<Element> foundParams = new HashSet<Element>();
jjg@1455 92 Set<TypeMirror> foundThrows = new HashSet<TypeMirror>();
jjg@1455 93 Set<String> foundAnchors = new HashSet<String>();
jjg@1455 94 boolean foundInheritDoc = false;
jjg@1455 95 boolean foundReturn = false;
jjg@1455 96
jjg@1506 97 public enum Flag {
jjg@1455 98 TABLE_HAS_CAPTION,
jjg@1455 99 HAS_ELEMENT,
jjg@1650 100 HAS_INLINE_TAG,
jjg@1507 101 HAS_TEXT,
jjg@1507 102 REPORTED_BAD_INLINE
jjg@1455 103 }
jjg@1455 104
jjg@1455 105 static class TagStackItem {
jjg@1455 106 final DocTree tree; // typically, but not always, StartElementTree
jjg@1455 107 final HtmlTag tag;
jjg@1455 108 final Set<HtmlTag.Attr> attrs;
jjg@1455 109 final Set<Flag> flags;
jjg@1455 110 TagStackItem(DocTree tree, HtmlTag tag) {
jjg@1455 111 this.tree = tree;
jjg@1455 112 this.tag = tag;
jjg@1455 113 attrs = EnumSet.noneOf(HtmlTag.Attr.class);
jjg@1455 114 flags = EnumSet.noneOf(Flag.class);
jjg@1455 115 }
jjg@1455 116 @Override
jjg@1455 117 public String toString() {
jjg@1455 118 return String.valueOf(tag);
jjg@1455 119 }
jjg@1455 120 }
jjg@1455 121
jjg@1455 122 private Deque<TagStackItem> tagStack; // TODO: maybe want to record starting tree as well
jjg@1455 123 private HtmlTag currHeaderTag;
jjg@1455 124
jjg@1455 125 // <editor-fold defaultstate="collapsed" desc="Top level">
jjg@1455 126
jjg@1455 127 Checker(Env env) {
jjg@1455 128 env.getClass();
jjg@1455 129 this.env = env;
jjg@1455 130 tagStack = new LinkedList<TagStackItem>();
jjg@1455 131 }
jjg@1455 132
jjg@1455 133 public Void scan(DocCommentTree tree, TreePath p) {
jjg@1455 134 env.setCurrent(p, tree);
jjg@1455 135
jjg@1455 136 boolean isOverridingMethod = !env.currOverriddenMethods.isEmpty();
jjg@1455 137
jjg@1455 138 if (tree == null) {
jjg@1455 139 if (!isSynthetic() && !isOverridingMethod)
jjg@1455 140 reportMissing("dc.missing.comment");
jjg@1455 141 return null;
jjg@1455 142 }
jjg@1455 143
jjg@1455 144 tagStack.clear();
jjg@1455 145 currHeaderTag = null;
jjg@1455 146
jjg@1455 147 foundParams.clear();
jjg@1455 148 foundThrows.clear();
jjg@1455 149 foundInheritDoc = false;
jjg@1455 150 foundReturn = false;
jjg@1455 151
jjg@1455 152 scan(tree, (Void) null);
jjg@1455 153
jjg@1455 154 if (!isOverridingMethod) {
jjg@1455 155 switch (env.currElement.getKind()) {
jjg@1455 156 case METHOD:
jjg@1455 157 case CONSTRUCTOR: {
jjg@1455 158 ExecutableElement ee = (ExecutableElement) env.currElement;
jjg@1455 159 checkParamsDocumented(ee.getTypeParameters());
jjg@1455 160 checkParamsDocumented(ee.getParameters());
jjg@1455 161 switch (ee.getReturnType().getKind()) {
jjg@1455 162 case VOID:
jjg@1455 163 case NONE:
jjg@1455 164 break;
jjg@1455 165 default:
jjg@1455 166 if (!foundReturn
jjg@1455 167 && !foundInheritDoc
jjg@1455 168 && !env.types.isSameType(ee.getReturnType(), env.java_lang_Void)) {
jjg@1455 169 reportMissing("dc.missing.return");
jjg@1455 170 }
jjg@1455 171 }
jjg@1455 172 checkThrowsDocumented(ee.getThrownTypes());
jjg@1455 173 }
jjg@1455 174 }
jjg@1455 175 }
jjg@1455 176
jjg@1455 177 return null;
jjg@1455 178 }
jjg@1455 179
jjg@1455 180 private void reportMissing(String code, Object... args) {
jjg@1455 181 env.messages.report(MISSING, Kind.WARNING, env.currPath.getLeaf(), code, args);
jjg@1455 182 }
jjg@1455 183
jjg@1455 184 @Override
jjg@1455 185 public Void visitDocComment(DocCommentTree tree, Void ignore) {
jjg@1455 186 super.visitDocComment(tree, ignore);
jjg@1455 187 for (TagStackItem tsi: tagStack) {
jjg@1455 188 if (tsi.tree.getKind() == DocTree.Kind.START_ELEMENT
jjg@1455 189 && tsi.tag.endKind == HtmlTag.EndKind.REQUIRED) {
jjg@1455 190 StartElementTree t = (StartElementTree) tsi.tree;
jjg@1455 191 env.messages.error(HTML, t, "dc.tag.not.closed", t.getName());
jjg@1455 192 }
jjg@1455 193 }
jjg@1455 194 return null;
jjg@1455 195 }
jjg@1455 196 // </editor-fold>
jjg@1455 197
jjg@1455 198 // <editor-fold defaultstate="collapsed" desc="Text and entities.">
jjg@1455 199
jjg@1455 200 @Override
jjg@1455 201 public Void visitText(TextTree tree, Void ignore) {
jjg@1507 202 if (hasNonWhitespace(tree)) {
jjg@1507 203 checkAllowsText(tree);
jjg@1455 204 markEnclosingTag(Flag.HAS_TEXT);
jjg@1455 205 }
jjg@1455 206 return null;
jjg@1455 207 }
jjg@1455 208
jjg@1455 209 @Override
jjg@1455 210 public Void visitEntity(EntityTree tree, Void ignore) {
jjg@1507 211 checkAllowsText(tree);
jjg@1455 212 markEnclosingTag(Flag.HAS_TEXT);
jjg@1455 213 String name = tree.getName().toString();
jjg@1455 214 if (name.startsWith("#")) {
jjg@1455 215 int v = name.toLowerCase().startsWith("#x")
jjg@1455 216 ? Integer.parseInt(name.substring(2), 16)
jjg@1455 217 : Integer.parseInt(name.substring(1), 10);
jjg@1455 218 if (!Entity.isValid(v)) {
jjg@1455 219 env.messages.error(HTML, tree, "dc.entity.invalid", name);
jjg@1455 220 }
jjg@1455 221 } else if (!Entity.isValid(name)) {
jjg@1455 222 env.messages.error(HTML, tree, "dc.entity.invalid", name);
jjg@1455 223 }
jjg@1455 224 return null;
jjg@1455 225 }
jjg@1455 226
jjg@1507 227 void checkAllowsText(DocTree tree) {
jjg@1507 228 TagStackItem top = tagStack.peek();
jjg@1507 229 if (top != null
jjg@1507 230 && top.tree.getKind() == DocTree.Kind.START_ELEMENT
jjg@1507 231 && !top.tag.acceptsText()) {
jjg@1507 232 if (top.flags.add(Flag.REPORTED_BAD_INLINE)) {
jjg@1507 233 env.messages.error(HTML, tree, "dc.text.not.allowed",
jjg@1507 234 ((StartElementTree) top.tree).getName());
jjg@1507 235 }
jjg@1507 236 }
jjg@1507 237 }
jjg@1507 238
jjg@1455 239 // </editor-fold>
jjg@1455 240
jjg@1455 241 // <editor-fold defaultstate="collapsed" desc="HTML elements">
jjg@1455 242
jjg@1455 243 @Override
jjg@1455 244 public Void visitStartElement(StartElementTree tree, Void ignore) {
jjg@1455 245 markEnclosingTag(Flag.HAS_ELEMENT);
jjg@1455 246 final Name treeName = tree.getName();
jjg@1455 247 final HtmlTag t = HtmlTag.get(treeName);
jjg@1455 248 if (t == null) {
jjg@1455 249 env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
jjg@1455 250 } else {
jjg@1552 251 boolean done = false;
jjg@1507 252 for (TagStackItem tsi: tagStack) {
jjg@1507 253 if (tsi.tag.accepts(t)) {
jjg@1507 254 while (tagStack.peek() != tsi) tagStack.pop();
jjg@1552 255 done = true;
jjg@1507 256 break;
jjg@1552 257 } else if (tsi.tag.endKind != HtmlTag.EndKind.OPTIONAL) {
jjg@1552 258 done = true;
jjg@1507 259 break;
jjg@1552 260 }
jjg@1552 261 }
jjg@1552 262 if (!done && HtmlTag.BODY.accepts(t)) {
jjg@1552 263 tagStack.clear();
jjg@1507 264 }
jjg@1507 265
jjg@1507 266 checkStructure(tree, t);
jjg@1507 267
jjg@1455 268 // tag specific checks
jjg@1455 269 switch (t) {
jjg@1455 270 // check for out of sequence headers, such as <h1>...</h1> <h3>...</h3>
jjg@1455 271 case H1: case H2: case H3: case H4: case H5: case H6:
jjg@1455 272 checkHeader(tree, t);
jjg@1455 273 break;
jjg@1455 274 }
jjg@1455 275
jjg@1455 276 if (t.flags.contains(HtmlTag.Flag.NO_NEST)) {
jjg@1455 277 for (TagStackItem i: tagStack) {
jjg@1455 278 if (t == i.tag) {
jjg@1455 279 env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName);
jjg@1455 280 break;
jjg@1455 281 }
jjg@1455 282 }
jjg@1455 283 }
jjg@1455 284 }
jjg@1455 285
jjg@1455 286 // check for self closing tags, such as <a id="name"/>
jjg@1455 287 if (tree.isSelfClosing()) {
jjg@1455 288 env.messages.error(HTML, tree, "dc.tag.self.closing", treeName);
jjg@1455 289 }
jjg@1455 290
jjg@1455 291 try {
jjg@1455 292 TagStackItem parent = tagStack.peek();
jjg@1455 293 TagStackItem top = new TagStackItem(tree, t);
jjg@1455 294 tagStack.push(top);
jjg@1455 295
jjg@1455 296 super.visitStartElement(tree, ignore);
jjg@1455 297
jjg@1455 298 // handle attributes that may or may not have been found in start element
jjg@1455 299 if (t != null) {
jjg@1455 300 switch (t) {
jjg@1455 301 case CAPTION:
jjg@1455 302 if (parent != null && parent.tag == HtmlTag.TABLE)
jjg@1455 303 parent.flags.add(Flag.TABLE_HAS_CAPTION);
jjg@1455 304 break;
jjg@1455 305
jjg@1455 306 case IMG:
jjg@1455 307 if (!top.attrs.contains(HtmlTag.Attr.ALT))
jjg@1455 308 env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image");
jjg@1455 309 break;
jjg@1455 310 }
jjg@1455 311 }
jjg@1455 312
jjg@1455 313 return null;
jjg@1455 314 } finally {
jjg@1455 315
jjg@1455 316 if (t == null || t.endKind == HtmlTag.EndKind.NONE)
jjg@1455 317 tagStack.pop();
jjg@1455 318 }
jjg@1455 319 }
jjg@1455 320
jjg@1507 321 private void checkStructure(StartElementTree tree, HtmlTag t) {
jjg@1507 322 Name treeName = tree.getName();
jjg@1507 323 TagStackItem top = tagStack.peek();
jjg@1507 324 switch (t.blockType) {
jjg@1507 325 case BLOCK:
jjg@1507 326 if (top == null || top.tag.accepts(t))
jjg@1507 327 return;
jjg@1507 328
jjg@1507 329 switch (top.tree.getKind()) {
jjg@1507 330 case START_ELEMENT: {
jjg@1507 331 if (top.tag.blockType == HtmlTag.BlockType.INLINE) {
jjg@1507 332 Name name = ((StartElementTree) top.tree).getName();
jjg@1507 333 env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.element",
jjg@1507 334 treeName, name);
jjg@1507 335 return;
jjg@1507 336 }
jjg@1507 337 }
jjg@1507 338 break;
jjg@1507 339
jjg@1507 340 case LINK:
jjg@1507 341 case LINK_PLAIN: {
jjg@1507 342 String name = top.tree.getKind().tagName;
jjg@1507 343 env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.tag",
jjg@1507 344 treeName, name);
jjg@1507 345 return;
jjg@1507 346 }
jjg@1507 347 }
jjg@1507 348 break;
jjg@1507 349
jjg@1507 350 case INLINE:
jjg@1507 351 if (top == null || top.tag.accepts(t))
jjg@1507 352 return;
jjg@1507 353 break;
jjg@1507 354
jjg@1507 355 case LIST_ITEM:
jjg@1507 356 case TABLE_ITEM:
jjg@1507 357 if (top != null) {
jjg@1507 358 // reset this flag so subsequent bad inline content gets reported
jjg@1507 359 top.flags.remove(Flag.REPORTED_BAD_INLINE);
jjg@1507 360 if (top.tag.accepts(t))
jjg@1507 361 return;
jjg@1507 362 }
jjg@1507 363 break;
jjg@1507 364
jjg@1507 365 case OTHER:
jjg@1507 366 env.messages.error(HTML, tree, "dc.tag.not.allowed", treeName);
jjg@1507 367 return;
jjg@1507 368 }
jjg@1507 369
jjg@1507 370 env.messages.error(HTML, tree, "dc.tag.not.allowed.here", treeName);
jjg@1507 371 }
jjg@1507 372
jjg@1455 373 private void checkHeader(StartElementTree tree, HtmlTag tag) {
jjg@1455 374 // verify the new tag
jjg@1455 375 if (getHeaderLevel(tag) > getHeaderLevel(currHeaderTag) + 1) {
jjg@1455 376 if (currHeaderTag == null) {
jjg@1455 377 env.messages.error(ACCESSIBILITY, tree, "dc.tag.header.sequence.1", tag);
jjg@1455 378 } else {
jjg@1455 379 env.messages.error(ACCESSIBILITY, tree, "dc.tag.header.sequence.2",
jjg@1455 380 tag, currHeaderTag);
jjg@1455 381 }
jjg@1455 382 }
jjg@1455 383
jjg@1455 384 currHeaderTag = tag;
jjg@1455 385 }
jjg@1455 386
jjg@1455 387 private int getHeaderLevel(HtmlTag tag) {
jjg@1455 388 if (tag == null)
jjg@1455 389 return 0;
jjg@1455 390 switch (tag) {
jjg@1455 391 case H1: return 1;
jjg@1455 392 case H2: return 2;
jjg@1455 393 case H3: return 3;
jjg@1455 394 case H4: return 4;
jjg@1455 395 case H5: return 5;
jjg@1455 396 case H6: return 6;
jjg@1455 397 default: throw new IllegalArgumentException();
jjg@1455 398 }
jjg@1455 399 }
jjg@1455 400
jjg@1455 401 @Override
jjg@1455 402 public Void visitEndElement(EndElementTree tree, Void ignore) {
jjg@1455 403 final Name treeName = tree.getName();
jjg@1455 404 final HtmlTag t = HtmlTag.get(treeName);
jjg@1455 405 if (t == null) {
jjg@1455 406 env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
jjg@1455 407 } else if (t.endKind == HtmlTag.EndKind.NONE) {
jjg@1455 408 env.messages.error(HTML, tree, "dc.tag.end.not.permitted", treeName);
jjg@1455 409 } else {
jjg@1499 410 boolean done = false;
jjg@1455 411 while (!tagStack.isEmpty()) {
jjg@1455 412 TagStackItem top = tagStack.peek();
jjg@1455 413 if (t == top.tag) {
jjg@1455 414 switch (t) {
jjg@1455 415 case TABLE:
jjg@1455 416 if (!top.attrs.contains(HtmlTag.Attr.SUMMARY)
jjg@1455 417 && !top.flags.contains(Flag.TABLE_HAS_CAPTION)) {
jjg@1455 418 env.messages.error(ACCESSIBILITY, tree,
jjg@1455 419 "dc.no.summary.or.caption.for.table");
jjg@1455 420 }
jjg@1455 421 }
jjg@1455 422 if (t.flags.contains(HtmlTag.Flag.EXPECT_CONTENT)
jjg@1455 423 && !top.flags.contains(Flag.HAS_TEXT)
jjg@1650 424 && !top.flags.contains(Flag.HAS_ELEMENT)
jjg@1650 425 && !top.flags.contains(Flag.HAS_INLINE_TAG)) {
jjg@1455 426 env.messages.warning(HTML, tree, "dc.tag.empty", treeName);
jjg@1455 427 }
jjg@1455 428 tagStack.pop();
jjg@1499 429 done = true;
jjg@1455 430 break;
jjg@1455 431 } else if (top.tag == null || top.tag.endKind != HtmlTag.EndKind.REQUIRED) {
jjg@1455 432 tagStack.pop();
jjg@1455 433 } else {
jjg@1455 434 boolean found = false;
jjg@1455 435 for (TagStackItem si: tagStack) {
jjg@1455 436 if (si.tag == t) {
jjg@1455 437 found = true;
jjg@1455 438 break;
jjg@1455 439 }
jjg@1455 440 }
jjg@1455 441 if (found && top.tree.getKind() == DocTree.Kind.START_ELEMENT) {
jjg@1455 442 env.messages.error(HTML, top.tree, "dc.tag.start.unmatched",
jjg@1455 443 ((StartElementTree) top.tree).getName());
jjg@1455 444 tagStack.pop();
jjg@1455 445 } else {
jjg@1455 446 env.messages.error(HTML, tree, "dc.tag.end.unexpected", treeName);
jjg@1499 447 done = true;
jjg@1455 448 break;
jjg@1455 449 }
jjg@1455 450 }
jjg@1455 451 }
jjg@1499 452
jjg@1499 453 if (!done && tagStack.isEmpty()) {
jjg@1499 454 env.messages.error(HTML, tree, "dc.tag.end.unexpected", treeName);
jjg@1499 455 }
jjg@1455 456 }
jjg@1455 457
jjg@1455 458 return super.visitEndElement(tree, ignore);
jjg@1455 459 }
jjg@1455 460 // </editor-fold>
jjg@1455 461
jjg@1455 462 // <editor-fold defaultstate="collapsed" desc="HTML attributes">
jjg@1455 463
jjg@1455 464 @Override @SuppressWarnings("fallthrough")
jjg@1455 465 public Void visitAttribute(AttributeTree tree, Void ignore) {
jjg@1455 466 HtmlTag currTag = tagStack.peek().tag;
jjg@1455 467 if (currTag != null) {
jjg@1455 468 Name name = tree.getName();
jjg@1455 469 HtmlTag.Attr attr = currTag.getAttr(name);
jjg@1455 470 if (attr != null) {
jjg@1455 471 boolean first = tagStack.peek().attrs.add(attr);
jjg@1455 472 if (!first)
jjg@1455 473 env.messages.error(HTML, tree, "dc.attr.repeated", name);
jjg@1455 474 }
jjg@1455 475 AttrKind k = currTag.getAttrKind(name);
jjg@1455 476 switch (k) {
jjg@1455 477 case OK:
jjg@1455 478 break;
jjg@1455 479
jjg@1455 480 case INVALID:
jjg@1455 481 env.messages.error(HTML, tree, "dc.attr.unknown", name);
jjg@1455 482 break;
jjg@1455 483
jjg@1455 484 case OBSOLETE:
jjg@1455 485 env.messages.warning(ACCESSIBILITY, tree, "dc.attr.obsolete", name);
jjg@1455 486 break;
jjg@1455 487
jjg@1455 488 case USE_CSS:
jjg@1455 489 env.messages.warning(ACCESSIBILITY, tree, "dc.attr.obsolete.use.css", name);
jjg@1455 490 break;
jjg@1455 491 }
jjg@1455 492
jjg@1455 493 if (attr != null) {
jjg@1455 494 switch (attr) {
jjg@1455 495 case NAME:
jjg@1455 496 if (currTag != HtmlTag.A) {
jjg@1455 497 break;
jjg@1455 498 }
jjg@1495 499 // fallthrough
jjg@1455 500 case ID:
jjg@1455 501 String value = getAttrValue(tree);
jjg@1495 502 if (value == null) {
jjg@1495 503 env.messages.error(HTML, tree, "dc.anchor.value.missing");
jjg@1495 504 } else {
jjg@1495 505 if (!validName.matcher(value).matches()) {
jjg@1495 506 env.messages.error(HTML, tree, "dc.invalid.anchor", value);
jjg@1495 507 }
jjg@1495 508 if (!foundAnchors.add(value)) {
jjg@1495 509 env.messages.error(HTML, tree, "dc.anchor.already.defined", value);
jjg@1495 510 }
jjg@1455 511 }
jjg@1455 512 break;
jjg@1455 513
jjg@1455 514 case HREF:
jjg@1455 515 if (currTag == HtmlTag.A) {
jjg@1455 516 String v = getAttrValue(tree);
jjg@1455 517 if (v == null || v.isEmpty()) {
jjg@1455 518 env.messages.error(HTML, tree, "dc.attr.lacks.value");
jjg@1455 519 } else {
jjg@1455 520 Matcher m = docRoot.matcher(v);
jjg@1455 521 if (m.matches()) {
jjg@1455 522 String rest = m.group(2);
jjg@1455 523 if (!rest.isEmpty())
jjg@1455 524 checkURI(tree, rest);
jjg@1455 525 } else {
jjg@1455 526 checkURI(tree, v);
jjg@1455 527 }
jjg@1455 528 }
jjg@1455 529 }
jjg@1455 530 break;
jjg@1455 531 }
jjg@1455 532 }
jjg@1455 533 }
jjg@1455 534
jjg@1455 535 // TODO: basic check on value
jjg@1455 536
jjg@1455 537 return super.visitAttribute(tree, ignore);
jjg@1455 538 }
jjg@1455 539
jjg@1455 540 // http://www.w3.org/TR/html401/types.html#type-name
jjg@1455 541 private static final Pattern validName = Pattern.compile("[A-Za-z][A-Za-z0-9-_:.]*");
jjg@1455 542
jjg@1455 543 // pattern to remove leading {@docRoot}/?
jjg@1455 544 private static final Pattern docRoot = Pattern.compile("(?i)(\\{@docRoot *\\}/?)?(.*)");
jjg@1455 545
jjg@1455 546 private String getAttrValue(AttributeTree tree) {
jjg@1455 547 if (tree.getValue() == null)
jjg@1455 548 return null;
jjg@1455 549
jjg@1455 550 StringWriter sw = new StringWriter();
jjg@1455 551 try {
jjg@1455 552 new DocPretty(sw).print(tree.getValue());
jjg@1455 553 } catch (IOException e) {
jjg@1455 554 // cannot happen
jjg@1455 555 }
jjg@1455 556 // ignore potential use of entities for now
jjg@1455 557 return sw.toString();
jjg@1455 558 }
jjg@1455 559
jjg@1455 560 private void checkURI(AttributeTree tree, String uri) {
jjg@1455 561 try {
jjg@1455 562 URI u = new URI(uri);
jjg@1455 563 } catch (URISyntaxException e) {
jjg@1455 564 env.messages.error(HTML, tree, "dc.invalid.uri", uri);
jjg@1455 565 }
jjg@1455 566 }
jjg@1455 567 // </editor-fold>
jjg@1455 568
jjg@1455 569 // <editor-fold defaultstate="collapsed" desc="javadoc tags">
jjg@1455 570
jjg@1455 571 @Override
jjg@1455 572 public Void visitAuthor(AuthorTree tree, Void ignore) {
jjg@1455 573 warnIfEmpty(tree, tree.getName());
jjg@1455 574 return super.visitAuthor(tree, ignore);
jjg@1455 575 }
jjg@1455 576
jjg@1455 577 @Override
jjg@1650 578 public Void visitDocRoot(DocRootTree tree, Void ignore) {
jjg@1650 579 markEnclosingTag(Flag.HAS_INLINE_TAG);
jjg@1650 580 return super.visitDocRoot(tree, ignore);
jjg@1650 581 }
jjg@1650 582
jjg@1650 583 @Override
jjg@1455 584 public Void visitInheritDoc(InheritDocTree tree, Void ignore) {
jjg@1650 585 markEnclosingTag(Flag.HAS_INLINE_TAG);
jjg@1455 586 // TODO: verify on overridden method
jjg@1455 587 foundInheritDoc = true;
jjg@1455 588 return super.visitInheritDoc(tree, ignore);
jjg@1455 589 }
jjg@1455 590
jjg@1455 591 @Override
jjg@1455 592 public Void visitLink(LinkTree tree, Void ignore) {
jjg@1650 593 markEnclosingTag(Flag.HAS_INLINE_TAG);
jjg@1455 594 // simulate inline context on tag stack
jjg@1455 595 HtmlTag t = (tree.getKind() == DocTree.Kind.LINK)
jjg@1455 596 ? HtmlTag.CODE : HtmlTag.SPAN;
jjg@1455 597 tagStack.push(new TagStackItem(tree, t));
jjg@1455 598 try {
jjg@1455 599 return super.visitLink(tree, ignore);
jjg@1455 600 } finally {
jjg@1455 601 tagStack.pop();
jjg@1455 602 }
jjg@1455 603 }
jjg@1455 604
jjg@1455 605 @Override
jjg@1499 606 public Void visitLiteral(LiteralTree tree, Void ignore) {
jjg@1650 607 markEnclosingTag(Flag.HAS_INLINE_TAG);
jjg@1499 608 if (tree.getKind() == DocTree.Kind.CODE) {
jjg@1499 609 for (TagStackItem tsi: tagStack) {
jjg@1499 610 if (tsi.tag == HtmlTag.CODE) {
jjg@1502 611 env.messages.warning(HTML, tree, "dc.tag.code.within.code");
jjg@1499 612 break;
jjg@1499 613 }
jjg@1499 614 }
jjg@1499 615 }
jjg@1499 616 return super.visitLiteral(tree, ignore);
jjg@1499 617 }
jjg@1499 618
jjg@1499 619 @Override
jjg@1455 620 public Void visitParam(ParamTree tree, Void ignore) {
jjg@1455 621 boolean typaram = tree.isTypeParameter();
jjg@1455 622 IdentifierTree nameTree = tree.getName();
jjg@1455 623 Element e = env.currElement;
jjg@1455 624 switch (e.getKind()) {
jjg@1455 625 case METHOD: case CONSTRUCTOR: {
jjg@1455 626 ExecutableElement ee = (ExecutableElement) e;
jjg@1455 627 checkParamDeclared(nameTree, typaram ? ee.getTypeParameters() : ee.getParameters());
jjg@1455 628 break;
jjg@1455 629 }
jjg@1455 630
jjg@1455 631 case CLASS: case INTERFACE: {
jjg@1455 632 TypeElement te = (TypeElement) e;
jjg@1455 633 if (typaram) {
jjg@1455 634 checkParamDeclared(nameTree, te.getTypeParameters());
jjg@1455 635 } else {
jjg@1455 636 env.messages.error(REFERENCE, tree, "dc.invalid.param");
jjg@1455 637 }
jjg@1455 638 break;
jjg@1455 639 }
jjg@1455 640
jjg@1455 641 default:
jjg@1455 642 env.messages.error(REFERENCE, tree, "dc.invalid.param");
jjg@1455 643 break;
jjg@1455 644 }
jjg@1455 645 warnIfEmpty(tree, tree.getDescription());
jjg@1455 646 return super.visitParam(tree, ignore);
jjg@1455 647 }
jjg@1455 648 // where
jjg@1455 649 private void checkParamDeclared(IdentifierTree nameTree, List<? extends Element> list) {
jjg@1455 650 Name name = nameTree.getName();
jjg@1455 651 boolean found = false;
jjg@1455 652 for (Element e: list) {
jjg@1455 653 if (name.equals(e.getSimpleName())) {
jjg@1455 654 foundParams.add(e);
jjg@1455 655 found = true;
jjg@1455 656 }
jjg@1455 657 }
jjg@1455 658 if (!found)
jjg@1455 659 env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found");
jjg@1455 660 }
jjg@1455 661
jjg@1455 662 private void checkParamsDocumented(List<? extends Element> list) {
jjg@1455 663 if (foundInheritDoc)
jjg@1455 664 return;
jjg@1455 665
jjg@1455 666 for (Element e: list) {
jjg@1455 667 if (!foundParams.contains(e)) {
jjg@1455 668 CharSequence paramName = (e.getKind() == ElementKind.TYPE_PARAMETER)
jjg@1455 669 ? "<" + e.getSimpleName() + ">"
jjg@1455 670 : e.getSimpleName();
jjg@1455 671 reportMissing("dc.missing.param", paramName);
jjg@1455 672 }
jjg@1455 673 }
jjg@1455 674 }
jjg@1455 675
jjg@1455 676 @Override
jjg@1455 677 public Void visitReference(ReferenceTree tree, Void ignore) {
jjg@1455 678 Element e = env.trees.getElement(env.currPath, tree);
jjg@1455 679 if (e == null)
jjg@1455 680 env.messages.error(REFERENCE, tree, "dc.ref.not.found");
jjg@1455 681 return super.visitReference(tree, ignore);
jjg@1455 682 }
jjg@1455 683
jjg@1455 684 @Override
jjg@1455 685 public Void visitReturn(ReturnTree tree, Void ignore) {
jjg@1455 686 Element e = env.trees.getElement(env.currPath);
jjg@1455 687 if (e.getKind() != ElementKind.METHOD
jjg@1455 688 || ((ExecutableElement) e).getReturnType().getKind() == TypeKind.VOID)
jjg@1455 689 env.messages.error(REFERENCE, tree, "dc.invalid.return");
jjg@1455 690 foundReturn = true;
jjg@1455 691 warnIfEmpty(tree, tree.getDescription());
jjg@1455 692 return super.visitReturn(tree, ignore);
jjg@1455 693 }
jjg@1455 694
jjg@1455 695 @Override
jjg@1455 696 public Void visitSerialData(SerialDataTree tree, Void ignore) {
jjg@1455 697 warnIfEmpty(tree, tree.getDescription());
jjg@1455 698 return super.visitSerialData(tree, ignore);
jjg@1455 699 }
jjg@1455 700
jjg@1455 701 @Override
jjg@1455 702 public Void visitSerialField(SerialFieldTree tree, Void ignore) {
jjg@1455 703 warnIfEmpty(tree, tree.getDescription());
jjg@1455 704 return super.visitSerialField(tree, ignore);
jjg@1455 705 }
jjg@1455 706
jjg@1455 707 @Override
jjg@1455 708 public Void visitSince(SinceTree tree, Void ignore) {
jjg@1455 709 warnIfEmpty(tree, tree.getBody());
jjg@1455 710 return super.visitSince(tree, ignore);
jjg@1455 711 }
jjg@1455 712
jjg@1455 713 @Override
jjg@1455 714 public Void visitThrows(ThrowsTree tree, Void ignore) {
jjg@1455 715 ReferenceTree exName = tree.getExceptionName();
jjg@1455 716 Element ex = env.trees.getElement(env.currPath, exName);
jjg@1455 717 if (ex == null) {
jjg@1455 718 env.messages.error(REFERENCE, tree, "dc.ref.not.found");
jjg@1455 719 } else if (ex.asType().getKind() == TypeKind.DECLARED
jjg@1455 720 && env.types.isAssignable(ex.asType(), env.java_lang_Throwable)) {
jjg@1455 721 switch (env.currElement.getKind()) {
jjg@1455 722 case CONSTRUCTOR:
jjg@1455 723 case METHOD:
jjg@1455 724 if (isCheckedException(ex.asType())) {
jjg@1455 725 ExecutableElement ee = (ExecutableElement) env.currElement;
jjg@1455 726 checkThrowsDeclared(exName, ex.asType(), ee.getThrownTypes());
jjg@1455 727 }
jjg@1455 728 break;
jjg@1455 729 default:
jjg@1455 730 env.messages.error(REFERENCE, tree, "dc.invalid.throws");
jjg@1455 731 }
jjg@1455 732 } else {
jjg@1455 733 env.messages.error(REFERENCE, tree, "dc.invalid.throws");
jjg@1455 734 }
jjg@1455 735 warnIfEmpty(tree, tree.getDescription());
jjg@1455 736 return scan(tree.getDescription(), ignore);
jjg@1455 737 }
jjg@1455 738
jjg@1455 739 private void checkThrowsDeclared(ReferenceTree tree, TypeMirror t, List<? extends TypeMirror> list) {
jjg@1455 740 boolean found = false;
jjg@1455 741 for (TypeMirror tl : list) {
jjg@1455 742 if (env.types.isAssignable(t, tl)) {
jjg@1455 743 foundThrows.add(tl);
jjg@1455 744 found = true;
jjg@1455 745 }
jjg@1455 746 }
jjg@1455 747 if (!found)
jjg@1455 748 env.messages.error(REFERENCE, tree, "dc.exception.not.thrown", t);
jjg@1455 749 }
jjg@1455 750
jjg@1455 751 private void checkThrowsDocumented(List<? extends TypeMirror> list) {
jjg@1455 752 if (foundInheritDoc)
jjg@1455 753 return;
jjg@1455 754
jjg@1455 755 for (TypeMirror tl: list) {
jjg@1455 756 if (isCheckedException(tl) && !foundThrows.contains(tl))
jjg@1455 757 reportMissing("dc.missing.throws", tl);
jjg@1455 758 }
jjg@1455 759 }
jjg@1455 760
jjg@1455 761 @Override
jjg@1650 762 public Void visitValue(ValueTree tree, Void ignore) {
jjg@1650 763 markEnclosingTag(Flag.HAS_INLINE_TAG);
jjg@1650 764 return super.visitValue(tree, ignore);
jjg@1650 765 }
jjg@1650 766
jjg@1650 767 @Override
jjg@1455 768 public Void visitVersion(VersionTree tree, Void ignore) {
jjg@1455 769 warnIfEmpty(tree, tree.getBody());
jjg@1455 770 return super.visitVersion(tree, ignore);
jjg@1455 771 }
jjg@1455 772
jjg@1455 773 @Override
jjg@1455 774 public Void visitErroneous(ErroneousTree tree, Void ignore) {
jjg@1455 775 env.messages.error(SYNTAX, tree, null, tree.getDiagnostic().getMessage(null));
jjg@1455 776 return null;
jjg@1455 777 }
jjg@1455 778 // </editor-fold>
jjg@1455 779
jjg@1455 780 // <editor-fold defaultstate="collapsed" desc="Utility methods">
jjg@1455 781
jjg@1455 782 private boolean isCheckedException(TypeMirror t) {
jjg@1455 783 return !(env.types.isAssignable(t, env.java_lang_Error)
jjg@1455 784 || env.types.isAssignable(t, env.java_lang_RuntimeException));
jjg@1455 785 }
jjg@1455 786
jjg@1455 787 private boolean isSynthetic() {
jjg@1455 788 switch (env.currElement.getKind()) {
jjg@1455 789 case CONSTRUCTOR:
jjg@1455 790 // A synthetic default constructor has the same pos as the
jjg@1455 791 // enclosing class
jjg@1455 792 TreePath p = env.currPath;
jjg@1455 793 return env.getPos(p) == env.getPos(p.getParentPath());
jjg@1455 794 }
jjg@1455 795 return false;
jjg@1455 796 }
jjg@1455 797
jjg@1455 798 void markEnclosingTag(Flag flag) {
jjg@1455 799 TagStackItem top = tagStack.peek();
jjg@1455 800 if (top != null)
jjg@1455 801 top.flags.add(flag);
jjg@1455 802 }
jjg@1455 803
jjg@1455 804 String toString(TreePath p) {
jjg@1455 805 StringBuilder sb = new StringBuilder("TreePath[");
jjg@1455 806 toString(p, sb);
jjg@1455 807 sb.append("]");
jjg@1455 808 return sb.toString();
jjg@1455 809 }
jjg@1455 810
jjg@1455 811 void toString(TreePath p, StringBuilder sb) {
jjg@1455 812 TreePath parent = p.getParentPath();
jjg@1455 813 if (parent != null) {
jjg@1455 814 toString(parent, sb);
jjg@1455 815 sb.append(",");
jjg@1455 816 }
jjg@1455 817 sb.append(p.getLeaf().getKind()).append(":").append(env.getPos(p)).append(":S").append(env.getStartPos(p));
jjg@1455 818 }
jjg@1455 819
jjg@1455 820 void warnIfEmpty(DocTree tree, List<? extends DocTree> list) {
jjg@1455 821 for (DocTree d: list) {
jjg@1455 822 switch (d.getKind()) {
jjg@1455 823 case TEXT:
jjg@1507 824 if (hasNonWhitespace((TextTree) d))
jjg@1455 825 return;
jjg@1455 826 break;
jjg@1455 827 default:
jjg@1455 828 return;
jjg@1455 829 }
jjg@1455 830 }
jjg@1455 831 env.messages.warning(SYNTAX, tree, "dc.empty", tree.getKind().tagName);
jjg@1455 832 }
jjg@1507 833
jjg@1507 834 boolean hasNonWhitespace(TextTree tree) {
jjg@1507 835 String s = tree.getBody();
jjg@1507 836 for (int i = 0; i < s.length(); i++) {
jjg@1507 837 if (!Character.isWhitespace(s.charAt(i)))
jjg@1507 838 return true;
jjg@1507 839 }
jjg@1507 840 return false;
jjg@1507 841 }
jjg@1507 842
jjg@1455 843 // </editor-fold>
jjg@1455 844
jjg@1455 845 }

mercurial