001/*
002 * JDrupes MDoclet
003 * Copyright (C) 2017 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018package org.jdrupes.mdoclet.renderers;
019
020import java.util.ArrayList;
021import java.util.HashSet;
022import java.util.List;
023import java.util.Set;
024
025import org.jdrupes.mdoclet.MDoclet;
026
027import com.sun.javadoc.Doc;
028import com.sun.javadoc.Tag;
029import com.sun.tools.doclets.Taglet;
030
031
032/**
033 * Renders a tag by invocing the taglet's `toString` method.
034 */
035public class TagletTagRenderer implements TagRenderer<Tag> {
036
037        private Taglet taglet;
038        private Set<Doc> handledDocs = new HashSet<>();
039
040        /**
041         * Create a new renderer for the given taglet.
042         * 
043         * @param taglet the taglet
044         */
045    public TagletTagRenderer(Taglet taglet) {
046                this.taglet = taglet;
047        }
048
049        @Override
050    public void render(Tag tag, StringBuilder target, MDoclet doclet) {
051                Doc doc = tag.holder();
052                if (handledDocs.contains(doc)) {
053                        return;
054                }
055                handledDocs.add(doc);
056                List<Tag> tags = new ArrayList<>();
057                for (Tag t: doc.tags()) {
058                        if (t.name().equals(tag.name())) {
059                                tags.add(t);
060                        }
061                }
062        target.append(taglet.toString(tags.toArray(new Tag[tags.size()])));
063    }
064
065}