src/share/classes/com/sun/tools/javadoc/Comment.java

Mon, 25 Mar 2013 16:55:14 -0700

author
mfang
date
Mon, 25 Mar 2013 16:55:14 -0700
changeset 1658
fdf30b225e1c
parent 1359
25e14ad23cef
child 1722
38c4bade0ec1
permissions
-rw-r--r--

8010521: jdk8 l10n resource file translation update 2
Reviewed-by: naoto, yhuang

duke@1 1 /*
jjg@1326 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javadoc;
duke@1 27
duke@1 28 import com.sun.javadoc.*;
duke@1 29 import com.sun.tools.javac.util.ListBuffer;
duke@1 30
duke@1 31 /**
duke@1 32 * Comment contains all information in comment part.
duke@1 33 * It allows users to get first sentence of this comment, get
duke@1 34 * comment for different tags...
duke@1 35 *
jjg@1359 36 * <p><b>This is NOT part of any supported API.
jjg@1359 37 * If you write code that depends on this, you do so at your own risk.
jjg@1359 38 * This code and its internal interfaces are subject to change or
jjg@1359 39 * deletion without notice.</b>
jjg@1359 40 *
duke@1 41 * @author Kaiyang Liu (original)
duke@1 42 * @author Robert Field (rewrite)
duke@1 43 * @author Atul M Dambalkar
duke@1 44 * @author Neal Gafter (rewrite)
duke@1 45 */
duke@1 46 class Comment {
duke@1 47
duke@1 48 /**
duke@1 49 * sorted comments with different tags.
duke@1 50 */
duke@1 51 private final ListBuffer<Tag> tagList = new ListBuffer<Tag>();
duke@1 52
duke@1 53 /**
duke@1 54 * text minus any tags.
duke@1 55 */
duke@1 56 private String text;
duke@1 57
duke@1 58 /**
duke@1 59 * Doc environment
duke@1 60 */
duke@1 61 private final DocEnv docenv;
duke@1 62
duke@1 63 /**
duke@1 64 * constructor of Comment.
duke@1 65 */
duke@1 66 Comment(final DocImpl holder, final String commentString) {
duke@1 67 this.docenv = holder.env;
duke@1 68
duke@1 69 /**
duke@1 70 * Separate the comment into the text part and zero to N tags.
duke@1 71 * Simple state machine is in one of three states:
duke@1 72 * <pre>
duke@1 73 * IN_TEXT: parsing the comment text or tag text.
duke@1 74 * TAG_NAME: parsing the name of a tag.
duke@1 75 * TAG_GAP: skipping through the gap between the tag name and
duke@1 76 * the tag text.
duke@1 77 * </pre>
duke@1 78 */
jjg@198 79 @SuppressWarnings("fallthrough")
duke@1 80 class CommentStringParser {
duke@1 81 /**
duke@1 82 * The entry point to the comment string parser
duke@1 83 */
duke@1 84 void parseCommentStateMachine() {
duke@1 85 final int IN_TEXT = 1;
duke@1 86 final int TAG_GAP = 2;
duke@1 87 final int TAG_NAME = 3;
duke@1 88 int state = TAG_GAP;
duke@1 89 boolean newLine = true;
duke@1 90 String tagName = null;
duke@1 91 int tagStart = 0;
duke@1 92 int textStart = 0;
duke@1 93 int lastNonWhite = -1;
duke@1 94 int len = commentString.length();
duke@1 95 for (int inx = 0; inx < len; ++inx) {
duke@1 96 char ch = commentString.charAt(inx);
duke@1 97 boolean isWhite = Character.isWhitespace(ch);
duke@1 98 switch (state) {
duke@1 99 case TAG_NAME:
duke@1 100 if (isWhite) {
duke@1 101 tagName = commentString.substring(tagStart, inx);
duke@1 102 state = TAG_GAP;
duke@1 103 }
duke@1 104 break;
duke@1 105 case TAG_GAP:
duke@1 106 if (isWhite) {
duke@1 107 break;
duke@1 108 }
duke@1 109 textStart = inx;
duke@1 110 state = IN_TEXT;
duke@1 111 /* fall thru */
duke@1 112 case IN_TEXT:
duke@1 113 if (newLine && ch == '@') {
duke@1 114 parseCommentComponent(tagName, textStart,
duke@1 115 lastNonWhite+1);
duke@1 116 tagStart = inx;
duke@1 117 state = TAG_NAME;
duke@1 118 }
duke@1 119 break;
ksrini@1065 120 }
duke@1 121 if (ch == '\n') {
duke@1 122 newLine = true;
duke@1 123 } else if (!isWhite) {
duke@1 124 lastNonWhite = inx;
duke@1 125 newLine = false;
duke@1 126 }
duke@1 127 }
duke@1 128 // Finish what's currently being processed
duke@1 129 switch (state) {
duke@1 130 case TAG_NAME:
duke@1 131 tagName = commentString.substring(tagStart, len);
duke@1 132 /* fall thru */
duke@1 133 case TAG_GAP:
duke@1 134 textStart = len;
duke@1 135 /* fall thru */
duke@1 136 case IN_TEXT:
duke@1 137 parseCommentComponent(tagName, textStart, lastNonWhite+1);
duke@1 138 break;
ksrini@1065 139 }
duke@1 140 }
duke@1 141
duke@1 142 /**
duke@1 143 * Save away the last parsed item.
duke@1 144 */
duke@1 145 void parseCommentComponent(String tagName,
duke@1 146 int from, int upto) {
duke@1 147 String tx = upto <= from ? "" : commentString.substring(from, upto);
duke@1 148 if (tagName == null) {
duke@1 149 text = tx;
duke@1 150 } else {
duke@1 151 TagImpl tag;
duke@1 152 if (tagName.equals("@exception") || tagName.equals("@throws")) {
duke@1 153 warnIfEmpty(tagName, tx);
duke@1 154 tag = new ThrowsTagImpl(holder, tagName, tx);
duke@1 155 } else if (tagName.equals("@param")) {
duke@1 156 warnIfEmpty(tagName, tx);
duke@1 157 tag = new ParamTagImpl(holder, tagName, tx);
duke@1 158 } else if (tagName.equals("@see")) {
duke@1 159 warnIfEmpty(tagName, tx);
duke@1 160 tag = new SeeTagImpl(holder, tagName, tx);
duke@1 161 } else if (tagName.equals("@serialField")) {
duke@1 162 warnIfEmpty(tagName, tx);
duke@1 163 tag = new SerialFieldTagImpl(holder, tagName, tx);
duke@1 164 } else if (tagName.equals("@return")) {
duke@1 165 warnIfEmpty(tagName, tx);
duke@1 166 tag = new TagImpl(holder, tagName, tx);
duke@1 167 } else if (tagName.equals("@author")) {
duke@1 168 warnIfEmpty(tagName, tx);
duke@1 169 tag = new TagImpl(holder, tagName, tx);
duke@1 170 } else if (tagName.equals("@version")) {
duke@1 171 warnIfEmpty(tagName, tx);
duke@1 172 tag = new TagImpl(holder, tagName, tx);
duke@1 173 } else {
duke@1 174 tag = new TagImpl(holder, tagName, tx);
duke@1 175 }
duke@1 176 tagList.append(tag);
duke@1 177 }
duke@1 178 }
duke@1 179
duke@1 180 void warnIfEmpty(String tagName, String tx) {
duke@1 181 if (tx.length() == 0) {
duke@1 182 docenv.warning(holder, "tag.tag_has_no_arguments", tagName);
duke@1 183 }
duke@1 184 }
duke@1 185
duke@1 186 }
duke@1 187
duke@1 188 new CommentStringParser().parseCommentStateMachine();
duke@1 189 }
duke@1 190
duke@1 191 /**
duke@1 192 * Return the text of the comment.
duke@1 193 */
duke@1 194 String commentText() {
duke@1 195 return text;
duke@1 196 }
duke@1 197
duke@1 198 /**
duke@1 199 * Return all tags in this comment.
duke@1 200 */
duke@1 201 Tag[] tags() {
duke@1 202 return tagList.toArray(new Tag[tagList.length()]);
duke@1 203 }
duke@1 204
duke@1 205 /**
duke@1 206 * Return tags of the specified kind in this comment.
duke@1 207 */
duke@1 208 Tag[] tags(String tagname) {
duke@1 209 ListBuffer<Tag> found = new ListBuffer<Tag>();
duke@1 210 String target = tagname;
duke@1 211 if (target.charAt(0) != '@') {
duke@1 212 target = "@" + target;
duke@1 213 }
duke@1 214 for (Tag tag : tagList) {
duke@1 215 if (tag.kind().equals(target)) {
duke@1 216 found.append(tag);
duke@1 217 }
duke@1 218 }
duke@1 219 return found.toArray(new Tag[found.length()]);
duke@1 220 }
duke@1 221
duke@1 222 /**
duke@1 223 * Return throws tags in this comment.
duke@1 224 */
duke@1 225 ThrowsTag[] throwsTags() {
duke@1 226 ListBuffer<ThrowsTag> found = new ListBuffer<ThrowsTag>();
duke@1 227 for (Tag next : tagList) {
duke@1 228 if (next instanceof ThrowsTag) {
duke@1 229 found.append((ThrowsTag)next);
duke@1 230 }
duke@1 231 }
duke@1 232 return found.toArray(new ThrowsTag[found.length()]);
duke@1 233 }
duke@1 234
duke@1 235 /**
duke@1 236 * Return param tags (excluding type param tags) in this comment.
duke@1 237 */
duke@1 238 ParamTag[] paramTags() {
duke@1 239 return paramTags(false);
duke@1 240 }
duke@1 241
duke@1 242 /**
duke@1 243 * Return type param tags in this comment.
duke@1 244 */
duke@1 245 ParamTag[] typeParamTags() {
duke@1 246 return paramTags(true);
duke@1 247 }
duke@1 248
duke@1 249 /**
duke@1 250 * Return param tags in this comment. If typeParams is true
duke@1 251 * include only type param tags, otherwise include only ordinary
duke@1 252 * param tags.
duke@1 253 */
duke@1 254 private ParamTag[] paramTags(boolean typeParams) {
duke@1 255 ListBuffer<ParamTag> found = new ListBuffer<ParamTag>();
duke@1 256 for (Tag next : tagList) {
duke@1 257 if (next instanceof ParamTag) {
duke@1 258 ParamTag p = (ParamTag)next;
duke@1 259 if (typeParams == p.isTypeParameter()) {
duke@1 260 found.append(p);
duke@1 261 }
duke@1 262 }
duke@1 263 }
duke@1 264 return found.toArray(new ParamTag[found.length()]);
duke@1 265 }
duke@1 266
duke@1 267 /**
duke@1 268 * Return see also tags in this comment.
duke@1 269 */
duke@1 270 SeeTag[] seeTags() {
duke@1 271 ListBuffer<SeeTag> found = new ListBuffer<SeeTag>();
duke@1 272 for (Tag next : tagList) {
duke@1 273 if (next instanceof SeeTag) {
duke@1 274 found.append((SeeTag)next);
duke@1 275 }
duke@1 276 }
duke@1 277 return found.toArray(new SeeTag[found.length()]);
duke@1 278 }
duke@1 279
duke@1 280 /**
duke@1 281 * Return serialField tags in this comment.
duke@1 282 */
duke@1 283 SerialFieldTag[] serialFieldTags() {
duke@1 284 ListBuffer<SerialFieldTag> found = new ListBuffer<SerialFieldTag>();
duke@1 285 for (Tag next : tagList) {
duke@1 286 if (next instanceof SerialFieldTag) {
duke@1 287 found.append((SerialFieldTag)next);
duke@1 288 }
duke@1 289 }
duke@1 290 return found.toArray(new SerialFieldTag[found.length()]);
duke@1 291 }
duke@1 292
duke@1 293 /**
duke@1 294 * Return array of tags with text and inline See Tags for a Doc comment.
duke@1 295 */
duke@1 296 static Tag[] getInlineTags(DocImpl holder, String inlinetext) {
duke@1 297 ListBuffer<Tag> taglist = new ListBuffer<Tag>();
duke@1 298 int delimend = 0, textstart = 0, len = inlinetext.length();
duke@1 299 DocEnv docenv = holder.env;
duke@1 300
duke@1 301 if (len == 0) {
duke@1 302 return taglist.toArray(new Tag[taglist.length()]);
duke@1 303 }
duke@1 304 while (true) {
duke@1 305 int linkstart;
duke@1 306 if ((linkstart = inlineTagFound(holder, inlinetext,
duke@1 307 textstart)) == -1) {
duke@1 308 taglist.append(new TagImpl(holder, "Text",
duke@1 309 inlinetext.substring(textstart)));
duke@1 310 break;
duke@1 311 } else {
duke@1 312 int seetextstart = linkstart;
duke@1 313 for (int i = linkstart; i < inlinetext.length(); i++) {
duke@1 314 char c = inlinetext.charAt(i);
duke@1 315 if (Character.isWhitespace(c) ||
duke@1 316 c == '}') {
duke@1 317 seetextstart = i;
duke@1 318 break;
duke@1 319 }
duke@1 320 }
duke@1 321 String linkName = inlinetext.substring(linkstart+2, seetextstart);
duke@1 322 //Move past the white space after the inline tag name.
duke@1 323 while (Character.isWhitespace(inlinetext.
duke@1 324 charAt(seetextstart))) {
duke@1 325 if (inlinetext.length() <= seetextstart) {
duke@1 326 taglist.append(new TagImpl(holder, "Text",
duke@1 327 inlinetext.substring(textstart, seetextstart)));
duke@1 328 docenv.warning(holder,
duke@1 329 "tag.Improper_Use_Of_Link_Tag",
duke@1 330 inlinetext);
duke@1 331 return taglist.toArray(new Tag[taglist.length()]);
duke@1 332 } else {
duke@1 333 seetextstart++;
duke@1 334 }
duke@1 335 }
duke@1 336 taglist.append(new TagImpl(holder, "Text",
duke@1 337 inlinetext.substring(textstart, linkstart)));
duke@1 338 textstart = seetextstart; // this text is actually seetag
duke@1 339 if ((delimend = findInlineTagDelim(inlinetext, textstart)) == -1) {
duke@1 340 //Missing closing '}' character.
duke@1 341 // store the text as it is with the {@link.
duke@1 342 taglist.append(new TagImpl(holder, "Text",
duke@1 343 inlinetext.substring(textstart)));
duke@1 344 docenv.warning(holder,
duke@1 345 "tag.End_delimiter_missing_for_possible_SeeTag",
duke@1 346 inlinetext);
duke@1 347 return taglist.toArray(new Tag[taglist.length()]);
duke@1 348 } else {
duke@1 349 //Found closing '}' character.
duke@1 350 if (linkName.equals("see")
duke@1 351 || linkName.equals("link")
duke@1 352 || linkName.equals("linkplain")) {
duke@1 353 taglist.append( new SeeTagImpl(holder, "@" + linkName,
duke@1 354 inlinetext.substring(textstart, delimend)));
duke@1 355 } else {
duke@1 356 taglist.append( new TagImpl(holder, "@" + linkName,
duke@1 357 inlinetext.substring(textstart, delimend)));
duke@1 358 }
duke@1 359 textstart = delimend + 1;
duke@1 360 }
duke@1 361 }
duke@1 362 if (textstart == inlinetext.length()) {
duke@1 363 break;
duke@1 364 }
duke@1 365 }
duke@1 366 return taglist.toArray(new Tag[taglist.length()]);
duke@1 367 }
duke@1 368
duke@1 369 /**
duke@1 370 * Recursively find the index of the closing '}' character for an inline tag
duke@1 371 * and return it. If it can't be found, return -1.
duke@1 372 * @param inlineText the text to search in.
duke@1 373 * @param searchStart the index of the place to start searching at.
duke@1 374 * @return the index of the closing '}' character for an inline tag.
duke@1 375 * If it can't be found, return -1.
duke@1 376 */
duke@1 377 private static int findInlineTagDelim(String inlineText, int searchStart) {
duke@1 378 int delimEnd, nestedOpenBrace;
duke@1 379 if ((delimEnd = inlineText.indexOf("}", searchStart)) == -1) {
duke@1 380 return -1;
duke@1 381 } else if (((nestedOpenBrace = inlineText.indexOf("{", searchStart)) != -1) &&
duke@1 382 nestedOpenBrace < delimEnd){
duke@1 383 //Found a nested open brace.
duke@1 384 int nestedCloseBrace = findInlineTagDelim(inlineText, nestedOpenBrace + 1);
duke@1 385 return (nestedCloseBrace != -1) ?
duke@1 386 findInlineTagDelim(inlineText, nestedCloseBrace + 1) :
duke@1 387 -1;
duke@1 388 } else {
duke@1 389 return delimEnd;
duke@1 390 }
duke@1 391 }
duke@1 392
duke@1 393 /**
jjg@1326 394 * Recursively search for the characters '{', '@', followed by
duke@1 395 * name of inline tag and white space,
duke@1 396 * if found
duke@1 397 * return the index of the text following the white space.
duke@1 398 * else
duke@1 399 * return -1.
duke@1 400 */
ksrini@1065 401 private static int inlineTagFound(DocImpl holder, String inlinetext, int start) {
duke@1 402 DocEnv docenv = holder.env;
ksrini@1065 403 int linkstart = inlinetext.indexOf("{@", start);
ksrini@1065 404 if (start == inlinetext.length() || linkstart == -1) {
duke@1 405 return -1;
ksrini@1065 406 } else if (inlinetext.indexOf('}', linkstart) == -1) {
duke@1 407 //Missing '}'.
duke@1 408 docenv.warning(holder, "tag.Improper_Use_Of_Link_Tag",
ksrini@1065 409 inlinetext.substring(linkstart, inlinetext.length()));
duke@1 410 return -1;
duke@1 411 } else {
duke@1 412 return linkstart;
duke@1 413 }
duke@1 414 }
duke@1 415
duke@1 416
duke@1 417 /**
duke@1 418 * Return array of tags for the locale specific first sentence in the text.
duke@1 419 */
duke@1 420 static Tag[] firstSentenceTags(DocImpl holder, String text) {
duke@1 421 DocLocale doclocale = holder.env.doclocale;
duke@1 422 return getInlineTags(holder,
duke@1 423 doclocale.localeSpecificFirstSentence(holder, text));
duke@1 424 }
duke@1 425
duke@1 426 /**
duke@1 427 * Return text for this Doc comment.
duke@1 428 */
ksrini@1065 429 @Override
duke@1 430 public String toString() {
duke@1 431 return text;
duke@1 432 }
duke@1 433 }

mercurial