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;
}
}
Aggregations