001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.site;
021
022import java.lang.reflect.Field;
023import java.util.Set;
024import java.util.regex.Pattern;
025
026import org.apache.maven.doxia.macro.MacroExecutionException;
027import org.apache.maven.doxia.sink.Sink;
028
029import com.puppycrawl.tools.checkstyle.PropertyType;
030import com.puppycrawl.tools.checkstyle.XdocsPropertyType;
031import com.puppycrawl.tools.checkstyle.api.DetailNode;
032import com.puppycrawl.tools.checkstyle.api.JavadocCommentsTokenTypes;
033import com.puppycrawl.tools.checkstyle.meta.JavadocMetadataScraperUtil;
034import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
035
036/**
037 * Utility class for parsing javadocs of modules.
038 */
039public final class ModuleJavadocParsingUtil {
040    /** New line escape character. */
041    public static final String NEWLINE = System.lineSeparator();
042    /** A newline with 4 spaces of indentation. */
043    public static final String INDENT_LEVEL_4 = SiteUtil.getNewlineAndIndentSpaces(4);
044    /** A newline with 6 spaces of indentation. */
045    public static final String INDENT_LEVEL_6 = SiteUtil.getNewlineAndIndentSpaces(6);
046    /** A newline with 8 spaces of indentation. */
047    public static final String INDENT_LEVEL_8 = SiteUtil.getNewlineAndIndentSpaces(8);
048    /** A newline with 10 spaces of indentation. */
049    public static final String INDENT_LEVEL_10 = SiteUtil.getNewlineAndIndentSpaces(10);
050    /** A newline with 12 spaces of indentation. */
051    public static final String INDENT_LEVEL_12 = SiteUtil.getNewlineAndIndentSpaces(12);
052    /** A newline with 14 spaces of indentation. */
053    public static final String INDENT_LEVEL_14 = SiteUtil.getNewlineAndIndentSpaces(14);
054    /** A newline with 16 spaces of indentation. */
055    public static final String INDENT_LEVEL_16 = SiteUtil.getNewlineAndIndentSpaces(16);
056    /** A newline with 18 spaces of indentation. */
057    public static final String INDENT_LEVEL_18 = SiteUtil.getNewlineAndIndentSpaces(18);
058    /** A newline with 20 spaces of indentation. */
059    public static final String INDENT_LEVEL_20 = SiteUtil.getNewlineAndIndentSpaces(20);
060    /** A set of all html tags that need to be considered as text formatting for this macro. */
061    public static final Set<String> HTML_TEXT_FORMAT_TAGS = Set.of("<code>", "<a", "</a>", "<b>",
062        "</b>", "<strong>", "</strong>", "<i>", "</i>", "<em>", "</em>", "<small>", "</small>",
063        "<ins>", "<sub>", "<sup>");
064    /** "Notes:" javadoc marking. */
065    public static final String NOTES = "Notes:";
066    /** "Notes:" line. */
067    public static final Pattern NOTES_LINE = Pattern.compile("\\s*" + NOTES + "$");
068    /** "Notes:" line with new line accounted. */
069    public static final Pattern NOTES_LINE_WITH_NEWLINE = Pattern.compile("\r?\n\\s?" + NOTES);
070
071    /**
072     * Private utility constructor.
073     */
074    private ModuleJavadocParsingUtil() {
075    }
076
077    /**
078     * Gets properties of the specified module.
079     *
080     * @param moduleName name of module.
081     * @return set of properties name if present, otherwise null.
082     * @throws MacroExecutionException if the module could not be retrieved.
083     */
084    public static Set<String> getPropertyNames(String moduleName)
085            throws MacroExecutionException {
086        final Object instance = SiteUtil.getModuleInstance(moduleName);
087        final Class<?> clss = instance.getClass();
088
089        return SiteUtil.getPropertiesForDocumentation(clss, instance);
090    }
091
092    /**
093     * Determines whether the given HTML node marks the start of the "Notes" section.
094     *
095     * @param htmlElement html element to check.
096     * @return true if the element starts the "Notes" section, false otherwise.
097     */
098    private static boolean isStartOfNotesSection(DetailNode htmlElement) {
099        final DetailNode htmlContentNode = JavadocUtil.findFirstToken(
100            htmlElement, JavadocCommentsTokenTypes.HTML_CONTENT);
101
102        return htmlContentNode != null && JavadocMetadataScraperUtil.isChildNodeTextMatches(
103            htmlContentNode, NOTES_LINE);
104    }
105
106    /**
107     * Writes the given javadoc chunk into xdoc.
108     *
109     * @param javadocPortion javadoc text.
110     * @param sink sink of the macro.
111     */
112    public static void writeOutJavadocPortion(String javadocPortion, Sink sink) {
113        final String[] javadocPortionLinesSplit = javadocPortion.split(NEWLINE
114            .replace("\r", ""));
115
116        sink.rawText(javadocPortionLinesSplit[0]);
117        String lastHtmlTag = javadocPortionLinesSplit[0];
118
119        for (int index = 1; index < javadocPortionLinesSplit.length; index++) {
120            final String currentLine = javadocPortionLinesSplit[index].trim();
121            final String processedLine;
122
123            if (currentLine.isEmpty()) {
124                processedLine = NEWLINE;
125            }
126            else if (currentLine.startsWith("<")
127                && !startsWithTextFormattingHtmlTag(currentLine)) {
128
129                processedLine = INDENT_LEVEL_8 + currentLine;
130                lastHtmlTag = currentLine;
131            }
132            else if (lastHtmlTag.contains("<pre")) {
133                final String currentLineWithPreservedIndent = javadocPortionLinesSplit[index]
134                    .substring(1);
135
136                processedLine = NEWLINE + currentLineWithPreservedIndent;
137            }
138            else {
139                processedLine = INDENT_LEVEL_10 + currentLine;
140            }
141
142            sink.rawText(processedLine);
143        }
144
145    }
146
147    /**
148     * Checks if given line starts with HTML text-formatting tag.
149     *
150     * @param line line to check on.
151     * @return whether given line starts with HTML text-formatting tag.
152     */
153    public static boolean startsWithTextFormattingHtmlTag(String line) {
154        boolean result = false;
155
156        for (String tag : HTML_TEXT_FORMAT_TAGS) {
157            if (line.startsWith(tag)) {
158                result = true;
159                break;
160            }
161        }
162
163        return result;
164    }
165
166    /**
167     * Gets the description of module from module javadoc.
168     *
169     * @param moduleJavadoc module javadoc.
170     * @return module description.
171     */
172    public static String getModuleDescription(DetailNode moduleJavadoc) {
173        final DetailNode descriptionEndNode = getDescriptionEndNode(moduleJavadoc);
174
175        return JavadocMetadataScraperUtil.constructSubTreeText(moduleJavadoc, descriptionEndNode);
176    }
177
178    /**
179     * Gets the {@code @since} version of module from module javadoc.
180     *
181     * @param moduleJavadoc module javadoc
182     * @return module {@code @since} version. For instance, {@code 8.0}
183     */
184    public static String getModuleSinceVersion(DetailNode moduleJavadoc) {
185        final DetailNode sinceTagNode = getModuleSinceVersionTagStartNode(moduleJavadoc);
186        return JavadocMetadataScraperUtil
187                    .constructSubTreeText(sinceTagNode, sinceTagNode.getFirstChild())
188                    .replace("@since ", "");
189    }
190
191    /**
192     * Gets the end node of the description.
193     *
194     * @param moduleJavadoc javadoc of module.
195     * @return the end index.
196     */
197    public static DetailNode getDescriptionEndNode(DetailNode moduleJavadoc) {
198        final DetailNode descriptionEndNode;
199
200        final DetailNode notesStartingNode =
201            getNotesSectionStartNode(moduleJavadoc);
202
203        if (notesStartingNode != null) {
204            descriptionEndNode = notesStartingNode.getPreviousSibling();
205        }
206        else {
207            descriptionEndNode = getModuleSinceVersionTagStartNode(
208                                        moduleJavadoc).getPreviousSibling();
209        }
210
211        return descriptionEndNode;
212    }
213
214    /**
215     * Gets the start node of the Notes section.
216     *
217     * @param moduleJavadoc javadoc of module.
218     * @return start node.
219     */
220    public static DetailNode getNotesSectionStartNode(DetailNode moduleJavadoc) {
221        DetailNode notesStartNode = null;
222        DetailNode node = moduleJavadoc.getFirstChild();
223
224        while (node != null) {
225            if (node.getType() == JavadocCommentsTokenTypes.HTML_ELEMENT) {
226                boolean found = false;
227                if (JavadocUtil.isTag(node, "ul")) {
228                    final DetailNode htmlContentNode = JavadocUtil.findFirstToken(
229                        node, JavadocCommentsTokenTypes.HTML_CONTENT);
230                    if (isStartOfNotesSection(htmlContentNode.getFirstChild())) {
231                        notesStartNode = node;
232                        found = true;
233                    }
234                }
235                else if ((JavadocUtil.isTag(node, "p")
236                            || JavadocUtil.isTag(node, "li"))
237                            && isStartOfNotesSection(node)) {
238                    notesStartNode = node;
239                    found = true;
240                }
241                if (found) {
242                    break;
243                }
244            }
245            node = node.getNextSibling();
246        }
247
248        return notesStartNode;
249    }
250
251    /**
252     * Gets the node representing the start of the {@code @since} version tag
253     * in the module's Javadoc.
254     *
255     * @param moduleJavadoc the root Javadoc node of the module
256     * @return the {@code @since} tag start node, or {@code null} if not found
257     */
258    public static DetailNode getModuleSinceVersionTagStartNode(DetailNode moduleJavadoc) {
259        return JavadocUtil.getAllNodesOfType(
260                moduleJavadoc, JavadocCommentsTokenTypes.JAVADOC_BLOCK_TAG).stream()
261            .filter(javadocTag -> {
262                return javadocTag.getFirstChild().getType()
263                        == JavadocCommentsTokenTypes.SINCE_BLOCK_TAG;
264            })
265            .findFirst()
266            .orElse(null);
267    }
268
269    /**
270     * Gets the Notes section of module from module javadoc.
271     *
272     * @param moduleJavadoc module javadoc.
273     * @return Notes section of module.
274     */
275    public static String getModuleNotes(DetailNode moduleJavadoc) {
276        final String result;
277
278        final DetailNode notesStartNode = getNotesSectionStartNode(moduleJavadoc);
279
280        if (notesStartNode == null) {
281            result = "";
282        }
283        else {
284            final DetailNode notesEndNode = getNotesEndNode(moduleJavadoc);
285
286            final String unprocessedNotes =
287                    JavadocMetadataScraperUtil.constructSubTreeText(
288                        notesStartNode, notesEndNode);
289            result = NOTES_LINE_WITH_NEWLINE.matcher(unprocessedNotes).replaceAll("");
290        }
291
292        return result;
293    }
294
295    /**
296     * Gets the end node of the Notes.
297     *
298     * @param moduleJavadoc javadoc of module.
299     * @return the end index.
300     */
301    public static DetailNode getNotesEndNode(DetailNode moduleJavadoc) {
302        return getModuleSinceVersionTagStartNode(
303                moduleJavadoc).getPreviousSibling();
304    }
305
306    /**
307     * Checks whether property is to contain tokens.
308     *
309     * @param propertyField property field.
310     * @return true if property is to contain tokens, false otherwise.
311     */
312    public static boolean isPropertySpecialTokenProp(Field propertyField) {
313        boolean result = false;
314
315        if (propertyField != null) {
316            final XdocsPropertyType fieldXdocAnnotation =
317                propertyField.getAnnotation(XdocsPropertyType.class);
318
319            result = fieldXdocAnnotation != null
320                && fieldXdocAnnotation.value() == PropertyType.TOKEN_ARRAY;
321        }
322
323        return result;
324    }
325
326}