Search in sources :

Example 1 with SimpleLocation

use of com.helger.commons.location.SimpleLocation in project ph-schematron by phax.

the class AbstractCollectingPSErrorHandler method handle.

@Override
protected void handle(@Nullable final IReadableResource aRes, @Nonnull final IErrorLevel aErrorLevel, @Nullable final IPSElement aSourceElement, @Nonnull final String sMessage, @Nullable final Throwable t) {
    final SingleErrorBuilder aBuilder = SingleError.builder().setErrorLevel(aErrorLevel).setErrorLocation(aRes == null ? null : new SimpleLocation(aRes.getResourceID())).setErrorText(sMessage).setLinkedException(t);
    if (aSourceElement != null) {
        String sField = ClassHelper.getClassLocalName(aSourceElement);
        if (aSourceElement instanceof IPSHasID && ((IPSHasID) aSourceElement).hasID())
            sField += " [ID=" + ((IPSHasID) aSourceElement).getID() + "]";
        aBuilder.setErrorFieldName(sField);
    }
    m_aErrorList.add(aBuilder.build());
}
Also used : SingleErrorBuilder(com.helger.commons.error.SingleError.SingleErrorBuilder) IPSHasID(com.helger.schematron.pure.model.IPSHasID) SimpleLocation(com.helger.commons.location.SimpleLocation)

Example 2 with SimpleLocation

use of com.helger.commons.location.SimpleLocation in project ph-schematron by phax.

the class PSXPathBoundSchema method _createXPathContext.

@Nonnull
private XPath _createXPathContext() {
    final MapBasedNamespaceContext aNamespaceContext = getNamespaceContext();
    final XPath aXPathContext = XPathHelper.createNewXPath(m_aXPathConfig.getXPathFactory(), m_aXPathConfig.getXPathVariableResolver(), m_aXPathConfig.getXPathFunctionResolver(), aNamespaceContext);
    if ("net.sf.saxon.xpath.XPathEvaluator".equals(aXPathContext.getClass().getName())) {
        // Saxon implementation special handling
        final XPathEvaluator aSaxonXPath = (XPathEvaluator) aXPathContext;
        // Since 9.7.0-4 it must implement NamespaceResolver
        aSaxonXPath.setNamespaceContext(new SaxonNamespaceContext(aNamespaceContext));
        // Wrap the PSErrorHandler to a ErrorListener
        final Function<Configuration, ? extends ErrorReporter> factory = cfg -> {
            final IPSErrorHandler aErrHdl = getErrorHandler();
            return (final XmlProcessingError error) -> {
                final ILocation aLocation = error.getLocation() == null ? null : new SimpleLocation(error.getLocation().getSystemId(), error.getLocation().getLineNumber(), error.getLocation().getColumnNumber());
                aErrHdl.handleError(SingleError.builder().errorLevel(error.isWarning() ? EErrorLevel.WARN : EErrorLevel.ERROR).errorID(error.getErrorCode() != null ? error.getErrorCode().toString() : null).errorLocation(aLocation).errorText(error.getMessage()).linkedException(error.getCause()).build());
            };
        };
        aSaxonXPath.getConfiguration().setErrorReporterFactory(factory);
    }
    return aXPathContext;
}
Also used : MapBasedNamespaceContext(com.helger.xml.namespace.MapBasedNamespaceContext) XPath(javax.xml.xpath.XPath) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ErrorReporter(net.sf.saxon.lib.ErrorReporter) LoggerFactory(org.slf4j.LoggerFactory) PSPhase(com.helger.schematron.pure.model.PSPhase) ToStringGenerator(com.helger.commons.string.ToStringGenerator) AbstractPSBoundSchema(com.helger.schematron.pure.bound.AbstractPSBoundSchema) Configuration(net.sf.saxon.Configuration) XmlProcessingError(net.sf.saxon.s9api.XmlProcessingError) PSRule(com.helger.schematron.pure.model.PSRule) XPathFactoryConfigurationException(javax.xml.xpath.XPathFactoryConfigurationException) PSSchema(com.helger.schematron.pure.model.PSSchema) IXPathConfig(com.helger.schematron.pure.xpath.IXPathConfig) XPathHelper(com.helger.xml.xpath.XPathHelper) IPSElement(com.helger.schematron.pure.model.IPSElement) Map(java.util.Map) IPSErrorHandler(com.helger.schematron.pure.errorhandler.IPSErrorHandler) PSName(com.helger.schematron.pure.model.PSName) ILocation(com.helger.commons.location.ILocation) IPSQueryBinding(com.helger.schematron.pure.binding.IPSQueryBinding) XPathFunctionResolver(javax.xml.xpath.XPathFunctionResolver) SchematronValidationException(com.helger.schematron.pure.validation.SchematronValidationException) IPSHasMixedContent(com.helger.schematron.pure.model.IPSHasMixedContent) XPathConfigBuilder(com.helger.schematron.pure.xpath.XPathConfigBuilder) SingleError(com.helger.commons.error.SingleError) XPathVariableResolver(javax.xml.xpath.XPathVariableResolver) ICommonsList(com.helger.commons.collection.impl.ICommonsList) XPathEvaluator(net.sf.saxon.xpath.XPathEvaluator) SchematronBindException(com.helger.schematron.pure.binding.SchematronBindException) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) XPath(javax.xml.xpath.XPath) PSXPathVariables(com.helger.schematron.pure.binding.xpath.PSXPathVariables) SaxonNamespaceContext(com.helger.schematron.saxon.SaxonNamespaceContext) XPathExpression(javax.xml.xpath.XPathExpression) Function(java.util.function.Function) Node(org.w3c.dom.Node) IPSValidationHandler(com.helger.schematron.pure.validation.IPSValidationHandler) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) EErrorLevel(com.helger.commons.error.level.EErrorLevel) Logger(org.slf4j.Logger) NodeList(org.w3c.dom.NodeList) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) MapBasedNamespaceContext(com.helger.xml.namespace.MapBasedNamespaceContext) PSDiagnostic(com.helger.schematron.pure.model.PSDiagnostic) ValueEnforcer(com.helger.commons.ValueEnforcer) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) PSPattern(com.helger.schematron.pure.model.PSPattern) PSValueOf(com.helger.schematron.pure.model.PSValueOf) PSAssertReport(com.helger.schematron.pure.model.PSAssertReport) XPathEvaluationHelper(com.helger.schematron.pure.xpath.XPathEvaluationHelper) SimpleLocation(com.helger.commons.location.SimpleLocation) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) XmlProcessingError(net.sf.saxon.s9api.XmlProcessingError) IPSErrorHandler(com.helger.schematron.pure.errorhandler.IPSErrorHandler) ILocation(com.helger.commons.location.ILocation) Configuration(net.sf.saxon.Configuration) SaxonNamespaceContext(com.helger.schematron.saxon.SaxonNamespaceContext) XPathEvaluator(net.sf.saxon.xpath.XPathEvaluator) SimpleLocation(com.helger.commons.location.SimpleLocation) Nonnull(javax.annotation.Nonnull)

