All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.web.checks.AbstractPageCheck Maven / Gradle / Ivy

There is a newer version: 2.3
Show newest version
/*
 * Sonar Web Plugin
 * Copyright (C) 2010 Matthijs Galesloot
 * [email protected]
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sonar.plugins.web.checks;

import org.apache.commons.lang.StringUtils;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.Violation;
import org.sonar.plugins.web.node.Attribute;
import org.sonar.plugins.web.node.TagNode;
import org.sonar.plugins.web.visitor.DefaultNodeVisitor;

import java.util.ArrayList;
import java.util.List;

/**
 * Abtract superclass for checks.
 *
 * @author Matthijs Galesloot
 */
public abstract class AbstractPageCheck extends DefaultNodeVisitor {

  private Rule rule;

  protected static final class QualifiedAttribute {

    private String attributeName;

    private String nodeName;

    private QualifiedAttribute(String nodeName, String attributeName) {
      this.setNodeName(nodeName);
      this.setAttributeName(attributeName);
    }

    public void setAttributeName(String attributeName) {
      this.attributeName = attributeName;
    }

    public String getAttributeName() {
      return attributeName;
    }

    public void setNodeName(String nodeName) {
      this.nodeName = nodeName;
    }

    public String getNodeName() {
      return nodeName;
    }
  }

  protected String getAttributesAsString(QualifiedAttribute[] qualifiedAttributes) {
    StringBuilder sb = new StringBuilder();
    if (qualifiedAttributes != null) {
      for (QualifiedAttribute a : qualifiedAttributes) {
        if (sb.length() > 0) {
          sb.append(",");
        }
        if (a.getNodeName() != null) {
          sb.append(a.getNodeName());
          sb.append('.');
        }
        sb.append(a.getAttributeName());
      }
    }
    return sb.toString();
  }

  public boolean isUnifiedExpression(String value) {
    return value != null && value.length() > 0 && (value.contains("#{") || value.contains("${"));
  }

  public String[] trimSplitCommaSeparatedList(String value) {
    String[] tokens = StringUtils.split(value, ",");
    for (int i = 0; i < tokens.length; i++) {
      tokens[i] = tokens[i].trim();
    }
    return tokens;
  }

  public QualifiedAttribute[] parseAttributes(String attributesList) {
    String[] qualifiedAttributeList = StringUtils.split(attributesList, ",");

    QualifiedAttribute[] qualifiedAttributes = new QualifiedAttribute[qualifiedAttributeList.length];
    int n = 0;
    for (String qualifiedAttribute : qualifiedAttributeList) {
      qualifiedAttribute = qualifiedAttribute.trim();
      if (qualifiedAttribute.indexOf('.') >= 0) {
        qualifiedAttributes[n++] = new QualifiedAttribute(StringUtils.substringBefore(qualifiedAttribute, "."), StringUtils.substringAfter(
            qualifiedAttribute, "."));
      } else {
        qualifiedAttributes[n++] = new QualifiedAttribute(null, qualifiedAttribute);
      }
    }
    return qualifiedAttributes;
  }

  protected final void createViolation(int linePosition, String message) {
    Violation violation = Violation.create(rule, getWebSourceCode().getResource());
    violation.setMessage(message);
    violation.setLineId(linePosition == 0 ? null : linePosition);
    getWebSourceCode().addViolation(violation);
  }

  public final Rule getRule() {
    return rule;
  }

  public final String getRuleKey() {
    return rule.getConfigKey();
  }

  public final void setRule(Rule rule) {
    this.rule = rule;
  }

  protected List getMatchingAttributes(TagNode element, QualifiedAttribute[] attributes) {
    List matchingAttributes = new ArrayList();

    for (QualifiedAttribute qualifiedAttribute : attributes) {
      if (qualifiedAttribute.getNodeName() == null
        || StringUtils.equalsIgnoreCase(element.getLocalName(), qualifiedAttribute.getNodeName())
        || StringUtils.equalsIgnoreCase(element.getNodeName(), qualifiedAttribute.getNodeName())) {
        for (Attribute a : element.getAttributes()) {
          if (qualifiedAttribute.getAttributeName().equalsIgnoreCase(a.getName())) {
            matchingAttributes.add(a);
          }
        }
      }
    }
    return matchingAttributes;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy