commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From leosu...@apache.org
Subject svn commit: r353837 - /jakarta/commons/proper/attributes/trunk/compiler/src/java/org/apache/commons/attributes/javadoc/CATaglet.java
Date Sun, 04 Dec 2005 09:06:04 GMT
Author: leosutic
Date: Sun Dec  4 01:05:52 2005
New Revision: 353837

URL: http://svn.apache.org/viewcvs?rev=353837&view=rev
Log:
Fixes bug 37691: CATaglet don't work with Javadoc tool of JDK1.5 

Javadoc can not be generated with JDK1.5 because in 1.5
there is a new taglet interface
 (com.sun.tools.doclets.internal.toolkit.taglets). I
wrote a wrapper that if the legacy taglet class
(com.sun.tools.doclets.internal.toolkit.taglets.LegacyTaglet)
exists, CATaglets will be wrapped. See modified CATaglet
class that I will attach.

Modified:
    jakarta/commons/proper/attributes/trunk/compiler/src/java/org/apache/commons/attributes/javadoc/CATaglet.java

Modified: jakarta/commons/proper/attributes/trunk/compiler/src/java/org/apache/commons/attributes/javadoc/CATaglet.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/attributes/trunk/compiler/src/java/org/apache/commons/attributes/javadoc/CATaglet.java?rev=353837&r1=353836&r2=353837&view=diff
==============================================================================
--- jakarta/commons/proper/attributes/trunk/compiler/src/java/org/apache/commons/attributes/javadoc/CATaglet.java
(original)
+++ jakarta/commons/proper/attributes/trunk/compiler/src/java/org/apache/commons/attributes/javadoc/CATaglet.java
Sun Dec  4 01:05:52 2005
@@ -15,249 +15,313 @@
  */
 package org.apache.commons.attributes.javadoc;
 
-import com.sun.tools.doclets.Taglet;
-import com.sun.javadoc.*;
-import java.io.File;
 import java.io.BufferedReader;
+import java.io.File;
 import java.io.FileReader;
-import java.util.Map;
-import java.util.HashMap;
-import java.util.TreeMap;
-import java.util.Set;
+import java.lang.reflect.Constructor;
 import java.util.ArrayList;
-import java.util.HashSet;
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.Iterator;
-import java.util.StringTokenizer;
 import java.util.List;
