Skip to content

Commit

Permalink
SVRL XSD extension for #85
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Dec 4, 2020
1 parent 64cdebe commit 0dbb027
Show file tree
Hide file tree
Showing 13 changed files with 189 additions and 23 deletions.
12 changes: 12 additions & 0 deletions ph-schematron-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
<groupId>com.helger</groupId>
<artifactId>ph-jaxb</artifactId>
</dependency>
<dependency>
<groupId>com.helger.xsd</groupId>
<artifactId>ph-xsds-xml</artifactId>
</dependency>
<!-- Saxon is required! - does not work with integrated JAXP nor with Xalan 2.7.1! -->
<dependency>
<groupId>net.sf.saxon</groupId>
Expand Down Expand Up @@ -127,13 +131,15 @@ osgi.serviceloader; filter:="(osgi.serviceloader=com.helger.schematron.svrl.ISVR
<schemaIncludes>
<schemaInclude>svrl.xsd</schemaInclude>
</schemaIncludes>
<catalog>${basedir}/src/main/jaxb/catalog.txt</catalog>
<generateDirectory>${project.build.directory}/generated-sources/svrl</generateDirectory>
<staleFile>${project.build.directory}/stale/.stale-svrl</staleFile>
</configuration>
</execution>
</executions>
<configuration>
<extension>true</extension>
<strict>false</strict>
<args>
<arg>-no-header</arg>
<arg>-Xph-default-locale</arg>
Expand All @@ -149,6 +155,12 @@ osgi.serviceloader; filter:="(osgi.serviceloader=com.helger.schematron.svrl.ISVR
<arg>-Xph-csu</arg>
<arg>-Xph-cloneable2</arg>
</args>
<episodes>
<episode>
<groupId>com.helger.xsd</groupId>
<artifactId>ph-xsds-xml</artifactId>
</episode>
</episodes>
</configuration>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.helger.commons.annotation.PresentForCodeCoverage;
import com.helger.commons.collection.impl.CommonsArrayList;
import com.helger.commons.io.resource.ClassPathResource;
import com.helger.xsds.xml.CXML_XSD;

