1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.javadoc;
21  
22  import com.puppycrawl.tools.checkstyle.StatelessCheck;
23  import com.puppycrawl.tools.checkstyle.api.DetailNode;
24  import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
25  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
26  import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
27  
28  /**
29   * Checks that:
30   * <ul>
31   * <li>There is one blank line between each of two paragraphs
32   * and one blank line before the at-clauses block if it is present.</li>
33   * <li>Each paragraph but the first has &lt;p&gt; immediately
34   * before the first word, with no space after.</li>
35   * </ul>
36   *
37   * <p>The check can be specified by option allowNewlineParagraph,
38   * which says whether the &lt;p&gt; tag should be placed immediately before
39   * the first word.
40   *
41   * <p>Default configuration:
42   * </p>
43   * <pre>
44   * &lt;module name=&quot;JavadocParagraph&quot;/&gt;
45   * </pre>
46   *
47   * <p>To allow newlines and spaces immediately after the &lt;p&gt; tag:
48   * <pre>
49   * &lt;module name=&quot;JavadocParagraph&quot;&gt;
50   *      &lt;property name=&quot;allowNewlineParagraph&quot;
51   *                   value==&quot;false&quot;/&gt;
52   * &lt;/module&quot;&gt;
53   * </pre>
54   *
55   * <p>In case of allowNewlineParagraph set to false
56   * the following example will not have any violations:
57   * <pre>
58   *   /**
59   *    * &lt;p&gt;
60   *    * Some Javadoc.
61   *    *
62   *    * &lt;p&gt;  Some Javadoc.
63   *    *
64   *    * &lt;p&gt;
65   *    * &lt;pre&gt;
66   *    * Some preformatted Javadoc.
67   *    * &lt;/pre&gt;
68   *    *
69   *    *&#47;
70   * </pre>
71   *
72   */
73  @StatelessCheck
74  public class JavadocParagraphCheck extends AbstractJavadocCheck {
75  
76      /**
77       * A key is pointing to the warning message text in "messages.properties"
78       * file.
79       */
80      public static final String MSG_TAG_AFTER = "javadoc.paragraph.tag.after";
81  
82      /**
83       * A key is pointing to the warning message text in "messages.properties"
84       * file.
85       */
86      public static final String MSG_LINE_BEFORE = "javadoc.paragraph.line.before";
87  
88      /**
89       * A key is pointing to the warning message text in "messages.properties"
90       * file.
91       */
92      public static final String MSG_REDUNDANT_PARAGRAPH = "javadoc.paragraph.redundant.paragraph";
93  
94      /**
95       * A key is pointing to the warning message text in "messages.properties"
96       * file.
97       */
98      public static final String MSG_MISPLACED_TAG = "javadoc.paragraph.misplaced.tag";
99  
100     /**
101      * Whether the &lt;p&gt; tag should be placed immediately before the first word.
102      */
103     private boolean allowNewlineParagraph = true;
104 
105     /**
106      * Sets allowNewlineParagraph.
107      * @param value value to set.
108      */
109     public void setAllowNewlineParagraph(boolean value) {
110         allowNewlineParagraph = value;
111     }
112 
113     @Override
114     public int[] getDefaultJavadocTokens() {
115         return new int[] {
116             JavadocTokenTypes.NEWLINE,
117             JavadocTokenTypes.HTML_ELEMENT,
118         };
119     }
120 
121     @Override
122     public int[] getRequiredJavadocTokens() {
123         return getAcceptableJavadocTokens();
124     }
125 
126     @Override
127     public void visitJavadocToken(DetailNode ast) {
128         if (ast.getType() == JavadocTokenTypes.NEWLINE && isEmptyLine(ast)) {
129             checkEmptyLine(ast);
130         }
131         else if (ast.getType() == JavadocTokenTypes.HTML_ELEMENT
132                 && JavadocUtil.getFirstChild(ast).getType() == JavadocTokenTypes.P_TAG_START) {
133             checkParagraphTag(ast);
134         }
135     }
136 
137     /**
138      * Determines whether or not the next line after empty line has paragraph tag in the beginning.
139      * @param newline NEWLINE node.
140      */
141     private void checkEmptyLine(DetailNode newline) {
142         final DetailNode nearestToken = getNearestNode(newline);
143         if (nearestToken.getType() == JavadocTokenTypes.TEXT
144                 && !CommonUtil.isBlank(nearestToken.getText())) {
145             log(newline.getLineNumber(), MSG_TAG_AFTER);
146         }
147     }
148 
149     /**
150      * Determines whether or not the line with paragraph tag has previous empty line.
151      * @param tag html tag.
152      */
153     private void checkParagraphTag(DetailNode tag) {
154         final DetailNode newLine = getNearestEmptyLine(tag);
155         if (isFirstParagraph(tag)) {
156             log(tag.getLineNumber(), MSG_REDUNDANT_PARAGRAPH);
157         }
158         else if (newLine == null || tag.getLineNumber() - newLine.getLineNumber() != 1) {
159             log(tag.getLineNumber(), MSG_LINE_BEFORE);
160         }
161         if (allowNewlineParagraph && isImmediatelyFollowedByText(tag)) {
162             log(tag.getLineNumber(), MSG_MISPLACED_TAG);
163         }
164     }
165 
166     /**
167      * Returns nearest node.
168      * @param node DetailNode node.
169      * @return nearest node.
170      */
171     private static DetailNode getNearestNode(DetailNode node) {
172         DetailNode tag = JavadocUtil.getNextSibling(node);
173         while (tag.getType() == JavadocTokenTypes.LEADING_ASTERISK
174                 || tag.getType() == JavadocTokenTypes.NEWLINE) {
175             tag = JavadocUtil.getNextSibling(tag);
176         }
177         return tag;
178     }
179 
180     /**
181      * Determines whether or not the line is empty line.
182      * @param newLine NEWLINE node.
183      * @return true, if line is empty line.
184      */
185     private static boolean isEmptyLine(DetailNode newLine) {
186         boolean result = false;
187         DetailNode previousSibling = JavadocUtil.getPreviousSibling(newLine);
188         if (previousSibling != null
189                 && previousSibling.getParent().getType() == JavadocTokenTypes.JAVADOC) {
190             if (previousSibling.getType() == JavadocTokenTypes.TEXT
191                     && CommonUtil.isBlank(previousSibling.getText())) {
192                 previousSibling = JavadocUtil.getPreviousSibling(previousSibling);
193             }
194             result = previousSibling != null
195                     && previousSibling.getType() == JavadocTokenTypes.LEADING_ASTERISK;
196         }
197         return result;
198     }
199 
200     /**
201      * Determines whether or not the line with paragraph tag is first line in javadoc.
202      * @param paragraphTag paragraph tag.
203      * @return true, if line with paragraph tag is first line in javadoc.
204      */
205     private static boolean isFirstParagraph(DetailNode paragraphTag) {
206         boolean result = true;
207         DetailNode previousNode = JavadocUtil.getPreviousSibling(paragraphTag);
208         while (previousNode != null) {
209             if (previousNode.getType() == JavadocTokenTypes.TEXT
210                     && !CommonUtil.isBlank(previousNode.getText())
211                 || previousNode.getType() != JavadocTokenTypes.LEADING_ASTERISK
212                     && previousNode.getType() != JavadocTokenTypes.NEWLINE
213                     && previousNode.getType() != JavadocTokenTypes.TEXT) {
214                 result = false;
215                 break;
216             }
217             previousNode = JavadocUtil.getPreviousSibling(previousNode);
218         }
219         return result;
220     }
221 
222     /**
223      * Finds and returns nearest empty line in javadoc.
224      * @param node DetailNode node.
225      * @return Some nearest empty line in javadoc.
226      */
227     private static DetailNode getNearestEmptyLine(DetailNode node) {
228         DetailNode newLine = JavadocUtil.getPreviousSibling(node);
229         while (newLine != null) {
230             final DetailNode previousSibling = JavadocUtil.getPreviousSibling(newLine);
231             if (newLine.getType() == JavadocTokenTypes.NEWLINE && isEmptyLine(newLine)) {
232                 break;
233             }
234             newLine = previousSibling;
235         }
236         return newLine;
237     }
238 
239     /**
240      * Tests whether the paragraph tag is immediately followed by the text.
241      * @param tag html tag.
242      * @return true, if the paragraph tag is immediately followed by the text.
243      */
244     private static boolean isImmediatelyFollowedByText(DetailNode tag) {
245         final DetailNode nextSibling = JavadocUtil.getNextSibling(tag);
246         return nextSibling.getType() == JavadocTokenTypes.NEWLINE
247                 || nextSibling.getType() == JavadocTokenTypes.EOF
248                 || CommonUtil.startsWithChar(nextSibling.getText(), ' ');
249     }
250 
251 }