001    /*
002     * CSVFieldMappingTaglet.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 field mapping tag information.  The
033     * <code>@csv.field-mapping</code> tags will be displayed for classes
034     * that have CSV field mapping set in the method Javadoc documentation.
035     * The tag can only be used on the mapped bean's <code>getXXX()</code> method Javadocs.
036     *
037     * @author Anupam Sengupta
038     * @version $Revision: 1.2 $
039     * @since 1.5
040     */
041    public class CSVFieldMappingTaglet
042            implements com.sun.tools.doclets.Taglet {
043    
044        /**
045         * name of the tag.
046         */
047        private static final String NAME = "csv.field-mapping";
048    
049        /**
050         * The Javadoc header to print.
051         */
052        private static final String HEADER = "CSV Field Mapping:";
053    
054        /**
055         * Constructor for CSVBeanMappingTaglet.
056         */
057        public CSVFieldMappingTaglet() {
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>true</code>
095         * @see com.sun.tools.doclets.internal.toolkit.taglets.Taglet#inMethod()
096         */
097        public boolean inMethod() {
098            return true;
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>false</code>
125         * @see com.sun.tools.doclets.internal.toolkit.taglets.Taglet#inType()
126         */
127        public boolean inType() {
128            return false;
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 CSVFieldMappingTaglet()));
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 position = "Unknown";
164            String reformatter = "None";
165            while (tokenizer.hasMoreTokens()) {
166    
167                final String [] nameValuePair = tokenizer.nextToken().split("=");
168    
169                if (nameValuePair != null) {
170    
171                    if (nameValuePair[0].equalsIgnoreCase("position")) {
172                        position = nameValuePair[1];
173                    } else if (nameValuePair[0].equalsIgnoreCase("reformat")) {
174                        reformatter = nameValuePair[1];
175                    }
176                }
177            }
178            return "<DT><B>"
179                    + HEADER + "</B><DD>" + "<table>" + "<tr>" + "<td>"
180                    + "CSV Field Position: " + position + "<BR>Reformatter: "
181                    + reformatter + "</td>" + "</tr>" + "</table>" + "</DD>\n";
182        }
183    
184        /**
185         * Returns the string to be included the the output Javadoc for the specified tags.
186         *
187         * @param tags the tags for which the string representation should be returned
188         * @return the string representation to include in the output 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    }