/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.astrid.xmltools.beantoxml;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 *
 * @author István Zoltán Nagy
 */
public class XmlConverter {

    /**
     * The XML version of the generated document (default: 1.0)
     */
    protected String version = "1.0";
    /**
     * Validate or not, during the conversion process?
     */
    protected boolean validate = true;

    /**
     * Returns the current value of validate
     * @return validate
     */
    public boolean isValidate() {
        return validate;
    }

    /**
     * Sets the value of validate.
     * @param validate The new value of validate.
     */
    public void setValidate(boolean validate) {
        this.validate = validate;
    }

    /**
     * Returns the version of the generated XML document
     * @return version
     */
    public String getVersion() {
        return version;
    }

    /**
     * Sets the version of the generated XML document
     * @param version The new version of the XML
     */
    public void setVersion(String version) {
        this.version = version;
    }

    /**
     * Creates a new DocumentBuilder instance with the default error handler
     * @param builderFactory The builder factory, which is used to this process.
     * @return builder
     * @throws ParserConfigurationException When the parser couldn't be configured.
     */
    protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory builderFactory) throws ParserConfigurationException {
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        builder.setErrorHandler(new ErrorHandler() {

            public void warning(SAXParseException exception) throws SAXException {
                throw exception;
            }

            public void error(SAXParseException exception) throws SAXException {
                throw exception;
            }

            public void fatalError(SAXParseException exception) throws SAXException {
                throw exception;
            }
        });
        return builder;
    }

    /**
     * Creates a new DocumentBuilderFactory with or without the validating feature
     * @return builderFactory
     * @throws IllegalArgumentException Probably never, but I could be wrong, when I set a wrong attribute.
     */
    protected DocumentBuilderFactory createDocumentBuilderFactory() throws IllegalArgumentException {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        builderFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
        builderFactory.setValidating(this.validate);
        return builderFactory;
    }

    /**
     * Creates a new document using the values of the data tags of the current instance.
     * @return document The new document
     * @throws DOMException When the version couldn1t be set.
     * @throws ParserConfigurationException If the parser couldn't be configured.
     */
    protected Document createDocument() throws DOMException, ParserConfigurationException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();
        return doc;
    }



    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final XmlConverter other = (XmlConverter) obj;
        if ((this.version == null) ? (other.version != null) : !this.version.equals(other.version)) {
            return false;
        }
        if (this.validate != other.validate) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + (this.version != null ? this.version.hashCode() : 0);
        hash = 67 * hash + (this.validate ? 1 : 0);
        return hash;
    }
}
