View Javadoc
1   package org.codehaus.plexus.javadoc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Enumeration;
23  import java.util.StringTokenizer;
24  
25  import javax.swing.text.AttributeSet;
26  import javax.swing.text.MutableAttributeSet;
27  import javax.swing.text.SimpleAttributeSet;
28  
29  import com.sun.javadoc.Tag;
30  import com.sun.tools.doclets.Taglet;
31  
32  /**
33   * Abstract <code>Taglet</code> for <a href="http://plexus.codehaus.org/"/>Plexus</a> tags.
34   *
35   * @see <a href="http://plexus.codehaus.org/guides/developer-guide/appendices/javadoc-tags-reference.html">
36   * http://plexus.codehaus.org/guides/developer-guide/appendices/javadoc-tags-reference.html</a>;
37   *
38   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
39   * @version $Id$
40   */
41  public abstract class AbstractPlexusTaglet
42      implements Taglet
43  {
44      /** {@inheritDoc} */
45      public String toString( Tag tag )
46      {
47          if ( tag == null )
48          {
49              return null;
50          }
51  
52          String tagText = tag.text();
53          MutableAttributeSet att = getAttributes( tagText );
54  
55          StringBuffer sb = new StringBuffer();
56          if ( ( att == null ) || ( att.getAttributeCount() == 0 ) )
57          {
58              sb.append( "<DT><B>" ).append( getHeader() ).append( "</B></DT><DD></DD>" );
59          }
60          else
61          {
62              sb.append( "<DT><B>" ).append( getHeader() ).append( ":</B></DT>" );
63              sb.append( "<DD><TABLE CELLPADDING=\"2\" CELLSPACING=\"0\"><TR><TD>" );
64  
65              appendPlexusTag( sb, att );
66  
67              sb.append( "</TD></TR></TABLE></DD>" );
68          }
69  
70          return sb.toString();
71      }
72  
73      /** {@inheritDoc} */
74      public String toString( Tag[] tags )
75      {
76          if ( tags.length == 0 )
77          {
78              return null;
79          }
80  
81          boolean hasParameters = false;
82          for (Tag tag : tags) {
83              String tagText = tag.text();
84              MutableAttributeSet att = getAttributes(tagText);
85              if (att != null) {
86                  hasParameters = att.getAttributeCount() > 0;
87              }
88          }
89  
90          StringBuffer sb = new StringBuffer();
91          sb.append( "<DT><B>" ).append( getHeader() ).append( ( hasParameters ? ":" : "" ) ).append( "</B></DT>" );
92          sb.append( "<DD><TABLE CELLPADDING=\"2\" CELLSPACING=\"0\"><TR><TD>" );
93          for ( int i = 0; i < tags.length; i++ )
94          {
95              if ( i > 0 )
96              {
97                  sb.append( ", " );
98              }
99  
100             String tagText = tags[i].text();
101             appendPlexusTag( sb, getAttributes( tagText ) );
102         }
103 
104         sb.append( "</TD></TR></TABLE></DD>" );
105 
106         return sb.toString();
107     }
108 
109     /**
110      * @return the header to display
111      */
112     public abstract String getHeader();
113 
114     /**
115      * @param text the Tag text returned by {@link Tag#text()}
116      * @return a MutableAttributeSet or null if text was null
117      */
118     private MutableAttributeSet getAttributes( String text )
119     {
120         if ( text == null || text.trim().length() == 0 )
121         {
122             return null;
123         }
124 
125         MutableAttributeSet att = new SimpleAttributeSet();
126 
127         StringTokenizer token = new StringTokenizer( text, " " );
128         while ( token.hasMoreTokens() )
129         {
130             String nextToken = token.nextToken();
131 
132             StringTokenizer token2 = new StringTokenizer( nextToken, "=" );
133             if ( token2.countTokens() != 2 )
134             {
135                 System.err.println( "The annotation '" + getName() + "' has a wrong Plexus annotations: " + text );
136                 continue;
137             }
138 
139             String name = token2.nextToken();
140             String value = token2.nextToken();
141 
142             att.addAttribute( name, value );
143         }
144 
145         return att;
146     }
147 
148     /**
149      * Append the wanted display in the javadoc.
150      *
151      * @param sb
152      * @param att
153      */
154     private static void appendPlexusTag( StringBuffer sb, MutableAttributeSet att )
155     {
156         if ( att == null )
157         {
158             return;
159         }
160 
161         if ( att.getAttributeCount() > 0 )
162         {
163             sb.append( "<DL>" );
164 
165             Enumeration names = att.getAttributeNames();
166 
167             while ( names.hasMoreElements() )
168             {
169                 Object key = names.nextElement();
170                 Object value = att.getAttribute( key );
171 
172                 if ( value instanceof AttributeSet )
173                 {
174                     // ignored
175                 }
176                 else
177                 {
178                     sb.append( "<DT><B>" ).append( key ).append( ":</B></DT>" );
179                     sb.append( "<DD>" ).append( value ).append( "</DD>" );
180                 }
181             }
182 
183             sb.append( "</DL>" );
184         }
185     }
186 }