Skip to content

Commit

Permalink
Continued refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Sep 16, 2024
1 parent 47b2cee commit e287856
Show file tree
Hide file tree
Showing 25 changed files with 199 additions and 120 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ Please ensure that your stack size is at least 1MB (for Saxon). Using the Oracle
* Each `IValidationExecutorManager` now has an `IValidityDeterminator`
* By using `IValidityDeterminator.getDefault()` the previous state can be re-created
* Renamed method `IValidationArtefact.getValidationArtefactType` to `getValidationType`
* Moved classes `(Abstract|I)ValidationExecutor` to its own `.executor` package
* Renamed class `TestFile` to `PhiveTestFile`
* v9.2.2 - 2024-07-29
* Switched from custom error level to `CustomErrorDetails`
* v9.2.1 - 2024-04-25
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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.phive.api;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.helger.commons.annotation.Nonempty;
import com.helger.commons.id.IHasID;
import com.helger.commons.lang.EnumHelper;
import com.helger.commons.name.IHasName;

/**
* Enum with all predefined validation based types.
*
* @author Philip Helger
*/
public enum EValidationBaseType implements IHasID <String>, IHasName
{
XML ("xml", "XML"),
XSD ("xsd", "XML Schema"),
SCHEMATRON ("sch", "Schematron"),
EDIFACT ("edifact", "EDIFACT");

private final String m_sID;
private final String m_sName;

EValidationBaseType (@Nonnull @Nonempty final String sID, @Nonnull @Nonempty final String sName)
{
m_sID = sID;
m_sName = sName;
}

@Nonnull
@Nonempty
public String getID ()
{
return m_sID;
}

@Nonnull
@Nonempty
public String getName ()
{
return m_sName;
}

public boolean isXML ()
{
return this == XML;
}

public boolean isXSD ()
{
return this == XSD;
}

public boolean isSchematron ()
{
return this == SCHEMATRON;
}

public boolean isEdifact ()
{
return this == EDIFACT;
}

@Nullable
public static EValidationBaseType getFromIDOrNull (@Nullable final String sID)
{
return EnumHelper.getFromIDOrNull (EValidationBaseType.class, sID);
}
}
53 changes: 20 additions & 33 deletions phive-api/src/main/java/com/helger/phive/api/EValidationType.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,53 @@ public enum EValidationType implements IValidationType
* Validate XML syntax by parsing without assigned XSDs. This is the
* wellformedness check.
*/
XML ("xml", "XML Syntax"),
XML ("xml", EValidationBaseType.XML, "XML Syntax"),
/** Validate XML against the rules of an XML Schema (XSD) */
XSD ("xsd", "XML Schema"),
XSD ("xsd", EValidationBaseType.XSD, "XML Schema"),
/**
* Validate part of an XML against the rules of an XML Schema (XSD) - e.g. for
* extension/plugins. The context object needed for this type is an
* <code>ValidationExecutorXSDPartial.ContextData</code>.
*/
PARTIAL_XSD ("partial-xsd", "Partial XML Schema"),
PARTIAL_XSD ("partial-xsd", EValidationBaseType.XSD, "Partial XML Schema"),
/**
* Pure Java implementation of Schematron - can only handle XPath 2 (was
* originally called SCHEMATRON)
*/
SCHEMATRON_PURE ("schematron-pure", "Schematron (pure; XPath-only)"),
SCHEMATRON_PURE ("schematron-pure", EValidationBaseType.SCHEMATRON, "Schematron (pure; XPath-only)"),
/**
* Schematron implementation that must convert the SCH to XSLT before
* validation
*/
SCHEMATRON_SCH ("schematron-sch", "Schematron (SCH)"),
SCHEMATRON_SCH ("schematron-sch", EValidationBaseType.SCHEMATRON, "Schematron (SCH; ISO XSLT2)"),
/**
* Schematron validation with a pre-build XSLT file (e.g. from the Maven
* plugin)
*
* @since 7.0.0
*/
SCHEMATRON_SCHXSLT ("schematron-schxslt-xslt2", "Schematron (SchXslt XSLT2)"),
SCHEMATRON_SCHXSLT ("schematron-schxslt-xslt2", EValidationBaseType.SCHEMATRON, "Schematron (SchXslt XSLT2)"),
/**
* Schematron validation with a pre-build XSLT file (e.g. from the Maven
* plugin)
*/
SCHEMATRON_XSLT ("schematron-xslt", "Schematron (ISO XSLT2)"),
SCHEMATRON_XSLT ("schematron-xslt", EValidationBaseType.SCHEMATRON, "Schematron (ISO XSLT2)"),
/**
* Schematron validation with a pre-build XSLT file (e.g. from the Maven
* plugin) with different output (for OIOUBL only)
*/
SCHEMATRON_OIOUBL ("schematron-xslt-oioubl", "Schematron (OIOUBL XSLT)");
SCHEMATRON_OIOUBL ("schematron-xslt-oioubl", EValidationBaseType.SCHEMATRON, "Schematron (OIOUBL XSLT)");

