Example usage for org.dom4j.io OutputFormat setExpandEmptyElements

List of usage examples for org.dom4j.io OutputFormat setExpandEmptyElements

Introduction

In this page you can find the example usage for org.dom4j.io OutputFormat setExpandEmptyElements.

Prototype

public void setExpandEmptyElements(boolean expandEmptyElements) 

Source Link

Document

This will set whether empty elements are expanded from <tagName> to <tagName></tagName>.

Usage

From source file:au.com.acegi.xmlformat.XmlFormatPlugin.java

License:Apache License

private OutputFormat buildFormatter() {
    final OutputFormat fmt = createPrettyPrint();
    fmt.setAttributeQuoteCharacter(attributeQuoteChar);
    fmt.setEncoding(encoding);//from  ww  w .  j a  v  a2s. c  o m
    fmt.setExpandEmptyElements(expandEmptyElements);
    fmt.setIndentSize(indentSize);
    fmt.setLineSeparator(determineLineSeparator());
    fmt.setNewLineAfterDeclaration(newLineAfterDeclaration);
    fmt.setNewLineAfterNTags(newLineAfterNTags);
    fmt.setNewlines(newlines);
    fmt.setOmitEncoding(omitEncoding);
    fmt.setPadText(padText);
    fmt.setSuppressDeclaration(suppressDeclaration);
    fmt.setTrimText(trimText);
    fmt.setXHTML(xhtml);
    return fmt;
}

From source file:com.ai.tools.generator.util.XMLFormatter.java

License:Open Source License

public static String toString(Branch branch, String indent, boolean expandEmptyElements) throws IOException {
    ByteArrayMaker bam = new ByteArrayMaker();

    OutputFormat format = OutputFormat.createPrettyPrint();

    format.setExpandEmptyElements(expandEmptyElements);
    format.setIndent(indent);//from   w  w w. ja  v a 2  s.c  o  m
    format.setLineSeparator("\n");

    XMLWriter writer = new XMLWriter(bam, format);

    writer.write(branch);

    String content = bam.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");
    }

    return content;
}

From source file:com.augmentum.common.util.XMLFormatter.java

License:Open Source License

public static String toString(Branch branch, String indent, boolean expandEmptyElements) throws IOException {

    ByteArrayMaker bam = new ByteArrayMaker();

    OutputFormat format = OutputFormat.createPrettyPrint();

    format.setExpandEmptyElements(expandEmptyElements);
    format.setIndent(indent);//from w w  w . ja  v  a 2 s  . c  om
    format.setLineSeparator("\n");

    XMLWriter writer = new XMLWriter(bam, format);

    writer.write(branch);

    String content = bam.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");
    }

    return content;
}

From source file:com.chingo247.structureapi.plan.document.AbstractDocumentManager.java

