Skip to content

Commit

Permalink
Added error role customizing; #66
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Apr 12, 2018
1 parent 15d9374 commit 9bea2e3
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.helger.commons.error.ErrorTextProvider;
import com.helger.commons.error.IError;
import com.helger.commons.error.level.EErrorLevel;
import com.helger.commons.error.level.IErrorLevel;
import com.helger.commons.error.list.IErrorList;
import com.helger.commons.io.resource.FileSystemResource;
import com.helger.commons.string.StringHelper;
Expand All @@ -52,6 +53,7 @@
import com.helger.schematron.pure.SchematronResourcePure;
import com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler;
import com.helger.schematron.svrl.AbstractSVRLMessage;
import com.helger.schematron.svrl.DefaultSVRLErrorLevelDeterminator;
import com.helger.schematron.svrl.SVRLHelper;
import com.helger.schematron.svrl.SVRLMarshaller;
import com.helger.schematron.xslt.SchematronResourceSCH;
Expand All @@ -67,6 +69,32 @@
*/
public class Schematron extends Task
{
public class ErrorRole
{
private String m_sRole;

public ErrorRole ()
{}

public void setRole (@Nullable final String sRole)
{
m_sRole = sRole;
if (sRole != null)
log ("Using role '" + sRole + "' to trigger an error");
}

@Nullable
public String getRole ()
{
return m_sRole;
}

public boolean equalsIgnoreCase (@Nonnull final String sValue)
{
return sValue.equals (m_sRole);
}
}

/**
* The Schematron file. This may also be an XSLT file if it is precompiled.
*/
Expand Down Expand Up @@ -115,6 +143,12 @@ public class Schematron extends Task
*/
private boolean m_bExpectSuccess = true;

/**
* List of "role" attribute values that will trigger an error. If combined
* with "failOnError" it will break the build.
*/
private final ICommonsList <ErrorRole> m_aErrorRoles = new CommonsArrayList <> ();

/**
* <code>true</code> if the build should fail if any error occurs. Defaults to
* <code>true</code>. Since v5.0.0.
Expand Down Expand Up @@ -194,6 +228,14 @@ public void setExpectSuccess (final boolean bExpectSuccess)
Project.MSG_DEBUG);
}

@Nonnull
public ErrorRole createErrorRole ()
{
final ErrorRole aErrorRole = new ErrorRole ();
m_aErrorRoles.add (aErrorRole);
return aErrorRole;
}

public void setFailOnError (final boolean bFailOnError)
{
m_bFailOnError = bFailOnError;
Expand Down Expand Up @@ -457,7 +499,7 @@ public void execute () throws BuildException
else
if (m_eSchematronProcessingEngine == null)
_error ("An invalid Schematron processing instance is specified! Only one of the following values is allowed: " +
StringHelper.getImplodedMapped (", ", ESchematronMode.values (), x -> "'" + x.getID () + "'"));
StringHelper.getImplodedMapped (", ", ESchematronMode.values (), x -> "'" + x.getID () + "'"));
else
if (m_aResCollections.isEmpty ())
_error ("No XML resources to be validated specified! Add e.g. a <fileset> element.");
Expand All @@ -469,6 +511,30 @@ public void execute () throws BuildException

if (bCanRun)
{
// Set error level
if (m_aErrorRoles.isNotEmpty ())
{
// Set global default error level determinator
SVRLHelper.setErrorLevelDeterminator (new DefaultSVRLErrorLevelDeterminator ()
{
@Override
@Nonnull
public IErrorLevel getErrorLevelFromString (@Nullable final String sFlag)
{
if (sFlag != null)
{
// Check custom error roles; #66
for (final ErrorRole aCustomRole : m_aErrorRoles)
if (aCustomRole.equalsIgnoreCase (sFlag))
return EErrorLevel.ERROR;
}

// Fall back to default
return super.getErrorLevelFromString (sFlag);
}
});
}

// 1. Parse Schematron file
final Locale aDisplayLocale = Locale.US;
ISchematronResource aSch = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (C) 2017-2018 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.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class Issue66Test
{
private static final Logger s_aLogger = LoggerFactory.getLogger (Issue66Test.class);

@Rule
public final BuildFileRule m_aBuildRule = new BuildFileRule ();

@Before
public void init ()
{
m_aBuildRule.configureProject ("src/test/resources/issues/66/build.xml");
m_aBuildRule.getProject ().addBuildListener (new LoggingBuildListener ());
}

@Test
public void testWithExternalDTD ()
{
try
{
// Do not redirect stdout etc.
m_aBuildRule.getProject ().executeTarget ("schematron");
}
catch (final BuildException ex)
{
s_aLogger.error ("Ooops", ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tmp/
17 changes: 17 additions & 0 deletions ph-schematron-ant-task/src/test/resources/issues/66/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<taskdef name="schematron" classname="com.helger.schematron.ant.Schematron" />

<target name="schematron" description="Schematron rule tests">
<schematron svrlDirectory="tmp"
schematronProcessingEngine="pure"
schematronFile="rules.sch">
<fileset dir=".">
<include name="dog*.xml" />
</fileset>
<errorRole role="foo" />
<errorRole role="bar" />
</schematron>
</target>

</project>
4 changes: 4 additions & 0 deletions ph-schematron-ant-task/src/test/resources/issues/66/dog1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<dog xml:lang="de">
<fleas/>
</dog>
9 changes: 9 additions & 0 deletions ph-schematron-ant-task/src/test/resources/issues/66/rules.sch
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<schema xmlns="http://purl.oclc.org/dsdl/schematron" xml:lang="de">
<title>Example of a custom error role</title>
<pattern>
<rule context="dog">
<assert test="bone" role="bar"> A dog should have a bone.</assert>
<assert test="xyz" role="info">Information only that xyz should be present.</assert>
</rule>
</pattern>
</schema>

0 comments on commit 9bea2e3

Please sign in to comment.