From a0e969bebd04c951ffbac9f504bf330af774901a Mon Sep 17 00:00:00 2001 From: Bertrand Lorentz Date: Wed, 7 Aug 2024 17:48:45 +0200 Subject: [PATCH] pure: Evaluate variables in the order they are declared Store the variables from the Schematron in a linked hashmap, to preserve their order. They are then evaluated in the order they are declared. Before this, variables were sorted with the longest first. We were replacing variable references with their expressions, so starting with the longest was needed. With the new way of evaluating variables, this caused problems when a variable referenced another variable that has a shorter name. This changes the IPSXPathVariables interface to return maps and sets, instead of navigable maps and sorted sets. It does not seem that the specifics of these types were used. Add unit test that checks the correction of this bug. Fixes #177. --- .../pure/binding/xpath/IPSXPathVariables.java | 8 +-- .../pure/binding/xpath/PSXPathVariables.java | 17 +++--- .../pure/supplementary/Issue177Test.java | 61 +++++++++++++++++++ .../external/issues/github177/schematron.sch | 10 +++ .../external/issues/github177/test.xml | 12 ++++ 5 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 ph-schematron-pure/src/test/java/com/helger/schematron/pure/supplementary/Issue177Test.java create mode 100644 ph-schematron-pure/src/test/resources/external/issues/github177/schematron.sch create mode 100644 ph-schematron-pure/src/test/resources/external/issues/github177/test.xml diff --git a/ph-schematron-pure/src/main/java/com/helger/schematron/pure/binding/xpath/IPSXPathVariables.java b/ph-schematron-pure/src/main/java/com/helger/schematron/pure/binding/xpath/IPSXPathVariables.java index 1703d6133..5d98ba016 100644 --- a/ph-schematron-pure/src/main/java/com/helger/schematron/pure/binding/xpath/IPSXPathVariables.java +++ b/ph-schematron-pure/src/main/java/com/helger/schematron/pure/binding/xpath/IPSXPathVariables.java @@ -23,8 +23,8 @@ import javax.xml.xpath.XPathExpression; import com.helger.commons.annotation.ReturnsMutableCopy; -import com.helger.commons.collection.impl.ICommonsNavigableMap; -import com.helger.commons.collection.impl.ICommonsSortedSet; +import com.helger.commons.collection.impl.ICommonsMap; +import com.helger.commons.collection.impl.ICommonsSet; import com.helger.commons.lang.ICloneable; /** @@ -39,7 +39,7 @@ public interface IPSXPathVariables extends ICloneable , Serial */ @Nonnull @ReturnsMutableCopy - ICommonsNavigableMap getAll (); + ICommonsMap getAll (); /** * @return All contained variable names. Never null. @@ -47,7 +47,7 @@ public interface IPSXPathVariables extends ICloneable , Serial */ @Nonnull @ReturnsMutableCopy - ICommonsSortedSet getAllNames (); + ICommonsSet getAllNames (); /** * @param sName diff --git a/ph-schematron-pure/src/main/java/com/helger/schematron/pure/binding/xpath/PSXPathVariables.java b/ph-schematron-pure/src/main/java/com/helger/schematron/pure/binding/xpath/PSXPathVariables.java index fc6c568f1..1e88df731 100644 --- a/ph-schematron-pure/src/main/java/com/helger/schematron/pure/binding/xpath/PSXPathVariables.java +++ b/ph-schematron-pure/src/main/java/com/helger/schematron/pure/binding/xpath/PSXPathVariables.java @@ -26,10 +26,9 @@ import com.helger.commons.ValueEnforcer; import com.helger.commons.annotation.Nonempty; import com.helger.commons.annotation.ReturnsMutableCopy; -import com.helger.commons.collection.impl.CommonsTreeMap; -import com.helger.commons.collection.impl.ICommonsNavigableMap; -import com.helger.commons.collection.impl.ICommonsSortedSet; -import com.helger.commons.compare.IComparator; +import com.helger.commons.collection.impl.CommonsLinkedHashMap; +import com.helger.commons.collection.impl.ICommonsMap; +import com.helger.commons.collection.impl.ICommonsSet; import com.helger.commons.state.EChange; import com.helger.commons.string.StringHelper; import com.helger.commons.string.ToStringGenerator; @@ -44,12 +43,12 @@ public class PSXPathVariables implements IPSXPathVariables { @Nonnull @ReturnsMutableCopy - private static ICommonsNavigableMap _createMap () + private static ICommonsMap _createMap () { - return new CommonsTreeMap <> (IComparator.getComparatorStringLongestFirst ()); + return new CommonsLinkedHashMap <> (); } - private final ICommonsNavigableMap m_aMap; + private final ICommonsMap m_aMap; public PSXPathVariables () { @@ -138,14 +137,14 @@ public EChange removeAll (@Nullable final Iterable aVars) @Nonnull @ReturnsMutableCopy - public ICommonsNavigableMap getAll () + public ICommonsMap getAll () { return m_aMap.getClone (); } @Nonnull @ReturnsMutableCopy - public ICommonsSortedSet getAllNames () + public ICommonsSet getAllNames () { return m_aMap.copyOfKeySet (); } diff --git a/ph-schematron-pure/src/test/java/com/helger/schematron/pure/supplementary/Issue177Test.java b/ph-schematron-pure/src/test/java/com/helger/schematron/pure/supplementary/Issue177Test.java new file mode 100644 index 000000000..07da7fe38 --- /dev/null +++ b/ph-schematron-pure/src/test/java/com/helger/schematron/pure/supplementary/Issue177Test.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2014-2024 Philip Helger (www.helger.com) + * philip[at]helger[dot]com + * + * 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 com.helger.schematron.pure.supplementary; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.File; + +import javax.annotation.Nonnull; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.helger.commons.io.resource.FileSystemResource; +import com.helger.schematron.pure.SchematronResourcePure; +import com.helger.schematron.pure.errorhandler.LoggingPSErrorHandler; +import com.helger.schematron.svrl.SVRLHelper; +import com.helger.schematron.svrl.SVRLMarshaller; +import com.helger.schematron.svrl.jaxb.SchematronOutputType; + +public final class Issue177Test +{ + private static final Logger LOGGER = LoggerFactory.getLogger (Issue177Test.class); + + public static void validateAndProduceSVRL (@Nonnull final File aSchematron, final File aXML) throws Exception + { + final SchematronResourcePure aSCH = SchematronResourcePure.fromFile (aSchematron); + aSCH.validateCompletely (new LoggingPSErrorHandler ()); + + // Perform validation + final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL (new FileSystemResource (aXML)); + assertNotNull (aSVRL); + if (true) + LOGGER.info (new SVRLMarshaller ().getAsString (aSVRL)); + + assertEquals (0, SVRLHelper.getAllFailedAssertionsAndSuccessfulReports (aSVRL).size ()); + } + + @Test + public void testIssue () throws Exception + { + validateAndProduceSVRL (new File ("src/test/resources/external/issues/github177/schematron.sch"), + new File ("src/test/resources/external/issues/github177/test.xml")); + } +} diff --git a/ph-schematron-pure/src/test/resources/external/issues/github177/schematron.sch b/ph-schematron-pure/src/test/resources/external/issues/github177/schematron.sch new file mode 100644 index 000000000..4d5403058 --- /dev/null +++ b/ph-schematron-pure/src/test/resources/external/issues/github177/schematron.sch @@ -0,0 +1,10 @@ + + + + + + + error message + + + diff --git a/ph-schematron-pure/src/test/resources/external/issues/github177/test.xml b/ph-schematron-pure/src/test/resources/external/issues/github177/test.xml new file mode 100644 index 000000000..859ef5aee --- /dev/null +++ b/ph-schematron-pure/src/test/resources/external/issues/github177/test.xml @@ -0,0 +1,12 @@ + + + + 1 + + + 2 + + + 3 + + \ No newline at end of file