/**
* SVRL constants.
Expand All @@ -44,8 +45,9 @@ private static ClassLoader _getCL ()
public static final String SVRL_XSD_PATH = "schemas/svrl.xsd";

@CodingStyleguideUnaware
public static final List <ClassPathResource> SVRL_XSDS = new CommonsArrayList <> (new ClassPathResource (SVRL_XSD_PATH,
_getCL ())).getAsUnmodifiable ();
public static final List <ClassPathResource> SVRL_XSDS = new CommonsArrayList <> (CXML_XSD.getXSDResource (),
new ClassPathResource (SVRL_XSD_PATH, _getCL ()))
.getAsUnmodifiable ();

/** Path to the SVRL RelaxNG Compact file within the class path */
public static final String SVRL_RNC_PATH = "schemas/svrl.rnc";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public SVRLFailedAssert (@Nonnull final FailedAssert aFailedAssert,
@Nonnull final Function <? super FailedAssert, String> aLocationProvider,
@Nonnull final Function <? super FailedAssert, ? extends IErrorLevel> aErrLevelProvider)
{
super (aFailedAssert.getDiagnosticReference (),
super (SVRLHelper.getAllDiagnosticReferences (aFailedAssert),
aFailedAssert.getId (),
SVRLHelper.getAsString (aFailedAssert.getText ()),
SVRLHelper.getAsString (SVRLHelper.getText (aFailedAssert)),
aLocationProvider.apply (aFailedAssert),
aFailedAssert.getTest (),
aFailedAssert.getRole (),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.PresentForCodeCoverage;
import com.helger.commons.annotation.ReturnsMutableCopy;
import com.helger.commons.collection.CollectionHelper;
import com.helger.commons.collection.impl.CommonsArrayList;
import com.helger.commons.collection.impl.ICommonsList;
import com.helger.commons.concurrent.SimpleReadWriteLock;
import com.helger.commons.error.level.IErrorLevel;
import com.helger.commons.regex.RegExHelper;
import com.helger.commons.string.StringHelper;
import com.helger.schematron.svrl.jaxb.DiagnosticReference;
import com.helger.schematron.svrl.jaxb.FailedAssert;
import com.helger.schematron.svrl.jaxb.PropertyReference;
import com.helger.schematron.svrl.jaxb.SchematronOutputType;
import com.helger.schematron.svrl.jaxb.SuccessfulReport;
import com.helger.schematron.svrl.jaxb.Text;
Expand Down Expand Up @@ -264,4 +267,48 @@ public static String getBeautifiedLocation (@Nonnull final String sLocation)
}
return sResult;
}

@Nonnull
public static ICommonsList <DiagnosticReference> getAllDiagnosticReferences (@Nonnull final FailedAssert aFA)
{
return CommonsArrayList.createFiltered (aFA.getDiagnosticReferenceOrPropertyReferenceOrText (),
x -> x instanceof DiagnosticReference,
(final Object x) -> (DiagnosticReference) x);
}

@Nonnull
public static ICommonsList <PropertyReference> getAllPropertyReferences (@Nonnull final FailedAssert aFA)
{
return CommonsArrayList.createFiltered (aFA.getDiagnosticReferenceOrPropertyReferenceOrText (),
x -> x instanceof PropertyReference,
(final Object x) -> (PropertyReference) x);
}

@Nonnull
public static Text getText (@Nonnull final FailedAssert aFA)
{
return CollectionHelper.findFirstMapped (aFA.getDiagnosticReferenceOrPropertyReferenceOrText (), x -> x instanceof Text, x -> (Text) x);
}

@Nonnull
public static ICommonsList <DiagnosticReference> getAllDiagnosticReferences (@Nonnull final SuccessfulReport aSR)
{
return CommonsArrayList.createFiltered (aSR.getDiagnosticReferenceOrPropertyReferenceOrText (),
x -> x instanceof DiagnosticReference,
(final Object x) -> (DiagnosticReference) x);
}

@Nonnull
public static ICommonsList <PropertyReference> getAllPropertyReferences (@Nonnull final SuccessfulReport aSR)
{
return CommonsArrayList.createFiltered (aSR.getDiagnosticReferenceOrPropertyReferenceOrText (),
x -> x instanceof PropertyReference,
(final Object x) -> (PropertyReference) x);
}

@Nonnull
public static Text getText (@Nonnull final SuccessfulReport aSR)
{
return CollectionHelper.findFirstMapped (aSR.getDiagnosticReferenceOrPropertyReferenceOrText (), x -> x instanceof Text, x -> (Text) x);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public SVRLSuccessfulReport (@Nonnull final SuccessfulReport aSuccessfulReport,
@Nonnull final Function <? super SuccessfulReport, String> aLocationProvider,
@Nonnull final Function <? super SuccessfulReport, ? extends IErrorLevel> aErrLevelProvider)
{
super (aSuccessfulReport.getDiagnosticReference (),
super (SVRLHelper.getAllDiagnosticReferences (aSuccessfulReport),
aSuccessfulReport.getId (),
SVRLHelper.getAsString (aSuccessfulReport.getText ()),
SVRLHelper.getAsString (SVRLHelper.getText (aSuccessfulReport)),
aLocationProvider.apply (aSuccessfulReport),
aSuccessfulReport.getTest (),
aSuccessfulReport.getRole (),
Expand Down
1 change: 1 addition & 0 deletions ph-schematron-api/src/main/jaxb/catalog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PUBLIC "http://www.w3.org/XML/1998/namespace" "maven:com.helger.xsd:ph-xsds-xml:jar::!/schemas/xml.xsd"
25 changes: 15 additions & 10 deletions ph-schematron-api/src/main/resources/schemas/svrl.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://purl.oclc.org/dsdl/svrl" xmlns:svrl="http://purl.oclc.org/dsdl/svrl">

<xs:import namespace="http://www.w3.org/XML/1998/namespace" />

<!-- The root element -->
<xs:element name="schematron-output" type="svrl:SchematronOutputType" />

Expand Down Expand Up @@ -75,33 +78,35 @@
</xs:element>
<!-- only references are reported, not the diagnostic -->
<xs:element name="diagnostic-reference">
<xs:complexType>
<xs:complexType mixed="true">
<xs:sequence>
<xs:element ref="svrl:text"/>
<xs:element ref="svrl:text" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="diagnostic" use="required" type="xs:NMTOKEN"/>
<xs:attribute ref="xml:lang" use="optional" />
</xs:complexType>
</xs:element>
<!-- only failed assertions are reported -->
<xs:element name="failed-assert">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="svrl:diagnostic-reference"/>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="svrl:property-reference"/>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="svrl:diagnostic-reference"/>
<xs:element ref="svrl:property-reference"/>
<xs:element ref="svrl:text"/>
<!-- Note: XSLT based version may emit the diagnostic-reference here :( -->
</xs:sequence>
</xs:choice>
<xs:attributeGroup ref="svrl:attlist.assert-and-report"/>
</xs:complexType>
</xs:element>
<!-- only successful asserts are reported -->
<xs:element name="successful-report">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="svrl:diagnostic-reference"/>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="svrl:property-reference"/>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="svrl:diagnostic-reference"/>
<xs:element ref="svrl:property-reference"/>
<xs:element ref="svrl:text"/>
</xs:sequence>
<!-- Note: XSLT based version may emit the diagnostic-reference here :( -->
</xs:choice>
<xs:attributeGroup ref="svrl:attlist.assert-and-report"/>
</xs:complexType>
</xs:element>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private Text _getErrorText (@Nonnull final List <PSXPathBoundElement> aBoundCont
* @throws SchematronValidationException
*/
private void _handleDiagnosticReferences (@Nullable final List <String> aSrcDiagnostics,
@Nonnull final List <DiagnosticReference> aDstList,
@Nonnull final List <? super DiagnosticReference> aDstList,
@Nonnull final PSXPathBoundAssertReport aBoundAssertReport,
@Nonnull final Node aRuleMatchingNode) throws SchematronValidationException
{
Expand All @@ -306,7 +306,7 @@ private void _handleDiagnosticReferences (@Nullable final List <String> aSrcDiag
// Create the SVRL diagnostic-reference element
final DiagnosticReference aDR = new DiagnosticReference ();
aDR.setDiagnostic (sDiagnosticID);
aDR.setText (_getErrorText (aDiagnostic.getAllBoundContentElements (), aRuleMatchingNode));
aDR.getContent ().add (_getErrorText (aDiagnostic.getAllBoundContentElements (), aRuleMatchingNode));
aDstList.add (aDR);
}
}
Expand Down Expand Up @@ -341,9 +341,10 @@ public EContinue onFailedAssert (@Nonnull final PSAssertReport aAssertReport,
if (aAssertReport.hasLinkable ())
aFailedAssert.setRole (aAssertReport.getLinkable ().getRole ());
aFailedAssert.setTest (sTestExpression);
aFailedAssert.setText (_getErrorText (aBoundAssertReport.getAllBoundContentElements (), aRuleMatchingNode));
aFailedAssert.getDiagnosticReferenceOrPropertyReferenceOrText ()
.add (_getErrorText (aBoundAssertReport.getAllBoundContentElements (), aRuleMatchingNode));
_handleDiagnosticReferences (aAssertReport.getAllDiagnostics (),
aFailedAssert.getDiagnosticReference (),
aFailedAssert.getDiagnosticReferenceOrPropertyReferenceOrText (),
aBoundAssertReport,
aRuleMatchingNode);
m_aSchematronOutput.addActivePatternAndFiredRuleAndFailedAssert (aFailedAssert);
Expand All @@ -369,9 +370,10 @@ public EContinue onSuccessfulReport (@Nonnull final PSAssertReport aAssertReport
if (aAssertReport.hasLinkable ())
aSuccessfulReport.setRole (aAssertReport.getLinkable ().getRole ());
aSuccessfulReport.setTest (sTestExpression);
aSuccessfulReport.setText (_getErrorText (aBoundAssertReport.getAllBoundContentElements (), aRuleMatchingNode));
aSuccessfulReport.getDiagnosticReferenceOrPropertyReferenceOrText ()
.add (_getErrorText (aBoundAssertReport.getAllBoundContentElements (), aRuleMatchingNode));
_handleDiagnosticReferences (aAssertReport.getAllDiagnostics (),
aSuccessfulReport.getDiagnosticReference (),
aSuccessfulReport.getDiagnosticReferenceOrPropertyReferenceOrText (),
aBoundAssertReport,
aRuleMatchingNode);
m_aSchematronOutput.addActivePatternAndFiredRuleAndFailedAssert (aSuccessfulReport);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void testIssue16 () throws Exception
for (final DiagnosticReference diagnisticRef : diagnisticReferences)
{
System.out.println ("Diag ref: " + diagnisticRef.getDiagnostic ());
System.out.println ("Diag text: " + diagnisticRef.getText ());
System.out.println ("Diag text: " + diagnisticRef.getContentAtIndex (0));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (C) 2014-2020 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.supplementary;

import static org.junit.Assert.assertNotNull;

import java.io.File;

import javax.annotation.Nonnull;

import org.junit.Test;

import com.helger.commons.io.resource.FileSystemResource;
import com.helger.schematron.sch.SchematronResourceSCH;
import com.helger.schematron.svrl.SVRLMarshaller;
import com.helger.schematron.svrl.jaxb.SchematronOutputType;
import com.helger.xml.serialize.write.XMLWriter;

public final class Issue085Test
{
@Test
public void testIssue () throws Exception
{
validateAndProduceSVRL (new File ("src/test/resources/issues/github85/schematron.sch"),
new File ("src/test/resources/issues/github85/test.xml"));
}

public static void validateAndProduceSVRL (@Nonnull final File aSchematron, final File aXML) throws Exception
{
final SchematronResourceSCH aSCH = SchematronResourceSCH.fromFile (aSchematron);

if (false)
System.out.println (XMLWriter.getNodeAsString (aSCH.getXSLTProvider ().getXSLTDocument ()));

// Perform validation
final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL (new FileSystemResource (aXML));
assertNotNull (aSVRL);
if (false)
System.out.println (new SVRLMarshaller ().getAsString (aSVRL));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<sch:schema xmlns="http://purl.oclc.org/dsdl/schematron" xmlns:sch="http://purl.oclc.org/dsdl/schematron"
queryBinding="xslt2" schemaVersion="ISO19757-3">
<sch:title>ISO schematron validation file for descriptive extended constraints</sch:title>

<sch:pattern>
<sch:rule context="/">
<sch:report test="true()" diagnostics="d1" />
</sch:rule>
</sch:pattern>
<sch:diagnostics>
<sch:diagnostic id="d1" xml:lang="de">Foobar</sch:diagnostic>
</sch:diagnostics>

</sch:schema>
20 changes: 20 additions & 0 deletions ph-schematron-xslt/src/test/resources/issues/github85/test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<file>
<Customer>
<firstname> abc</firstname>
<ContactDetails>
<mobile>12345</mobile>
<mobile></mobile>
</ContactDetails>
<ContactDetails>
<mobile>12345</mobile>
<mobile></mobile>
</ContactDetails>
</Customer>
<Customer>
<firstname> abc</firstname>
<ContactDetails>
<mobile></mobile>
</ContactDetails>
</Customer>
</file>
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.helger.xsd</groupId>
<artifactId>ph-xsds-parent-pom</artifactId>
<version>2.4.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
Expand Down

0 comments on commit 0dbb027

Please sign in to comment.