Example 3 with SimpleLocation

use of com.helger.commons.location.SimpleLocation in project phive by phax.

the class ValidationExecutorXSD method applyValidation.

@Nonnull
public ValidationResult applyValidation(@Nonnull final IValidationSourceXML aSource, @Nullable final Locale aLocale) {
    ValueEnforcer.notNull(aSource, "Source");
    final IValidationArtefact aVA = getValidationArtefact();
    // Find the XML schema required for validation
    // as we don't have a node, we need to trust the implementation class
    final Schema aSchema = m_aSchemaProvider.get();
    assert aSchema != null;
    final ErrorList aErrorList = new ErrorList();
    try {
        // Apply the XML schema validation
        XMLSchemaValidationHelper.validate(aSchema, aSource.getAsTransformSource(), aErrorList, aLocale);
    } catch (final IllegalArgumentException ex) {
        // Happens when non-XML document is trying to be parsed
        if (ex.getCause() instanceof SAXParseException) {
            aErrorList.add(AbstractSAXErrorHandler.getSaxParseError(EErrorLevel.FATAL_ERROR, (SAXParseException) ex.getCause()));
        } else {
            aErrorList.add(SingleError.builderFatalError().errorLocation(new SimpleLocation(aVA.getRuleResourcePath())).errorText("The document to be validated is not an XML document").linkedException(ex).build());
        }
    }
    // Build result object
    return new ValidationResult(aVA, aErrorList.getAllFailures());
}
Also used : ErrorList(com.helger.commons.error.list.ErrorList) IValidationArtefact(com.helger.phive.api.artefact.IValidationArtefact) SAXParseException(org.xml.sax.SAXParseException) Schema(javax.xml.validation.Schema) SimpleLocation(com.helger.commons.location.SimpleLocation) ValidationResult(com.helger.phive.api.result.ValidationResult) Nonnull(javax.annotation.Nonnull)

Example 4 with SimpleLocation

use of com.helger.commons.location.SimpleLocation in project phive by phax.

the class PhiveJsonHelperTest method testSVRLError.

@Test
public void testSVRLError() {
    final IError aError = new SVRLResourceError(EErrorLevel.ERROR, "id2", "field1", new SimpleLocation("res12", 3, 4), new ConstantHasErrorText("bla failed"), null, " 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);
    CommonsTestHelper.testDefaultImplementationWithEqualContentObject(aError, aError2);
}
Also used : IJsonObject(com.helger.json.IJsonObject) SVRLResourceError(com.helger.schematron.svrl.SVRLResourceError) SimpleLocation(com.helger.commons.location.SimpleLocation) IError(com.helger.commons.error.IError) ConstantHasErrorText(com.helger.commons.error.text.ConstantHasErrorText) Test(org.junit.Test)

Example 5 with SimpleLocation

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());
}
Also used : SimpleLocation(com.helger.commons.location.SimpleLocation)

Aggregations

SimpleLocation (com.helger.commons.location.SimpleLocation)10 Nonnull (javax.annotation.Nonnull)5 IError (com.helger.commons.error.IError)4 SingleError (com.helger.commons.error.SingleError)3 ErrorList (com.helger.commons.error.list.ErrorList)3 ConstantHasErrorText (com.helger.commons.error.text.ConstantHasErrorText)3 IJsonObject (com.helger.json.IJsonObject)3 SVRLResourceError (com.helger.schematron.svrl.SVRLResourceError)3 ValueEnforcer (com.helger.commons.ValueEnforcer)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)2 ICommonsList (com.helger.commons.collection.impl.ICommonsList)2 IErrorLevel (com.helger.commons.error.level.IErrorLevel)2 ILocation (com.helger.commons.location.ILocation)2 Wrapper (com.helger.commons.wrapper.Wrapper)2 IValidationArtefact (com.helger.phive.api.artefact.IValidationArtefact)2 ValidationResult (com.helger.phive.api.result.ValidationResult)2 SVRLFailedAssert (com.helger.schematron.svrl.SVRLFailedAssert)2 SchematronOutputType (com.helger.schematron.svrl.jaxb.SchematronOutputType)2 DOMSource (javax.xml.transform.dom.DOMSource)2 Test (org.junit.Test)2