protected void save(K key, final V document) {
    documentPool.execute(key, new Runnable() {

        @Override//from  w  ww.  j  a  va2s  .  co m
        public void run() {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setExpandEmptyElements(true);
            XMLWriter writer = null;
            try {
                writer = new XMLWriter(new FileWriter(document.documentFile), format);
                writer.write(document.document);
            } catch (IOException ex) {
                Logger.getLogger(PlanDocumentManager.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if (writer != null) {
                    try {
                        writer.close();
                    } catch (IOException ex) {
                        Logger.getLogger(PlanDocumentManager.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    });
}

From source file:com.chingo247.structureapi.plan.document.AbstractDocumentManager.java

protected void save(K key, final DocumentPluginElement element) {
    documentPool.execute(key, new Runnable() {

        @Override//from w  ww  . j  a v a 2  s. com
        public void run() {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setExpandEmptyElements(true);
            XMLWriter writer = null;
            try {
                File d = element.root.documentFile;
                writer = new XMLWriter(new FileWriter(d), format);
                writer.write(element.pluginElement.getDocument());
            } catch (IOException ex) {
                Logger.getLogger(PlanDocumentManager.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if (writer != null) {
                    try {
                        writer.close();
                    } catch (IOException ex) {
                        Logger.getLogger(PlanDocumentManager.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    });
}

From source file:com.googlecode.fascinator.common.sax.SafeSAXReader.java

License:Open Source License

/**
 * Convert node to string/*from www .  ja v a  2  s. c  o m*/
 * 
 * @param outDoc Node to be converted
 * @return String of the converted node
 * @throws IOException if the conversion fail
 */
public String docToString(Node outDoc) throws IOException {
    Writer osw = new StringWriter();
    OutputFormat opf = new OutputFormat("", false, "UTF-8");
    opf.setSuppressDeclaration(true);
    opf.setExpandEmptyElements(true);
    XMLWriter writer = new XMLWriter(osw, opf);
    writer.setEscapeText(false);
    writer.write(outDoc);
    writer.close();

    return osw.toString();
}

From source file:com.googlecode.fascinator.common.sax.SafeSAXReader.java

License:Open Source License

/**
 * Convert node to stream// w ww.  j a v  a 2  s .co  m
 * 
 * @param outDoc Node to be converted
 * @param outStream output stream of the converted node
 * @throws IOException if the conversion fail
 */
public void docToStream(Node outDoc, OutputStream outStream) throws IOException {
    OutputFormat opf = new OutputFormat("", false, "UTF-8");
    opf.setSuppressDeclaration(true);
    opf.setExpandEmptyElements(true);
    XMLWriter writer = new XMLWriter(outStream, opf);
    writer.setEscapeText(false);
    writer.write(outDoc);
    writer.close();
}

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  .  ja v  a2 s.com*/
    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.liveneo.plat.web.action.MakeLicfile.java

public static void createXmlFile(String fileName, Document doc) {
    try {/*  www .j a v  a 2s.co  m*/
        File testfile = new File("fileName");
        if (testfile.exists()) {
            testfile.delete();
        }
        FileWriter fileWriter = new FileWriter(fileName);
        OutputFormat xmlFormat = OutputFormat.createPrettyPrint();
        xmlFormat.setEncoding("UTF-8");
        xmlFormat.setSuppressDeclaration(false);
        xmlFormat.setExpandEmptyElements(false);
        XMLWriter xmlWriter = new XMLWriter(fileWriter, xmlFormat);
        xmlWriter.write(doc);
        xmlWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.tedi.engine.XMLOutput.java

License:Open Source License

/**
 * Handles functionality of formatting and writing document.
 * /*from  w w w .j av  a  2  s.  c o  m*/
 * @param doc
 *            The document
 * @return the cleaned document.
 * @throws Exception
 */
private String cleanDocument(Document doc) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("Cleaning the document object.");
    }
    String docStr = "";
    // --COMMENTED OUT 10-10-2005
    // JBG----------------------------------------------------------------------------------
    // if (dtd != null && dtd.length()>0) {
    // xmlUtils.setDocument(doc);
    // try {
    // AbstractSchemaDescriptor schema =
    // AbstractSchemaDescriptor.createDescriptor(absoluteDTD_URL.toString());
    // schema.processSchema(doc.getRootElement().getName());
    // xmlUtils.setSchema(schema);
    // xmlUtils.cleanDocument();
    // }
    // catch (Exception e) {
    // execResults.addMessage(ExecutionResults.M_WARNING,
    // ExecutionResults.J2EE_TARGET_ERR,
    // "Error removing optional attributes/elements: " + e.getMessage());
    // }
    // }
    // ----------------------------------------------------------------------------------------------------------------
    cleanElement(doc.getRootElement());
    StringWriter sw = new StringWriter();
    OutputFormat format = isCompact ? OutputFormat.createCompactFormat() : OutputFormat.createPrettyPrint();
    format.setExpandEmptyElements(false);
    XMLWriter writer = new XMLWriter(sw, format);
    writer.setMaximumAllowedCharacter(127);
    writer.write(doc);
    writer.close();
    docStr = sw.toString();
    if (isSuppressDocType && doc.getDocType() != null) {
        int ndx = docStr.indexOf("<" + doc.getRootElement().getName());
        docStr = docStr.substring(ndx);
    }
    return docStr;
}