Example usage for org.dom4j.io OutputFormat setNewlines

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

Introduction

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

Prototype

public void setNewlines(boolean newlines) 

Source Link

Document

DOCUMENT ME!

Usage

From source file:de.innovationgate.wgpublisher.WGACore.java

License:Open Source License

public String updateConfigDocument(Document newConfig) throws UnsupportedEncodingException,
        FileNotFoundException, IOException, ParserConfigurationException, DocumentException {
    String newTimestamp = WGACore.DATEFORMAT_GMT.format(new Date());
    newConfig.getRootElement().addAttribute("timestamp", newTimestamp);

    OutputFormat outputFormat = OutputFormat.createPrettyPrint();
    outputFormat.setTrimText(true);//from   www .  j  a v a2s.c  o  m
    outputFormat.setNewlines(true);

    XMLWriter writer = new XMLWriter(new FileOutputStream(getConfigFile()), outputFormat);
    writer.write(newConfig);
    writer.close();
    return newTimestamp;
}

From source file:edu.ccut.saturn.manager.dict.core.dbmanager.Dom4jUtils.java

License:Apache License

public static String writeXml(String file, Document doc) {

    String message = null;// w w  w . ja v  a2s .co  m
    XMLWriter writer = null;
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("GB2312");
    format.setIndent(true);
    format.setIndent(" ");
    format.setNewlines(true);
    OutputStream out = null;
    try {
        out = new FileOutputStream(file);
    } catch (FileNotFoundException e1) {
        try {
            String path = Thread.currentThread().getContextClassLoader().getResource(BLANK_STRING).getFile()
                    + file.substring(1);

            out = new FileOutputStream(path);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    try {
        writer = new XMLWriter(out, format);
        writer.write(doc);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            writer.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    message = TRUE;
    return message;
}

From source file:eml.studio.server.oozie.workflow.WFGraph.java

License:Open Source License

/**
 * Transform the Graph into an workflow xml definition
 * @param jobname the job name of Oozie job, can't be null
 * @return workflow xml//from   w  w w. j a  v a2  s  .  com
 */
public String toWorkflow(String jobname) {
    Namespace xmlns = new Namespace("", "uri:oozie:workflow:0.4"); // root namespace uri
    QName qName = QName.get("workflow-app", xmlns); // your root element's name
    Element workflow = DocumentHelper.createElement(qName);
    Document xmldoc = DocumentHelper.createDocument(workflow);
    // Create workflow root
    workflow.addAttribute("xmlns", "uri:oozie:workflow:0.4");
    // <workflow-app name='xxx'></workflow-app>
    if (jobname == null || "".equals(jobname))
        workflow.addAttribute("name", "Not specified");
    else
        workflow.addAttribute("name", jobname);

    Queue<NodeDef> que = new LinkedList<NodeDef>();
    que.add(start);

    while (!que.isEmpty()) {
        NodeDef cur = que.remove();

        cur.append2XML(workflow);

        for (NodeDef toNode : cur.getOutNodes()) {
            toNode.delInNode(cur);
            if (toNode.getInDegree() == 0)
                que.add(toNode);
        }
    }

    // Set XML document format
    OutputFormat outputFormat = OutputFormat.createPrettyPrint();
    // Set XML encoding, use the specified encoding to save the XML document to the string, it can be specified GBK or ISO8859-1
    outputFormat.setEncoding("UTF-8");
    outputFormat.setSuppressDeclaration(true); // Whether generate xml header
    outputFormat.setIndent(true); // Whether set indentation
    outputFormat.setIndent("    "); // Implement indentation with four spaces
    outputFormat.setNewlines(true); // Set whether to wrap

    try {
        // stringWriter is used to save xml document
        StringWriter stringWriter = new StringWriter();
        // xmlWriter is used to write XML document to string(tool)
        XMLWriter xmlWriter = new XMLWriter(stringWriter, outputFormat);

        // Write the created XML document into the string
        xmlWriter.write(xmldoc);

        xmlWriter.close();

        System.out.println(stringWriter.toString().trim());
        // Print the string, that is, the XML document
        return stringWriter.toString().trim();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:eml.studio.server.oozie.workflow.WFGraph.java

License:Open Source License

public static void main(String args[]) {

    Namespace rootNs = new Namespace("", "uri:oozie:workflow:0.4"); // root namespace uri
    QName rootQName = QName.get("workflow-app", rootNs); // your root element's name
    Element workflow = DocumentHelper.createElement(rootQName);
    Document doc = DocumentHelper.createDocument(workflow);

    workflow.addAttribute("name", "test");
    Element test = workflow.addElement("test");
    test.addText("hello");
    OutputFormat outputFormat = OutputFormat.createPrettyPrint();
    outputFormat.setEncoding("UTF-8");
    outputFormat.setIndent(true);//w  ww . j ava  2 s  .  co  m
    outputFormat.setIndent("    ");
    outputFormat.setNewlines(true);
    try {
        StringWriter stringWriter = new StringWriter();
        XMLWriter xmlWriter = new XMLWriter(stringWriter);
        xmlWriter.write(doc);
        xmlWriter.close();
        System.out.println(doc.asXML());
        System.out.println(stringWriter.toString().trim());

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:eu.planets_project.pp.plato.action.workflow.ValidatePlanAction.java

License:Open Source License

/**
 * reads the executable preservation plan and formats it.
 * //from ww  w  . j  a  va 2 s.  co m
 */
private String formatExecutablePlan(String executablePlan) {

    if (executablePlan == null || "".equals(executablePlan)) {
        return "";
    }

    try {
        Document doc = DocumentHelper.parseText(executablePlan);

        StringWriter sw = new StringWriter();

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setNewlines(true);
        format.setTrimText(true);
        format.setIndent("  ");
        format.setExpandEmptyElements(false);
        format.setNewLineAfterNTags(20);

        XMLWriter writer = new XMLWriter(sw, format);

        writer.write(doc);
        writer.close();

        return sw.toString();

    } catch (DocumentException e) {
        return "";
    } catch (IOException e) {
        return "";
    }
}

From source file:gov.guilin.util.SettingUtils.java

License:Open Source License

/**
 * /*  w ww .  j av a  2  s  . com*/
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File guilinXmlFile = new ClassPathResource(CommonAttributes.XML_PATH).getFile();
        Document document = new SAXReader().read(guilinXmlFile);
        List<Element> elements = document.selectNodes("/guilin/setting");
        for (Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = beanUtils.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        FileOutputStream fileOutputStream = null;
        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            fileOutputStream = new FileOutputStream(guilinXmlFile);
            xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
            xmlWriter.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                }
            }
            IOUtils.closeQuietly(fileOutputStream);
        }

        Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:iqq.app.service.impl.IMResourceServiceImpl.java

License:Apache License

@Override
public void writeXml(Document document, File xmlFile) {
    try {//w  ww. j  ava 2 s. c  om
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();// XML?
        outputFormat.setEncoding("UTF-8");// XML?
        outputFormat.setIndent(true);// ?
        outputFormat.setIndent("   ");// TAB?
        outputFormat.setNewlines(true);// ??
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(xmlFile), outputFormat);
        xmlWriter.write(document);
        xmlWriter.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:iqq.app.util.XmlUtils.java

License:Apache License

/**
 * XML//from   w w  w.j  a  v  a 2 s .c o  m
 *
 * @param document
 * @param xmlFile
 * @throws IOException
 */
public static void writeXml(String filename, Document document) throws IOException {
    LOG.debug("XML: " + filename);
    OutputFormat outputFormat = OutputFormat.createPrettyPrint();// XML?
    outputFormat.setEncoding("UTF-8");// XML?
    outputFormat.setIndent(true);// ?
    outputFormat.setIndent("   ");// TAB?
    outputFormat.setNewlines(true);// ??
    synchronized (document) {
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(filename), outputFormat);
        xmlWriter.write(document);
        xmlWriter.close();
    }
}

From source file:net.bpfurtado.ljcolligo.util.Util.java

License:Open Source License

public static void save(Element root, File file) {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setIndentSize(4);//from  www . jav  a 2  s .  c om
    format.setEncoding("UTF-8");
    format.setNewlines(true);
    format.setLineSeparator(System.getProperty("line.separator"));
    XMLWriter writer;
    try {
        writer = new XMLWriter(new FileWriter(file), format);
        writer.startDocument();
        writer.write(root);
        writer.close();
        logger.debug("All entries saved to file [" + file.getAbsolutePath() + "]");
    } catch (Exception e) {
        throw new LJColligoException("File [" + file.getAbsolutePath() + "]", e);
    }
}

From source file:net.bpfurtado.tas.model.persistence.XMLAdventureWriter.java

License:Open Source License

private File save(Document doc) {
    try {//  ww w .j av  a2 s.co m
        if (!saveFile.getName().endsWith(".adv.xml")) {
            saveFile = new File(saveFile.getAbsolutePath() + ".adv.xml");
        }

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("ISO-8859-1");
        format.setNewlines(true);
        format.setLineSeparator(System.getProperty("line.separator"));
        XMLWriter writer = new XMLWriter(new FileWriter(saveFile), format);

        writer.write(doc);
        writer.close();

        return saveFile;
    } catch (Exception e) {
        throw new AdventureException("Error writing adventure", e);
    }
}