1 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 ; 21 import java.io.BufferedReader ; 22 import java.io.FileReader ; 23 import java.util.Map ; 24 import java.util.HashMap ; 25 import java.util.TreeMap ; 26 import java.util.Set ; 27 import java.util.ArrayList ; 28 import java.util.HashSet ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 import java.util.StringTokenizer ; 32 import java.util.List ; 33 import java.util.ArrayList ; 34 35 public class CATaglet implements Taglet { 36 37 public static class AttributeTaglet extends CATaglet { 38 39 private final String name; 40 private final CATaglet caTaglet; 41 42 public AttributeTaglet (String name, CATaglet caTaglet) { 43 this.name = name; 44 this.caTaglet = caTaglet; 45 } 46 47 public String getName() { 48 return name; 49 } 50 51 public String toString(Tag[] tags) { 52 caTaglet.addTags (tags); 53 return null; 54 } 55 } 56 57 private List tagList = new ArrayList (); 58 private static final String 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 72 public String 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 tagletMap) { 105 CATaglet caTaglet = new CATaglet (); 106 caTaglet.registerTags (tagletMap); 107 } 108 109 public void registerTags (Map tagletMap) { 110 Set tagNames = new HashSet (); 111 112 StringTokenizer tok = new StringTokenizer (System.getProperty ("org.apache.commons.attributes.javadoc.CATaglet.sources"), File.pathSeparator); 113 while (tok.hasMoreTokens ()) { 114 try { 115 scanFiles (new File (tok.nextToken ()), tagNames); 116 } catch (Exception 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 iter = tagNames.iterator (); 122 while (iter.hasNext ()) { 123 String name = (String ) iter.next (); 124 register(name, tagletMap); 125 } 126 tagletMap.put (NAME, this); 127 } 128 } 129 130 private void scanFiles (File directory, Collection tagNames) throws Exception { 131 File [] 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 file, Collection tagNames) throws Exception { 146 BufferedReader br = new BufferedReader (new FileReader (file)); 147 try { 148 String 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 line, Collection tagNames) throws Exception { 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 name, Map 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 toString(Tag tag) { 178 return null; 179 } 180 181 public String toString (Tag[] _t) { 182 String [] tags = (String []) tagList.toArray (new String [0]); 183 184 if (tags.length == 0) { 185 return null; 186 } 187 188 Map targets = new TreeMap (); 190 for (int i = 0; i < tags.length; i++) { 191 String target = ""; 192 String 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 ()); 205 } 206 207 List tagsForTarget = (List ) targets.get (target); 208 tagsForTarget.add (attribute); 209 } 210 211 StringBuffer result = new StringBuffer (); 212 result.append ("<DT><B>Attributes:</B>"); 213 List attrs = (List ) targets.remove (""); 214 if (attrs != null) { 215 result.append ("<DD><CODE>"); 216 Iterator 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 returnAttrs = (List ) targets.remove ("return"); 227 if (targets.size () > 0) { 228 result.append ("<DT><B>Parameter Attributes:</B>"); 229 Iterator parameterTargets = targets.keySet ().iterator (); 230 while (parameterTargets.hasNext ()) { 231 String target = (String ) parameterTargets.next (); 232 attrs = (List ) targets.remove (target); 233 result.append ("<DD><CODE>" + target + "</CODE> - <BR><CODE>"); 234 Iterator iter = attrs.iterator (); 235 while (iter.hasNext ()) { 236 result.append ("    " + 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 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 |