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

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 198
b4b1f7732289
permissions
-rw-r--r--

Initial load

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

mercurial