KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > incava > javadoc > JavadocParser


1 package org.incava.javadoc;
2
3 import java.awt.Point JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6
7
8 /**
9  * Parses Javadoc into a list of points, which represent the locations of
10  * description and tagged comments in a Javadoc comment block.
11  */

12 public class JavadocParser
13 {
14     /**
15      * The current position.
16      */

17     private int pos;
18
19     /**
20      * The length of the Javadoc being parsed.
21      */

22     private int len;
23
24     /**
25      * The Javadoc being parsed.
26      */

27     private String JavaDoc text;
28
29     /**
30      * Parses the Javadoc in the text. Assumes a start line of 1 and a start
31      * column of 1.
32      */

33     public List JavaDoc parse(String JavaDoc text)
34     {
35         return parse(text, 1, 1);
36     }
37
38     public List JavaDoc parse(String JavaDoc text, int startLine, int startColumn)
39     {
40         // tr.Ace.log("text", "+" + text + "+");
41

42         this.text = text;
43         
44         len = text.length();
45         ArrayList JavaDoc ary = new ArrayList JavaDoc();
46         pos = 0;
47
48         while (pos < len && Character.isWhitespace(text.charAt(pos))) {
49             ++pos;
50         }
51         
52         if (pos + 3 < len && text.startsWith("/**")) { // unmangle Emacs: */
53
// tr.Ace.log("got comment start");
54
pos += 3;
55
56             while (pos < len && (Character.isWhitespace(text.charAt(pos)) || text.charAt(pos) == '*')) {
57                 ++pos;
58             }
59
60             --len;
61             while (len >= 0) {
62                 // tr.Ace.log("char[" + len + "]: '" + text.charAt(len) + "'");
63
if (Character.isWhitespace(text.charAt(len)) || text.charAt(len) == '*') {
64                     // tr.Ace.log("star or WS; (text: '" + text.charAt(len) + "')");
65
--len;
66                 }
67                 else if (len > 0 && text.charAt(len) == '/' && text.charAt(len - 1) == '*') {
68                     // tr.Ace.log("at end of comment");
69
len -= 2;
70                 }
71                 else {
72                     break;
73                 }
74             }
75             ++len;
76
77             // tr.Ace.log("pos: " + pos + "; len: " + len);
78

79             if (pos < len) {
80                 // the description
81
if (text.charAt(pos) == '@') {
82                     // tr.Ace.log("got tag start -- no description");
83
// null means no description
84
ary.add(null);
85                 }
86                 else {
87                     // tr.Ace.log("at description start: " + pos);
88

89                     Point JavaDoc desc = new Point JavaDoc(pos, -1);
90
91                     read(desc);
92                 
93                     // tr.Ace.log("at end, pos: " + pos + "; desc pos : " + desc);
94

95                     ary.add(desc);
96                 }
97
98                 // now, the tagged comments:
99
while (pos < len && text.charAt(pos) == '@') {
100                     // tr.Ace.log("tag starting.");
101

102                     Point JavaDoc tag = new Point JavaDoc(pos, -1);
103
104                     ++pos;
105                     read(tag);
106
107                     // tr.Ace.log("tag pos : " + tag);
108

109                     ary.add(tag);
110                 }
111             }
112             
113             // tr.Ace.log("returning: " + ary);
114

115             return ary;
116         }
117         else {
118             // tr.Ace.log("no Javadoc comment in this string.");
119
return null;
120         }
121     }
122
123     /**
124      * Reads to the next Javadoc field, or to the end of the comment.
125      */

126     protected void read(Point JavaDoc pt)
127     {
128         // tr.Ace.log("pos: " + pos + ", len: " + len + ", text: " + text + ", pt: " + pt);
129

130         pt.y = pos;
131         while (pos < len && (text.charAt(pos) != '@' || (pos >= 0 && text.charAt(pos - 1) == '{'))) {
132             // // tr.Ace.log("pos: " + pos);
133
pt.y = pos;
134
135             ++pos;
136                         
137             // read to end of line, or end of text.
138
// Mac: \r, Unix: \n, DOS: \r\n:
139
if (text.charAt(pos) == '\r') {
140                 if (pos + 1 < len && text.charAt(pos + 1) == '\n') {
141                     ++pos;
142                 }
143             }
144             else if (text.charAt(pos) != '\n') {
145                 continue;
146             }
147
148             // tr.Ace.log("looking for next position");
149

150             // now, we're at the start of a new line:
151
while (pos < len && (Character.isWhitespace(text.charAt(pos)) || text.charAt(pos) == '*')) {
152                 ++pos;
153                 // tr.Ace.log("advanced pos: " + pos);
154
}
155         }
156
157         ++pt.y;
158         // tr.Ace.log("at end -- pos : " + pos + ", len: " + len + ", char: '" + text.charAt(pos) + "', text: " + text + ", pt: " + pt);
159
}
160
161 }
162
Popular Tags