001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.tools.plugin.extractor.annotations.converter;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.util.Map;
026
027import com.thoughtworks.qdox.model.DocletTag;
028import org.apache.maven.tools.plugin.extractor.annotations.converter.tag.block.JavadocBlockTagToHtmlConverter;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * Converts a given
034 * <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/javadoc/doc-comment-spec.html#block-tags">
035 * javadoc block taglet</a> to HTML.
036 * The implementations have a name equal to the supported doclet name.
037 * Must be called for each block tag individually.
038 */
039@Named
040@Singleton
041public class JavadocBlockTagsToXhtmlConverter {
042    private static final Logger LOG = LoggerFactory.getLogger(JavadocBlockTagsToXhtmlConverter.class);
043
044    private final JavadocInlineTagsToXhtmlConverter inlineTagsConverter;
045    private final Map<String, JavadocBlockTagToHtmlConverter> blockTagConverters;
046
047    @Inject
048    public JavadocBlockTagsToXhtmlConverter(
049            JavadocInlineTagsToXhtmlConverter inlineTagsConverter,
050            Map<String, JavadocBlockTagToHtmlConverter> blockTagConverters) {
051        this.inlineTagsConverter = inlineTagsConverter;
052        this.blockTagConverters = blockTagConverters;
053    }
054
055    public String convert(DocletTag docletTag, ConverterContext context) {
056        return convert(docletTag.getName(), docletTag.getValue(), context);
057    }
058
059    public String convert(String name, String text, ConverterContext context) {
060        JavadocBlockTagToHtmlConverter converter = blockTagConverters.get(name);
061        if (converter == null) {
062            return getOriginalTag(name, text) + "<!-- unknown block tag '" + name + "' -->";
063        } else {
064            try {
065                String convertedBlockTagValue = converter.convert(text, context);
066                return inlineTagsConverter.convert(convertedBlockTagValue, context);
067            } catch (Throwable t) {
068                LOG.warn("Error converting javadoc block tag '{}' in {}", name, context.getLocation(), t);
069                return getOriginalTag(name, text) + "<!-- error processing javadoc tag '" + name + "': "
070                        + t.getMessage() + "-->"; // leave original javadoc in place
071            }
072        }
073    }
074
075    private static String getOriginalTag(String name, String text) {
076        return "@" + name + " " + text;
077    }
078}