View Javadoc
1   package org.codehaus.mojo.jaxb2.schemageneration.postprocessing.javadoc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import com.thoughtworks.qdox.model.DocletTag;
23  import org.codehaus.mojo.jaxb2.AbstractJaxbMojo;
24  
25  import java.util.List;
26  import java.util.Map;
27  import java.util.SortedMap;
28  import java.util.TreeMap;
29  
30  /**
31   * Simplified structure containing comments and tags read from a JavaDoc comment block.
32   *
33   * @author <a href="mailto:lj@jguru.se">Lennart J&ouml;relid</a>, jGuru Europe AB
34   * @since 2.0
35   */
36  public class JavaDocData {
37  
38      /**
39       * Substitution value for when no JavaDoc comment text was found within a JavaDoc comment block.
40       */
41      public static final String NO_COMMENT = "";
42  
43      // Internal state
44      private String comment;
45      private SortedMap<String, String> tag2ValueMap;
46  
47      /**
48       * Creates a JavaDocData for a particular entry with the supplied JavaDoc comment and List of DocletTags.
49       *
50       * @param comment The actual comment in the JavaDoc. Null values are replaced with the value {@code NO_COMMENT},
51       *                to ensure that the {@code getComment() } method does not return null values.
52       * @param tags    The DocletTags of the JavaDoc entry. Can be null or empty.
53       */
54      public JavaDocData(final String comment, final List<DocletTag> tags) {
55  
56          // Assign internal state
57          this.comment = comment == null ? NO_COMMENT : comment;
58          this.tag2ValueMap = new TreeMap<String, String>();
59  
60          // Parse, and assign internal state
61          for (DocletTag current : tags) {
62  
63              final String tagName = current.getName();
64              String tagValue = current.getValue();
65  
66              // Handle the case of multi-valued tags, such as
67              //
68              // @author SomeAuthor
69              // @author AnotherAuthor
70              //
71              // which becomes [author] --> [SomeAuthor, AnotherAuthor]
72              String currentValue = tag2ValueMap.get(tagName);
73              if (currentValue != null) {
74                  tagValue = currentValue + ", " + current.getValue();
75              }
76  
77              tag2ValueMap.put(tagName, tagValue);
78          }
79      }
80  
81      /**
82       * Retrieves the comment/text in the JavaDoc structure, minus the names and values of any given JavaDoc tags.
83       *
84       * @return the comment/text in the JavaDoc structure, or {@code NO_COMMENT} if no JavaDoc was provided.
85       * Never returns a {@code null} value.
86       */
87      public String getComment() {
88          return comment;
89      }
90  
91      /**
92       * Retrieves the names and values of all JavaDoc tags found.
93       * If two tags were found (such as two {@code @author} tags, the
94       * value contains all found
95       *
96       * @return A non-null Map relating the names of all supplied JavaDoc Tags to their value(s).
97       */
98      public SortedMap<String, String> getTag2ValueMap() {
99          return tag2ValueMap;
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public String toString() {
107 
108         StringBuilder toReturn = new StringBuilder();
109 
110         toReturn.append("\n+=================\n");
111         toReturn.append("| Comment: ").append(comment).append("\n");
112 
113         if (tag2ValueMap.size() == 0) {
114             toReturn.append("| No JavaDoc tags.\n");
115         } else {
116 
117             toReturn.append("| ").append(tag2ValueMap.size()).append(" JavaDoc tags ...\n");
118             for (Map.Entry<String, String> current : tag2ValueMap.entrySet()) {
119                 toReturn.append("| ").append(current.getKey()).append(": ").append(current.getValue()).append("\n");
120             }
121         }
122         toReturn.append("+=================\n\n");
123         return toReturn.toString().replace("\n", AbstractJaxbMojo.NEWLINE);
124     }
125 }