Search in sources :

Example 1 with ConvException

use of com.owent.xresloader.data.err.ConvException in project xresloader by xresloader.

the class DataDstXml method build.

@Override
public final byte[] build(DataDstImpl compiler) throws ConvException {
    // pretty print
    OutputFormat of = null;
    if (ProgramOptions.getInstance().prettyIndent <= 0) {
        of = OutputFormat.createCompactFormat();
    } else {
        of = OutputFormat.createPrettyPrint();
        of.setIndentSize(ProgramOptions.getInstance().prettyIndent);
    }
    // build data
    DataDstObject data_obj = build_data(compiler);
    // build xml tree
    Document doc = DocumentHelper.createDocument();
    String encoding = SchemeConf.getInstance().getKey().getEncoding();
    if (null != encoding && false == encoding.isEmpty()) {
        doc.setXMLEncoding(encoding);
        of.setEncoding(encoding);
    }
    doc.setRootElement(DocumentHelper.createElement(ProgramOptions.getInstance().xmlRootName));
    doc.getRootElement().addComment("this file is generated by xresloader, please don't edit it.");
    Element header = DocumentHelper.createElement("header");
    Element body = DocumentHelper.createElement("body");
    writeData(header, data_obj.header, header.getName());
    // body
    for (Map.Entry<String, List<Object>> item : data_obj.body.entrySet()) {
        for (Object obj : item.getValue()) {
            Element xml_item = DocumentHelper.createElement(item.getKey());
            writeData(xml_item, obj, item.getKey());
            body.add(xml_item);
        }
    }
    writeData(body, data_obj.body, body.getName());
    doc.getRootElement().add(header);
    doc.getRootElement().add(body);
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        XMLWriter writer = new XMLWriter(bos, of);
        writer.write(doc);
        return bos.toByteArray();
    } catch (Exception e) {
        ProgramOptions.getLoger().error("write xml failed, %s", e.getMessage());
        return null;
    }
}
Also used : Element(org.dom4j.Element) OutputFormat(org.dom4j.io.OutputFormat) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.dom4j.Document) HashMap(java.util.HashMap) Map(java.util.Map) XMLWriter(org.dom4j.io.XMLWriter) ConvException(com.owent.xresloader.data.err.ConvException)

Aggregations

ConvException (com.owent.xresloader.data.err.ConvException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Document (org.dom4j.Document)1 Element (org.dom4j.Element)1 OutputFormat (org.dom4j.io.OutputFormat)1 XMLWriter (org.dom4j.io.XMLWriter)1