Example usage for org.dom4j.io OutputFormat createPrettyPrint

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

Introduction

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

Prototype

public static OutputFormat createPrettyPrint() 

Source Link

Document

A static helper method to create the default pretty printing format.

Usage

From source file:com.amalto.workbench.utils.Util.java

License:Open Source License

public static String formatXsdSource(String xsdSource, boolean suppressDeclaration) {
    try {//from   w  ww  .  java 2 s . c  o m
        SAXReader reader = new SAXReader();
        org.dom4j.Document document = reader.read(new StringReader(xsdSource));
        StringWriter writer = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");//$NON-NLS-1$
        format.setSuppressDeclaration(suppressDeclaration);
        XMLWriter xmlwriter = new XMLWriter(writer, format);
        xmlwriter.write(document);
        String str = writer.toString();
        writer.close();
        xmlwriter.close();
        return str;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return xsdSource;

}

From source file:com.amalto.workbench.utils.XmlUtil.java

License:Open Source License

public static void write(Document document, String filePath, String printMode, String encoding)
        throws IOException {

    OutputFormat format = null;/*from   w w  w.  jav a2 s .c  om*/

    if (printMode.toLowerCase().equals("pretty")) {//$NON-NLS-1$
        // Pretty print the document
        format = OutputFormat.createPrettyPrint();
    } else if (printMode.toLowerCase().equals("compact")) {//$NON-NLS-1$
        // Compact format
        format = OutputFormat.createCompactFormat();
    }

    format.setEncoding(encoding);

    // lets write to a file
    XMLWriter writer = new XMLWriter(new FileOutputStream(filePath), format);

    // XMLWriter logger = new XMLWriter( System.out, format );

    writer.write(document);

    logger.info(Messages.bind(Messages.XmlUtil_Loginfo1, filePath));

    // logger.write( document );

    // logger.close();

    writer.close();
}

From source file:com.amalto.workbench.utils.XmlUtil.java

License:Open Source License

public static String formatPretty(String xml, String encoding) throws DocumentException {
    Document document = fromXml(xml);
    return format(document, OutputFormat.createPrettyPrint(), encoding);
}

From source file:com.amalto.workbench.utils.XmlUtil.java

License:Open Source License

public static String formatXmlSource(String xmlSource) {

    SAXReader reader = new SAXReader();
    StringReader stringReader = new StringReader(xmlSource);
    StringWriter writer = null;/*  w ww  .  ja  v  a 2 s.  c om*/
    XMLWriter xmlwriter = null;
    String result = xmlSource;

    try {
        Document document = reader.read(stringReader);
        writer = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");//$NON-NLS-1$
        format.setIndentSize(4);
        format.setSuppressDeclaration(true);
        xmlwriter = new XMLWriter(writer, format);
        xmlwriter.write(document);
        result = writer.toString();
    } catch (Exception e) {

    } finally {

        try {
            if (stringReader != null)
                stringReader.close();

            if (xmlwriter != null)
                xmlwriter.close();

            if (writer != null)
                writer.close();

        } catch (Exception e) {

        }

    }
    return result;

}

From source file:com.app.util.SettingUtils.java

License:Open Source License

/**
 * /*from ww w . j a  v  a  2s . com*/
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File appXmlFile = new ClassPathResource(CommonAttributes.APP_XML_PATH).getFile();
        Document document = new SAXReader().read(appXmlFile);
        List<Element> elements = document.selectNodes("/app/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(appXmlFile);
            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:com.arc.mw.util.StateSaver.java

License:Open Source License

/**
 * Save the state of the root object into the given file.
 * @param out ascii stream to write to.//from   w ww .  j a va2  s.c  om
 * @param object object whose state is to be saved.
 * @exception IOException error occurred in writing file.
 */
public static void saveState(Writer out, IXMLSavable object) throws IOException {
    DocumentFactory factory = DocumentFactory.getInstance();
    Document document = factory.createDocument();
    Element e = factory.createElement("root");
    object.saveState(e);
    document.setRootElement(e);
    XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
    writer.write(document);
}

From source file:com.arthurh.xmergel.Xmergel.java

License:Open Source License

public void combine(List<Path> files)
        throws IOException, SAXException, TransformerException, DocumentException {
    Document finalDocument = this.saxReader.read(files.get(0).toFile());
    for (Path file : files) {
        finalDocument = this.xdtTransformer.transform(finalDocument, this.saxReader.read(file.toFile()));
    }//from   w  ww  .  j a v a2  s  . c  o  m
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter xmlWriter = new XMLWriter(new FileWriter(this.resultFile.toFile()), format);
    xmlWriter.write(finalDocument);
    xmlWriter.close();
}

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  ww  w.  j  a 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.beetle.framework.resource.container.ContainerConfig.java

License:Apache License

/**
 * //from  ww w . j a v a  2s.  c  o  m
 *
 * @param tagname --??
 * @param key     --??
 * @throws Exception
 */
public static void setContainValue(String tagname, String key, String value) throws Exception {
    Document doc = XMLReader.getXmlDoc(sysconfigFileName);
    Node node = doc.selectSingleNode(XMLReader.convertPath(tagname));
    if (node != null) {
        @SuppressWarnings("rawtypes")
        Iterator it = node.selectNodes("item").iterator();
        while (it.hasNext()) {
            Element e = (Element) it.next();
            String id = e.valueOf("@name");
            if (id != null && id.equals(key)) {
                e.addAttribute("value", value);
                break;
            }
        }
    }
    File f = new File(sysconfigFileName);
    if (f.exists()) {
        OutputFormat format = OutputFormat.createPrettyPrint();
        FileOutputStream fos = new FileOutputStream(f);
        XMLWriter writer = new XMLWriter(fos, format);
        writer.write(doc);
        writer.close();
    } else {
        AppLogger.getInstance(ContainerConfig.class).error("??jarxml");
    }
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * XMLDocumentjava.io.Writer?//from  ww w.ja v  a 2 s . c  o m
 *
 *
 *
 * ??Schema
 *
 *
 *
 * @param document
 *            XML
 * @param outWriter
 *            
 *
 *
 *
 * @param encoding
 *            ?
 * @throws XMLDocException
 *             
 *
 *
 * @throws BaseException
 *
 */
public static void toXML(Document document, java.io.Writer outWriter, String encoding) throws BaseException {
    //
    OutputFormat outformat = OutputFormat.createPrettyPrint();
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    // ?
    outformat.setEncoding(encoding);
    XMLWriter xmlWriter = null;
    try {
        xmlWriter = new XMLWriter(outWriter, outformat);
        xmlWriter.write(document);
        xmlWriter.flush();
    } catch (IOException ex) {
        throw new BaseException("UTIL-0001", ex);
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (IOException ex) {
            }
        }
    }
}