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. import com.sun.tools.doclets.Taglet;
  18. import com.sun.javadoc.*;
  19. import java.io.File;
  20. import java.io.BufferedReader;
  21. import java.io.FileReader;
  22. import java.util.Map;
  23. import java.util.HashMap;
  24. import java.util.TreeMap;
  25. import java.util.Set;
  26. import java.util.ArrayList;
  27. import java.util.HashSet;
  28. import java.util.Collection;
  29. import java.util.Iterator;
  30. import java.util.StringTokenizer;
  31. import java.util.List;
  32. import java.util.ArrayList;
  33. public class CATaglet implements Taglet {
  34. public static class AttributeTaglet extends CATaglet {
  35. private final String name;
  36. private final CATaglet caTaglet;
  37. public AttributeTaglet (String name, CATaglet caTaglet) {
  38. this.name = name;
  39. this.caTaglet = caTaglet;
  40. }
  41. public String getName() {
  42. return name;
  43. }
  44. public String toString(Tag[] tags) {
  45. caTaglet.addTags (tags);
  46. return null;
  47. }
  48. }
  49. private List tagList = new ArrayList ();
  50. private static final String NAME = "org.apache.commons.attributes.CATaglet";
  51. public CATaglet () {
  52. }
  53. public void addTags (Tag[] tags) {
  54. for (int i = 0; i < tags.length; i++) {
  55. tagList.add (tags[i].name() + " " + tags[i].text ());
  56. }
  57. }
  58. /**
  59. * Return the name of this custom tag.
  60. */
  61. public String getName() {
  62. return NAME;
  63. }
  64. public boolean inField() {
  65. return true;
  66. }
  67. public boolean inConstructor() {
  68. return true;
  69. }
  70. public boolean inMethod() {
  71. return true;
  72. }
  73. public boolean inOverview() {
  74. return false;
  75. }
  76. public boolean inPackage() {
  77. return false;
  78. }
  79. public boolean inType() {
  80. return true;
  81. }
  82. public boolean isInlineTag() {
  83. return false;
  84. }
  85. public static void register(Map tagletMap) {
  86. CATaglet caTaglet = new CATaglet ();
  87. caTaglet.registerTags (tagletMap);
  88. }
  89. public void registerTags (Map tagletMap) {
  90. Set tagNames = new HashSet ();
  91. StringTokenizer tok = new StringTokenizer (System.getProperty ("org.apache.commons.attributes.javadoc.CATaglet.sources"), File.pathSeparator);
  92. while (tok.hasMoreTokens ()) {
  93. try {
  94. scanFiles (new File (tok.nextToken ()), tagNames);
  95. } catch (Exception e) {
  96. System.err.println ("Caught " + e.toString () + " trying to scan Java sources. Javadoc of attributes may be incomplete.");
  97. }
  98. }
  99. if (tagNames.size () > 0) {
  100. Iterator iter = tagNames.iterator ();
  101. while (iter.hasNext ()) {
  102. String name = (String) iter.next ();
  103. register(name, tagletMap);
  104. }
  105. tagletMap.put (NAME, this);
  106. }
  107. }
  108. private void scanFiles (File directory, Collection tagNames) throws Exception {
  109. File[] files = directory.listFiles ();
  110. if (files == null) {
  111. return;
  112. }
  113. for (int i = 0; i < files.length; i++) {
  114. if (files[i].isDirectory ()) {
  115. scanFiles (files[i], tagNames);
  116. } else {
  117. scanFile (files[i], tagNames);
  118. }
  119. }
  120. }
  121. private void scanFile (File file, Collection tagNames) throws Exception {
  122. BufferedReader br = new BufferedReader (new FileReader (file));
  123. try {
  124. String line = null;
  125. while ((line = br.readLine ()) != null) {
  126. scanLine (line, tagNames);
  127. }
  128. } finally {
  129. br.close ();
  130. }
  131. }
  132. private void scanLine (String line, Collection tagNames) throws Exception {
  133. int start = line.indexOf ("@@");
  134. while (start != -1) {
  135. int end = line.indexOf (" ", start);
  136. if (end == -1) {
  137. end = line.length ();
  138. }
  139. tagNames.add (line.substring (start + 1, end));
  140. start = line.indexOf ("@@", end);
  141. }
  142. }
  143. private void register (String name, Map tagletMap) {
  144. Taglet tag = new AttributeTaglet ("@" + name, this);
  145. if (tagletMap.get (name) != null) {
  146. tagletMap.remove(name);
  147. }
  148. tagletMap.put (name, tag);
  149. }
  150. public String toString(Tag tag) {
  151. return null;
  152. }
  153. public String toString (Tag[] _t) {
  154. String[] tags = (String[]) tagList.toArray (new String[0]);
  155. if (tags.length == 0) {
  156. return null;
  157. }
  158. // Sort by target
  159. Map targets = new TreeMap ();
  160. for (int i = 0; i < tags.length; i++) {
  161. String target = "";
  162. String attribute = tags[i];
  163. if (tags[i].startsWith ("@@.")) {
  164. int targetEnd = tags[i].indexOf (" ", 3);
  165. if (targetEnd != -1) {
  166. target = tags[i].substring (3, targetEnd);
  167. attribute = "@@" + tags[i].substring (targetEnd).trim ();
  168. } else {
  169. }
  170. }
  171. if (!targets.containsKey (target)) {
  172. targets.put (target, new ArrayList ());
  173. }
  174. List tagsForTarget = (List) targets.get (target);
  175. tagsForTarget.add (attribute);
  176. }
  177. StringBuffer result = new StringBuffer ();
  178. result.append ("<DT><B>Attributes:</B>");
  179. List attrs = (List) targets.remove ("");
  180. if (attrs != null) {
  181. result.append ("<DD><CODE>");
  182. Iterator iter = attrs.iterator ();
  183. while (iter.hasNext ()) {
  184. result.append (iter.next ());
  185. if (iter.hasNext ()) {
  186. result.append ("<BR>");
  187. }
  188. }
  189. result.append ("</CODE>");
  190. }
  191. List returnAttrs = (List) targets.remove ("return");
  192. if (targets.size () > 0) {
  193. result.append ("<DT><B>Parameter Attributes:</B>");
  194. Iterator parameterTargets = targets.keySet ().iterator ();
  195. while (parameterTargets.hasNext ()) {
  196. String target = (String) parameterTargets.next ();
  197. attrs = (List) targets.remove (target);
  198. result.append ("<DD><CODE>" + target + "</CODE> - <BR><CODE>");
  199. Iterator iter = attrs.iterator ();
  200. while (iter.hasNext ()) {
  201. result.append ("    " + iter.next ());
  202. if (iter.hasNext ()) {
  203. result.append ("<BR>");
  204. }
  205. }
  206. result.append ("</CODE>");
  207. }
  208. }
  209. if (returnAttrs != null) {
  210. result.append ("<DT><B>Return Value Attributes:</B>");
  211. result.append ("<DD><CODE>");
  212. Iterator iter = returnAttrs.iterator ();
  213. while (iter.hasNext ()) {
  214. result.append (iter.next ());
  215. if (iter.hasNext ()) {
  216. result.append ("<BR>");
  217. }
  218. }
  219. result.append ("</CODE>");
  220. }
  221. tagList.clear ();
  222. return result.toString ();
  223. }
  224. }