You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Comment.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  3. * This file is part of the debugger and core tools for the AspectJ(tm)
  4. * programming language; see http://aspectj.org
  5. *
  6. * The contents of this file are subject to the Mozilla Public License
  7. * Version 1.1 (the "License"); you may not use this file except in
  8. * compliance with the License. You may obtain a copy of the License at
  9. * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is AspectJ.
  17. *
  18. * The Initial Developer of the Original Code is Xerox Corporation. Portions
  19. * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
  20. * All Rights Reserved.
  21. */
  22. package org.aspectj.tools.ajdoc;
  23. import com.sun.javadoc.Doc;
  24. import com.sun.javadoc.ParamTag;
  25. import com.sun.javadoc.SeeTag;
  26. import com.sun.javadoc.SerialFieldTag;
  27. import com.sun.javadoc.Tag;
  28. import com.sun.javadoc.ThrowsTag;
  29. import java.util.ArrayList;
  30. import java.util.Iterator;
  31. import java.util.List;
  32. import java.util.Locale;
  33. public class Comment {
  34. /** The parsed comment text. */
  35. private String commentText;
  36. /** The raw comment text. */
  37. private String rawCommentText;
  38. /** The list of tags. */
  39. private List tags;
  40. /** The Doc to which the Comment belongs. */
  41. private Doc doc;
  42. /** The Locale in which this comment resides. */
  43. private Locale loc;
  44. /** The ErrPrinter used by this Comment to output messages. */
  45. private ErrPrinter err;
  46. public Comment(Doc doc, String formalComment) {
  47. this(doc, formalComment, ErrPrinter.instance);
  48. }
  49. public Comment(Doc doc, String formalComment, ErrPrinter err) {
  50. this(doc, formalComment, err, Locale.US);
  51. }
  52. public Comment(Doc doc, String formalComment, ErrPrinter err, Locale loc) {
  53. this.rawCommentText = Util.rawCommentText(formalComment);
  54. this.commentText = Util.commentText(rawCommentText);
  55. this.doc = doc;
  56. this.err = err;
  57. this.loc = loc;
  58. }
  59. /**
  60. * Returns the parsed comment text.
  61. *
  62. * @return the parsed comment text.
  63. */
  64. public String commentText() {
  65. return commentText;
  66. }
  67. /**
  68. * Returns the full unprocessed text of the comment.
  69. *
  70. * @return the full unprocessed text of the comment.
  71. */
  72. public String getRawCommentText() {
  73. return rawCommentText;
  74. }
  75. /**
  76. * Sets the comment text.
  77. *
  78. * @param commentText the new comment text.
  79. */
  80. public void setCommentText(String commentText) {
  81. this.commentText = commentText;
  82. }
  83. /**
  84. * Returns the raw comment text.
  85. *
  86. * @return the raw comment text.
  87. */
  88. public String rawCommentText() {
  89. return rawCommentText;
  90. }
  91. /**
  92. * Sets the raw comment text.
  93. *
  94. * @param rawCommentText the new raw comment text.
  95. */
  96. public void setRawCommentText(String rawCommentText) {
  97. this.rawCommentText = rawCommentText;
  98. }
  99. /**
  100. * Returns all this comment's tags.
  101. *
  102. * @return a List of tags whose elements are Comment instances
  103. */
  104. public List getTags() {
  105. if (tags == null) {
  106. tags = findTags();
  107. }
  108. return tags;
  109. }
  110. /**
  111. * Sets the Doc for this comment.
  112. *
  113. * @param doc the new Doc.
  114. */
  115. public void setDoc(Doc doc) {
  116. this.doc = doc;
  117. }
  118. /**
  119. * Returns the Doc for this comment.
  120. *
  121. * @return the Doc for this comment.
  122. */
  123. public Doc doc() {
  124. return doc;
  125. }
  126. /**
  127. * Sets the locale for this comment.
  128. *
  129. * @param loc the new locale for this comment.
  130. */
  131. public void setLocale(Locale loc) {
  132. this.loc = loc;
  133. }
  134. /**
  135. * Returns the Locale for this comment.
  136. *
  137. * @return the Locale for this comment.
  138. */
  139. public Locale locale() {
  140. return loc;
  141. }
  142. /**
  143. * Sets the ErrPrinter for this comment.
  144. *
  145. * @param err the new ErrPrinter for this comment.
  146. */
  147. public void setErr(ErrPrinter err) {
  148. this.err = err;
  149. }
  150. /**
  151. * Returns the ErrPrinter for this comment.
  152. *
  153. * @return the ErrPrinter for this comment.
  154. */
  155. public ErrPrinter err() {
  156. return err;
  157. }
  158. /**
  159. * Initializes the Doc, Locale, and ErrPrinter.
  160. *
  161. * @param doc the new Doc.
  162. * @param loc the new Locale.
  163. * @param err the new ErrPrinter.
  164. */
  165. public void init(Doc doc, Locale loc, ErrPrinter err) {
  166. setDoc(doc);
  167. setLocale(loc);
  168. setErr(err);
  169. }
  170. /**
  171. * Returns the comment as an array of Tag.
  172. *
  173. * @return an array of Tag representing the comment.
  174. */
  175. public Tag[] inlineTags() {
  176. return Util.inlineTags(doc(),
  177. commentText(),
  178. locale(),
  179. err());
  180. }
  181. /**
  182. * Returns all tags of the comment whose name equals
  183. * <code>tagname</code>.
  184. *
  185. * @return an array of Tag representing all tags of the
  186. * comment whose name equals <code>tagname</code>.
  187. */
  188. public Tag[] tags(String type) {
  189. type = type.startsWith("@") ? type : "@"+type;
  190. List result = new ArrayList();
  191. Tag tag;
  192. for (Iterator i = getTags().iterator(); i.hasNext();) {
  193. if ((tag = (Tag)i.next()).kind().equals(type)) {
  194. result.add(tag);
  195. }
  196. }
  197. return (Tag[])result.toArray(new Tag[result.size()]);
  198. }
  199. /**
  200. * Returns the param tags describing parameters taken
  201. * by this code.
  202. *
  203. * @return an array of ParamTag representing the
  204. * parameters taken by this code.
  205. */
  206. public ParamTag[] paramTags() {
  207. List result = new ArrayList();
  208. Tag tag;
  209. for (Iterator i = getTags().iterator(); i.hasNext();) {
  210. if ((tag = (Tag)i.next()) instanceof ParamTag) {
  211. result.add((ParamTag)tag);
  212. }
  213. }
  214. return (ParamTag[])result.toArray(new ParamTag[result.size()]);
  215. }
  216. /**
  217. * Returns the see tags of the comment.
  218. *
  219. * @return an array of SeeTag representing the
  220. * see tags of the comment.
  221. */
  222. public SeeTag[] seeTags() {
  223. List result = new ArrayList();
  224. Tag tag;
  225. for (Iterator i = getTags().iterator(); i.hasNext();) {
  226. if ((tag = (Tag)i.next()) instanceof SeeTag) {
  227. result.add((SeeTag)tag);
  228. }
  229. }
  230. return (SeeTag[])result.toArray(new SeeTag[result.size()]);
  231. }
  232. /**
  233. * Returns the serial field tags for this field.
  234. *
  235. * @return an array of SerialFieldTag representing the
  236. * serial field tags for this field.
  237. */
  238. public SerialFieldTag[] serialFieldTags() {
  239. List result = new ArrayList();
  240. Tag tag;
  241. for (Iterator i = getTags().iterator(); i.hasNext();) {
  242. if ((tag = (Tag)i.next()) instanceof SerialFieldTag) {
  243. result.add((SerialFieldTag)tag);
  244. }
  245. }
  246. return (SerialFieldTag[])result.toArray
  247. (new SerialFieldTag[result.size()]);
  248. }
  249. /**
  250. * Returns the throw tags describing exceptions thrown
  251. * declared by this code.
  252. *
  253. * @return an array of ThrowsTag representing the exception
  254. * this code declares to throw.
  255. */
  256. public ThrowsTag[] throwsTags() {
  257. List result = new ArrayList();
  258. Tag tag;
  259. for (Iterator i = getTags().iterator(); i.hasNext();) {
  260. if ((tag = (Tag)i.next()) instanceof ThrowsTag) {
  261. result.add((ThrowsTag)tag);
  262. }
  263. }
  264. return (ThrowsTag[])result.toArray
  265. (new ThrowsTag[result.size()]);
  266. }
  267. /**
  268. * Returns all tags of the comment.
  269. *
  270. * @return an array of Tag representing all
  271. * tags of the comment.
  272. */
  273. public Tag[] tags() {
  274. return (Tag[])getTags().toArray
  275. (new Tag[getTags().size()]);
  276. }
  277. /**
  278. * Returns the Tags that comprise the first
  279. * sentence of the comment.
  280. *
  281. * @return an array of Tag representing the first
  282. * sentence of the comment.
  283. */
  284. public Tag[] firstSentenceTags() {
  285. return Util.firstSentenceTags(doc(),
  286. commentText(),
  287. locale(),
  288. err());
  289. }
  290. /**
  291. * Used to lazily initialize the tags of this comment.
  292. *
  293. * @return a List of tags whose elements of Tag instances
  294. * and each represent a tag in this comment.
  295. */
  296. private List findTags() {
  297. return Util.findTags(doc(),
  298. rawCommentText(),
  299. locale(),
  300. err());
  301. }
  302. }