
org.sonar.plugins.xml.checks.XPathCheck Maven / Gradle / Ivy
/*
* SonarQube XML Plugin
* Copyright (C) 2010-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.plugins.xml.checks;
import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.xml.utils.PrefixResolver;
import org.apache.xml.utils.PrefixResolverDefault;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.squidbridge.annotations.RuleTemplate;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
@Rule(
key = "XPathCheck",
name = "XPath rule",
priority = Priority.MAJOR)
@RuleTemplate
public class XPathCheck extends AbstractXmlCheck {
@RuleProperty(key = "expression", description = "The XPath query", type = "TEXT")
private String expression;
@RuleProperty(key = "filePattern", description = "Files to be validated using Ant-style matching patterns.")
private String filePattern;
@RuleProperty(
key = "message",
description = "The issue message",
defaultValue = "The XPath expression matches this piece of code")
private String message;
private static final class DocumentNamespaceContext implements NamespaceContext {
private final PrefixResolver resolver;
private DocumentNamespaceContext(PrefixResolver resolver) {
this.resolver = resolver;
}
@Override
public String getNamespaceURI(String prefix) {
return resolver.getNamespaceForPrefix(prefix);
}
@Override
// Dummy implementation - not used!
public String getPrefix(String uri) {
return null;
}
@Override
// Dummy implementation - not used!
public Iterator
© 2015 - 2025 Weber Informatics LLC | Privacy Policy