/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  org.dom4j.Document
 *  org.dom4j.DocumentException
 *  org.dom4j.io.OutputFormat
 *  org.dom4j.io.SAXReader
 *  org.dom4j.io.XMLWriter
 */
package com.hst.common.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class Xml {
    public String xmlToString(Document document) throws Exception {
        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("GBK");
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            XMLWriter writer = new XMLWriter((OutputStream)outputStream, format);
            writer.write(document);
            writer.close();
            return ((Object)outputStream).toString();
        }
        catch (Throwable t) {
            throw new Exception(t);
        }
    }

    public void xmlToFile(String filePath, Document document, String encoding) throws Exception {
        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding(encoding);
            XMLWriter writer = new XMLWriter((Writer)new FileWriter(new File(filePath)), format);
            writer.write(document);
            writer.close();
        }
        catch (IOException io) {
            throw new Exception(io);
        }
        catch (Throwable t) {
            throw new Exception(t);
        }
    }

    public String readXml(String filePath) throws Exception {
        try {
            File file = new File(filePath);
            byte[] bytesFile = new byte[new Long(file.length()).intValue()];
            FileInputStream fileInputStream = new FileInputStream(file);
            fileInputStream.read(bytesFile);
            return new String(bytesFile);
        }
        catch (FileNotFoundException fe) {
            throw new Exception(fe);
        }
        catch (IOException io) {
            throw new Exception(io);
        }
        catch (Throwable t) {
            throw new Exception(t);
        }
    }

    public Document createDocument(String xmlStr) throws Exception {
        try {
            SAXReader xmlReader = new SAXReader();
            StringReader sr = new StringReader(xmlStr);
            Document document = xmlReader.read((Reader)sr);
            return document;
        }
        catch (DocumentException de) {
            throw new Exception(de);
        }
        catch (Throwable t) {
            throw new Exception(t);
        }
        finally {
            Object xmlReader = null;
        }
    }
}
