View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.tools.plugin.extractor.annotations.converter;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.Map;
26  
27  import com.thoughtworks.qdox.model.DocletTag;
28  import org.apache.maven.tools.plugin.extractor.annotations.converter.tag.block.JavadocBlockTagToHtmlConverter;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * Converts a given
34   * <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/javadoc/doc-comment-spec.html#block-tags">
35   * javadoc block taglet</a> to HTML.
36   * The implementations have a name equal to the supported doclet name.
37   * Must be called for each block tag individually.
38   */
39  @Named
40  @Singleton
41  public class JavadocBlockTagsToXhtmlConverter {
42      private static final Logger LOG = LoggerFactory.getLogger(JavadocBlockTagsToXhtmlConverter.class);
43  
44      private final JavadocInlineTagsToXhtmlConverter inlineTagsConverter;
45      private final Map<String, JavadocBlockTagToHtmlConverter> blockTagConverters;
46  
47      @Inject
48      public JavadocBlockTagsToXhtmlConverter(
49              JavadocInlineTagsToXhtmlConverter inlineTagsConverter,
50              Map<String, JavadocBlockTagToHtmlConverter> blockTagConverters) {
51          this.inlineTagsConverter = inlineTagsConverter;
52          this.blockTagConverters = blockTagConverters;
53      }
54  
55      public String convert(DocletTag docletTag, ConverterContext context) {
56          return convert(docletTag.getName(), docletTag.getValue(), context);
57      }
58  
59      public String convert(String name, String text, ConverterContext context) {
60          JavadocBlockTagToHtmlConverter converter = blockTagConverters.get(name);
61          if (converter == null) {
62              return getOriginalTag(name, text) + "<!-- unknown block tag '" + name + "' -->";
63          } else {
64              try {
65                  String convertedBlockTagValue = converter.convert(text, context);
66                  return inlineTagsConverter.convert(convertedBlockTagValue, context);
67              } catch (Throwable t) {
68                  LOG.warn("Error converting javadoc block tag '{}' in {}", name, context.getLocation(), t);
69                  return getOriginalTag(name, text) + "<!-- error processing javadoc tag '" + name + "': "
70                          + t.getMessage() + "-->"; // leave original javadoc in place
71              }
72          }
73      }
74  
75      private static String getOriginalTag(String name, String text) {
76          return "@" + name + " " + text;
77      }
78  }