View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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 java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.BitSet;
25  import java.util.List;
26  import java.util.Optional;
27  import java.util.regex.Pattern;
28  import java.util.stream.Stream;
29  
30  import javax.annotation.Nullable;
31  
32  import com.puppycrawl.tools.checkstyle.StatelessCheck;
33  import com.puppycrawl.tools.checkstyle.api.DetailNode;
34  import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
35  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
36  import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
37  import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
38  
39  /**
40   * <p>
41   * Checks that
42   * <a href="https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html#firstsentence">
43   * Javadoc summary sentence</a> does not contain phrases that are not recommended to use.
44   * Summaries that contain only the {@code {@inheritDoc}} tag are skipped.
45   * Summaries that contain a non-empty {@code {@return}} are allowed.
46   * Check also violate Javadoc that does not contain first sentence, though with {@code {@return}} a
47   * period is not required as the Javadoc tool adds it.
48   * </p>
49   * <ul>
50   * <li>
51   * Property {@code forbiddenSummaryFragments} - Specify the regexp for forbidden summary fragments.
52   * Type is {@code java.util.regex.Pattern}.
53   * Default value is {@code "^$"}.
54   * </li>
55   * <li>
56   * Property {@code period} - Specify the period symbol. Used to check the first sentence ends with a
57   * period. Periods that are not followed by a whitespace character are ignored (eg. the period in
58   * v1.0). Because some periods include whitespace built into the character, if this is set to a
59   * non-default value any period will end the sentence, whether it is followed by whitespace or not.
60   * Type is {@code java.lang.String}.
61   * Default value is {@code "."}.
62   * </li>
63   * <li>
64   * Property {@code violateExecutionOnNonTightHtml} - Control when to print violations
65   * if the Javadoc being examined by this check violates the tight html rules defined at
66   * <a href="https://checkstyle.org/writingjavadocchecks.html#Tight-HTML_rules">Tight-HTML Rules</a>.
67   * Type is {@code boolean}.
68   * Default value is {@code false}.
69   * </li>
70   * </ul>
71   * <p>
72   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
73   * </p>
74   * <p>
75   * Violation Message Keys:
76   * </p>
77   * <ul>
78   * <li>
79   * {@code javadoc.missed.html.close}
80   * </li>
81   * <li>
82   * {@code javadoc.parse.rule.error}
83   * </li>
84   * <li>
85   * {@code javadoc.unclosedHtml}
86   * </li>
87   * <li>
88   * {@code javadoc.wrong.singleton.html.tag}
89   * </li>
90   * <li>
91   * {@code summary.first.sentence}
92   * </li>
93   * <li>
94   * {@code summary.javaDoc}
95   * </li>
96   * <li>
97   * {@code summary.javaDoc.missing}
98   * </li>
99   * <li>
100  * {@code summary.javaDoc.missing.period}
101  * </li>
102  * </ul>
103  *
104  * @since 6.0
105  */
106 @StatelessCheck
107 public class SummaryJavadocCheck extends AbstractJavadocCheck {
108 
109     /**
110      * A key is pointing to the warning message text in "messages.properties"
111      * file.
112      */
113     public static final String MSG_SUMMARY_FIRST_SENTENCE = "summary.first.sentence";
114 
115     /**
116      * A key is pointing to the warning message text in "messages.properties"
117      * file.
118      */
119     public static final String MSG_SUMMARY_JAVADOC = "summary.javaDoc";
120 
121     /**
122      * A key is pointing to the warning message text in "messages.properties"
123      * file.
124      */
125     public static final String MSG_SUMMARY_JAVADOC_MISSING = "summary.javaDoc.missing";
126 
127     /**
128      * A key is pointing to the warning message text in "messages.properties" file.
129      */
130     public static final String MSG_SUMMARY_MISSING_PERIOD = "summary.javaDoc.missing.period";
131 
132     /**
133      * This regexp is used to convert multiline javadoc to single-line without stars.
134      */
135     private static final Pattern JAVADOC_MULTILINE_TO_SINGLELINE_PATTERN =
136             Pattern.compile("\n +(\\*)|^ +(\\*)");
137 
138     /**
139      * This regexp is used to remove html tags, whitespace, and asterisks from a string.
140      */
141     private static final Pattern HTML_ELEMENTS =
142             Pattern.compile("<[^>]*>");
143 
144     /** Default period literal. */
145     private static final String DEFAULT_PERIOD = ".";
146 
147     /** Summary tag text. */
148     private static final String SUMMARY_TEXT = "@summary";
149 
150     /** Return tag text. */
151     private static final String RETURN_TEXT = "@return";
152 
153     /** Set of allowed Tokens tags in summary java doc. */
154     private static final BitSet ALLOWED_TYPES = TokenUtil.asBitSet(
155                     JavadocTokenTypes.WS,
156                     JavadocTokenTypes.DESCRIPTION,
157                     JavadocTokenTypes.TEXT);
158 
159     /**
160      * Specify the regexp for forbidden summary fragments.
161      */
162     private Pattern forbiddenSummaryFragments = CommonUtil.createPattern("^$");
163 
164     /**
165      * Specify the period symbol. Used to check the first sentence ends with a period. Periods that
166      * are not followed by a whitespace character are ignored (eg. the period in v1.0). Because some
167      * periods include whitespace built into the character, if this is set to a non-default value
168      * any period will end the sentence, whether it is followed by whitespace or not.
169      */
170     private String period = DEFAULT_PERIOD;
171 
172     /**
173      * Setter to specify the regexp for forbidden summary fragments.
174      *
175      * @param pattern a pattern.
176      * @since 6.0
177      */
178     public void setForbiddenSummaryFragments(Pattern pattern) {
179         forbiddenSummaryFragments = pattern;
180     }
181 
182     /**
183      * Setter to specify the period symbol. Used to check the first sentence ends with a period.
184      * Periods that are not followed by a whitespace character are ignored (eg. the period in v1.0).
185      * Because some periods include whitespace built into the character, if this is set to a
186      * non-default value any period will end the sentence, whether it is followed by whitespace or
187      * not.
188      *
189      * @param period period's value.
190      * @since 6.2
191      */
192     public void setPeriod(String period) {
193         this.period = period;
194     }
195 
196     @Override
197     public int[] getDefaultJavadocTokens() {
198         return new int[] {
199             JavadocTokenTypes.JAVADOC,
200         };
201     }
202 
203     @Override
204     public int[] getRequiredJavadocTokens() {
205         return getAcceptableJavadocTokens();
206     }
207 
208     @Override
209     public void visitJavadocToken(DetailNode ast) {
210         final Optional<DetailNode> inlineTag = getInlineTagNode(ast);
211         final DetailNode inlineTagNode = inlineTag.orElse(null);
212         if (inlineTag.isPresent()
213             && isSummaryTag(inlineTagNode)
214             && isDefinedFirst(inlineTagNode)) {
215             validateSummaryTag(inlineTagNode);
216         }
217         else if (inlineTag.isPresent() && isInlineReturnTag(inlineTagNode)) {
218             validateInlineReturnTag(inlineTagNode);
219         }
220         else if (!startsWithInheritDoc(ast)) {
221             validateUntaggedSummary(ast);
222         }
223     }
224 
225     /**
226      * Checks the javadoc text for {@code period} at end and forbidden fragments.
227      *
228      * @param ast the javadoc text node
229      */
230     private void validateUntaggedSummary(DetailNode ast) {
231         final String summaryDoc = getSummarySentence(ast);
232         if (summaryDoc.isEmpty()) {
233             log(ast.getLineNumber(), MSG_SUMMARY_JAVADOC_MISSING);
234         }
235         else if (!period.isEmpty()) {
236             if (summaryDoc.contains(period)) {
237                 final String firstSentence = getFirstSentenceOrNull(ast, period);
238                 if (firstSentence == null) {
239                     log(ast.getLineNumber(), MSG_SUMMARY_FIRST_SENTENCE);
240                 }
241                 else if (containsForbiddenFragment(firstSentence)) {
242                     log(ast.getLineNumber(), MSG_SUMMARY_JAVADOC);
243                 }
244             }
245             else {
246                 log(ast.getLineNumber(), MSG_SUMMARY_FIRST_SENTENCE);
247             }
248         }
249     }
250 
251     /**
252      * Gets the node for the inline tag if present.
253      *
254      * @param javadoc javadoc root node.
255      * @return the node for the inline tag if present.
256      */
257     private static Optional<DetailNode> getInlineTagNode(DetailNode javadoc) {
258         return Arrays.stream(javadoc.getChildren())
259             .filter(SummaryJavadocCheck::isInlineTagPresent)
260             .findFirst()
261             .map(SummaryJavadocCheck::getInlineTagNodeForAst);
262     }
263 
264     /**
265      * Whether the {@code {@summary}} tag is defined first in the javadoc.
266      *
267      * @param inlineSummaryTag node of type {@link JavadocTokenTypes#JAVADOC_INLINE_TAG}
268      * @return {@code true} if the {@code {@summary}} tag is defined first in the javadoc
269      */
270     private static boolean isDefinedFirst(DetailNode inlineSummaryTag) {
271         boolean isDefinedFirst = true;
272         DetailNode currentAst = inlineSummaryTag;
273         while (currentAst != null && isDefinedFirst) {
274             switch (currentAst.getType()) {
275                 case JavadocTokenTypes.TEXT:
276                     isDefinedFirst = currentAst.getText().isBlank();
277                     break;
278                 case JavadocTokenTypes.HTML_ELEMENT:
279                     isDefinedFirst = !isTextPresentInsideHtmlTag(currentAst);
280                     break;
281                 default:
282                     break;
283             }
284             currentAst = JavadocUtil.getPreviousSibling(currentAst);
285         }
286         return isDefinedFirst;
287     }
288 
289     /**
290      * Whether some text is present inside the HTML element or tag.
291      *
292      * @param node DetailNode of type {@link JavadocTokenTypes#HTML_TAG}
293      *             or {@link JavadocTokenTypes#HTML_ELEMENT}
294      * @return {@code true} if some text is present inside the HTML element or tag
295      */
296     public static boolean isTextPresentInsideHtmlTag(DetailNode node) {
297         DetailNode nestedChild = JavadocUtil.getFirstChild(node);
298         if (node.getType() == JavadocTokenTypes.HTML_ELEMENT) {
299             nestedChild = JavadocUtil.getFirstChild(nestedChild);
300         }
301         boolean isTextPresentInsideHtmlTag = false;
302         while (nestedChild != null && !isTextPresentInsideHtmlTag) {
303             switch (nestedChild.getType()) {
304                 case JavadocTokenTypes.TEXT:
305                     isTextPresentInsideHtmlTag = !nestedChild.getText().isBlank();
306                     break;
307                 case JavadocTokenTypes.HTML_TAG:
308                 case JavadocTokenTypes.HTML_ELEMENT:
309                     isTextPresentInsideHtmlTag = isTextPresentInsideHtmlTag(nestedChild);
310                     break;
311                 default:
312                     break;
313             }
314             nestedChild = JavadocUtil.getNextSibling(nestedChild);
315         }
316         return isTextPresentInsideHtmlTag;
317     }
318 
319     /**
320      * Checks if the inline tag node is present.
321      *
322      * @param ast ast node to check.
323      * @return true, if the inline tag node is present.
324      */
325     private static boolean isInlineTagPresent(DetailNode ast) {
326         return getInlineTagNodeForAst(ast) != null;
327     }
328 
329     /**
330      * Returns an inline javadoc tag node that is within a html tag.
331      *
332      * @param ast html tag node.
333      * @return inline summary javadoc tag node or null if no node is found.
334      */
335     private static DetailNode getInlineTagNodeForAst(DetailNode ast) {
336         DetailNode node = ast;
337         DetailNode result = null;
338         // node can never be null as this method is called when there is a HTML_ELEMENT
339         if (node.getType() == JavadocTokenTypes.JAVADOC_INLINE_TAG) {
340             result = node;
341         }
342         else if (node.getType() == JavadocTokenTypes.HTML_TAG) {
343             // HTML_TAG always has more than 2 children.
344             node = node.getChildren()[1];
345             result = getInlineTagNodeForAst(node);
346         }
347         else if (node.getType() == JavadocTokenTypes.HTML_ELEMENT
348                 // Condition for SINGLETON html element which cannot contain summary node
349                 && node.getChildren()[0].getChildren().length > 1) {
350             // Html elements have one tested tag before actual content inside it
351             node = node.getChildren()[0].getChildren()[1];
352             result = getInlineTagNodeForAst(node);
353         }
354         return result;
355     }
356 
357     /**
358      * Checks if the javadoc inline tag is {@code {@summary}} tag.
359      *
360      * @param javadocInlineTag node of type {@link JavadocTokenTypes#JAVADOC_INLINE_TAG}
361      * @return {@code true} if inline tag is summary tag.
362      */
363     private static boolean isSummaryTag(DetailNode javadocInlineTag) {
364         return isInlineTagWithName(javadocInlineTag, SUMMARY_TEXT);
365     }
366 
367     /**
368      * Checks if the first tag inside ast is {@code {@return}} tag.
369      *
370      * @param javadocInlineTag node of type {@link JavadocTokenTypes#JAVADOC_INLINE_TAG}
371      * @return {@code true} if first tag is return tag.
372      */
373     private static boolean isInlineReturnTag(DetailNode javadocInlineTag) {
374         return isInlineTagWithName(javadocInlineTag, RETURN_TEXT);
375     }
376 
377     /**
378      * Checks if the first tag inside ast is a tag with the given name.
379      *
380      * @param javadocInlineTag node of type {@link JavadocTokenTypes#JAVADOC_INLINE_TAG}
381      * @param name name of inline tag.
382      *
383      * @return {@code true} if first tag is a tag with the given name.
384      */
385     private static boolean isInlineTagWithName(DetailNode javadocInlineTag, String name) {
386         final DetailNode[] child = javadocInlineTag.getChildren();
387 
388         // Checking size of ast is not required, since ast contains
389         // children of Inline Tag, as at least 2 children will be present which are
390         // RCURLY and LCURLY.
391         return name.equals(child[1].getText());
392     }
393 
394     /**
395      * Checks the inline summary (if present) for {@code period} at end and forbidden fragments.
396      *
397      * @param inlineSummaryTag node of type {@link JavadocTokenTypes#JAVADOC_INLINE_TAG}
398      */
399     private void validateSummaryTag(DetailNode inlineSummaryTag) {
400         final String inlineSummary = getContentOfInlineCustomTag(inlineSummaryTag);
401         final String summaryVisible = getVisibleContent(inlineSummary);
402         if (summaryVisible.isEmpty()) {
403             log(inlineSummaryTag.getLineNumber(), MSG_SUMMARY_JAVADOC_MISSING);
404         }
405         else if (!period.isEmpty()) {
406             final boolean isPeriodNotAtEnd =
407                     summaryVisible.lastIndexOf(period) != summaryVisible.length() - 1;
408             if (isPeriodNotAtEnd) {
409                 log(inlineSummaryTag.getLineNumber(), MSG_SUMMARY_MISSING_PERIOD);
410             }
411             else if (containsForbiddenFragment(inlineSummary)) {
412                 log(inlineSummaryTag.getLineNumber(), MSG_SUMMARY_JAVADOC);
413             }
414         }
415     }
416 
417     /**
418      * Checks the inline return for forbidden fragments.
419      *
420      * @param inlineReturnTag node of type {@link JavadocTokenTypes#JAVADOC_INLINE_TAG}
421      */
422     private void validateInlineReturnTag(DetailNode inlineReturnTag) {
423         final String inlineReturn = getContentOfInlineCustomTag(inlineReturnTag);
424         final String returnVisible = getVisibleContent(inlineReturn);
425         if (returnVisible.isEmpty()) {
426             log(inlineReturnTag.getLineNumber(), MSG_SUMMARY_JAVADOC_MISSING);
427         }
428         else if (containsForbiddenFragment(inlineReturn)) {
429             log(inlineReturnTag.getLineNumber(), MSG_SUMMARY_JAVADOC);
430         }
431     }
432 
433     /**
434      * Gets the content of inline custom tag.
435      *
436      * @param inlineTag inline tag node.
437      * @return String consisting of the content of inline custom tag.
438      */
439     public static String getContentOfInlineCustomTag(DetailNode inlineTag) {
440         final DetailNode[] childrenOfInlineTag = inlineTag.getChildren();
441         final StringBuilder customTagContent = new StringBuilder(256);
442         final int indexOfContentOfSummaryTag = 3;
443         if (childrenOfInlineTag.length != indexOfContentOfSummaryTag) {
444             DetailNode currentNode = childrenOfInlineTag[indexOfContentOfSummaryTag];
445             while (currentNode.getType() != JavadocTokenTypes.JAVADOC_INLINE_TAG_END) {
446                 extractInlineTagContent(currentNode, customTagContent);
447                 currentNode = JavadocUtil.getNextSibling(currentNode);
448             }
449         }
450         return customTagContent.toString();
451     }
452 
453     /**
454      * Extracts the content of inline custom tag recursively.
455      *
456      * @param node DetailNode
457      * @param customTagContent content of custom tag
458      */
459     private static void extractInlineTagContent(DetailNode node,
460         StringBuilder customTagContent) {
461         final DetailNode[] children = node.getChildren();
462         if (children.length == 0) {
463             customTagContent.append(node.getText());
464         }
465         else {
466             for (DetailNode child : children) {
467                 if (child.getType() != JavadocTokenTypes.LEADING_ASTERISK) {
468                     extractInlineTagContent(child, customTagContent);
469                 }
470             }
471         }
472     }
473 
474     /**
475      * Gets the string that is visible to user in javadoc.
476      *
477      * @param summary entire content of summary javadoc.
478      * @return string that is visible to user in javadoc.
479      */
480     private static String getVisibleContent(String summary) {
481         final String visibleSummary = HTML_ELEMENTS.matcher(summary).replaceAll("");
482         return visibleSummary.trim();
483     }
484 
485     /**
486      * Tests if first sentence contains forbidden summary fragment.
487      *
488      * @param firstSentence string with first sentence.
489      * @return {@code true} if first sentence contains forbidden summary fragment.
490      */
491     private boolean containsForbiddenFragment(String firstSentence) {
492         final String javadocText = JAVADOC_MULTILINE_TO_SINGLELINE_PATTERN
493                 .matcher(firstSentence).replaceAll(" ");
494         return forbiddenSummaryFragments.matcher(trimExcessWhitespaces(javadocText)).find();
495     }
496 
497     /**
498      * Trims the given {@code text} of duplicate whitespaces.
499      *
500      * @param text the text to transform.
501      * @return the finalized form of the text.
502      */
503     private static String trimExcessWhitespaces(String text) {
504         final StringBuilder result = new StringBuilder(256);
505         boolean previousWhitespace = true;
506 
507         for (char letter : text.toCharArray()) {
508             final char print;
509             if (Character.isWhitespace(letter)) {
510                 if (previousWhitespace) {
511                     continue;
512                 }
513 
514                 previousWhitespace = true;
515                 print = ' ';
516             }
517             else {
518                 previousWhitespace = false;
519                 print = letter;
520             }
521 
522             result.append(print);
523         }
524 
525         return result.toString();
526     }
527 
528     /**
529      * Checks if the node starts with an {&#64;inheritDoc}.
530      *
531      * @param root the root node to examine.
532      * @return {@code true} if the javadoc starts with an {&#64;inheritDoc}.
533      */
534     private static boolean startsWithInheritDoc(DetailNode root) {
535         boolean found = false;
536 
537         for (DetailNode child : root.getChildren()) {
538             if (child.getType() == JavadocTokenTypes.JAVADOC_INLINE_TAG
539                     && child.getChildren()[1].getType() == JavadocTokenTypes.INHERIT_DOC_LITERAL) {
540                 found = true;
541             }
542             if ((child.getType() == JavadocTokenTypes.TEXT
543                     || child.getType() == JavadocTokenTypes.HTML_ELEMENT)
544                     && !CommonUtil.isBlank(child.getText())) {
545                 break;
546             }
547         }
548 
549         return found;
550     }
551 
552     /**
553      * Finds and returns summary sentence.
554      *
555      * @param ast javadoc root node.
556      * @return violation string.
557      */
558     private static String getSummarySentence(DetailNode ast) {
559         final StringBuilder result = new StringBuilder(256);
560         for (DetailNode child : ast.getChildren()) {
561             if (child.getType() != JavadocTokenTypes.EOF
562                     && ALLOWED_TYPES.get(child.getType())) {
563                 result.append(child.getText());
564             }
565             else {
566                 final String summary = result.toString();
567                 if (child.getType() == JavadocTokenTypes.HTML_ELEMENT
568                         && CommonUtil.isBlank(summary)) {
569                     result.append(getStringInsideTag(summary,
570                             child.getChildren()[0].getChildren()[0]));
571                 }
572             }
573         }
574         return result.toString().trim();
575     }
576 
577     /**
578      * Get concatenated string within text of html tags.
579      *
580      * @param result javadoc string
581      * @param detailNode javadoc tag node
582      * @return java doc tag content appended in result
583      */
584     private static String getStringInsideTag(String result, DetailNode detailNode) {
585         final StringBuilder contents = new StringBuilder(result);
586         DetailNode tempNode = detailNode;
587         while (tempNode != null) {
588             if (tempNode.getType() == JavadocTokenTypes.TEXT) {
589                 contents.append(tempNode.getText());
590             }
591             tempNode = JavadocUtil.getNextSibling(tempNode);
592         }
593         return contents.toString();
594     }
595 
596     /**
597      * Finds and returns the first sentence.
598      *
599      * @param ast The Javadoc root node.
600      * @param period The configured period symbol.
601      * @return The first sentence up to and excluding the period, or null if no ending was found.
602      */
603     @Nullable
604     private static String getFirstSentenceOrNull(DetailNode ast, String period) {
605         final List<String> sentenceParts = new ArrayList<>();
606         String sentence = null;
607         for (String text : (Iterable<String>) streamTextParts(ast)::iterator) {
608             final String sentenceEnding = findSentenceEndingOrNull(text, period);
609             if (sentenceEnding != null) {
610                 sentenceParts.add(sentenceEnding);
611                 sentence = String.join("", sentenceParts);
612                 break;
613             }
614             else {
615                 sentenceParts.add(text);
616             }
617         }
618         return sentence;
619     }
620 
621     /**
622      * Streams through all the text under the given node.
623      *
624      * @param node The Javadoc node to examine.
625      * @return All the text in all nodes that have no child nodes.
626      */
627     private static Stream<String> streamTextParts(DetailNode node) {
628         final Stream<String> stream;
629         if (node.getChildren().length == 0) {
630             stream = Stream.of(node.getText());
631         }
632         else {
633             stream = Stream.of(node.getChildren())
634                 .flatMap(SummaryJavadocCheck::streamTextParts);
635         }
636         return stream;
637     }
638 
639     /**
640      * Finds the end of a sentence. If a sentence ending period was found, returns the whole string
641      * up to and excluding that period. The end of sentence detection here could be replaced in the
642      * future by Java's built-in BreakIterator class.
643      *
644      * @param text The string to search.
645      * @param period The period character to find.
646      * @return The string up to and excluding the period, or null if no ending was found.
647      */
648     @Nullable
649     private static String findSentenceEndingOrNull(String text, String period) {
650         int periodIndex = text.indexOf(period);
651         String sentenceEnding = null;
652         while (periodIndex >= 0) {
653             final int afterPeriodIndex = periodIndex + period.length();
654 
655             // Handle western period separately as it is only the end of a sentence if followed
656             // by whitespace. Other period characters often include whitespace in the character.
657             if (!DEFAULT_PERIOD.equals(period)
658                 || afterPeriodIndex >= text.length()
659                 || Character.isWhitespace(text.charAt(afterPeriodIndex))) {
660                 sentenceEnding = text.substring(0, periodIndex);
661                 break;
662             }
663             else {
664                 periodIndex = text.indexOf(period, afterPeriodIndex);
665             }
666         }
667         return sentenceEnding;
668     }
669 }