View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.api.DetailNode;
23  import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
24  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
25  import com.puppycrawl.tools.checkstyle.utils.JavadocUtils;
26  
27  /**
28   * Checks that:
29   * <ul>
30   * <li>There is one blank line between each of two paragraphs
31   * and one blank line before the at-clauses block if it is present.</li>
32   * <li>Each paragraph but the first has &lt;p&gt; immediately
33   * before the first word, with no space after.</li>
34   * </ul>
35   *
36   * <p>The check can be specified by option allowNewlineParagraph,
37   * which says whether the &lt;p&gt; tag should be placed immediately before
38   * the first word.
39   *
40   * <p>Default configuration:
41   * </p>
42   * <pre>
43   * &lt;module name=&quot;JavadocParagraph&quot;/&gt;
44   * </pre>
45   *
46   * <p>To allow newlines and spaces immediately after the &lt;p&gt; tag:
47   * <pre>
48   * &lt;module name=&quot;JavadocParagraph&quot;&gt;
49   *      &lt;property name=&quot;allowNewlineParagraph&quot;
50   *                   value==&quot;false&quot;/&gt;
51   * &lt;/module&quot;&gt;
52   * </pre>
53   *
54   * <p>In case of allowNewlineParagraph set to false
55   * the following example will not have any violations:
56   * <pre>
57   *   /**
58   *    * &lt;p&gt;
59   *    * Some Javadoc.
60   *    *
61   *    * &lt;p&gt;  Some Javadoc.
62   *    *
63   *    * &lt;p&gt;
64   *    * &lt;pre&gt;
65   *    * Some preformatted Javadoc.
66   *    * &lt;/pre&gt;
67   *    *
68   *    *&#47;
69   * </pre>
70   * @author maxvetrenko
71   * @author Vladislav Lisetskiy
72   *
73   */
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                 && JavadocUtils.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 (!isLastEmptyLine(newline) && nearestToken.getType() == JavadocTokenTypes.TEXT
144                 && !CommonUtils.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 = JavadocUtils.getNextSibling(node);
173         while (tag.getType() == JavadocTokenTypes.LEADING_ASTERISK
174                 || tag.getType() == JavadocTokenTypes.NEWLINE) {
175             tag = JavadocUtils.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 = JavadocUtils.getPreviousSibling(newLine);
188         if (previousSibling != null
189                 && previousSibling.getParent().getType() == JavadocTokenTypes.JAVADOC) {
190             if (previousSibling.getType() == JavadocTokenTypes.TEXT
191                     && CommonUtils.isBlank(previousSibling.getText())) {
192                 previousSibling = JavadocUtils.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 = JavadocUtils.getPreviousSibling(paragraphTag);
208         while (previousNode != null) {
209             if (previousNode.getType() == JavadocTokenTypes.TEXT
210                     && !CommonUtils.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 = JavadocUtils.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 = JavadocUtils.getPreviousSibling(node);
229         while (newLine != null) {
230             final DetailNode previousSibling = JavadocUtils.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 if NEWLINE node is a last node in javadoc.
241      * @param newLine NEWLINE node.
242      * @return true, if NEWLINE node is a last node in javadoc.
243      */
244     private static boolean isLastEmptyLine(DetailNode newLine) {
245         boolean result = true;
246         DetailNode nextNode = JavadocUtils.getNextSibling(newLine);
247         while (nextNode != null && nextNode.getType() != JavadocTokenTypes.JAVADOC_TAG) {
248             if (nextNode.getType() == JavadocTokenTypes.TEXT
249                     && !CommonUtils.isBlank(nextNode.getText())
250                     || nextNode.getType() == JavadocTokenTypes.HTML_ELEMENT) {
251                 result = false;
252                 break;
253             }
254             nextNode = JavadocUtils.getNextSibling(nextNode);
255         }
256         return result;
257     }
258 
259     /**
260      * Tests whether the paragraph tag is immediately followed by the text.
261      * @param tag html tag.
262      * @return true, if the paragraph tag is immediately followed by the text.
263      */
264     private static boolean isImmediatelyFollowedByText(DetailNode tag) {
265         final DetailNode nextSibling = JavadocUtils.getNextSibling(tag);
266         return nextSibling.getType() == JavadocTokenTypes.NEWLINE
267                 || nextSibling.getType() == JavadocTokenTypes.EOF
268                 || CommonUtils.startsWithChar(nextSibling.getText(), ' ');
269     }
270 }