private final String m_sID;
private final EValidationBaseType m_eBaseType;
private final String m_sName;

EValidationType (@Nonnull @Nonempty final String sID, @Nonnull @Nonempty final String sName)
EValidationType (@Nonnull @Nonempty final String sID,
@Nonnull final EValidationBaseType eBaseType,
@Nonnull @Nonempty final String sName)
{
m_sID = sID;
m_eBaseType = eBaseType;
m_sName = sName;
}

Expand All @@ -88,38 +92,21 @@ public String getID ()
}

@Nonnull
@Nonempty
public String getName ()
{
return m_sName;
}

/**
* @return <code>true</code> if this is an XML validation. For XSD and
* Schematron this will return <code>false</code>.
*/
public boolean isXML ()
public EValidationBaseType getBaseType ()
{
return this == XML;
return m_eBaseType;
}

public boolean isXSD ()
{
return this == XSD || this == PARTIAL_XSD;
}

public boolean isSchematron ()
@Nonnull
@Nonempty
public String getName ()
{
return this == SCHEMATRON_PURE ||
this == SCHEMATRON_SCH ||
this == SCHEMATRON_SCHXSLT ||
this == SCHEMATRON_XSLT ||
this == SCHEMATRON_OIOUBL;
return m_sName;
}

public boolean isStopValidationOnError ()
{
return isXML () || isXSD ();
return m_eBaseType.isXML () || m_eBaseType.isXSD ();
}

public boolean isContextRequired ()
Expand Down
18 changes: 5 additions & 13 deletions phive-api/src/main/java/com/helger/phive/api/IValidationType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.helger.phive.api;

import javax.annotation.Nonnull;

