View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.vm.rule.basic;
5   
6   import java.util.regex.Matcher;
7   import java.util.regex.Pattern;
8   
9   import net.sourceforge.pmd.lang.vm.ast.ASTText;
10  import net.sourceforge.pmd.lang.vm.rule.AbstractVmRule;
11  
12  public class NoInlineJavaScriptRule extends AbstractVmRule {
13      @Override
14      public Object visit(final ASTText node, final Object data) {
15          final Pattern scriptPattern = Pattern.compile("<script\\s[^>]*>", Pattern.CASE_INSENSITIVE);
16          final Pattern srcPattern = Pattern.compile("\\ssrc\\s*=", Pattern.CASE_INSENSITIVE);
17          final Matcher matcher = scriptPattern.matcher(node.literal());
18          while (matcher.find()) {
19              final String currentMatch = matcher.group();
20              if (!srcPattern.matcher(currentMatch).find()) {
21                  addViolation(data, node);
22              }
23          }
24          return super.visit(node, data);
25      }
26  }