Skip to content

Commit

Permalink
Serializing and restoring ValidationSource in ValidationResultList
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Dec 5, 2024
1 parent a991e6a commit 53a2b8d
Show file tree
Hide file tree
Showing 14 changed files with 487 additions and 34 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ Please ensure that your stack size is at least 1MB (for Saxon). Using the Oracle
* Added new class `ValidityDeterminatorRegistry` to support validity determination processes
* Added optional `IValidationSource` to `ValidationResultList`
* Changed parameter types in `PhiveJsonHelper` and `PhiveXMLHelper` from `List <? extends ValidationResult>` to `ValidationResultList`
* Added new class `ValidationSourceXMLReadableResource`
* Extended `IValidationSource` interface to make content serializable
* v10.0.3 - 2024-12-01
* Added default XML serialization of validation results
* Added new `EValidationBaseType` entries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@
*/
package com.helger.phive.api.source;

import java.io.IOException;
import java.io.OutputStream;

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

import com.helger.commons.annotation.Nonempty;
import com.helger.commons.string.StringHelper;

/**
* Abstract validation source interface. This represents an object to be
Expand All @@ -26,6 +34,24 @@
*/
public interface IValidationSource
{
/**
* @return The validation source type ID. Neither <code>null</code> nor empty.
* @since 10.1.0
*/
@Nonnull
@Nonempty
String getValidationSourceTypeID ();

/**
* @return <code>true</code> if a system ID is present, <code>false</code> if
* not.
* @since 10.1.0
*/
default boolean hasSystemID ()
{
return StringHelper.hasText (getSystemID ());
}

/**
* @return The system ID (e.g. filename) of the source to be validated. May be
* <code>null</code>.
Expand All @@ -39,4 +65,15 @@ public interface IValidationSource
* be a way to define the necessary part(s) in the implementation.
*/
boolean isPartialSource ();

/**
* Write the content of the validation source to the provided OutputStream.
*
* @param aOS
* The output stream to write to. May not be <code>null</code>.
* @throws IOException
* In case writing fails
* @since 10.1.0
*/
void writeTo (@Nonnull @WillNotClose OutputStream aOS) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*/
public interface IValidationSourceBinary extends IValidationSource
{
String VALIDATION_SOURCE_TYPE = "binary";

/**
* @return The bytes to be validated. May be <code>null</code>.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
*/
package com.helger.phive.api.source;

import java.io.IOException;
import java.io.OutputStream;

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

import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.io.ByteArrayWrapper;
import com.helger.commons.string.ToStringGenerator;

Expand All @@ -35,14 +40,23 @@ public class ValidationSourceBinary implements IValidationSourceBinary
private final boolean m_bPartialSource;
private final ByteArrayWrapper m_aBAW;

protected ValidationSourceBinary (@Nullable final String sSystemID, @Nonnull final ByteArrayWrapper aBAW, final boolean bPartialSource)
protected ValidationSourceBinary (@Nullable final String sSystemID,
@Nonnull final ByteArrayWrapper aBAW,
final boolean bPartialSource)
{
ValueEnforcer.notNull (aBAW, "BAW");
m_sSystemID = sSystemID;
m_aBAW = aBAW;
m_bPartialSource = bPartialSource;
}

@Nonnull
@Nonempty
public String getValidationSourceTypeID ()
{
return VALIDATION_SOURCE_TYPE;
}

@Nullable
public String getSystemID ()
{
Expand All @@ -60,6 +74,12 @@ public ByteArrayWrapper getBytes ()
return m_aBAW;
}

public void writeTo (@Nonnull @WillNotClose final OutputStream aOS) throws IOException
{
// Just forward
m_aBAW.writeTo (aOS);
}

@Override
public String toString ()
{
Expand All @@ -84,4 +104,22 @@ public static ValidationSourceBinary create (@Nullable final String sSystemID, @
ValueEnforcer.notNull (aBytes, "Bytes");
return new ValidationSourceBinary (sSystemID, new ByteArrayWrapper (aBytes, false), false);
}

/**
* Create a partial validation source from an existing byte array.
*
* @param sSystemID
* System ID to use. May be <code>null</code>.
* @param aBytes
* The bytes to use. May not be <code>null</code>.
* @return Never <code>null</code>.
* @since 10.1.0
*/
@Nonnull
public static ValidationSourceBinary createPartial (@Nullable final String sSystemID, @Nonnull final byte [] aBytes)
{
ValueEnforcer.notNull (aBytes, "Bytes");
return new ValidationSourceBinary (sSystemID, new ByteArrayWrapper (aBytes, false), true);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.result;

import javax.annotation.Nullable;

import com.helger.phive.api.source.IValidationSource;

/**
* Callback interface to restore a {@link IValidationSource} from deserialized
* parameters.
*
* @author Philip Helger
* @since 10.1.0
*/
public interface IValidationSourceRestorer
{
@Nullable
IValidationSource restoreValidationSource (@Nullable String sValidationSourceTypeID,
@Nullable String sSystemID,
boolean bIsPartialSource,
@Nullable byte [] aPayloadBytes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.error.level.EErrorLevel;
Expand All @@ -34,6 +37,12 @@
import com.helger.commons.io.resource.wrapped.IWrappedReadableResource;
import com.helger.commons.state.ETriState;
import com.helger.commons.string.StringHelper;
import com.helger.phive.api.source.IValidationSource;
import com.helger.phive.api.source.IValidationSourceBinary;
import com.helger.phive.api.source.ValidationSourceBinary;
import com.helger.phive.xml.source.IValidationSourceXML;
import com.helger.phive.xml.source.ValidationSourceXML;
import com.helger.xml.serialize.read.DOMReader;

/**
* Contains stateless phive result helper methods.
Expand All @@ -51,6 +60,8 @@ public final class PhiveResultHelper
public static final String VALUE_TRISTATE_FALSE = "FALSE";
public static final String VALUE_TRISTATE_UNDEFINED = "UNDEFINED";

private static final Logger LOGGER = LoggerFactory.getLogger (PhiveResultHelper.class);

private PhiveResultHelper ()
{}

Expand Down Expand Up @@ -198,4 +209,29 @@ public static IReadableResource getAsValidationResource (@Nullable final String
// Default to class path
return new ClassPathResource (sArtefactPath);
}

@Nullable
public static IValidationSource createValidationSource (@Nullable final String sValidationSourceTypeID,
@Nullable final String sSystemID,
final boolean bIsPartialSource,
@Nullable final byte [] aPayloadBytes)
{
if (StringHelper.hasNoText (sValidationSourceTypeID))
return null;
if (aPayloadBytes == null)
return null;

switch (sValidationSourceTypeID)
{
case IValidationSourceBinary.VALIDATION_SOURCE_TYPE:
return bIsPartialSource ? ValidationSourceBinary.createPartial (sSystemID, aPayloadBytes)
: ValidationSourceBinary.create (sSystemID, aPayloadBytes);
case IValidationSourceXML.VALIDATION_SOURCE_TYPE:
// Parse on demand only
return new ValidationSourceXML (sSystemID, () -> DOMReader.readXMLDOM (aPayloadBytes), bIsPartialSource);
default:
LOGGER.warn ("Unsupported Validation Source Type ID '" + sValidationSourceTypeID + "'");
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.helger.phive.api.executorset.IValidationExecutorSet;
import com.helger.phive.api.result.ValidationResult;
import com.helger.phive.api.result.ValidationResultList;
import com.helger.phive.api.source.IValidationSource;
import com.helger.phive.api.validity.EExtendedValidity;
import com.helger.phive.result.PhiveResultHelper;

Expand All @@ -50,6 +51,9 @@
*/
public class JsonValidationResultListHelper
{
// By default, include source content
private Function <IValidationSource, IJsonObject> m_aSourceToJson = vs -> PhiveJsonHelper.getJsonValidationSource (vs,
true);
private IValidationExecutorSet <?> m_aVES;
private Function <IValidationExecutorSet <?>, IJsonObject> m_aVESToJson = PhiveJsonHelper::getJsonVES;
private Function <IReadableResource, String> m_aArtifactPathTypeToJson = PhiveResultHelper::getArtifactPathType;
Expand All @@ -61,6 +65,13 @@ public class JsonValidationResultListHelper
public JsonValidationResultListHelper ()
{}

@Nonnull
public JsonValidationResultListHelper sourceToJson (@Nullable final Function <IValidationSource, IJsonObject> a)
{
m_aSourceToJson = a;
return this;
}

@Nonnull
public JsonValidationResultListHelper ves (@Nullable final IValidationExecutorSet <?> a)
{
Expand Down Expand Up @@ -154,6 +165,13 @@ public void applyTo (@Nonnull final IJsonObject aResponse,
ValueEnforcer.notNull (aDisplayLocale, "DisplayLocale");
ValueEnforcer.isGE0 (nDurationMilliseconds, "DurationMilliseconds");

// Added in 10.1.0
if (aValidationResultList.hasValidationSource () && m_aSourceToJson != null)
{
aResponse.addIfNotNull (PhiveJsonHelper.JSON_VALIDATION_SOURCE,
m_aSourceToJson.apply (aValidationResultList.getValidationSource ()));
}

if (m_aVES != null && m_aVESToJson != null)
aResponse.addIfNotNull (PhiveJsonHelper.JSON_VES, m_aVESToJson.apply (m_aVES));

Expand Down
Loading

0 comments on commit 53a2b8d

Please sign in to comment.