import com.helger.commons.annotation.MustImplementEqualsAndHashcode;
import com.helger.commons.id.IHasID;
import com.helger.commons.name.IHasName;
Expand All @@ -32,20 +34,10 @@
public interface IValidationType extends IHasID <String>, IHasName
{
/**
* @return <code>true</code> if this is an XML Schema validation.
*/
default boolean isXSD ()
{
return false;
}

/**
* @return <code>true</code> if this is a Schematron validation.
* @return The validation based type. Never <code>null</code>.
*/
default boolean isSchematron ()
{
return false;
}
@Nonnull
EValidationBaseType getBaseType ();

/**
* @return <code>true</code> to stop validation if an error occurs when using
Expand Down
13 changes: 13 additions & 0 deletions phive-api/src/main/java/com/helger/phive/api/ValidationType.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,22 @@
public class ValidationType implements IValidationType
{
private final String m_sID;
private final EValidationBaseType m_eBaseType;
private final String m_sName;
private final boolean m_bStopValidationOnError;
private final boolean m_bContextRequired;

public ValidationType (@Nonnull @Nonempty final String sID,
@Nonnull final EValidationBaseType eBaseType,
@Nonnull @Nonempty final String sName,
final boolean bStopValidationOnError,
final boolean bContextRequired)
{
ValueEnforcer.notEmpty (sID, "ID");
ValueEnforcer.notNull (eBaseType, "BaseType");
ValueEnforcer.notEmpty (sName, "Name");
m_sID = sID;
m_eBaseType = eBaseType;
m_sName = sName;
m_bStopValidationOnError = bStopValidationOnError;
m_bContextRequired = bContextRequired;
Expand All @@ -58,6 +62,12 @@ public String getID ()
return m_sID;
}

@Nonnull
public EValidationBaseType getBaseType ()
{
return m_eBaseType;
}

@Nonnull
@Nonempty
public String getName ()
Expand All @@ -84,6 +94,7 @@ public boolean equals (final Object o)
return false;
final ValidationType rhs = (ValidationType) o;
return m_sID.equals (rhs.m_sID) &&
m_eBaseType.equals (rhs.m_eBaseType) &&
m_sName.equals (rhs.m_sName) &&
m_bStopValidationOnError == rhs.m_bStopValidationOnError &&
m_bContextRequired == rhs.m_bContextRequired;
Expand All @@ -93,6 +104,7 @@ public boolean equals (final Object o)
public int hashCode ()
{
return new HashCodeGenerator (this).append (m_sID)
.append (m_eBaseType)
.append (m_sName)
.append (m_bStopValidationOnError)
.append (m_bContextRequired)
Expand All @@ -103,6 +115,7 @@ public int hashCode ()
public String toString ()
{
return new ToStringGenerator (this).append ("ID", m_sID)
.append ("BaseType", m_eBaseType)
.append ("Name", m_sName)
.append ("StopValidationOnError", m_bStopValidationOnError)
.append ("ContextRequired", m_bContextRequired)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
import javax.annotation.Nonnull;

/**
* Nested interface for validation executors that support caching. Each
* implementation of IValidationExecutor is free to also implement this
* interface.
* Interface for validation executors that support caching. Each implementation
* of IValidationExecutor is free to also implement this interface.
*
* @author Philip Helger
* @since 3.1.1
*/
public interface IValidationExecutorWithCacheSupport
public interface IValidationExecutorCacheSupport
{
/** By default caching should be enabled. */
boolean DEFAULT_CACHE = true;

/**
* @return <code>true</code> if caching is enabled, <code>false</code> if
* not.
* @return <code>true</code> if caching is enabled, <code>false</code> if not.
*/
boolean isCacheArtefact ();

Expand All @@ -30,12 +28,12 @@ public interface IValidationExecutorWithCacheSupport
* @return this for chaining
*/
@Nonnull
IValidationExecutorWithCacheSupport setCacheArtefact (boolean bCacheArtefact);
IValidationExecutorCacheSupport setCacheArtefact (boolean bCacheArtefact);

/**
* If caching of this artefact is enabled, ensure it is in the cache.
*
* @since 7.1.1
*/
void ensureItemIsInCache ();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.helger.commons.collection.impl.ICommonsIterable;
import com.helger.commons.collection.impl.ICommonsList;
import com.helger.commons.state.EValidity;
import com.helger.phive.api.executor.IValidationExecutor;
import com.helger.phive.api.executorset.IValidationExecutorSet;
import com.helger.phive.api.result.ValidationResult;
import com.helger.phive.api.result.ValidationResultList;
Expand Down Expand Up @@ -167,10 +168,12 @@ public void executeValidation (@Nonnull final SOURCETYPE aSource,
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Executing validation on source " + aSource + (aLocale == null ? "" : " and locale " + aLocale));

boolean bIgnoreRest = false;
boolean bSkipRest = false;

// Run over all executors
for (final IValidationExecutor <SOURCETYPE> aExecutor : getAllExecutors ())
{
if (bIgnoreRest)
if (bSkipRest)
{
// Ignore executor because of previous failures
aValidationResults.add (ValidationResult.createSkippedResult (aExecutor.getValidationArtefact ()));
Expand All @@ -182,11 +185,12 @@ public void executeValidation (@Nonnull final SOURCETYPE aSource,
assert aResult != null;
aValidationResults.add (aResult);

// Determine validity
final EExtendedValidity eValidity = m_aValidityDeterminator.getValidity (aExecutor, aResult.getErrorList ());
if (eValidity.isInvalid () && aExecutor.isStopValidationOnError ())
{
// Ignore all following executors
bIgnoreRest = true;
bSkipRest = true;
}
}
}
Expand All @@ -202,6 +206,8 @@ public EValidity executeFastValidation (@Nonnull final SOURCETYPE aSource)
// Execute validation
// Note: locale doesn't matter because we don't use the texts
final ValidationResult aResult = aExecutor.applyValidation (aSource, (Locale) null);

// Determine validity
final EExtendedValidity eValidity = m_aValidityDeterminator.getValidity (aExecutor, aResult.getErrorList ());
if (eValidity.isInvalid ())
{
Expand Down
Loading

0 comments on commit e287856

Please sign in to comment.