Example usage for com.liferay.portal.kernel.io.unsync UnsyncByteArrayOutputStream toString

List of usage examples for com.liferay.portal.kernel.io.unsync UnsyncByteArrayOutputStream toString

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.io.unsync UnsyncByteArrayOutputStream toString.

Prototype

public String toString(String charsetName) throws UnsupportedEncodingException 

Source Link

Usage

From source file:com.liferay.petra.xml.Dom4jUtil.java

License:Open Source License

public static String toString(Node node, String indent, boolean expandEmptyElements, boolean trimText)
        throws IOException {

    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

    OutputFormat outputFormat = OutputFormat.createPrettyPrint();

    outputFormat.setExpandEmptyElements(expandEmptyElements);
    outputFormat.setIndent(indent);//from w ww. j av  a2  s  .co  m
    outputFormat.setLineSeparator(StringPool.NEW_LINE);
    outputFormat.setTrimText(trimText);

    XMLWriter xmlWriter = new XMLWriter(unsyncByteArrayOutputStream, outputFormat);

    xmlWriter.write(node);

    String content = unsyncByteArrayOutputStream.toString(StringPool.UTF8);

    // LEP-4257

    //content = StringUtil.replace(content, "\n\n\n", "\n\n");

    if (content.endsWith("\n\n")) {
        content = content.substring(0, content.length() - 2);
    }

    if (content.endsWith("\n")) {
        content = content.substring(0, content.length() - 1);
    }

    while (content.contains(" \n")) {
        content = StringUtil.replace(content, " \n", "\n");
    }

    if (content.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
        content = StringUtil.replaceFirst(content, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
                "<?xml version=\"1.0\"?>");
    }

    return content;
}

From source file:com.liferay.util.xml.XMLFormatter.java

License:Open Source License

public static String toString(Node node, String indent, boolean expandEmptyElements, boolean trimText)
        throws IOException {

    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

    OutputFormat outputFormat = OutputFormat.createPrettyPrint();

    outputFormat.setExpandEmptyElements(expandEmptyElements);
    outputFormat.setIndent(indent);//from   ww w . j ava  2  s  .co  m
    outputFormat.setLineSeparator(StringPool.NEW_LINE);
    outputFormat.setTrimText(trimText);

    XMLWriter xmlWriter = new XMLWriter(unsyncByteArrayOutputStream, outputFormat);

    xmlWriter.write(node);

    String content = unsyncByteArrayOutputStream.toString(StringPool.UTF8);

    // LEP-4257

    //content = StringUtil.replace(content, "\n\n\n", "\n\n");

    if (content.endsWith("\n\n")) {
        content = content.substring(0, content.length() - 2);
    }

    if (content.endsWith("\n")) {
        content = content.substring(0, content.length() - 1);
    }

    while (content.indexOf(" \n") != -1) {
        content = StringUtil.replace(content, " \n", "\n");
    }

    if (content.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
        content = StringUtil.replaceFirst(content, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
                "<?xml version=\"1.0\"?>");
    }

    return content;
}