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