Update some usages early

This commit is contained in:
Clément Fournier 2020-04-08 21:59:41 +02:00
parent 5067c79eb2
commit 1de1d1c66d
4 changed files with 50 additions and 21 deletions

View File

@ -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();

View File

@ -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<String> 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;
}
}

View File

@ -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);

View File

@ -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);