<commit_before>/*
 * Copyright 2020 Red Hat, Inc. and/or its affiliates.
 *
 * 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 org.optaplanner.core.impl.io.jaxb;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.util.ValidationEventCollector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.optaplanner.core.impl.io.OptaPlannerXmlSerializationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;

public final class GenericJaxbIO<T> implements JaxbIO<T> {
    private static final int DEFAULT_INDENTATION = 2;

    private static final String ERR_MSG_WRITE = "Failed to marshall a root element class (%s) to XML.";
    private static final String ERR_MSG_READ = "Failed to unmarshall a root element class (%s) from XML.";
    private static final String ERR_MSG_READ_OVERRIDE_NAMESPACE =
            "Failed to unmarshall a root element class (%s) from XML with overriding elements' namespaces: (%s).";

    private final JAXBContext jaxbContext;
    private final Marshaller marshaller;
    private final Class<T> rootClass;
    private final int indentation;

    public GenericJaxbIO(Class<T> rootClass) {
        this(rootClass, DEFAULT_INDENTATION);
    }

    public GenericJaxbIO(Class<T> rootClass, int indentation) {
        Objects.requireNonNull(rootClass);
        this.rootClass = rootClass;
        this.indentation = indentation;
        try {
            jaxbContext = JAXBContext.newInstance(rootClass);
            marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.toString());
        } catch (JAXBException jaxbException) {
            String errorMessage = String.format("Failed to create JAXB Marshaller for a root element class (%s).",
                    rootClass.getName());
            throw new OptaPlannerXmlSerializationException(errorMessage, jaxbException);
        }
    }

    @Override
    public T read(Reader reader) {
        Objects.requireNonNull(reader);
        try {
            return (T) createUnmarshaller().unmarshal(reader);
        } catch (JAXBException jaxbException) {
            String errorMessage = String.format(ERR_MSG_READ, rootClass.getName());
            throw new OptaPlannerXmlSerializationException(errorMessage, jaxbException);
        }
    }

    public T readAndValidate(Reader reader, String schemaResource) {
        Objects.requireNonNull(reader);
        Schema schema = readSchemaResource(schemaResource);
        return readAndValidate(reader, schema);
    }

    public T readAndValidate(Document document, String schemaResource) {
        return readAndValidate(document, readSchemaResource(schemaResource));
    }

    private Schema readSchemaResource(String schemaResource) {
        String nonNullSchemaResource = Objects.requireNonNull(schemaResource);
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        try {
            schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
            schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
        } catch (SAXNotSupportedException | SAXNotRecognizedException saxException) {
            String errorMessage = String.format(
                    "Failed to configure the %s to validate an XML for a root class (%s) using the (%s) XML Schema.",
                    SchemaFactory.class.getSimpleName(), rootClass.getName(), schemaResource);
            throw new OptaPlannerXmlSerializationException(errorMessage, saxException);
        }

        try {
            return schemaFactory.newSchema(GenericJaxbIO.class.getResource(nonNullSchemaResource));
        } catch (SAXException saxException) {
            String errorMessage =
                    String.format("Failed to read an XML Schema resource (%s) to validate an XML for a root class (%s).",
                            nonNullSchemaResource, rootClass.getName());
            throw new OptaPlannerXmlSerializationException(errorMessage, saxException);
        }
    }

    public T readAndValidate(Reader reader, Schema schema) {
        Document document = parseXml(Objects.requireNonNull(reader));
        return readAndValidate(document, Objects.requireNonNull(schema));
    }

    public T readAndValidate(Document document, Schema schema) {
        Document nonNullDocument = Objects.requireNonNull(document);
        Schema nonNullSchema = Objects.requireNonNull(schema);
        Unmarshaller unmarshaller = createUnmarshaller();
        unmarshaller.setSchema(nonNullSchema);

        ValidationEventCollector validationEventCollector = new ValidationEventCollector();
        try {
            unmarshaller.setEventHandler(validationEventCollector);
        } catch (JAXBException jaxbException) {
            String errorMessage = String.format("Failed to set a validation event handler to the %s for "
                    + "a root element class (%s).", Unmarshaller.class.getSimpleName(), rootClass.getName());
            throw new OptaPlannerXmlSerializationException(errorMessage, jaxbException);<commit_msg>PLANNER-2297 Fail fast if XML schema does not exist<commit_after>/*
 * Copyright 2020 Red Hat, Inc. and/or its affiliates.
 *
 * 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 org.optaplanner.core.impl.io.jaxb;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.util.ValidationEventCollector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.optaplanner.core.impl.io.OptaPlannerXmlSerializationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;

public final class GenericJaxbIO<T> implements JaxbIO<T> {
    private static final int DEFAULT_INDENTATION = 2;

    private static final String ERR_MSG_WRITE = "Failed to marshall a root element class (%s) to XML.";
    private static final String ERR_MSG_READ = "Failed to unmarshall a root element class (%s) from XML.";
    private static final String ERR_MSG_READ_OVERRIDE_NAMESPACE =
            "Failed to unmarshall a root element class (%s) from XML with overriding elements' namespaces: (%s).";

    private final JAXBContext jaxbContext;
    private final Marshaller marshaller;
    private final Class<T> rootClass;
    private final int indentation;

    public GenericJaxbIO(Class<T> rootClass) {
        this(rootClass, DEFAULT_INDENTATION);
    }

    public GenericJaxbIO(Class<T> rootClass, int indentation) {
        Objects.requireNonNull(rootClass);
        this.rootClass = rootClass;
        this.indentation = indentation;
        try {
            jaxbContext = JAXBContext.newInstance(rootClass);
            marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.toString());
        } catch (JAXBException jaxbException) {
            String errorMessage = String.format("Failed to create JAXB Marshaller for a root element class (%s).",
                    rootClass.getName());
            throw new OptaPlannerXmlSerializationException(errorMessage, jaxbException);
        }
    }

    @Override
    public T read(Reader reader) {
        Objects.requireNonNull(reader);
        try {
            return (T) createUnmarshaller().unmarshal(reader);
        } catch (JAXBException jaxbException) {
            String errorMessage = String.format(ERR_MSG_READ, rootClass.getName());
            throw new OptaPlannerXmlSerializationException(errorMessage, jaxbException);
        }
    }

    public T readAndValidate(Reader reader, String schemaResource) {
        Objects.requireNonNull(reader);
        Schema schema = readSchemaResource(schemaResource);
        return readAndValidate(reader, schema);
    }

    public T readAndValidate(Document document, String schemaResource) {
        return readAndValidate(document, readSchemaResource(schemaResource));
    }

    private Schema readSchemaResource(String schemaResource) {
        String nonNullSchemaResource = Objects.requireNonNull(schemaResource);
        URL schemaResourceUrl = GenericJaxbIO.class.getResource(nonNullSchemaResource);
        if (schemaResourceUrl == null) {
            throw new IllegalArgumentException("The XML schema (" + nonNullSchemaResource + ") does not exist.\n"
                    + "Maybe the JAXB maven plugin did not run?");
        }
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        try {
            schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
            schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
        } catch (SAXNotSupportedException | SAXNotRecognizedException saxException) {
            String errorMessage = String.format(
                    "Failed to configure the %s to validate an XML for a root class (%s) using the (%s) XML Schema.",
                    SchemaFactory.class.getSimpleName(), rootClass.getName(), schemaResource);
            throw new OptaPlannerXmlSerializationException(errorMessage, saxException);
        }

        try {
            return schemaFactory.newSchema(schemaResourceUrl);
        } catch (SAXException saxException) {
            String errorMessage =
                    String.format("Failed to read an XML Schema resource (%s) to validate an XML for a root class (%s).",
                            nonNullSchemaResource, rootClass.getName());
            throw new OptaPlannerXmlSerializationException(errorMessage, saxException);
        }
    }

    public T readAndValidate(Reader reader, Schema schema) {
        Document document = parseXml(Objects.requireNonNull(reader));
        return readAndValidate(document, Objects.requireNonNull(schema));
    }

    public T readAndValidate(Document document, Schema schema) {
        Document nonNullDocument = Objects.requireNonNull(document);
        Schema nonNullSchema = Objects.requireNonNull(schema);
        Unmarshaller unmarshaller = createUnmarshaller();
        unmarshaller.setSchema(nonNullSchema);

        ValidationEventCollector validationEventCollector = new ValidationEventCollector();
        try {
            unmarshaller.setEventHandler(validationEventCollector);
        } catch (JAXBException jaxbException) {
            String errorMessage = String.format("Failed to set a validation event handler to the %s for "
                    + "a root element class (%s).", Unmarshaller.class.getSimpleName(), rootClass.getName());
            throw new OptaPlannerXmlSerializationException(errorMessage, jaxbException);
{'fraction_non_alphanumeric': 0.08425338117582114, 'fraction_numerical': 0.0022081148219707425, 'mean_word_length': 3.633312020460358, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 52}, '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': '3876598', 'n_tokens_mistral': 3881, 'n_tokens_neox': 3870, 'n_words': 994}