Coverage Report - com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
JavadocUtils
95%
44/46
96%
27/28
8
JavadocUtils$JavadocTagType
100%
4/4
N/A
8
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  Oliver Burn
 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  
 package com.puppycrawl.tools.checkstyle.checks.javadoc;
 20  
 
 21  
 import com.google.common.collect.Lists;
 22  
 import com.puppycrawl.tools.checkstyle.api.JavadocTagInfo;
 23  
 import com.puppycrawl.tools.checkstyle.api.TextBlock;
 24  
 import com.puppycrawl.tools.checkstyle.api.Utils;
 25  
 import java.util.List;
 26  
 import java.util.regex.Matcher;
 27  
 import java.util.regex.Pattern;
 28  
 
 29  
 /**
 30  
  * Contains utility methods for working with Javadoc.
 31  
  * @author Lyle Hanson
 32  
  */
 33  
 public final class JavadocUtils
 34  
 {
 35  
     ///CLOVER:OFF
 36  
     /** prevent instantiation */
 37  
     private JavadocUtils()
 38  0
     {
 39  0
     }
 40  
     ///CLOVER:ON
 41  
 
 42  
     /**
 43  
      * Gets validTags from a given piece of Javadoc.
 44  
      * @param aCmt the Javadoc comment to process.
 45  
      * @param aTagType the type of validTags we're interested in
 46  
      * @return all standalone validTags from the given javadoc.
 47  
      */
 48  
     public static JavadocTags getJavadocTags(TextBlock aCmt,
 49  
                                              JavadocTagType aTagType)
 50  
     {
 51  82
         final String[] text = aCmt.getText();
 52  82
         final List<JavadocTag> tags = Lists.newArrayList();
 53  82
         final List<InvalidJavadocTag> invalidTags = Lists.newArrayList();
 54  82
         Pattern blockTagPattern =
 55  
             Utils.getPattern("/\\*{2,}\\s*@(\\p{Alpha}+)\\s");
 56  431
         for (int i = 0; i < text.length; i++) {
 57  349
             final String s = text[i];
 58  349
             final Matcher blockTagMatcher = blockTagPattern.matcher(s);
 59  349
             if ((aTagType.equals(JavadocTagType.ALL) || aTagType
 60  
                     .equals(JavadocTagType.BLOCK)) && blockTagMatcher.find())
 61  
             {
 62  105
                 final String tagName = blockTagMatcher.group(1);
 63  105
                 String content = s.substring(blockTagMatcher.end(1));
 64  105
                 if (content.endsWith("*/")) {
 65  12
                     content = content.substring(0, content.length() - 2);
 66  
                 }
 67  105
                 final int line = aCmt.getStartLineNo() + i;
 68  105
                 int col = blockTagMatcher.start(1) - 1;
 69  105
                 if (i == 0) {
 70  4
                     col += aCmt.getStartColNo();
 71  
                 }
 72  105
                 if (JavadocTagInfo.isValidName(tagName)) {
 73  102
                     tags.add(
 74  
                         new JavadocTag(line, col, tagName, content.trim()));
 75  
                 }
 76  
                 else {
 77  3
                     invalidTags.add(new InvalidJavadocTag(line, col, tagName));
 78  
                 }
 79  105
             }
 80  
             // No block tag, so look for inline validTags
 81  244
             else if (aTagType.equals(JavadocTagType.ALL)
 82  
                     || aTagType.equals(JavadocTagType.INLINE))
 83  
             {
 84  
                 // Match JavaDoc text after comment characters
 85  30
                 final Pattern commentPattern =
 86  
                     Utils.getPattern("^\\s*(?:/\\*{2,}|\\*+)\\s*(.*)");
 87  30
                 final Matcher commentMatcher = commentPattern.matcher(s);
 88  
                 final String commentContents;
 89  
                 final int commentOffset; // offset including comment characters
 90  30
                 if (!commentMatcher.find()) {
 91  2
                     commentContents = s; // No leading asterisks, still valid
 92  2
                     commentOffset = 0;
 93  
                 }
 94  
                 else {
 95  28
                     commentContents = commentMatcher.group(1);
 96  28
                     commentOffset = commentMatcher.start(1) - 1;
 97  
                 }
 98  30
                 final Pattern tagPattern =
 99  
                     Utils.getPattern(".*?\\{@(\\p{Alpha}+)\\s+(.*?)\\}");
 100  30
                 final Matcher tagMatcher = tagPattern.matcher(commentContents);
 101  45
                 while (tagMatcher.find()) {
 102  15
                     if (tagMatcher.groupCount() == 2) {
 103  15
                         final String tagName = tagMatcher.group(1);
 104  15
                         final String tagValue = tagMatcher.group(2).trim();
 105  15
                         final int line = aCmt.getStartLineNo() + i;
 106  15
                         int col = commentOffset + (tagMatcher.start(1) - 1);
 107  15
                         if (i == 0) {
 108  2
                             col += aCmt.getStartColNo();
 109  
                         }
 110  15
                         if (JavadocTagInfo.isValidName(tagName)) {
 111  14
                             tags.add(new JavadocTag(line, col, tagName,
 112  
                                     tagValue));
 113  
                         }
 114  
                         else {
 115  1
                             invalidTags.add(new InvalidJavadocTag(line, col,
 116  
                                     tagName));
 117  
                         }
 118  15
                     }
 119  
                     // else Error: Unexpected match count for inline JavaDoc
 120  
                     // tag!
 121  
                 }
 122  
             }
 123  349
             blockTagPattern =
 124  
                 Utils.getPattern("^\\s*\\**\\s*@(\\p{Alpha}+)\\s");
 125  
         }
 126  82
         return new JavadocTags(tags, invalidTags);
 127  
     }
 128  
 
 129  
     /**
 130  
      * The type of Javadoc tag we want returned.
 131  
      */
 132  4
     public enum JavadocTagType
 133  
     {
 134  
         /** block type. */
 135  1
         BLOCK,
 136  
         /** inline type. */
 137  1
         INLINE,
 138  
         /** all validTags. */
 139  1
         ALL;
 140  
     }
 141  
 }