////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2013 Saxonica Limited.
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package net.sf.saxon.trace;

import net.sf.saxon.Configuration;
import net.sf.saxon.event.Receiver;
import net.sf.saxon.lib.SaxonOutputKeys;
import net.sf.saxon.om.NamePool;
import net.sf.saxon.om.NoNamespaceName;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.type.BuiltInAtomicType;
import net.sf.saxon.type.TypeHierarchy;
import net.sf.saxon.type.Untyped;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.stream.StreamResult;
import java.io.OutputStream;
import java.util.Properties;

/**
 * This class handles the display of an abstract expression tree in an XML format
 * with some slight resemblence to XQueryX
 */
public class ExpressionPresenter {

    private Configuration config;
    /*@Nullable*/ private Receiver receiver;
    int depth = 0;
    boolean inStartTag = false;

    /**
     * Make an ExpressionPresenter that writes indented output to the standard error output
     * destination of the Configuration
     * @param config the Saxon configuration
     */

    public ExpressionPresenter(/*@NotNull*/ Configuration config) {
        this(config, config.getStandardErrorOutput());
    }

    /**
     * Make an ExpressionPresenter that writes indented output to a specified output stream
     * @param config the Saxon configuration
     * @param out the output stream
     */

    public ExpressionPresenter(/*@NotNull*/ Configuration config, OutputStream out) {
        Properties props = makeDefaultProperties();
        try {
            receiver = config.getSerializerFactory().getReceiver(
                            new StreamResult(out),
                            config.makePipelineConfiguration(),
                            props);
        } catch (XPathException err) {
            err.printStackTrace();
            throw new InternalError(err.getMessage());
        }
        this.config = config;
        try {
            receiver.open();
            receiver.startDocument(0);
        } catch (XPathException err) {
            err.printStackTrace();
            throw new InternalError(err.getMessage());
        }
    }

    /**
     * Make an ExpressionPresenter for a given Configuration using a user-supplied Receiver
     * to accept the output
     * @param config the Configuration
     * @param receiver the user-supplied Receiver
     */

    public ExpressionPresenter(Configuration config, /*@NotNull*/ Receiver receiver) {
        this.config = config;
        this.receiver = receiver;
        try {
            receiver.open();
            receiver.startDocument(0);
        } catch (XPathException err) {
            err.printStackTrace();
            throw new InternalError(err.getMessage());
        }
    }

    /**
     * Make a receiver, using default output properties, with serialized output going
     * to a specified OutputStream
     * @param config the Configuration
     * @param out the OutputStream
     * @return a Receiver that directs serialized output to this output stream
     * @throws XPathException
     */

    /*@Nullable*/ public static Receiver defaultDestination(/*@NotNull*/ Configuration config, OutputStream out) throws XPathException {
        Properties props = makeDefaultProperties();
        return config.getSerializerFactory().getReceiver(
                        new StreamResult(out),
                        config.makePipelineConfiguration(),
                        props);
    }


    /**
     * Make a Properties object containing defaulted serialization attributes for the expression tree
     * @return a default set of properties
     */

    /*@NotNull*/ public static Properties makeDefaultProperties() {
        Properties props = new Properties();
        props.setProperty(OutputKeys.METHOD, "xml");
        props.setProperty(OutputKeys.INDENT, "yes");
        props.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        props.setProperty(SaxonOutputKeys.INDENT_SPACES, "2");
        return props;
    }

    /**
     * Start an element
     * @param name the name of the element
     * @return the depth of the tree before this element: for diagnostics, this can be compared
     * with the value returned by endElement
     */

    public int startElement(String name) {
        try {
            if (inStartTag) {
                receiver.startContent();
                inStartTag = false;
            }
            receiver.startElement(new NoNamespaceName(name), Untyped.getInstance(), 0, 0);
        } catch (XPathException err) {
            err.printStackTrace();
            throw new InternalError(err.getMessage());
        }
        inStartTag = true;
        return depth++;
    }

    /**
     * Output an attribute node
     * @param name the name of the attribute
     * @param value the value of the attribute
     */

    public void emitAttribute(String name, String value) {
        try {
            receiver.attribute(new NoNamespaceName(name), BuiltInAtomicType.UNTYPED_ATOMIC, value, 0, 0);
        } catch (XPathException err) {
            err.printStackTrace();
            throw new InternalError(err.getMessage());
        }
    }

    /**
     * End an element in the expression tree
     * @return the depth of the tree after ending this element. For diagnostics, this can be compared with the
     * value returned by startElement()
     */

    public int endElement() {
        try {
            if (inStartTag) {
                receiver.startContent();
                inStartTag = false;
            }
            receiver.endElement();
        } catch (XPathException err) {
            err.printStackTrace();
            throw new InternalError(err.getMessage());
        }
        return --depth;
    }

    /**
     * Start a child element in the output
     * @param name the name of the child element
     */

    public void startSubsidiaryElement(String name) {
        startElement(name);
    }

    /**
     * End a child element in the output
     */

    public void endSubsidiaryElement() {
        endElement();
    }

    /**
     * Close the output
     */

    public void close() {
        try {
            receiver.endDocument();
            receiver.close();
        } catch (XPathException err) {
            err.printStackTrace();
            throw new InternalError(err.getMessage());
        }
    }

    /**
     * Get the Saxon configuration
     * @return the Saxon configuration
     */

    public Configuration getConfiguration() {
        return config;
    }

    /**
     * Get the name pool
     * @return the name pool
     */

    public NamePool getNamePool() {
        return config.getNamePool();
    }

    /**
     * Get the type hierarchy cache
     * @return the type hierarchy cache
     */

    public TypeHierarchy getTypeHierarchy() {
        return config.getTypeHierarchy();
    }
}

