KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > attributes > javadoc > CATaglet


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.attributes.javadoc;
17
18 import com.sun.tools.doclets.Taglet;
19 import com.sun.javadoc.*;
20 import java.io.File JavaDoc;
21 import java.io.BufferedReader JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.TreeMap JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.ArrayList JavaDoc;
34
35 public class CATaglet implements Taglet {
36     
37     public static class AttributeTaglet extends CATaglet {
38         
39         private final String JavaDoc name;
40         private final CATaglet caTaglet;
41         
42         public AttributeTaglet (String JavaDoc name, CATaglet caTaglet) {
43             this.name = name;
44             this.caTaglet = caTaglet;
45         }
46         
47         public String JavaDoc getName() {
48             return name;
49         }
50         
51         public String JavaDoc toString(Tag[] tags) {
52             caTaglet.addTags (tags);
53             return null;
54         }
55     }
56     
57     private List JavaDoc tagList = new ArrayList JavaDoc ();
58     private static final String JavaDoc NAME = "org.apache.commons.attributes.CATaglet";
59     
60     public CATaglet () {
61     }
62     
63     public void addTags (Tag[] tags) {
64         for (int i = 0; i < tags.length; i++) {
65             tagList.add (tags[i].name() + " " + tags[i].text ());
66         }
67     }
68     
69     /**
70      * Return the name of this custom tag.
71      */

72     public String JavaDoc getName() {
73         return NAME;
74     }
75     
76     public boolean inField() {
77         return true;
78     }
79     
80     public boolean inConstructor() {
81         return true;
82     }
83     
84     public boolean inMethod() {
85         return true;
86     }
87     
88     public boolean inOverview() {
89         return false;
90     }
91     
92     public boolean inPackage() {
93         return false;
94     }
95     
96     public boolean inType() {
97         return true;
98     }
99     
100     public boolean isInlineTag() {
101         return false;
102     }
103     
104     public static void register(Map JavaDoc tagletMap) {
105         CATaglet caTaglet = new CATaglet ();
106         caTaglet.registerTags (tagletMap);
107     }
108     
109     public void registerTags (Map JavaDoc tagletMap) {
110         Set JavaDoc tagNames = new HashSet JavaDoc ();
111         
112         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc (System.getProperty ("org.apache.commons.attributes.javadoc.CATaglet.sources"), File.pathSeparator);
113         while (tok.hasMoreTokens ()) {
114             try {
115                 scanFiles (new File JavaDoc (tok.nextToken ()), tagNames);
116             } catch (Exception JavaDoc e) {
117                 System.err.println ("Caught " + e.toString () + " trying to scan Java sources. Javadoc of attributes may be incomplete.");
118             }
119         }
120         if (tagNames.size () > 0) {
121             Iterator JavaDoc iter = tagNames.iterator ();
122             while (iter.hasNext ()) {
123                 String JavaDoc name = (String JavaDoc) iter.next ();
124                 register(name, tagletMap);
125             }
126             tagletMap.put (NAME, this);
127         }
128     }
129     
130     private void scanFiles (File JavaDoc directory, Collection JavaDoc tagNames) throws Exception JavaDoc {
131         File JavaDoc[] files = directory.listFiles ();
132         if (files == null) {
133             return;
134         }
135         
136         for (int i = 0; i < files.length; i++) {
137             if (files[i].isDirectory ()) {
138                 scanFiles (files[i], tagNames);
139             } else {
140                 scanFile (files[i], tagNames);
141             }
142         }
143     }
144     
145     private void scanFile (File JavaDoc file, Collection JavaDoc tagNames) throws Exception JavaDoc {
146         BufferedReader JavaDoc br = new BufferedReader JavaDoc (new FileReader JavaDoc (file));
147         try {
148             String JavaDoc line = null;
149             while ((line = br.readLine ()) != null) {
150                 scanLine (line, tagNames);
151             }
152         } finally {
153             br.close ();
154         }
155     }
156     
157     private void scanLine (String JavaDoc line, Collection JavaDoc tagNames) throws Exception JavaDoc {
158         int start = line.indexOf ("@@");
159         while (start != -1) {
160             int end = line.indexOf (" ", start);
161             if (end == -1) {
162                 end = line.length ();
163             }
164             tagNames.add (line.substring (start + 1, end));
165             start = line.indexOf ("@@", end);
166         }
167     }
168     
169     private void register (String JavaDoc name, Map JavaDoc tagletMap) {
170         Taglet tag = new AttributeTaglet ("@" + name, this);
171         if (tagletMap.get (name) != null) {
172             tagletMap.remove(name);
173         }
174         tagletMap.put (name, tag);
175     }
176     
177     public String JavaDoc toString(Tag tag) {
178         return null;
179     }
180     
181     public String JavaDoc toString (Tag[] _t) {
182         String JavaDoc[] tags = (String JavaDoc[]) tagList.toArray (new String JavaDoc[0]);
183         
184         if (tags.length == 0) {
185             return null;
186         }
187         
188         // Sort by target
189
Map JavaDoc targets = new TreeMap JavaDoc ();
190         for (int i = 0; i < tags.length; i++) {
191             String JavaDoc target = "";
192             String JavaDoc attribute = tags[i];
193             if (tags[i].startsWith ("@@.")) {
194                 int targetEnd = tags[i].indexOf (" ", 3);
195                 if (targetEnd != -1) {
196                     target = tags[i].substring (3, targetEnd);
197                     attribute = "@@" + tags[i].substring (targetEnd).trim ();
198                 } else {
199                     
200                 }
201             }
202             
203             if (!targets.containsKey (target)) {
204                 targets.put (target, new ArrayList JavaDoc ());
205             }
206             
207             List JavaDoc tagsForTarget = (List JavaDoc) targets.get (target);
208             tagsForTarget.add (attribute);
209         }
210         
211         StringBuffer JavaDoc result = new StringBuffer JavaDoc ();
212         result.append ("<DT><B>Attributes:</B>");
213         List JavaDoc attrs = (List JavaDoc) targets.remove ("");
214         if (attrs != null) {
215             result.append ("<DD><CODE>");
216             Iterator JavaDoc iter = attrs.iterator ();
217             while (iter.hasNext ()) {
218                 result.append (iter.next ());
219                 if (iter.hasNext ()) {
220                     result.append ("<BR>");
221                 }
222             }
223             result.append ("</CODE>");
224         }
225         
226         List JavaDoc returnAttrs = (List JavaDoc) targets.remove ("return");
227         if (targets.size () > 0) {
228             result.append ("<DT><B>Parameter Attributes:</B>");
229             Iterator JavaDoc parameterTargets = targets.keySet ().iterator ();
230             while (parameterTargets.hasNext ()) {
231                 String JavaDoc target = (String JavaDoc) parameterTargets.next ();
232                 attrs = (List JavaDoc) targets.remove (target);
233                 result.append ("<DD><CODE>" + target + "</CODE> - <BR><CODE>");
234                 Iterator JavaDoc iter = attrs.iterator ();
235                 while (iter.hasNext ()) {
236                     result.append ("&#160;&#160;&#160;&#160;" + iter.next ());
237                     if (iter.hasNext ()) {
238                         result.append ("<BR>");
239                     }
240                 }
241                 result.append ("</CODE>");
242             }
243         }
244         
245         if (returnAttrs != null) {
246             result.append ("<DT><B>Return Value Attributes:</B>");
247             result.append ("<DD><CODE>");
248             Iterator JavaDoc iter = returnAttrs.iterator ();
249             while (iter.hasNext ()) {
250                 result.append (iter.next ());
251                 if (iter.hasNext ()) {
252                     result.append ("<BR>");
253                 }
254             }
255             result.append ("</CODE>");
256         }
257         
258         tagList.clear ();
259         
260         return result.toString ();
261     }
262 }
263
264
Popular Tags