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:org.fireflow.engine.modules.persistence.hibernate.VariableHeaderType.java

License:Open Source License

public static String map2XmlString(Properties arg0) {
    if (arg0 == null)
        return null;
    Properties map = arg0;//from  w  w w .  ja  va2 s.  co  m
    Document document = documentFactory.createDocument();
    Element root = documentFactory.createElement("map");
    document.setRootElement(root);

    Iterator<Object> keys = map.keySet().iterator();
    while (keys.hasNext()) {
        String key = (String) keys.next();
        String value = (String) map.get(key);
        Element entry = documentFactory.createElement("entry");
        root.add(entry);
        Element keyElm = documentFactory.createElement("key");
        entry.add(keyElm);
        keyElm.setText(key);

        Element valueElm = documentFactory.createElement("value");
        entry.add(valueElm);

        valueElm.add(documentFactory.createCDATA(value));
    }

    try {
        StringWriter writer = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();

        String jvmEncoding = Charset.defaultCharset().name();
        format.setEncoding(jvmEncoding);

        XMLWriter xmlwriter = new XMLWriter(writer, format);
        xmlwriter.write(document);
        return writer.getBuffer().toString();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return document.toString();
}

From source file:org.foxbpm.engine.test.AbstractFoxBpmTestCase.java

License:Apache License

@Before
public void annotationDeploymentSetUp() throws Exception {

    Method method = null;//from ww  w. j av  a  2s. com
    try {
        method = this.getClass().getDeclaredMethod(name.getMethodName(), (Class<?>[]) null);
    } catch (Exception e) {
        throw new FoxBPMException("?!", e);
    }
    if (method.isAnnotationPresent(Clear.class)) {
        Clear clearAnnotation = method.getAnnotation(Clear.class);
        String[] tableNames = clearAnnotation.tables();
        for (String tableName : tableNames) {
            jdbcTemplate.execute("delete from " + tableName);
        }
    }
    Deployment deploymentAnnotation = method.getAnnotation(Deployment.class);
    if (deploymentAnnotation != null) {
        String[] resources = deploymentAnnotation.resources();
        if (resources.length == 0) {
            return;
        }
        DeploymentBuilder deploymentBuilder = null;// processEngine.getModelService().createDeployment().name("??");
        // ?????
        BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();
        BpmnModel bpmnModel = null;
        SAXReader reader = new SAXReader();
        ByteArrayOutputStream out = null;
        OutputFormat format = null;
        for (String resource : resources) {
            bpmnModel = bpmnXMLConverter
                    .convertToBpmnModel(reader.read(ReflectUtil.getResourceAsStream(resource)));
            deploymentBuilder = processEngine.getModelService().createDeployment().name("??");
            // deploymentBuilder.addClasspathResource(resource);
            try {
                out = new ByteArrayOutputStream();
                // ?
                format = OutputFormat.createPrettyPrint();
                format.setEncoding("UTF-8");
                XMLWriter xmlWriter = new XMLWriter(out, format);
                xmlWriter.setEscapeText(false);
                xmlWriter.write(bpmnXMLConverter.convertToXML(bpmnModel));
                xmlWriter.close();
                System.out.println(resource + "---------------" + out.toString());
                deploymentBuilder.addInputStream(resource, new ByteArrayInputStream(out.toByteArray()));
                deploymentBuilder.deploy();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.infoglue.cms.applications.common.actions.SimpleXmlServiceAction.java

License:Open Source License

protected String getFormattedDocument(Document doc, boolean compact, boolean supressDecl) {
    OutputFormat format = compact ? OutputFormat.createCompactFormat() : OutputFormat.createPrettyPrint();
    format.setSuppressDeclaration(supressDecl);
    format.setEncoding(ENCODING);
    format.setExpandEmptyElements(false);
    StringWriter stringWriter = new StringWriter();
    XMLWriter writer = new XMLWriter(stringWriter, format);
    try {/*from  w w  w. j a va 2s  . c  o  m*/
        writer.write(doc);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return stringWriter.toString();
}

From source file:org.infoglue.cms.applications.contenttool.actions.ContentTreeXMLAction.java

License:Open Source License

private String getFormattedDocument(Document doc, boolean compact) {
    OutputFormat format = compact ? OutputFormat.createCompactFormat() : OutputFormat.createPrettyPrint();
    // format.setEncoding("iso-8859-1");
    format.setEncoding("UTF-8");
    format.setExpandEmptyElements(false);
    StringWriter stringWriter = new StringWriter();
    XMLWriter writer = new XMLWriter(stringWriter, format);
    try {/*from   w w w  .  j  a  v a  2s . c  om*/
        writer.write(doc);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return stringWriter.toString();
}

From source file:org.infoglue.cms.util.dom.DOMBuilder.java

License:Open Source License

/**
 * This method writes a document to file.
 *///from  w  ww .j  av a 2 s .co m

public void write(Document document, String fileName) throws Exception {
    OutputFormat format = OutputFormat.createCompactFormat();
    format.setEncoding("UTF-8");
    XMLWriter writer = new XMLWriter(new FileWriter(fileName), format);
    writer.write(document);
    writer.close();
}

From source file:org.infoglue.cms.util.dom.DOMBuilder.java

License:Open Source License

/**
 * This method gets the xml as a string with the correct encoding.
 *///from   ww  w  .  j a v a2s.c o m

private String getEncodedString(Element element) throws Exception {
    OutputFormat outFormat = OutputFormat.createCompactFormat();
    outFormat.setEncoding("UTF-8");
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    XMLWriter out = new XMLWriter(bao, outFormat);
    out.write(element);
    out.flush();
    String s = bao.toString();
    logger.info("OUT: " + s);
    return s;
}

From source file:org.infoglue.cms.util.dom.DOMBuilder.java

License:Open Source License

public String getFormattedDocument(Document doc, boolean compact, boolean supressDecl, String encoding) {
    OutputFormat format = compact ? OutputFormat.createCompactFormat() : OutputFormat.createPrettyPrint();
    format.setSuppressDeclaration(supressDecl);
    format.setEncoding(encoding);
    format.setExpandEmptyElements(false);
    StringWriter stringWriter = new StringWriter();
    XMLWriter writer = new XMLWriter(stringWriter, format);
    try {/*from  w  w  w.j a  v a  2s . c  om*/
        writer.write(doc);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return stringWriter.toString();
}

From source file:org.infoglue.common.util.dom.DOMBuilder.java

License:Open Source License

/**
 * This method writes a document to file.
 *///from  w  w  w. ja  va 2  s  .co  m

public void write(Document document, String fileName) throws Exception {
    OutputFormat format = OutputFormat.createCompactFormat();
    format.setEncoding("UTF-8");
    XMLWriter writer = new XMLWriter(new FileWriter(fileName), format);
    writer.write(document);
    writer.close();

    /*
    FileHelper.writeToFile(new File(fileName + "2"), document.asXML(), false);
    FileHelper.writeUTF8ToFileSpecial(new File(fileName + "3"), document.asXML(), false);
    FileHelper.writeUTF8(new File(fileName + "4"), document.asXML(), false);
    FileHelper.writeUTF8ToFile(new File(fileName + "5"), document.asXML(), false);
    */
}

From source file:org.intalio.tempo.workflow.fds.dispatches.LoggerDispatcher.java

License:Open Source License

private void logMessage(String message, org.dom4j.Document doc) {
    if (logger.isDebugEnabled()) {
        try {//from   w ww . j a v a2s .  co m
            logger.debug("Dispatcher " + targetDispatcherClass.getCanonicalName() + ": " + message);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            OutputFormat of = OutputFormat.createPrettyPrint();
            of.setEncoding("UTF-8");

            XMLWriter writer = new XMLWriter(bos, of);
            writer.write(doc);

            String serializedDoc = bos.toString();

            logger.debug(serializedDoc);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:org.jin.dic.data.pub.ldoce.v5.Convert2Html.java

License:Open Source License

private static byte[] convert(String data) throws DocumentException, IOException {
    data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
            + rmES.matcher(rmDummyTag.matcher(data).replaceAll("")).replaceAll("");
    data = data.replaceAll("\\|", ",");
    _ByteArrayOutputStream bos = new _ByteArrayOutputStream();
    _ByteArrayInputStream bis = new _ByteArrayInputStream(data.getBytes("utf-8"));
    SAXReader saxR = null;// w w w.j av a2  s .c o  m
    Document doc = null, des = null;
    Element root = null, desRoot = null;
    XMLWriter xmlWriter = null;
    OutputFormat fmt = null;
    saxR = new SAXReader();
    doc = saxR.read(bis);
    root = doc.getRootElement();

    des = DocumentHelper.createDocument();
    desRoot = DocumentHelper.createElement("span");
    desRoot.addAttribute("class", getClass(root));
    Element child;
    List children = root.elements();
    for (int i = 0; i < children.size(); i++) {
        child = (Element) children.get(i);
        addChildren(child, desRoot);
    }
    des.setRootElement(desRoot);

    fmt = OutputFormat.createCompactFormat();
    fmt.setEncoding("utf-16le");
    fmt.setTrimText(false);
    xmlWriter = new XMLWriter(bos, fmt);
    xmlWriter.write(des);
    xmlWriter.close();
    return bos.toByteArray();
}