1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
4 | * |
5 | * This file is part of JavaParser. |
6 | * |
7 | * JavaParser can be used either under the terms of |
8 | * a) the GNU Lesser General Public License as published by |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. |
11 | * b) the terms of the Apache License |
12 | * |
13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
14 | * LICENCE.APACHE. Please refer to those files for details. |
15 | * |
16 | * JavaParser is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU Lesser General Public License for more details. |
20 | */ |
21 | |
22 | package com.github.javaparser.javadoc; |
23 | |
24 | import com.github.javaparser.ast.comments.JavadocComment; |
25 | import com.github.javaparser.javadoc.description.JavadocDescription; |
26 | |
27 | import java.util.LinkedList; |
28 | import java.util.List; |
29 | |
30 | import static com.github.javaparser.utils.Utils.*; |
31 | |
32 | /** |
33 | * The structured content of a single Javadoc comment. |
34 | * <p> |
35 | * It is composed by a description and a list of block tags. |
36 | * <p> |
37 | * An example would be the text contained in this very Javadoc comment. At the moment |
38 | * of this writing this comment does not contain any block tags (such as {@code @see AnotherClass}) |
39 | */ |
40 | public class Javadoc { |
41 | |
42 | private JavadocDescription description; |
43 | private List<JavadocBlockTag> blockTags; |
44 | |
45 | public Javadoc(JavadocDescription description) { |
46 | this.description = description; |
47 | this.blockTags = new LinkedList<>(); |
48 | } |
49 | |
50 | public Javadoc addBlockTag(JavadocBlockTag blockTag) { |
51 | this.blockTags.add(blockTag); |
52 | return this; |
53 | } |
54 | |
55 | /** |
56 | * For tags like "@return good things" where |
57 | * tagName is "return", |
58 | * and the rest is content. |
59 | */ |
60 | public Javadoc addBlockTag(String tagName, String content) { |
61 | return addBlockTag(new JavadocBlockTag(tagName, content)); |
62 | } |
63 | |
64 | /** |
65 | * For tags like "@param abc this is a parameter" where |
66 | * tagName is "param", |
67 | * parameter is "abc" |
68 | * and the rest is content. |
69 | */ |
70 | public Javadoc addBlockTag(String tagName, String parameter, String content) { |
71 | return addBlockTag(tagName, parameter + " " + content); |
72 | } |
73 | |
74 | public Javadoc addBlockTag(String tagName) { |
75 | return addBlockTag(tagName, ""); |
76 | } |
77 | |
78 | /** |
79 | * Return the text content of the document. It does not containing trailing spaces and asterisks |
80 | * at the start of the line. |
81 | */ |
82 | public String toText() { |
83 | StringBuilder sb = new StringBuilder(); |
84 | if (!description.isEmpty()) { |
85 | sb.append(description.toText()); |
86 | sb.append(SYSTEM_EOL); |
87 | } |
88 | if (!blockTags.isEmpty()) { |
89 | sb.append(SYSTEM_EOL); |
90 | } |
91 | blockTags.forEach(bt -> { |
92 | sb.append(bt.toText()); |
93 | sb.append(SYSTEM_EOL); |
94 | }); |
95 | return sb.toString(); |
96 | } |
97 | |
98 | /** |
99 | * Create a JavadocComment, by formatting the text of the Javadoc using no indentation (expecting the pretty printer to do the formatting.) |
100 | */ |
101 | public JavadocComment toComment() { |
102 | return toComment(""); |
103 | } |
104 | |
105 | /** |
106 | * Create a JavadocComment, by formatting the text of the Javadoc using the given indentation. |
107 | */ |
108 | public JavadocComment toComment(String indentation) { |
109 | for (char c : indentation.toCharArray()) { |
110 | if (!Character.isWhitespace(c)) { |
111 | throw new IllegalArgumentException("The indentation string should be composed only by whitespace characters"); |
112 | } |
113 | } |
114 | StringBuilder sb = new StringBuilder(); |
115 | sb.append(SYSTEM_EOL); |
116 | final String text = toText(); |
117 | if (!text.isEmpty()) { |
118 | for (String line : text.split(SYSTEM_EOL)) { |
119 | sb.append(indentation); |
120 | sb.append(" * "); |
121 | sb.append(line); |
122 | sb.append(SYSTEM_EOL); |
123 | } |
124 | } |
125 | sb.append(indentation); |
126 | sb.append(" "); |
127 | return new JavadocComment(sb.toString()); |
128 | } |
129 | |
130 | public JavadocDescription getDescription() { |
131 | return description; |
132 | } |
133 | |
134 | /** |
135 | * @return the current List of associated JavadocBlockTags |
136 | */ |
137 | public List<JavadocBlockTag> getBlockTags() { |
138 | return this.blockTags; |
139 | } |
140 | |
141 | @Override |
142 | public boolean equals(Object o) { |
143 | if (this == o) return true; |
144 | if (o == null || getClass() != o.getClass()) return false; |
145 | |
146 | Javadoc document = (Javadoc) o; |
147 | |
148 | return description.equals(document.description) && blockTags.equals(document.blockTags); |
149 | |
150 | } |
151 | |
152 | @Override |
153 | public int hashCode() { |
154 | int result = description.hashCode(); |
155 | result = 31 * result + blockTags.hashCode(); |
156 | return result; |
157 | } |
158 | |
159 | @Override |
160 | public String toString() { |
161 | return "Javadoc{" + |
162 | "description=" + description + |
163 | ", blockTags=" + blockTags + |
164 | '}'; |
165 | } |
166 | |
167 | } |
168 |
Members