Hi, I want to check if a string contains 'http://' at the start. My Java is poor, and I really can not find a person to ask.
Here is my code:
/*
* Copyright (C) 2009-2013 SonarSource SA
* All rights reserved
* mailto:contact AT sonarsource DOT com
*/
package org.sonar.samples.javascript.checks;
import com.google.common.collect.ImmutableSet;
import java.util.Set;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.check.Priority;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.sonar.check.Rule;
import org.sonar.plugins.javascript.api.tree.Tree.Kind;
import org.sonar.plugins.javascript.api.tree.statement.VariableDeclarationTree;
import org.sonar.plugins.javascript.api.visitors.DoubleDispatchVisitorCheck;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.squidbridge.annotations.SqaleSubCharacteristic;
@Rule(
key = "S1",
priority = Priority.MAJOR,
name = "HTTP protocol is not required.",
tags = {"convention"}
)
@SqaleSubCharacteristic(RulesDefinition.SubCharacteristics.DATA_RELIABILITY)
@SqaleConstantRemediation("5min")
public class ForbiddenHttpProtocalUseCheck extends DoubleDispatchVisitorCheck {
private static final Pattern variablePattern = Pattern.compile(":(http)", Pattern.CASE_INSENSITIVE);
@Override
public void visitVariableDeclaration(VariableDeclarationTree tree) {
Matcher matcher = variablePattern.matcher( /* Some String Maybe here */ );
if (matcher.find()) {
addIssue(tree, "Remove the usage of this HTTP protocol.");
}
super.visitVariableDeclaration(tree);
}
}On my Step, I only can match `var` statement.
Thanks,
Sam
--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/030c3cd0-2b7a-4fc5-a380-1e68c306c304%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
package org.sonar.samples.javascript.checks;
import java.util.Set;import org.sonar.api.server.rule.RulesDefinition;import org.sonar.check.Priority;
import org.sonar.check.Rule;import org.sonar.plugins.javascript.api.tree.Tree.Kind;import org.sonar.plugins.javascript.api.tree.expression.LiteralTree;
import org.sonar.plugins.javascript.api.visitors.DoubleDispatchVisitorCheck;import org.sonar.squidbridge.annotations.SqaleConstantRemediation;import org.sonar.squidbridge.annotations.SqaleSubCharacteristic;
@Rule( key = "S1", priority = Priority.MAJOR, name = "HTTP protocol is not required.", tags = {"convention"})@SqaleSubCharacteristic(RulesDefinition.SubCharacteristics.DATA_RELIABILITY)@SqaleConstantRemediation("5min")public class ForbiddenHttpProtocalUseCheck extends DoubleDispatchVisitorCheck {
@Override public void visitLiteral(LiteralTree tree) { if (tree.is(Kind.STRING_LITERAL)) { String value = tree.value(); if (value.length() > 1 && value.startsWith("http://")) {
addIssue(tree, "Remove the usage of this HTTP protocol."); } } }}
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/e9ca8d85-0754-403e-a838-49aca75a35d6%40googlegroups.com.
/*
* Copyright (C) 2009-2013 SonarSource SA
* All rights reserved
* mailto:contact AT sonarsource DOT com
*/
package org.sonar.samples.javascript.checks;
import java.util.Set;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.sonar.plugins.javascript.api.tree.Tree.Kind;
import org.sonar.plugins.javascript.api.tree.expression.LiteralTree;
import org.sonar.plugins.javascript.api.visitors.DoubleDispatchVisitorCheck;
import org.sonar.squidbridge.annotations.SqaleConstantRemediation;
import org.sonar.squidbridge.annotations.SqaleSubCharacteristic;
@Rule(
key = "S1",
priority = Priority.MAJOR,
name = "HTTP protocol is not required.",
tags = {"convention"}
)
@SqaleSubCharacteristic(RulesDefinition.SubCharacteristics.DATA_RELIABILITY)
@SqaleConstantRemediation("5min")
public class ForbiddenHttpProtocalUseCheck extends DoubleDispatchVisitorCheck {
private static final Pattern variablePattern = Pattern.compile("http\\://", Pattern.CASE_INSENSITIVE);
@Override
public void visitLiteral(LiteralTree tree) {
if (tree.is(Kind.STRING_LITERAL)) {
String literalValue = tree.value();
Matcher matcher = variablePattern.matcher(literalValue);
// System.out.println(literalValue.length());
// System.out.println(matcher.find());
if (literalValue.length() > 1 && matcher.find()) {
addIssue(tree, "Remove the usage of this HTTP protocol.");
}
}
}
}
That's because the value of the literal includes the quotes.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/c0ccdac4-9b13-4ca8-84f6-7ccd8852c8d5%40googlegroups.com.