Java Code Examples for org.dom4j.io.OutputFormat#setIndentSize()

The following examples show how to use org.dom4j.io.OutputFormat#setIndentSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VersionedXmlDoc.java    From onedev with MIT License 6 votes vote down vote up
public String toXML(boolean pretty) {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	try {
		OutputFormat format = new OutputFormat();
		format.setEncoding(StandardCharsets.UTF_8.name());
		if (pretty) {
			format.setIndent(true);
			format.setIndentSize(4);
	        format.setNewlines(true);
		} else {
	        format.setIndent(false);
	        format.setNewlines(false);
		}
		new XMLWriter(baos, format).write(getWrapped());
		return baos.toString(StandardCharsets.UTF_8.name());
	} catch (Exception e) {
		throw ExceptionUtils.unchecked(e);
	}
}
 
Example 2
Source File: ImportFileUpdater.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Get the writer for the import file
 * 
 * @param destination	the destination XML import file
 * @return				the XML writer
 */
private XMLWriter getWriter(String destination)
{
	try
	{
		 // Define output format
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setNewLineAfterDeclaration(false);
        format.setIndentSize(INDENT_SIZE);
        format.setEncoding(this.fileEncoding);

        return new XMLWriter(new FileOutputStream(destination), format);
	}
       catch (Exception exception)
       {
       	throw new AlfrescoRuntimeException("Unable to create XML writer.", exception);
       }
}
 
Example 3
Source File: XMLTransferDestinationReportWriter.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Start the transfer report
 */
public void startTransferReport(String encoding, Writer writer)
{
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setNewLineAfterDeclaration(false);
    format.setIndentSize(3);
    format.setEncoding(encoding);
    
    try 
    {
    
    this.writer = new XMLWriter(writer, format);
    this.writer.startDocument();
    
    this.writer.startPrefixMapping(PREFIX, TransferDestinationReportModel.TRANSFER_REPORT_MODEL_1_0_URI);

    // Start Transfer Manifest  // uri, name, prefix
    this.writer.startElement(TransferDestinationReportModel.TRANSFER_REPORT_MODEL_1_0_URI, TransferDestinationReportModel.LOCALNAME_TRANSFER_DEST_REPORT,  PREFIX + ":" + TransferDestinationReportModel.LOCALNAME_TRANSFER_DEST_REPORT, EMPTY_ATTRIBUTES);
    
    } 
    catch (SAXException se)
    {
        se.printStackTrace();
    }
}
 
Example 4
Source File: XMLTransferReportWriter.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Start the transfer report
 */
public void startTransferReport(String encoding, Writer writer) throws SAXException
{
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setNewLineAfterDeclaration(false);
    format.setIndentSize(3);
    format.setEncoding(encoding);
    
    this.writer = new XMLWriter(writer, format);
    this.writer.startDocument();
    
    this.writer.startPrefixMapping(PREFIX, TransferReportModel2.TRANSFER_REPORT_MODEL_2_0_URI);

    // Start Transfer Manifest  // uri, name, prefix
    this.writer.startElement(TransferReportModel2.TRANSFER_REPORT_MODEL_2_0_URI, TransferReportModel.LOCALNAME_TRANSFER_REPORT,  PREFIX + ":" + TransferReportModel.LOCALNAME_TRANSFER_REPORT, EMPTY_ATTRIBUTES);
}
 
Example 5
Source File: XmlHelper.java    From projectforge-webapp with GNU General Public License v3.0 6 votes vote down vote up
public static String toString(final Element el, final boolean prettyFormat)
{
  if (el == null) {
    return "";
  }
  final StringWriter out = new StringWriter();
  final OutputFormat format = new OutputFormat();
  if (prettyFormat == true) {
    format.setNewlines(true);
    format.setIndentSize(2);
  }
  final XMLWriter writer = new XMLWriter(out, format);
  String result = null;
  try {
    writer.write(el);
    result = out.toString();
    writer.close();
  } catch (final IOException ex) {
    log.error(ex.getMessage(), ex);
  }
  return result;
}
 
Example 6
Source File: XMLTransferRequsiteWriter.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public XMLTransferRequsiteWriter(Writer out)
{
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setNewLineAfterDeclaration(false);
    format.setIndentSize(3);
    format.setEncoding("UTF-8");

    this.writer = new XMLWriter(out, format);
}
 
Example 7
Source File: XMLWriter.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public XMLWriter(OutputStream outputStream, boolean prettyPrint, String encoding)
        throws UnsupportedEncodingException
{
    OutputFormat format = prettyPrint ? OutputFormat.createPrettyPrint() : OutputFormat.createCompactFormat();
    format.setNewLineAfterDeclaration(false);
    format.setIndentSize(3);
    format.setEncoding(encoding);
    output = outputStream;
    this.dom4jWriter = new org.dom4j.io.XMLWriter(outputStream, format);
}
 
Example 8
Source File: ExportSourceImporter.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private XMLWriter createXMLExporter(Writer writer)
{
    // Define output format
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setNewLineAfterDeclaration(false);
    format.setIndentSize(3);
    format.setEncoding("UTF-8");

    // Construct an XML Exporter

    XMLWriter xmlWriter = new XMLWriter(writer, format);
    return xmlWriter;
}
 
Example 9
Source File: XmlExport.java    From openmeetings with Apache License 2.0 5 votes vote down vote up
public static void toXml(Writer out, Document doc) throws IOException {
	OutputFormat outformat = OutputFormat.createPrettyPrint();
	outformat.setIndentSize(1);
	outformat.setIndent("\t");
	outformat.setEncoding(UTF_8.name());
	XMLWriter writer = new XMLWriter(out, outformat);
	writer.write(doc);
	writer.flush();
	out.flush();
	out.close();
}
 
Example 10
Source File: DataDstXml.java    From xresloader with MIT License 5 votes vote down vote up
/**
 * 转储常量数据
 * 
 * @return 常量数据,不支持的时候返回空
 */
public final byte[] dumpConst(HashMap<String, Object> data) throws ConvException, IOException {
    // pretty print
    OutputFormat of = null;
    if (ProgramOptions.getInstance().prettyIndent <= 0) {
        of = OutputFormat.createCompactFormat();
    } else {
        of = OutputFormat.createPrettyPrint();
        of.setIndentSize(ProgramOptions.getInstance().prettyIndent);
    }

    // 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.");

    writeData(doc.getRootElement(), data, "");

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLWriter writer = new XMLWriter(bos, of);
    writer.write(doc);

    return bos.toByteArray();
}
 
Example 11
Source File: FeedsApiControllerV1.java    From gocd with Apache License 2.0 5 votes vote down vote up
public static String prettyPrint(Document document) throws IOException {
    StringWriter writer = new StringWriter();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setNewLineAfterDeclaration(false);
    format.setIndentSize(2);
    new XMLWriter(writer, format).write(document);

    return writer.toString();
}
 
Example 12
Source File: DataDstXml.java    From xresloader with MIT License 4 votes vote down vote up
@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");
    Element data_message_type = DocumentHelper.createElement("data_message_type");

    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());

    writeData(data_message_type, data_obj.data_message_type, data_message_type.getName());

    doc.getRootElement().add(header);
    doc.getRootElement().add(body);
    doc.getRootElement().add(data_message_type);

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        XMLWriter writer = new XMLWriter(bos, of);
        writer.write(doc);

        return bos.toByteArray();
    } catch (Exception e) {
        this.logErrorMessage("write xml failed, %s", e.getMessage());
        return null;
    }
}