<commit_before>/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.camel.component.schematron;

import org.apache.camel.Exchange;
import org.apache.camel.component.schematron.constant.Constants;
import org.apache.camel.component.schematron.exception.SchematronValidationException;
import org.apache.camel.component.schematron.processor.SchematronProcessor;
import org.apache.camel.component.schematron.processor.SchematronProcessorFactory;
import org.apache.camel.support.DefaultProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.transform.Source;
import java.util.HashMap;
import java.util.Map;

/**
 * The Schematron producer.
 */
public class SchematronProducer extends DefaultProducer {
    private Logger logger = LoggerFactory.getLogger(SchematronProducer.class);
    private SchematronEndpoint endpoint;


    /**
     * @param endpoint the schematron endpoint.
     */
    public SchematronProducer(final SchematronEndpoint endpoint) {
        super(endpoint);
        this.endpoint = endpoint;
    }

    /**
     * Processes the payload. Validates the XML using the SchematronEngine
     *
     * @param exchange
     * @throws Exception
     */
    public void process(Exchange exchange) throws Exception {
        final SchematronProcessor schematronProcessor = SchematronProcessorFactory.newScehamtronEngine(endpoint.getRules());
        final Object payload = exchange.getIn().getBody();
        final String report;

        if (payload instanceof Source) {
            logger.debug("Applying schematron validation on payload: {}", payload);
            report = schematronProcessor.validate((Source) payload);
        } else if (payload instanceof String) {
            logger.debug("Applying schematron validation on payload: {}", payload);
            report = schematronProcessor.validate((String) payload);
        } else {
            String stringPayload = exchange.getIn().getBody(String.class);
            logger.debug("Applying schematron validation on payload: {}", stringPayload);
            report = schematronProcessor.validate(stringPayload);
        }

        logger.debug("Schematron validation report \n {}", report);
        String status = getValidationStatus(report);
        logger.info("Schematron validation status : {}", status);
        setValidationReport(exchange, report, status);
    }

    /**
     * Sets validation report and status
     *
     * @param exchange
     * @param report
     * @param status
     */
    private void setValidationReport(Exchange exchange, String report, String status) {
        // if exchange pattern is In and Out set details on the Out message.
        Map<String, Object> headers = new HashMap<>();
        headers.put(Constants.VALIDATION_STATUS, status);
        headers.put(Constants.VALIDATION_REPORT, report);
        if (exchange.getPattern().isOutCapable()) {
            exchange.getOut().setHeaders(exchange.getIn().getHeaders());
            exchange.getOut().getHeaders().putAll(headers);
        } else {
            exchange.getIn().getHeaders().putAll(headers);
        }
    }

    /**
     * Get validation status, SUCCESS or FAILURE
     *
     * @param report
     * @return
     */
    private String getValidationStatus(final String report) {
        String status = report.contains(Constants.FAILED_ASSERT) ? Constants.FAILED : Constants.SUCCESS;
        if (this.endpoint.isAbort() && Constants.FAILED.equals(status)) {
            throw new SchematronValidationException("Schematron validation failure \n" + report);
        }
        return status;
    }

}
<commit_msg>Fix backport of Schematron fix to 3.x<commit_after>/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.camel.component.schematron;

import org.apache.camel.Exchange;
import org.apache.camel.component.schematron.constant.Constants;
import org.apache.camel.component.schematron.exception.SchematronValidationException;
import org.apache.camel.component.schematron.processor.SchematronProcessor;
import org.apache.camel.component.schematron.processor.SchematronProcessorFactory;
import org.apache.camel.support.DefaultProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.transform.Source;
import java.util.HashMap;
import java.util.Map;

/**
 * The Schematron producer.
 */
public class SchematronProducer extends DefaultProducer {
    private Logger logger = LoggerFactory.getLogger(SchematronProducer.class);
    private SchematronEndpoint endpoint;


    /**
     * @param endpoint the schematron endpoint.
     */
    public SchematronProducer(final SchematronEndpoint endpoint) {
        super(endpoint);
        this.endpoint = endpoint;
    }

    /**
     * Processes the payload. Validates the XML using the SchematronEngine
     *
     * @param exchange
     * @throws Exception
     */
    public void process(Exchange exchange) throws Exception {
        final SchematronProcessor schematronProcessor = SchematronProcessorFactory.newSchematronEngine(endpoint.getRules());
        final Object payload = exchange.getIn().getBody();
        final String report;

        if (payload instanceof Source) {
            logger.debug("Applying schematron validation on payload: {}", payload);
            report = schematronProcessor.validate((Source) payload);
        } else if (payload instanceof String) {
            logger.debug("Applying schematron validation on payload: {}", payload);
            report = schematronProcessor.validate((String) payload);
        } else {
            String stringPayload = exchange.getIn().getBody(String.class);
            logger.debug("Applying schematron validation on payload: {}", stringPayload);
            report = schematronProcessor.validate(stringPayload);
        }

        logger.debug("Schematron validation report \n {}", report);
        String status = getValidationStatus(report);
        logger.info("Schematron validation status : {}", status);
        setValidationReport(exchange, report, status);
    }

    /**
     * Sets validation report and status
     *
     * @param exchange
     * @param report
     * @param status
     */
    private void setValidationReport(Exchange exchange, String report, String status) {
        // if exchange pattern is In and Out set details on the Out message.
        Map<String, Object> headers = new HashMap<>();
        headers.put(Constants.VALIDATION_STATUS, status);
        headers.put(Constants.VALIDATION_REPORT, report);
        if (exchange.getPattern().isOutCapable()) {
            exchange.getOut().setHeaders(exchange.getIn().getHeaders());
            exchange.getOut().getHeaders().putAll(headers);
        } else {
            exchange.getIn().getHeaders().putAll(headers);
        }
    }

    /**
     * Get validation status, SUCCESS or FAILURE
     *
     * @param report
     * @return
     */
    private String getValidationStatus(final String report) {
        String status = report.contains(Constants.FAILED_ASSERT) ? Constants.FAILED : Constants.SUCCESS;
        if (this.endpoint.isAbort() && Constants.FAILED.equals(status)) {
            throw new SchematronValidationException("Schematron validation failure \n" + report);
        }
        return status;
    }

}

{'fraction_non_alphanumeric': 0.09292538671519564, 'fraction_numerical': 0.001478616924476797, 'mean_word_length': 3.223342939481268, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1896704', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2218, 'n_words': 658}