use of com.helger.commons.location.SimpleLocation in project ph-schematron by phax.
the class PSReader method _warn.
/**
* Emit a warning with the registered error handler.
*
* @param aSourceElement
* The source element where the error occurred.
* @param sMessage
* The main warning message.
*/
private void _warn(@Nonnull final IPSElement aSourceElement, @Nonnull final String sMessage) {
ValueEnforcer.notNull(aSourceElement, "SourceElement");
ValueEnforcer.notNull(sMessage, "Message");
m_aErrorHandler.handleError(SingleError.builderWarn().errorLocation(new SimpleLocation(m_aResource.getPath())).errorFieldName(IPSErrorHandler.getErrorFieldName(aSourceElement)).errorText(sMessage).build());
}
use of com.helger.commons.location.SimpleLocation in project phive by phax.
the class ValidationExecutorSchematron method applyValidation.
@Nonnull
public ValidationResult applyValidation(@Nonnull final IValidationSourceXML aSource, @Nullable final Locale aLocale) {
ValueEnforcer.notNull(aSource, "Source");
final IValidationArtefact aArtefact = getValidationArtefact();
// Get source as XML DOM Node
Node aNode = null;
try {
aNode = SchematronResourceHelper.getNodeOfSource(aSource.getAsTransformSource(), new DOMReaderSettings().setFeatureValues(EXMLParserFeature.AVOID_XML_ATTACKS));
} catch (final Exception ex) {
throw new IllegalStateException("For Schematron validation to work, the source must be valid XML which it is not.", ex);
}
if (StringHelper.hasText(m_sPrerequisiteXPath)) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Using Schematron prerequisite path '" + m_sPrerequisiteXPath + "'");
// Check if the artefact can be applied on the given document by
// checking the prerequisite XPath
final XPath aXPathContext = XPathHelper.createNewXPath();
if (m_aNamespaceContext != null)
aXPathContext.setNamespaceContext(m_aNamespaceContext);
try {
final Boolean aResult = XPathExpressionHelper.evalXPathToBoolean(aXPathContext, m_sPrerequisiteXPath, XMLHelper.getOwnerDocument(aNode));
if (aResult != null && !aResult.booleanValue()) {
if (LOGGER.isInfoEnabled())
LOGGER.info("Ignoring validation artefact " + aArtefact.getRuleResourcePath() + " because the prerequisite XPath expression '" + m_sPrerequisiteXPath + "' is not fulfilled.");
return ValidationResult.createIgnoredResult(aArtefact);
}
} catch (final IllegalArgumentException ex) {
// Catch errors in prerequisite XPaths - most likely because of
// missing namespace prefixes...
final String sErrorMsg = "Failed to verify if validation artefact " + aArtefact.getRuleResourcePath() + " matches the prerequisite XPath expression '" + m_sPrerequisiteXPath + "' - ignoring validation artefact.";
LOGGER.error(sErrorMsg, ex);
return new ValidationResult(aArtefact, new ErrorList(SingleError.builderError().errorText(sErrorMsg).linkedException(ex).build()));
}
}
// No prerequisite or prerequisite matched
final ErrorList aErrorList = new ErrorList();
final Wrapper<ESchematronOutput> aOutput = new Wrapper<>(ESchematronOutput.SVRL);
final AbstractSchematronResource aSCH = _createSchematronResource(aLocale, aErrorList, aOutput::set);
// Don't cache to avoid that errors in the Schematron are hidden on
// consecutive calls!
aSCH.setUseCache(m_bCacheSchematron);
try {
// Main application of Schematron
final Document aDoc = aSCH.applySchematronValidation(new DOMSource(aNode));
if (LOGGER.isDebugEnabled())
LOGGER.debug("SVRL: " + XMLWriter.getNodeAsString(aDoc));
switch(aOutput.get()) {
case SVRL:
{
final SchematronOutputType aSVRL = aDoc == null || aDoc.getDocumentElement() == null ? null : new SVRLMarshaller().read(aDoc);
if (aSVRL != null) {
// Convert failed asserts and successful reports to error objects
for (final SVRLFailedAssert aFailedAssert : SVRLHelper.getAllFailedAssertions(aSVRL)) aErrorList.add(aFailedAssert.getAsResourceError(aSource.getSystemID()));
for (final SVRLSuccessfulReport aSuccessfulReport : SVRLHelper.getAllSuccessfulReports(aSVRL)) aErrorList.add(aSuccessfulReport.getAsResourceError(aSource.getSystemID()));
} else {
// Schematron does not create SVRL!
LOGGER.warn("Failed to read the result as SVRL:" + (aDoc != null ? "\n" + XMLWriter.getNodeAsString(aDoc) : " no XML Document created"));
aErrorList.add(SingleError.builderError().errorLocation(aArtefact.getRuleResourcePath()).errorText("Internal error interpreting Schematron result").errorFieldName(aDoc != null ? XMLWriter.getNodeAsString(aDoc) : null).build());
}
break;
}
case OIOUBL:
{
if (aDoc != null && aDoc.getDocumentElement() != null) {
for (final Element eError : XMLHelper.getChildElementIterator(aDoc.getDocumentElement(), "Error")) {
// final String sContext = eError.getAttribute ("context");
final String sPattern = XMLHelper.getFirstChildElementOfName(eError, "Pattern").getTextContent();
final String sDescription = XMLHelper.getFirstChildElementOfName(eError, "Description").getTextContent();
final String sXPath = XMLHelper.getFirstChildElementOfName(eError, "Xpath").getTextContent();
aErrorList.add(new SVRLErrorBuilder(sPattern).errorLocation(new SimpleLocation(aSource.getSystemID())).errorText(sDescription).errorFieldName(sXPath).build());
}
} else {
// Schematron does not create SVRL!
LOGGER.warn("Failed to read the result as OIOUBL result:" + (aDoc != null ? "\n" + XMLWriter.getNodeAsString(aDoc) : " no XML Document created"));
aErrorList.add(SingleError.builderError().errorLocation(aArtefact.getRuleResourcePath()).errorText("Internal error - no Schematron output created for OIOUBL").build());
}
break;
}
default:
throw new IllegalStateException("Unsupported output type");
}
} catch (final Exception ex) {
// Usually an error in the Schematron
aErrorList.add(SingleError.builderError().errorLocation(aArtefact.getRuleResourcePath()).errorText(ex.getMessage()).linkedException(ex).build());
}
// Apply custom levels
if (m_aCustomErrorLevels != null && aErrorList.isNotEmpty()) {
final ErrorList aOldErrorList = aErrorList.getClone();
aErrorList.clear();
for (final IError aCurError : aOldErrorList) {
final String sErrorID = aCurError.getErrorID();
final IErrorLevel aCustomLevel = m_aCustomErrorLevels.get(sErrorID);
if (aCustomLevel != null) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Changing error level of '" + sErrorID + "' from " + aCurError.getErrorLevel().getNumericLevel() + " to " + aCustomLevel + " (" + aCustomLevel.getNumericLevel() + ")");
aErrorList.add(SingleError.builder(aCurError).errorLevel(aCustomLevel).build());
} else {
// No change
aErrorList.add(aCurError);
}
}
}
return new ValidationResult(aArtefact, aErrorList);
}
use of com.helger.commons.location.SimpleLocation in project phive by phax.
the class PhiveJsonHelper method getAsIError.
@Nonnull
public static IError getAsIError(@Nonnull final IJsonObject aObj) {
final IErrorLevel aErrorLevel = getAsErrorLevel(aObj.getAsString(JSON_ERROR_LEVEL));
final String sErrorID = aObj.getAsString(JSON_ERROR_ID);
final String sErrorFieldName = aObj.getAsString(JSON_ERROR_FIELD_NAME);
// Try new structured version
ILocation aErrorLocation = getAsErrorLocation(aObj.getAsObject(JSON_ERROR_LOCATION_OBJ));
if (aErrorLocation == null) {
final IJsonValue aErrorLocationValue = aObj.getAsValue(JSON_ERROR_LOCATION_STR);
if (aErrorLocationValue != null) {
// It's a string - old version
aErrorLocation = new SimpleLocation(aErrorLocationValue.getAsString());
}
}
final String sErrorText = aObj.getAsString(JSON_ERROR_TEXT);
final String sTest = aObj.getAsString(JSON_TEST);
final PhiveRestoredException aLinkedException = PhiveRestoredException.createFromJson(aObj.getAsObject(JSON_EXCEPTION));
if (sTest != null)
return new SVRLResourceError(aErrorLevel, sErrorID, sErrorFieldName, aErrorLocation, new ConstantHasErrorText(sErrorText), aLinkedException, sTest);
return new SingleError(aErrorLevel, sErrorID, sErrorFieldName, aErrorLocation, new ConstantHasErrorText(sErrorText), aLinkedException);
}
use of com.helger.commons.location.SimpleLocation in project phive by phax.
the class PhiveJsonHelperTest method testError.
@Test
public void testError() {
final IError aError = SingleError.builderError().errorID("id1").errorText("fla").errorLocation(new SimpleLocation("res12", 3, 4)).build();
final IJsonObject aJson = PhiveJsonHelper.getJsonError(aError, Locale.US);
assertNotNull(aJson);
final IError aError2 = PhiveJsonHelper.getAsIError(aJson);
assertNotNull(aError2);
CommonsTestHelper.testDefaultImplementationWithEqualContentObject(aError, aError2);
}
use of com.helger.commons.location.SimpleLocation in project phive by phax.
the class PhiveJsonHelperTest method testSVRLErrorWithException.
@Test
public void testSVRLErrorWithException() {
final IError aError = new SVRLResourceError(EErrorLevel.ERROR, "id2", "field1", new SimpleLocation("res12", 3, 4), new ConstantHasErrorText("bla failed"), new IllegalStateException("Sthg went wrong"), " my test <>");
// To Json
final IJsonObject aJson = PhiveJsonHelper.getJsonError(aError, Locale.US);
assertNotNull(aJson);
// And back
final IError aError2 = PhiveJsonHelper.getAsIError(aJson);
assertNotNull(aError2);
// And forth
final IJsonObject aJson2 = PhiveJsonHelper.getJsonError(aError2, Locale.US);
assertNotNull(aJson2);
CommonsTestHelper.testDefaultImplementationWithEqualContentObject(aJson, aJson2);
// The objects differ, because of the different exception types
assertTrue(aError2.getLinkedException() instanceof PhiveRestoredException);
if (false)
CommonsTestHelper.testDefaultImplementationWithEqualContentObject(aError, aError2);
}
Aggregations