Example usage for org.dom4j.io OutputFormat getEncoding

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

Introduction

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

Prototype

public String getEncoding() 

Source Link

Usage

From source file:architecture.common.xml.XmlWriter.java

License:Apache License

public XmlWriter(OutputStream out, OutputFormat format) throws UnsupportedEncodingException {
    this.format = format;
    this.writer = createWriter(out, format.getEncoding());
    this.autoFlush = true;
    namespaceStack.push(Namespace.NO_NAMESPACE);
}

From source file:architecture.common.xml.XmlWriter.java

License:Apache License

public XmlWriter(OutputFormat format) throws UnsupportedEncodingException {
    this.format = format;
    this.writer = createWriter(System.out, format.getEncoding());
    this.autoFlush = true;
    namespaceStack.push(Namespace.NO_NAMESPACE);
}

From source file:cn.feng.web.ssm.excel.controller.StringUtils.java

License:Apache License

/**
 * //w w w  . j  a v  a 2 s. c o m
 * dom4j 
 * @param document
 * @return
 */
public static String document2str(Document document, String chartset) {
    String result = "";
    OutputFormat format;
    ByteArrayOutputStream out;
    try {
        format = OutputFormat.createPrettyPrint();
        format.setEncoding(chartset);
        out = new ByteArrayOutputStream();
        XMLWriter writer = new XMLWriter(out, format);
        writer.write(document);
        writer.flush();
        writer.close();
        result = out.toString(format.getEncoding());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:com.smartwork.im.utils.XMLWriter.java

License:Open Source License

public XMLWriter(OutputStream out, OutputFormat format) throws UnsupportedEncodingException {
    this.format = format;
    this.writer = createWriter(out, format.getEncoding());
    this.autoFlush = true;
    namespaceStack.push(Namespace.NO_NAMESPACE);
}

From source file:com.smartwork.im.utils.XMLWriter.java

License:Open Source License

public XMLWriter(OutputFormat format) throws UnsupportedEncodingException {
    this.format = format;
    this.writer = createWriter(System.out, format.getEncoding());
    this.autoFlush = true;
    namespaceStack.push(Namespace.NO_NAMESPACE);
}

From source file:com.u2apple.rt.util.StandaloneWriter.java

@Override
protected void writeDeclaration() throws IOException {
    OutputFormat format = getOutputFormat();

    String encoding = format.getEncoding();

    if (!format.isSuppressDeclaration()) {
        if (encoding.equals("UTF8")) {
            writer.write("<?xml version=\"1.0\"");

            if (!format.isOmitEncoding()) {
                writer.write(" encoding=\"UTF-8\"");
            }//from   w w  w . ja  v a2 s. co  m

            writer.write(" standalone=\"yes\"");
            writer.write("?>");
        } else {
            writer.write("<?xml version=\"1.0\"");

            if (!format.isOmitEncoding()) {
                writer.write(" encoding=\"" + encoding + "\"");
            }

            writer.write(" standalone=\"yes\"");
            writer.write("?>");
        }

        if (format.isNewLineAfterDeclaration()) {
            println();
        }
    }
}

From source file:org.talend.core.model.genhtml.XMLHandler.java

License:Open Source License

/**
 * Generates xml file base on inputted path, file path and an instance of <code>Document</code>
 * //from  ww  w .j  a v a2  s . c  o m
 * @param tempFolderPath
 * @param filePath
 * @param document
 */
public static void generateXMLFile(String tempFolderPath, String filePath, Document document) {
    XMLWriter output = null;
    FileOutputStream out = null;
    Writer writer = null;

    try {
        // OutputFormat format = OutputFormat.createPrettyPrint();
        out = new java.io.FileOutputStream(filePath);
        writer = new OutputStreamWriter(out, "UTF-8"); //$NON-NLS-1$

        OutputFormat format = OutputFormat.createPrettyPrint();
        output = new XMLWriter(writer, format) {

            /*
             * (non-Javadoc)
             * 
             * @see org.dom4j.io.XMLWriter#writeDeclaration()
             */
            @Override
            protected void writeDeclaration() throws IOException {
                OutputFormat formatTmp = this.getOutputFormat();
                String encoding = formatTmp.getEncoding();

                // Only print of declaration is not suppressed
                if (!formatTmp.isSuppressDeclaration()) {
                    // Assume 1.0 version
                    if (encoding.equals("UTF8")) { //$NON-NLS-1$
                        writer.write("<?xml version=\"1.1\""); //$NON-NLS-1$

                        if (!formatTmp.isOmitEncoding()) {
                            writer.write(" encoding=\"UTF-8\""); //$NON-NLS-1$
                        }

                        writer.write("?>"); //$NON-NLS-1$
                    } else {
                        writer.write("<?xml version=\"1.1\""); //$NON-NLS-1$

                        if (!formatTmp.isOmitEncoding()) {
                            writer.write(" encoding=\"" + encoding + "\""); //$NON-NLS-1$ //$NON-NLS-2$
                        }

                        writer.write("?>"); //$NON-NLS-1$
                    }

                    if (formatTmp.isNewLineAfterDeclaration()) {
                        println();
                    }
                }
            }
        };
        output.setMaximumAllowedCharacter(127);
        output.write(document);
        output.flush();
    } catch (Exception e) {
        ExceptionHandler.process(e);
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (Exception e) {
                ExceptionHandler.process(e);
            }
        }
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception e) {
                ExceptionHandler.process(e);
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
                ExceptionHandler.process(e);
            }
        }

    }
}