/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package expressiontree.operands;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.apfloat.ApfloatMath;
import org.w3c.dom.Document;
import propertyhandler.Properties;

/**
 * A Pi konstans osztálya
 * @author Esta
 */
public class PiConstant extends NumberType {

    /**
     * Készít egy új Pi objektumot, és beállítja a pontosságot.
     * @param precision a pontosság.
     */
    public PiConstant(long precision) {
        super(precision, false);
        this.values[0][0] = ApfloatMath.pi(precision);
    }

    /**
     * Készít egy új Pi objektunot az alapértelmezett pontossággal.
     */
    public PiConstant() {
        this(PiConstant.DEFALUT_PRECISION);
    }

    @Override
    public boolean equals(Object o) {
        return super.equals(o);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public void saveToXML(File file) throws FileNotFoundException, IOException, ParserConfigurationException, XMLStreamException {
        file.createNewFile();
        if (!file.canWrite()) {
            throw new FileNotFoundException("CANT_WRITE");
        }
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setValidating(true);
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        Document doc = builder.newDocument();
        doc.setXmlVersion("1.0");
        XMLOutputFactory outfact = javax.xml.stream.XMLOutputFactory.newInstance();
        XMLStreamWriter out = outfact.createXMLStreamWriter(new FileOutputStream(file), doc.getXmlEncoding());
        out.writeStartDocument(doc.getXmlEncoding(), doc.getXmlVersion());
        out.writeCharacters("\n");
        //hozzáadjuk a style-sheet-et
        out.writeCharacters("\n");
        out.writeStartElement("?xml-stylesheet type=\"text/xsl\" href=\"" + xmlhandling.XMLBuilder.createXSL(file, false) + "\"?");
        out.writeCharacters("\n");
        //comment a tetejére
        out.writeComment("\n" +
                "Author     : " + Properties.getUserName() + "\n" +
                "Description: XMLCalculator output document.\n" +
                "Saved with XMLCalculator.\n");
        out.writeCharacters("\n");
        //hozzáadjuk a xsd-t
        out.writeStartElement("expression");
        out.writeAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        out.writeAttribute("\nxmlns", "http://localhost/");
        out.writeAttribute("\nxsi:schemaLocation", "http://localhost/ \n" + xmlhandling.XMLBuilder.createXSD(file));
        out.writeAttribute("\nprecision", "" + this.precision);
        out.writeCharacters("\n   ");
        out.writeStartElement("number");
        out.writeCharacters("pi");
        out.writeEndElement();
        out.writeCharacters("\n");
        out.writeEndElement();
        out.writeCharacters("\n");
        out.close();
    }
}
