avoid compiling patterns times to times

This commit is contained in:
XenoAmess
2020-08-24 16:22:33 +08:00
parent 3a7ea4b606
commit d9d4b31a3a

View File

@ -11,14 +11,15 @@ import net.sourceforge.pmd.lang.vm.ast.ASTText;
import net.sourceforge.pmd.lang.vm.rule.AbstractVmRule;
public class NoInlineJavaScriptRule extends AbstractVmRule {
private static final Pattern SCRIPT_PATTERN = Pattern.compile("<script\\s[^>]*>", Pattern.CASE_INSENSITIVE);
private static final Pattern SRC_PATTERN = Pattern.compile("\\ssrc\\s*=", Pattern.CASE_INSENSITIVE);
@Override
public Object visit(final ASTText node, final Object data) {
final Pattern scriptPattern = Pattern.compile("<script\\s[^>]*>", Pattern.CASE_INSENSITIVE);
final Pattern srcPattern = Pattern.compile("\\ssrc\\s*=", Pattern.CASE_INSENSITIVE);
final Matcher matcher = scriptPattern.matcher(node.literal());
final Matcher matcher = SCRIPT_PATTERN.matcher(node.literal());
while (matcher.find()) {
final String currentMatch = matcher.group();
if (!srcPattern.matcher(currentMatch).find()) {
if (!SRC_PATTERN.matcher(currentMatch).find()) {
addViolation(data, node);
}
}