Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pure: Evaluate variables in the order they are declared #178

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -39,15 +39,15 @@ public interface IPSXPathVariables extends ICloneable <PSXPathVariables>, Serial
*/
@Nonnull
@ReturnsMutableCopy
ICommonsNavigableMap <String, XPathExpression> getAll ();
ICommonsMap <String, XPathExpression> getAll ();

/**
* @return All contained variable names. Never <code>null</code>.
* @since v8
*/
@Nonnull
@ReturnsMutableCopy
ICommonsSortedSet <String> getAllNames ();
ICommonsSet <String> getAllNames ();

/**
* @param sName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,12 +43,12 @@ public class PSXPathVariables implements IPSXPathVariables
{
@Nonnull
@ReturnsMutableCopy
private static ICommonsNavigableMap <String, XPathExpression> _createMap ()
private static ICommonsMap <String, XPathExpression> _createMap ()
{
return new CommonsTreeMap <> (IComparator.getComparatorStringLongestFirst ());
return new CommonsLinkedHashMap <> ();
}

private final ICommonsNavigableMap <String, XPathExpression> m_aMap;
private final ICommonsMap <String, XPathExpression> m_aMap;

public PSXPathVariables ()
{
Expand Down Expand Up @@ -138,14 +137,14 @@ public EChange removeAll (@Nullable final Iterable <String> aVars)

@Nonnull
@ReturnsMutableCopy
public ICommonsNavigableMap <String, XPathExpression> getAll ()
public ICommonsMap <String, XPathExpression> getAll ()
{
return m_aMap.getClone ();
}

@Nonnull
@ReturnsMutableCopy
public ICommonsSortedSet <String> getAllNames ()
public ICommonsSet <String> getAllNames ()
{
return m_aMap.copyOfKeySet ();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<schema xmlns="http://purl.oclc.org/dsdl/schematron" queryBinding="xpath2">
<pattern>
<let name="var" value="1"/>
<let name="var-plus-one" value="$var + 1"/>

<rule context="/a">
<assert role="ERROR" test="$var-plus-one = 2">error message</assert>
</rule>
</pattern>
</schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<a>
<b>
<c>1</c>
</b>
<b>
<c>2</c>
</b>
<b>
<c>3</c>
</b>
</a>