001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.regexp; 021 022import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 023import com.puppycrawl.tools.checkstyle.api.DetailAST; 024import com.puppycrawl.tools.checkstyle.utils.CommonUtils; 025 026/** 027 * Implementation of a check that looks for a single line in Java files. 028 * Supports ignoring comments for matches. 029 * @author Oliver Burn 030 */ 031public class RegexpSinglelineJavaCheck extends AbstractCheck { 032 033 /** The format of the regular expression to match. */ 034 private String format = "$."; 035 /** The message to report for a match. */ 036 private String message; 037 /** The minimum number of matches required per file. */ 038 private int minimum; 039 /** The maximum number of matches required per file. */ 040 private int maximum; 041 /** Whether to ignore case when matching. */ 042 private boolean ignoreCase; 043 /** Suppress comments. **/ 044 private boolean ignoreComments; 045 046 @Override 047 public int[] getDefaultTokens() { 048 return getAcceptableTokens(); 049 } 050 051 @Override 052 public int[] getAcceptableTokens() { 053 return CommonUtils.EMPTY_INT_ARRAY; 054 } 055 056 @Override 057 public int[] getRequiredTokens() { 058 return getAcceptableTokens(); 059 } 060 061 @Override 062 public void beginTree(DetailAST rootAST) { 063 MatchSuppressor supressor = null; 064 if (ignoreComments) { 065 supressor = new CommentSuppressor(getFileContents()); 066 } 067 068 final DetectorOptions options = DetectorOptions.newBuilder() 069 .reporter(this) 070 .compileFlags(0) 071 .suppressor(supressor) 072 .format(format) 073 .message(message) 074 .minimum(minimum) 075 .maximum(maximum) 076 .ignoreCase(ignoreCase) 077 .build(); 078 final SinglelineDetector detector = new SinglelineDetector(options); 079 detector.processLines(getFileContents().getText()); 080 } 081 082 /** 083 * Set the format of the regular expression to match. 084 * @param format the format of the regular expression to match. 085 */ 086 public void setFormat(String format) { 087 this.format = format; 088 } 089 090 /** 091 * Set the message to report for a match. 092 * @param message the message to report for a match. 093 */ 094 public void setMessage(String message) { 095 this.message = message; 096 } 097 098 /** 099 * Set the minimum number of matches required per file. 100 * @param minimum the minimum number of matches required per file. 101 */ 102 public void setMinimum(int minimum) { 103 this.minimum = minimum; 104 } 105 106 /** 107 * Set the maximum number of matches required per file. 108 * @param maximum the maximum number of matches required per file. 109 */ 110 public void setMaximum(int maximum) { 111 this.maximum = maximum; 112 } 113 114 /** 115 * Set whether to ignore case when matching. 116 * @param ignoreCase whether to ignore case when matching. 117 */ 118 public void setIgnoreCase(boolean ignoreCase) { 119 this.ignoreCase = ignoreCase; 120 } 121 122 /** 123 * Set whether to ignore comments when matching. 124 * @param ignore whether to ignore comments when matching. 125 */ 126 public void setIgnoreComments(boolean ignore) { 127 ignoreComments = ignore; 128 } 129}