public void startValidation()

in engines/servicemix-validation/src/main/java/org/apache/servicemix/validation/ValidationEndpoint.java [183:291]


    public void startValidation(MessageExchange exchange, NormalizedMessage in,
            NormalizedMessage out, Fault fault) throws Exception {
        Validator validator = schema.newValidator();
        
        if (noNamespaceSchemaResource != null) {
            logger.info("Enabling validation for noNamespace-XML documents.");
            validator.setFeature("http://xml.org/sax/features/validation", true);
            validator.setFeature("http://apache.org/xml/features/validation/schema", true);
            validator.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", noNamespaceSchemaResource.getURL().toExternalForm());
        }
        
        // create a new errorHandler and set it on the validator
        MessageAwareErrorHandler errorHandler = errorHandlerFactory
                .createMessageAwareErrorHandler();
        validator.setErrorHandler(errorHandler);
        DOMResult result = new DOMResult();

        fault.setContent(null);

        try {
            // Only DOMSource and SAXSource are allowed for validating
            // See
            // http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/
            // Validator.html#validate(javax.xml.transform.Source,%20javax.xml.transform.Result)
            // As we expect a DOMResult as output, we must ensure that the input
            // is a DOMSource
            DOMSource src = sourceTransformer.toDOMSource(in.getContent());

            // call the validation method
            doValidation(validator, src, result);

            // check if there were errors while validating
            if (errorHandler.hasErrors()) {
                /*
                 * check if this error handler supports the capturing of error
                 * messages.
                 */
                if (errorHandler.capturesMessages()) {
                    /*
                     * In descending order of preference select a format to use.
                     * If neither DOMSource, StringSource or String are
                     * supported throw a messaging exception.
                     */
                    if (errorHandler.supportsMessageFormat(DOMSource.class)) {
                        fault.setContent((DOMSource) errorHandler
                                .getMessagesAs(DOMSource.class));
                    } else if (errorHandler
                            .supportsMessageFormat(StringSource.class)) {
                        fault.setContent(sourceTransformer
                                .toDOMSource((StringSource) errorHandler
                                        .getMessagesAs(StringSource.class)));
                    } else if (errorHandler.supportsMessageFormat(String.class)) {
                        fault.setContent(sourceTransformer
                                .toDOMSource(new StringSource(
                                        (String) errorHandler
                                                .getMessagesAs(String.class))));
                    } else {
                        throw new MessagingException(
                                "MessageAwareErrorHandler implementation "
                                        + errorHandler.getClass().getName()
                                        + " does not support a compatible error message format.");
                    }
                } else {
                    /*
                     * we can't do much here if the ErrorHandler implementation
                     * does not support capturing messages
                     */
                    StringBuffer resultString = new StringBuffer();
                    resultString.append(TAG_RESULT_START);
                    resultString.append('\n');
                    resultString.append(TAG_WARNING_START);
                    resultString.append(errorHandler.getWarningCount());
                    resultString.append(TAG_WARNING_END);
                    resultString.append('\n');
                    resultString.append(TAG_ERROR_START);
                    resultString.append(errorHandler.getErrorCount());
                    resultString.append(TAG_ERROR_END);
                    resultString.append('\n');
                    resultString.append(TAG_FATAL_START);
                    resultString.append(errorHandler.getFatalErrorCount());
                    resultString.append(TAG_FATAL_END);
                    resultString.append('\n');
                    resultString.append(TAG_RESULT_END);
                    resultString.append('\n');
                    fault.setContent(new StringSource(resultString.toString()));
                }

                if (!handlingErrorMethod.equalsIgnoreCase(FAULT_FLOW)) {
                    // HANDLE AS JBI FAULT
                    throw new FaultException("Failed to validate against schema: " + schema, exchange, fault);
                } else {
                    MessageUtil.transfer(fault, out);
                }
            } else {
                // Retrieve the ouput of the validation
                // as it may have been changed by the validator
                out.setContent(new DOMSource(result.getNode(), result
                        .getSystemId()));
            }
        } catch (SAXException e) {
            throw new MessagingException(e);
        } catch (IOException e) {
            throw new MessagingException(e);
        } catch (ParserConfigurationException e) {
            throw new MessagingException(e);
        } catch (TransformerException e) {
            throw new MessagingException(e);
        }
    }