From 1de1d1c66daf9e34947ad7f494a0f50c800d03c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Wed, 8 Apr 2020 21:59:41 +0200 Subject: [PATCH] Update some usages early --- .../net/sourceforge/pmd/RuleSetFactory.java | 2 +- .../sourceforge/pmd/lang/rule/XPathRule.java | 46 +++++++++++++------ .../pmd/lang/rule/xpath/XPathVersion.java | 9 ++-- .../pmd/docs/RuleDocGenerator.java | 14 +++++- 4 files changed, 50 insertions(+), 21 deletions(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java index a9229d480f..4603f6f727 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java @@ -305,7 +305,7 @@ public class RuleSetFactory { public RuleSet createSingleRuleRuleSet(final Rule rule) { // TODO make static? final long checksum; if (rule instanceof XPathRule) { - checksum = rule.getProperty(XPathRule.XPATH_DESCRIPTOR).hashCode(); + checksum = ((XPathRule) rule).getXPathExpression().hashCode(); } else { // TODO : Is this good enough? all properties' values + rule name checksum = rule.getPropertiesByPropertyDescriptor().values().hashCode() * 31 + rule.getName().hashCode(); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/XPathRule.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/XPathRule.java index f616d9ab66..f6a36b903f 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/XPathRule.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/XPathRule.java @@ -106,6 +106,21 @@ public class XPathRule extends AbstractRule { setVersion(version.getXmlName()); } + /** + * Returns the version for this rule. Returns null if this is not + * set or invalid. + */ + public XPathVersion getVersion() { + return XPathVersion.ofId(getProperty(VERSION_DESCRIPTOR)); + } + + /** + * Returns the XPath expression that implements this rule. + */ + public String getXPathExpression() { + return getProperty(XPATH_DESCRIPTOR); + } + /** * @deprecated Use the constructor {@link #XPathRule(XPathVersion, String)}, * don't set the expression after the fact. @@ -156,13 +171,21 @@ public class XPathRule extends AbstractRule { * engine in which the query will be run it looks at the XPath version. */ private void initXPathRuleQuery() { - String xpath = getProperty(XPATH_DESCRIPTOR); - String version = getProperty(VERSION_DESCRIPTOR); + String xpath = getXPathExpression(); + XPathVersion version = getVersion(); - initRuleQueryBasedOnVersion(version); + if (version == null) { + throw new IllegalStateException("Invalid XPath version, should have been caught by Rule::dysfunctionReason"); + } + + if (version.equals(XPathVersion.XPATH_1_0)) { + xpathRuleQuery = new JaxenXPathRuleQuery(); + } else { + xpathRuleQuery = new SaxonXPathRuleQuery(); + } xpathRuleQuery.setXPath(xpath); - xpathRuleQuery.setVersion(version); + xpathRuleQuery.setVersion(version.getXmlName()); xpathRuleQuery.setProperties(getPropertiesByPropertyDescriptor()); } @@ -175,10 +198,6 @@ public class XPathRule extends AbstractRule { return xpathRuleQuery == null; } - private void initRuleQueryBasedOnVersion(final String version) { - xpathRuleQuery = XPATH_1_0.equals(version) ? new JaxenXPathRuleQuery() : new SaxonXPathRuleQuery(); - } - @Override public List getRuleChainVisits() { if (xPathRuleQueryNeedsInitialization()) { @@ -193,10 +212,11 @@ public class XPathRule extends AbstractRule { @Override public String dysfunctionReason() { - return hasXPathExpression() ? null : "Missing xPath expression"; - } - - private boolean hasXPathExpression() { - return StringUtils.isNotBlank(getProperty(XPATH_DESCRIPTOR)); + if (getVersion() == null) { + return "Invalid XPath version '" + getProperty(VERSION_DESCRIPTOR) + "'"; + } else if (StringUtils.isBlank(getXPathExpression())) { + return "Missing XPath expression"; + } + return null; } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/xpath/XPathVersion.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/xpath/XPathVersion.java index 4ff10ee7c4..7177afa443 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/xpath/XPathVersion.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/xpath/XPathVersion.java @@ -14,14 +14,15 @@ public enum XPathVersion { /** * XPath 1.0. * - * @deprecated not supported anymore + * @deprecated Will become unsupported in 7.0.0 */ @Deprecated XPATH_1_0(XPathRuleQuery.XPATH_1_0), + /** * XPath 1.0 compatibility mode. * - * @deprecated Not supported any more. + * @deprecated Will become unsupported in 7.0.0 */ @Deprecated XPATH_1_0_COMPATIBILITY(XPathRuleQuery.XPATH_1_0_COMPATIBILITY), @@ -61,9 +62,7 @@ public enum XPathVersion { * * @param version A version string * - * @return An XPath version - * - * @return Null if the argument is not a valid version + * @return An XPath version, or null if the argument is not a valid version */ public static XPathVersion ofId(String version) { return BY_NAME.get(version); diff --git a/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java b/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java index 4d1ffa2dee..e0e6c2511f 100644 --- a/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java +++ b/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java @@ -405,10 +405,11 @@ public class RuleDocGenerator { lines.addAll(EscapeUtils.escapeLines(toLines(stripIndentation(rule.getDescription())))); lines.add(""); - if (rule instanceof XPathRule || rule instanceof RuleReference && ((RuleReference) rule).getRule() instanceof XPathRule) { + XPathRule xpathRule = asXPathRule(rule); + if (xpathRule != null) { lines.add("**This rule is defined by the following XPath expression:**"); lines.add("``` xpath"); - lines.addAll(toLines(StringUtils.stripToEmpty(rule.getProperty(XPathRule.XPATH_DESCRIPTOR)))); + lines.addAll(toLines(StringUtils.stripToEmpty(xpathRule.getXPathExpression()))); lines.add("```"); } else { lines.add("**This rule is defined by the following Java class:** " @@ -502,6 +503,15 @@ public class RuleDocGenerator { } } + private XPathRule asXPathRule(Rule rule) { + if (rule instanceof XPathRule) { + return (XPathRule) rule; + } else if (rule instanceof RuleReference && ((RuleReference) rule).getRule() instanceof XPathRule) { + return (XPathRule) ((RuleReference) rule).getRule(); + } + return null; + } + private static boolean isDeprecated(PropertyDescriptor propertyDescriptor) { return propertyDescriptor.description() != null && propertyDescriptor.description().toLowerCase(Locale.ROOT).startsWith(DEPRECATED_RULE_PROPERTY_MARKER);