KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.incava.javadoc;
2
3 import java.io.*;
4 import java.util.*;
5 import org.incava.text.LineMapping;
6 import org.incava.text.Location;
7
8
9 /**
10  * A tagged element, such as:
11  * @since 0.1
12  */

13 public class JavadocTaggedNode extends JavadocElement
14 {
15     private boolean parsed = false;
16
17     private JavadocTag tag = null;
18
19     private JavadocElement target = null;
20
21     private JavadocElement description = null;
22
23     private JavadocElement descriptionNonTarget = null;
24     
25     public JavadocTaggedNode(String JavaDoc text, Location start, Location end)
26     {
27         super(text, start, end);
28     }
29
30     public JavadocTag getTag()
31     {
32         parse();
33         return tag;
34     }
35
36     /**
37      * Tag targets may be one of three forms:
38      *
39      * @html <a HREF="http://www.foo.org/something.html">An HTML Target</a>
40      * @quoted "A Quoted Target"
41      * @word Word
42      */

43     public JavadocElement getTarget()
44     {
45         parse();
46         return target;
47     }
48
49     /**
50      * This returns the text following the tag, and including the target.
51      */

52     public JavadocElement getDescription()
53     {
54         parse();
55         return description;
56     }
57
58     /**
59      * This returns the text following the target.
60      */

61     public JavadocElement getDescriptionNonTarget()
62     {
63         parse();
64         return descriptionNonTarget;
65     }
66
67     protected void parse()
68     {
69         if (!parsed) {
70             int pos = 0;
71             int line = start.line;
72             int col = start.column;
73             int len = text.length();
74
75             // has to be a tag first
76
while (pos < len && !Character.isWhitespace(text.charAt(pos))) {
77                 ++pos;
78             }
79
80             // tr.Ace.log("after tag, pos: " + pos);
81
// tr.Ace.log("tag line : " + line);
82
// tr.Ace.log("start col: " + start.column);
83
// tr.Ace.log("end col : " + (pos - 1 + start.column));
84

85             tag = new JavadocTag(text.substring(0, pos), new Location(line, start.column), new Location(line, pos - 1 + start.column));
86
87             // tr.Ace.log("created tag '" + tag.text + "'");
88

89             LineMapping lines = new LineMapping(text, start.line, start.column);
90
91             // skip non text
92
while (pos < len && (Character.isWhitespace(text.charAt(pos)) || text.charAt(pos) == '*')) {
93                 ++pos;
94             }
95
96             // tr.Ace.log("position: " + pos);
97
// tr.Ace.log("current char: " + text.charAt(pos));
98

99             if (pos < len) {
100                 // tr.Ace.log("parsing target ...");
101

102                 // target types:
103
final int HTML = 0;
104                 final int QUOTED = 1;
105                 final int WORD = 2;
106
107                 int targetStart = pos;
108                 
109                 int type;
110                 if (pos + 2 < len && text.substring(pos, pos + 2).equalsIgnoreCase("<a")) {
111                     type = HTML;
112                 }
113                 else if (text.charAt(pos) == '"') {
114                     type = QUOTED;
115                 }
116                 else {
117                     type = WORD;
118                 }
119
120                 // tr.Ace.log("target type: " + type);
121

122                 // Also handle targets with balanced parentheses, for example:
123
// @see set(int, double, java.net.Socket)
124
// These can't be nested.
125

126                 boolean inParen = false;
127
128                 while (pos < len) {
129                     char ch = text.charAt(pos);
130                     if (ch == '\\' && pos + 1 < len) {
131                         ++pos;
132                     }
133                     else if (type == WORD) {
134                         if (ch == '(') {
135                             inParen = true;
136                         }
137                         else if (inParen && ch == ')') {
138                             inParen = false;
139                         }
140                         if (!inParen) {
141                             if (pos + 1 == len) {
142                                 // we'll never get a space, because we're at the end
143
++pos;
144                                 break;
145                             }
146                             else if (Character.isWhitespace(ch)) {
147                                 // we have a space between the target and the next word
148
break;
149                             }
150                         }
151                     }
152                     else if (type == HTML && ch == '>' && Character.toLowerCase(text.charAt(pos - 1)) == 'a') {
153                         // HTML target
154
++pos;
155                         break;
156                     }
157                     else if (type == QUOTED && ch == '"') {
158                         // quoted target
159
++pos;
160                         break;
161                     }
162                     ++pos;
163                 }
164
165                 // even unbalanced HTML or double-quoted strings will get a target:
166

167                 Location[] targetLocations = lines.getLocations(targetStart, pos - 1);
168
169                 // tr.Ace.log("creating target ...");
170
target = new JavadocElement(text.substring(targetStart, pos), targetLocations[0], targetLocations[1]);
171                 // tr.Ace.log("target: " + target);
172

173                 // skip non text
174
while (pos < len && (Character.isWhitespace(text.charAt(pos)) || text.charAt(pos) == '*')) {
175                     ++pos;
176                 }
177
178                 if (pos == len) {
179                     // no description beyond target
180
// tr.Ace.log("no description beyond target");
181
descriptionNonTarget = null;
182                     description = new JavadocElement(text.substring(targetStart, len), targetLocations[0], end);
183                 }
184                 else if (pos < len && !Character.isWhitespace(text.charAt(pos))) {
185                     // tr.Ace.log("creating description non-target");
186
Location dntStart = lines.getLocation(pos);
187                     descriptionNonTarget = new JavadocElement(text.substring(pos, len), dntStart, end);
188                     // tr.Ace.log("created description non-target: " + descriptionNonTarget);
189
description = new JavadocElement(text.substring(targetStart, len), targetLocations[0], end);
190                 }
191             }
192         }
193     }
194
195 }
196
Popular Tags