001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2015 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 java.util.Arrays; 023 024import org.apache.commons.lang3.ArrayUtils; 025 026import com.puppycrawl.tools.checkstyle.api.Check; 027import com.puppycrawl.tools.checkstyle.api.DetailAST; 028 029/** 030 * Implementation of a check that looks for a single line in Java files. 031 * Supports ignoring comments for matches. 032 * @author Oliver Burn 033 */ 034public class RegexpSinglelineJavaCheck extends Check { 035 /** The detection options to use. */ 036 private final DetectorOptions options = new DetectorOptions(0, this); 037 /** The detector to use. */ 038 private SinglelineDetector detector; 039 /** Suppress comments. **/ 040 private boolean ignoreComments; 041 042 @Override 043 public int[] getDefaultTokens() { 044 return getAcceptableTokens(); 045 } 046 047 @Override 048 public int[] getAcceptableTokens() { 049 return ArrayUtils.EMPTY_INT_ARRAY; 050 } 051 052 @Override 053 public int[] getRequiredTokens() { 054 return getAcceptableTokens(); 055 } 056 057 @Override 058 public void init() { 059 super.init(); 060 detector = new SinglelineDetector(options); 061 } 062 063 @Override 064 public void beginTree(DetailAST rootAST) { 065 066 if (ignoreComments) { 067 options.setSuppressor(new CommentSuppressor(getFileContents())); 068 } 069 else { 070 options.setSuppressor(NeverSuppress.INSTANCE); 071 } 072 073 detector.processLines(Arrays.asList(getLines())); 074 } 075 076 /** 077 * Set the format of the regular expression to match. 078 * @param format the format of the regular expression to match. 079 */ 080 public void setFormat(String format) { 081 options.setFormat(format); 082 } 083 084 /** 085 * Set the message to report for a match. 086 * @param message the message to report for a match. 087 */ 088 public void setMessage(String message) { 089 options.setMessage(message); 090 } 091 092 /** 093 * Set the minimum number of matches required per file. 094 * @param minimum the minimum number of matches required per file. 095 */ 096 public void setMinimum(int minimum) { 097 options.setMinimum(minimum); 098 } 099 100 /** 101 * Set the maximum number of matches required per file. 102 * @param maximum the maximum number of matches required per file. 103 */ 104 public void setMaximum(int maximum) { 105 options.setMaximum(maximum); 106 } 107 108 /** 109 * Set whether to ignore case when matching. 110 * @param ignore whether to ignore case when matching. 111 */ 112 public void setIgnoreCase(boolean ignore) { 113 options.setIgnoreCase(ignore); 114 } 115 116 /** 117 * Set whether to ignore comments when matching. 118 * @param ignore whether to ignore comments when matching. 119 */ 120 public void setIgnoreComments(boolean ignore) { 121 ignoreComments = ignore; 122 } 123}