001    /*
002     * CSVBeanMappingTaglet.java
003     *
004     * Copyright (C) 2005 Anupam Sengupta (anupamsg@users.sourceforge.net)
005     *
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU General Public License
008     * as published by the Free Software Foundation; either version 2
009     * of the License, or (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     *
020     * Version: $Revision: 1.2 $
021     */
022    package net.sf.anupam.csv.taglets;
023    
024    import com.sun.javadoc.Tag;
025    import com.sun.tools.doclets.internal.toolkit.taglets.LegacyTaglet;
026    import com.sun.tools.doclets.internal.toolkit.taglets.Taglet;
027    
028    import java.util.Map;
029    import java.util.StringTokenizer;
030    
031    /**
032     * Taglet for outputting the CSV bean mapping information. The
033     * <code>@csv.bean-mapping</code> tags will be displayed for classes
034     * that have CSV bean mapping set in the type Javadoc comment. The tag can
035     * only be specified in the type Javadoc.
036     *
037     * @author Anupam Sengupta
038     * @version $Revision: 1.2 $
039     * @since 1.5
040     */
041    public class CSVBeanMappingTaglet
042            implements com.sun.tools.doclets.Taglet {
043    
044        /**
045         * name of the tag.
046         */
047        private static final String NAME = "csv.bean-mapping";
048    
049        /**
050         * The Javadoc header to print.
051         */
052        private static final String HEADER = "CSV Mapping:";
053    
054        /**
055         * Constructor for CSVBeanMappingTaglet.
056         */
057        public CSVBeanMappingTaglet() {
058            super();
059        }
060    
061        /**
062         * Returns the name of this Javadoc tag.
063         *
064         * @return the name of this tag
065         * @see com.sun.tools.doclets.internal.toolkit.taglets.Taglet#getName()
066         */
067        public String getName() {
068            return NAME;
069        }
070    
071        /**
072         * Indicates whether this tag can be used in a constructor Javadoc.
073         *
074         * @return <code>false</code>
075         * @see com.sun.tools.doclets.internal.toolkit.taglets.Taglet#inConstructor()
076         */
077        public boolean inConstructor() {
078            return false;
079        }
080    
081        /**
082         * Indicates whether this tag can be used in a field Javadoc.
083         *
084         * @return <code>false</code>
085         * @see com.sun.tools.doclets.internal.toolkit.taglets.Taglet#inField()
086         */
087        public boolean inField() {
088            return false;
089        }
090    
091        /**
092         * Indicates whether this tag can be used in a method Javadoc.
093         *
094         * @return <code>false</code>
095         * @see com.sun.tools.doclets.internal.toolkit.taglets.Taglet#inMethod()
096         */
097        public boolean inMethod() {
098            return false;
099        }
100    
101        /**
102         * Indicates whether this tag can be used in the overview Javadoc.
103         *
104         * @return <code>false</code>
105         * @see com.sun.tools.doclets.internal.toolkit.taglets.Taglet#inOverview()
106         */
107        public boolean inOverview() {
108            return false;
109        }
110    
111        /**
112         * Indicates whether this tag can be used in the package Javadoc.
113         *
114         * @return <code>false</code>
115         * @see com.sun.tools.doclets.internal.toolkit.taglets.Taglet#inPackage()
116         */
117        public boolean inPackage() {
118            return false;
119        }
120    
121        /**
122         * Indicates whether this tag can be used in a Type Javadoc.
123         *
124         * @return <code>true</code>
125         * @see com.sun.tools.doclets.internal.toolkit.taglets.Taglet#inType()
126         */
127        public boolean inType() {
128            return true;
129        }
130    
131        /**
132         * Indicates whether this is an inline tag.
133         *
134         * @return <code>false</code>
135         * @see com.sun.tools.doclets.internal.toolkit.taglets.Taglet#isInlineTag()
136         */
137        public boolean isInlineTag() {
138            return false;
139        }
140    
141        /**
142         * Register a new instance of this taglet to the Javadoc taglet set.
143         *
144         * @param tagletMap the Javadoc taglet set.
145         */
146        public static void register(final Map<String, Taglet> tagletMap) {
147            if (tagletMap.containsKey(NAME)) {
148                tagletMap.remove(NAME);
149            }
150            tagletMap.put(NAME, new LegacyTaglet(new CSVBeanMappingTaglet()));
151        }
152    
153        /**
154         * Given the <code>Tag</code> representation of this custom tag, return
155         * its string representation.
156         *
157         * @param tag the <code>Tag</code> representation of this custom tag.
158         * @return String representation of the tag
159         */
160        public String toString(final Tag tag) {
161    
162            final StringTokenizer tokenizer = new StringTokenizer(tag.text());
163            String beanName = "Unknown";
164            String hasCVSHeader = "False";
165            while (tokenizer.hasMoreTokens()) {
166    
167                final String [] nameValuePair = tokenizer.nextToken().split("=");
168    
169                if (nameValuePair != null) {
170    
171                    if (nameValuePair[0].equalsIgnoreCase("bean-name")) {
172                        beanName = nameValuePair[1];
173                    } else if (nameValuePair[0].equalsIgnoreCase("csv-header")) {
174                        hasCVSHeader = nameValuePair[1];
175                    }
176                }
177            }
178            return "<DT><B>"
179                    + HEADER + "</B><DD>" + "<table>" + "<tr>" + "<td>"
180                    + "Mapped Bean Name: " + beanName + "<BR>Has CSV Header: "
181                    + hasCVSHeader + "</td>" + "</tr>" + "</table>" + "</DD>\n";
182        }
183    
184        /**
185         * Returns the string to be displayed on the Javadoc for the specified tags.
186         *
187         * @param tags the list of tags for which the string representation should be returned
188         * @return the string to display on the Javadoc
189         * @see com.sun.tools.doclets.Taglet#toString(com.sun.javadoc.Tag[])
190         */
191        public String toString(final Tag [] tags) {
192            if (tags.length == 0) {
193                return null;
194            }
195            final StringBuffer strBuffer = new StringBuffer();
196            for (Tag tag : tags) {
197                strBuffer.append(this.toString(tag));
198            }
199            return strBuffer.toString();
200        }
201    }