-import java.util.ArrayList;
+import java.util.Map;
+import java.util.Set;
+import java.util.StringTokenizer;
+import java.util.TreeMap;
+
+import com.sun.javadoc.Tag;
+import com.sun.tools.doclets.Taglet;
 
 public class CATaglet implements Taglet {
-    
+
     public static class AttributeTaglet extends CATaglet {
-        
+
         private final String name;
         private final CATaglet caTaglet;
-        
-        public AttributeTaglet (String name, CATaglet caTaglet) {
+
+        public AttributeTaglet(String name, CATaglet caTaglet) {
             this.name = name;
             this.caTaglet = caTaglet;
         }
-        
+
         public String getName() {
             return name;
         }
-        
+
         public String toString(Tag[] tags) {
-            caTaglet.addTags (tags);
+            caTaglet.addTags(tags);
             return null;
         }
     }
+
+    /**
+     * Name of custom tag.
+     */
+    public static final String NAME 
+        = "org.apache.commons.attributes.CATaglet";
     
-    private List tagList = new ArrayList ();
-    private static final String NAME = "org.apache.commons.attributes.CATaglet";
+    /**
+     * Fully qualified class name of the legacy taglet class of JDK1.5.
+     */
+    public static final String LEGACY_TAGLET_CLASS_NAME 
+        = "com.sun.tools.doclets.internal.toolkit.taglets.LegacyTaglet";
+
+    /**
+     * List of tags.
+     */
+    private List tagList = new ArrayList();
     
-    public CATaglet () {
-    }
+    /**
+     * Jdk1.5 legacy taglet class constructor.
+     */
+    private Constructor legacyTagletClassConstructor = null; 
     
-    public void addTags (Tag[] tags) {
+    /**
+     * Default constructor.
+     */
+    public CATaglet() {
+        try {
+            Class legacyTagletClass = Class.forName(LEGACY_TAGLET_CLASS_NAME);
+            legacyTagletClassConstructor 
+                = legacyTagletClass.getConstructor(new Class[] {Taglet.class});
+            //System.err.println("CATaglet will be wrapped.");
+        } catch (Exception e) {
+            /**
+             * Legacy taglet class not available. Seams that JDK1.4 is used to
+             * generate javadoc. No wrapping of taglets necessary.
+             */ 
+            
+            //System.err.println("CATaglet will not be wrapped. See stack trace.");
+            //e.printStackTrace();
+        }
+    }
+
+    public void addTags(Tag[] tags) {
         for (int i = 0; i < tags.length; i++) {
-            tagList.add (tags[i].name() + " " + tags[i].text ());
+            tagList.add(tags[i].name() + " " + tags[i].text());
         }
     }
-    
+
     /**
      * Return the name of this custom tag.
      */
     public String getName() {
         return NAME;
     }
-    
+
     public boolean inField() {
         return true;
     }
-    
+
     public boolean inConstructor() {
         return true;
     }
-    
+
     public boolean inMethod() {
         return true;
     }
-    
+
     public boolean inOverview() {
         return false;
     }
-    
+
     public boolean inPackage() {
         return false;
     }
-    
+
     public boolean inType() {
         return true;
     }
-    
+
     public boolean isInlineTag() {
         return false;
     }
-    
+
     public static void register(Map tagletMap) {
-        CATaglet caTaglet = new CATaglet ();
-        caTaglet.registerTags (tagletMap);
+        CATaglet caTaglet = new CATaglet();
+        caTaglet.registerTags(tagletMap);
     }
-    
-    public void registerTags (Map tagletMap) {
-        Set tagNames = new HashSet ();
-        
-        StringTokenizer tok = new StringTokenizer (System.getProperty ("org.apache.commons.attributes.javadoc.CATaglet.sources"),
File.pathSeparator);
-        while (tok.hasMoreTokens ()) {
+
+    public void registerTags(Map tagletMap) {
+        Set tagNames = new HashSet();
+
+        StringTokenizer tok = new StringTokenizer(
+            System.getProperty(
+                "org.apache.commons.attributes.javadoc.CATaglet.sources"),
+            File.pathSeparator);
+        while (tok.hasMoreTokens()) {
             try {
-                scanFiles (new File (tok.nextToken ()), tagNames);
+                scanFiles(new File(tok.nextToken()), tagNames);
             } catch (Exception e) {
-                System.err.println ("Caught " + e.toString () + " trying to scan Java sources.
Javadoc of attributes may be incomplete.");
+                System.err.println("Caught " + e.toString() + " trying to scan "
+                    + "Java sources. Javadoc of attributes may be incomplete.");
             }
         }
-        if (tagNames.size () > 0) {
-            Iterator iter = tagNames.iterator ();
-            while (iter.hasNext ()) {
-                String name = (String) iter.next ();
+        if (tagNames.size() > 0) {
+            Iterator iter = tagNames.iterator();
+            while (iter.hasNext()) {
+                String name = (String) iter.next();
                 register(name, tagletMap);
             }
-            tagletMap.put (NAME, this);
+            
+            if (tagletMap.containsKey(NAME)) {
+                tagletMap.remove(NAME);
+            }
+            tagletMap.put(NAME, wrapTaglet(this));
         }
     }
-    
-    private void scanFiles (File directory, Collection tagNames) throws Exception {
-        File[] files = directory.listFiles ();
+
+    private void scanFiles(File directory, Collection tagNames)
+        throws Exception {
+        File[] files = directory.listFiles();
         if (files == null) {
             return;
         }
-        
+
         for (int i = 0; i < files.length; i++) {
-            if (files[i].isDirectory ()) {
-                scanFiles (files[i], tagNames);
+            if (files[i].isDirectory()) {
+                scanFiles(files[i], tagNames);
             } else {
-                scanFile (files[i], tagNames);
+                scanFile(files[i], tagNames);
             }
         }
     }
-    
-    private void scanFile (File file, Collection tagNames) throws Exception {
-        BufferedReader br = new BufferedReader (new FileReader (file));
+
+    private void scanFile(File file, Collection tagNames) throws Exception {
+        BufferedReader br = new BufferedReader(new FileReader(file));
         try {
             String line = null;
-            while ((line = br.readLine ()) != null) {
-                scanLine (line, tagNames);
+            while ((line = br.readLine()) != null) {
+                scanLine(line, tagNames);
             }
         } finally {
-            br.close ();
+            br.close();
         }
     }
-    
-    private void scanLine (String line, Collection tagNames) throws Exception {
-        int start = line.indexOf ("@@");
+
+    private void scanLine(String line, Collection tagNames) throws Exception {
+        int start = line.indexOf("@@");
         while (start != -1) {
-            int end = line.indexOf (" ", start);
+            int end = line.indexOf(" ", start);
             if (end == -1) {
-                end = line.length ();
+                end = line.length();
             }
-            tagNames.add (line.substring (start + 1, end));
-            start = line.indexOf ("@@", end);
+            tagNames.add(line.substring(start + 1, end));
+            start = line.indexOf("@@", end);
         }
     }
-    
-    private void register (String name, Map tagletMap) {
-        Taglet tag = new AttributeTaglet ("@" + name, this);
-        if (tagletMap.get (name) != null) {
+
+    private void register(String name, Map tagletMap) {
+        Taglet tag = new AttributeTaglet("@" + name, this);
+        if (tagletMap.containsKey(name)) {
             tagletMap.remove(name);
         }
-        tagletMap.put (name, tag);
+        tagletMap.put(name, wrapTaglet(tag));
     }
-    
+
+    /**
+     * Wraps a JDK1.4 taglet with a JDK1.5 legacy taglet. This is only done if
+     * class <code>com.sun.tools.doclets.internal.toolkit.taglets.LegacyTaglet</code>

+     * exists, what means that JDK1.5 is used to generate Javadoc.
+     * 
+     * @param tag Is the taglet to wrap.
+     * @return Returns the wrapped taglet.
+     */
+    private Object wrapTaglet(Taglet taglet) {
+        Object wrappedTaglet = taglet;
+        if (legacyTagletClassConstructor != null) {
+            try {
+                wrappedTaglet = legacyTagletClassConstructor.newInstance(
+                    new Object[] {taglet});
+            } catch (Exception e) {
+                System.err.println("Wrapping of CATaglet failed.");
+                e.printStackTrace();
+            }
+        }
+        return wrappedTaglet;
+    }
+
     public String toString(Tag tag) {
         return null;
     }
-    
-    public String toString (Tag[] _t) {
-        String[] tags = (String[]) tagList.toArray (new String[0]);
-        
+
+    public String toString(Tag[] _t) {
+        String[] tags = (String[]) tagList.toArray(new String[0]);
+
         if (tags.length == 0) {
             return null;
         }
-        
+
         // Sort by target
-        Map targets = new TreeMap ();
+        Map targets = new TreeMap();
         for (int i = 0; i < tags.length; i++) {
             String target = "";
             String attribute = tags[i];
-            if (tags[i].startsWith ("@@.")) {
-                int targetEnd = tags[i].indexOf (" ", 3);
+            if (tags[i].startsWith("@@.")) {
+                int targetEnd = tags[i].indexOf(" ", 3);
                 if (targetEnd != -1) {
-                    target = tags[i].substring (3, targetEnd);
-                    attribute = "@@" + tags[i].substring (targetEnd).trim ();
-                } else {
-                    
+                    target = tags[i].substring(3, targetEnd);
+                    attribute = "@@" + tags[i].substring(targetEnd).trim();
                 }
             }
-            
-            if (!targets.containsKey (target)) {
-                targets.put (target, new ArrayList ());
+
+            if (!targets.containsKey(target)) {
+                targets.put(target, new ArrayList());
             }
-            
-            List tagsForTarget = (List) targets.get (target);
-            tagsForTarget.add (attribute);
+
+            List tagsForTarget = (List) targets.get(target);
+            tagsForTarget.add(attribute);
         }
-        
-        StringBuffer result = new StringBuffer ();
-        result.append ("<DT><B>Attributes:</B>");
-        List attrs = (List) targets.remove ("");
+
+        StringBuffer result = new StringBuffer();
+        result.append("<DT><B>Attributes:</B>");
+        List attrs = (List) targets.remove("");
         if (attrs != null) {
-            result.append ("<DD><CODE>");
-            Iterator iter = attrs.iterator ();
-            while (iter.hasNext ()) {
-                result.append (iter.next ());
-                if (iter.hasNext ()) {
-                    result.append ("<BR>");
+            result.append("<DD><CODE>");
+            Iterator iter = attrs.iterator();
+            while (iter.hasNext()) {
+                result.append(iter.next());
+                if (iter.hasNext()) {
+                    result.append("<BR>");
                 }
             }
-            result.append ("</CODE>");
+            result.append("</CODE>");
         }
-        
-        List returnAttrs = (List) targets.remove ("return");
-        if (targets.size () > 0) {
-            result.append ("<DT><B>Parameter Attributes:</B>");
-            Iterator parameterTargets = targets.keySet ().iterator ();
-            while (parameterTargets.hasNext ()) {
-                String target = (String) parameterTargets.next ();
-                attrs = (List) targets.remove (target);
-                result.append ("<DD><CODE>" + target + "</CODE> - <BR><CODE>");
-                Iterator iter = attrs.iterator ();
-                while (iter.hasNext ()) {
-                    result.append ("&#160;&#160;&#160;&#160;" + iter.next
());
-                    if (iter.hasNext ()) {
-                        result.append ("<BR>");
+
+        List returnAttrs = (List) targets.remove("return");
+        if (targets.size() > 0) {
+            result.append("<DT><B>Parameter Attributes:</B>");
+            Iterator parameterTargets = targets.keySet().iterator();
+            while (parameterTargets.hasNext()) {
+                String target = (String) parameterTargets.next();
+                attrs = (List) targets.remove(target);
+                result.append("<DD><CODE>" + target + "</CODE> - <BR><CODE>");
+                Iterator iter = attrs.iterator();
+                while (iter.hasNext()) {
+                    result.append("&#160;&#160;&#160;&#160;" + iter.next());
+                    if (iter.hasNext()) {
+                        result.append("<BR>");
                     }
                 }
-                result.append ("</CODE>");
+                result.append("</CODE>");
             }
         }
-        
+
         if (returnAttrs != null) {
-            result.append ("<DT><B>Return Value Attributes:</B>");
-            result.append ("<DD><CODE>");
-            Iterator iter = returnAttrs.iterator ();
-            while (iter.hasNext ()) {
-                result.append (iter.next ());
-                if (iter.hasNext ()) {
-                    result.append ("<BR>");
+            result.append("<DT><B>Return Value Attributes:</B>");
+            result.append("<DD><CODE>");
+            Iterator iter = returnAttrs.iterator();
+            while (iter.hasNext()) {
+                result.append(iter.next());
+                if (iter.hasNext()) {
+                    result.append("<BR>");
                 }
             }
-            result.append ("</CODE>");
+            result.append("</CODE>");
         }
-        
-        tagList.clear ();
-        
-        return result.toString ();
+
+        tagList.clear();
+
+        return result.toString();
     }
 }
-



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message