Example usage for org.dom4j.io OutputFormat setEncoding

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

Introduction

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

Prototype

public void setEncoding(String encoding) 

Source Link

Document

DOCUMENT ME!

Usage

From source file:com.alibaba.citrus.springext.util.ConvertToUnqualifiedStyle.java

License:Open Source License

private static void writeDocument(Document doc, OutputStream stream) throws IOException {
    String charset = "UTF-8";
    Writer writer = new OutputStreamWriter(stream, charset);

    OutputFormat format = new OutputFormat();

    format.setEncoding(charset);

    XMLWriter xmlWriter = new XMLWriter(writer, format);
    xmlWriter.write(doc);/*from   w  ww.j av  a  2  s . co m*/
    xmlWriter.flush();
}

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

License:Open Source License

public static String formatXsdSource(String xsdSource, boolean suppressDeclaration) {
    try {/*from  w w  w .  j av  a  2s  .  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;

    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();
    }/*from   w ww. j  a  v a2 s  .  co  m*/

    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 format(Document document, OutputFormat format, String encoding) {

    StringWriter writer = new StringWriter();

    format.setEncoding(encoding);

    format.setNewLineAfterDeclaration(false);
    // format.setSuppressDeclaration(suppressDeclaration);

    XMLWriter xmlwriter = new XMLWriter(writer, format);

    String result = ""; //$NON-NLS-1$

    try {/*  w  w w  . j  a  v a 2  s  .c o m*/

        xmlwriter.write(document);
        result = writer.toString().replaceAll("<\\?xml.*?\\?>", "").trim();//$NON-NLS-1$//$NON-NLS-2$
    } catch (Exception e) {
        log.error(e.getMessage(), e);

    } finally {
        try {
            if (xmlwriter != null)
                xmlwriter.close();

            if (writer != null)
                writer.close();
        } catch (IOException e) {

        }
    }

    return result;
}

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;/*from   ww w. j ava 2  s . c o m*/
    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

/**
 * // www  .  java  2  s  .  co  m
 * 
 * @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.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * XMLDocumentjava.io.Writer?/*from  w  ww. jav  a  2s. co  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) {
            }
        }
    }
}

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

License:Open Source License

/**
 * XMLDocumentjava.io.Writer?//from  w  w w.j a v a  2  s .co m
 *
 *
 *
 * ??Schema
 *
 *
 *
 * @param document
 *            XML
 * @param outStream
 *            
 *
 *
 *
 * @param encoding
 *            ?
 * @throws XMLDocException
 *             
 *
 *
 * @throws BaseException
 *
 */
public static void toXML(Document document, java.io.OutputStream outStream, String encoding)
        throws BaseException {
    //
    OutputFormat outformat = new OutputFormat();
    outformat.setIndentSize(0);
    outformat.setNewlines(true);
    outformat.setTrimText(true);

    // OutputFormat outformat = OutputFormat.createPrettyPrint();
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    // ?
    outformat.setEncoding(encoding);
    XMLWriter xmlWriter = null;
    try {
        xmlWriter = new XMLWriter(new OutputStreamWriter(outStream), 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) {
            }
        }
    }
}

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

License:Open Source License

public static void element2XML(Element element, java.io.OutputStream outStream, String encoding)
        throws BaseException {
    ////from w w  w  . j  a  v  a 2 s.  c  o m
    OutputFormat outformat = new OutputFormat();
    outformat.setIndentSize(0);
    outformat.setNewlines(false);
    outformat.setTrimText(true);

    // OutputFormat outformat = OutputFormat.createPrettyPrint();
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    // ?
    outformat.setEncoding(encoding);
    XMLWriter xmlWriter = null;
    try {
        xmlWriter = new XMLWriter(new OutputStreamWriter(outStream), outformat);
        xmlWriter.write(element);
        xmlWriter.flush();
    } catch (IOException ex) {
        throw new BaseException("UTIL-0001", ex);
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (IOException ex) {
            }
        }
    }
}

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

License:Open Source License

/**
 * This method will generate XML file in a StringBuffer based on the given
 * Dom4j object./*w ww.  j  ava  2  s  .  c  o m*/
 * 
 * @param xmlObj
 *            Object
 * @param encoding
 *            String
 * @throws IOException
 * @return StringBuffer
 * @throws BaseException
 */
public static StringBuffer generateXMLStringBuffer(Object xmlObj, String encoding) throws BaseException {
    StringWriter writer = new StringWriter();
    OutputFormat outformat = OutputFormat.createPrettyPrint();

    // ?
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    outformat.setEncoding(encoding);

    // dom4j ?OBJECT
    XMLWriter xmlWriter = null;
    xmlWriter = new XMLWriter(writer, outformat);

    try {
        xmlWriter.write(xmlObj);
        xmlWriter.flush();
    } catch (Exception ex) {
        throw new BaseException("UTIL-0002", ex);
    }

    return writer.getBuffer();
}