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.snipsnap.xmlrpc.SnipSnapHandler.java

License:Open Source License

public String getSnipAsXml(String name) {
    Snip snip = space.load(name);//  w  w w  .  j ava 2s  . c  om
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    OutputFormat outputFormat = OutputFormat.createCompactFormat();
    outputFormat.setEncoding("UTF-8");
    try {
        XMLWriter writer = new XMLWriter(out, outputFormat);
        writer.write(SnipSerializer.getInstance().serialize(snip));
        writer.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return out.toString();
}

From source file:org.talend.componentdesigner.manager.ComponentFolderManager.java

License:Open Source License

/**
 * DOC slanglois Comment method "writeXMLContent".
 * //from  w  w w .j a  v a 2s.c  o m
 * @param iFile
 * @param document
 * @param enCode
 * @throws CoreException
 */
private void writeXMLContent(IFile iFile, Document document, String enCode) throws CoreException {
    PrintWriter pw = null;
    XMLWriter writer = null;
    byte[] byteArray = null;

    // get xml content as inputstream
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    }
    DOMSource source = new DOMSource(document);
    transformer.setOutputProperty(OutputKeys.ENCODING, enCode);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$

    ByteArrayOutputStream sw = new ByteArrayOutputStream();
    pw = new PrintWriter(sw);
    StreamResult result = new StreamResult(pw);
    try {
        transformer.transform(source, result);
    } catch (TransformerException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    }
    try {
        sw.flush();
    } catch (IOException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    } finally {
        if (pw != null) {
            pw.close();
        }
    }
    byteArray = sw.toByteArray();

    // format the xml content
    SAXReader saxReader = new SAXReader();

    org.dom4j.Document dom4jDocument = null;
    try {
        dom4jDocument = saxReader.read(new ByteArrayInputStream(byteArray));
    } catch (DocumentException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    }

    /** format the output like the webBrowser */

    OutputFormat format = OutputFormat.createPrettyPrint();

    /** give the xml encoding */

    format.setEncoding(enCode);

    sw = new ByteArrayOutputStream();

    try {
        writer = new XMLWriter(sw, format);
    } catch (UnsupportedEncodingException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    }

    try {
        writer.write(dom4jDocument);
        writer.flush();
    } catch (IOException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                // e.printStackTrace();
                org.talend.componentdesigner.exception.ExceptionHandler.process(e);
            }
        }
    }
    byteArray = sw.toByteArray();

    // write content
    iFile.setContents(new ByteArrayInputStream(byteArray), true, false, null);

}

From source file:org.talend.componentdesigner.util.XMLUtil.java

License:Open Source License

/**
 * format the exist xml file./* w  w  w .  j  ava  2s.c om*/
 * 
 * @param filename
 * @return
 */
public static int formatXMLFile(String filename, String enCode) {

    int returnValue = 0;

    try {

        SAXReader saxReader = new SAXReader();

        org.dom4j.Document document = saxReader.read(new File(filename));

        XMLWriter writer = null;

        /** format the output like the webBrowser */

        OutputFormat format = OutputFormat.createPrettyPrint();

        /** give the xml encoding */

        format.setEncoding(enCode);

        writer = new XMLWriter(new FileWriter(new File(filename)), format);

        writer.write(document);

        writer.close();

        /** succes will retun 1 */

        returnValue = 1;

    } catch (Exception ex) {

        // ex.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(ex);

    }

    return returnValue;

}

From source file:org.talend.core.nexus.MavenResolverCreator.java

License:Open Source License

public String setupMavenWithNexus(String url, String userName, String password, String repId) {
    // return the repository id configured in the settings.xml
    String repositoryIdToReture = repId;
    try {/*from  www. j  a  v  a  2s  .  co  m*/
        if (url == null || userName == null || password == null) {
            throw new MavenSetupException("Please specifiy a nexus url to setup");
        }
        String userConfigPath = System.getProperty("user.home") + "/.m2/settings.xml";
        boolean modified = true;
        String nexusUrl = url.trim();

        URL settingsUrl = getSettingsFileUrl();

        Document document = new SAXReader(false).read(new File(settingsUrl.getPath()));
        Element root = document.getRootElement();
        Element profiles = getOrCreateElement(root, "profiles");

        Element activeProf = getOrCreateElement(root, "activeProfiles");
        List<String> activeProfileIds = getSubElementValues(activeProf, "activeProfile");

        Element servers = getOrCreateElement(root, "servers");

        Set<String> existingProIds = new HashSet<String>();
        Set<String> existingRepIds = new HashSet<String>();

        if (profiles != null) {
            Iterator profileIter = profiles.elementIterator("profile");
            String profileId = null;
            String repositoryId = null;
            boolean found = false;
            while (profileIter.hasNext()) {
                Element profile = (Element) profileIter.next();
                Element pIdElement = profile.element("id");
                if (pIdElement != null) {
                    profileId = pIdElement.getText();
                }
                Element repositories = profile.element("repositories");
                if (repositories != null) {
                    Iterator repIter = repositories.elementIterator("repository");
                    while (repIter.hasNext()) {
                        Element repository = (Element) repIter.next();
                        Element repIdElement = repository.element("id");
                        if (repIdElement != null) {
                            repositoryId = repIdElement.getText();
                            existingRepIds.add(repositoryId);
                        }
                        Element urlElement = repository.element("url");
                        if (urlElement != null) {
                            String repUrl = urlElement.getText().trim();
                            if (nexusUrl.equals(repUrl)) {
                                repositoryIdToReture = repositoryId;
                                found = true;
                                modified = false;
                                break;
                            }
                        }
                    }

                }
            }
            if (found) {
                // recheck active profile
                if (!activeProfileIds.contains(profileId)) {
                    addElement(activeProf, "activeProfile", profileId);
                }
            } else {
                // create profile
                repositoryIdToReture = createProfile(profiles, servers, activeProf, existingProIds,
                        existingRepIds, url, userName, password, repId);
            }
        }
        if (modified) {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");
            XMLWriter writer = null;
            try {
                writer = new XMLWriter(new FileWriter(userConfigPath), format);
                writer.write(document);
                writer.close();
            } catch (IOException e) {
                ExceptionHandler.process(e);
            } finally {
                if (writer != null) {
                    try {
                        writer.close();
                    } catch (IOException e) {
                        ExceptionHandler.process(e);
                    }
                }
            }

        }
    } catch (Exception e) {
        ExceptionHandler.process(e);
    }

    return repositoryIdToReture;
}

From source file:org.talend.mdm.webapp.base.server.util.XmlUtil.java

License:Open Source License

public static void write(Document document, String filePath, String printMode, String encoding)
        throws IOException {
    OutputFormat format;
    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();
    } else {//from ww w. j av  a 2  s.c o m
        format = null;
    }

    format.setEncoding(encoding);

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

    if (logger.isDebugEnabled()) {
        logger.debug("New xml file has bean exported on " + filePath); //$NON-NLS-1$
    }
}

From source file:org.talend.mdm.webapp.base.server.util.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);

    XMLWriter xmlwriter = new XMLWriter(writer, format);

    try {/* w ww.j a va2  s .c o  m*/
        xmlwriter.write(document);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }

    return writer.toString().replaceAll("<\\?xml.*?\\?>", "").trim(); //$NON-NLS-1$ //$NON-NLS-2$

}

From source file:org.talend.updates.runtime.nexus.component.ComponentIndexManager.java

License:Open Source License

public boolean createIndexFile(File indexFile, List<ComponentIndexBean> newIndexList) throws IOException {
    if (newIndexList == null || newIndexList.isEmpty() || indexFile == null) {
        return false;
    }/*w ww.ja va2 s.  c  o  m*/

    XMLWriter xmlWriter = null;
    boolean created = false;
    try {
        // write to index
        final DocumentFactory docFactory = DocumentFactory.getInstance();
        final Element components = docFactory.createElement(ELEM_COMPONENTS);
        Document newDoc = docFactory.createDocument(components);
        for (ComponentIndexBean b : newIndexList) {
            final Element elem = createXmlElement(b);
            if (elem != null) {
                components.add(elem);
            }
        }

        // 4 spaces
        OutputFormat format = new OutputFormat();
        format.setEncoding("UTF-8"); //$NON-NLS-1$
        format.setIndentSize(4);
        format.setNewlines(true);
        xmlWriter = new XMLWriter(new FileOutputStream(indexFile), format);

        xmlWriter.write(newDoc);

        created = true;
        return true;
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (IOException e) {
                //
            }
        }
        if (!created && indexFile.exists()) {
            indexFile.delete(); // remove the wrong file.
        }
    }
}

From source file:org.waarp.common.xml.XmlUtil.java

License:Open Source License

/**
 * Save the document into the file/*from  w  ww . j ava2  s.  c  o  m*/
 * 
 * @param file
 * @param document
 * @throws IOException
 */
static public void saveDocument(File file, Document document) throws IOException {
    if (file.exists() && (!file.canWrite())) {
        throw new IOException("File is not writable: " + file.getPath());
    }
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(WaarpStringUtils.UTF8.name());
    XMLWriter writer = new XMLWriter(new FileWriter(file), format);
    writer.write(document);
    writer.flush();
    writer.close();
}

From source file:org.waarp.common.xml.XmlUtil.java

License:Open Source License

/**
 * Save the branch from element into the file
 * //  w  ww .j a  v  a  2  s  .  com
 * @param file
 * @param element
 * @throws IOException
 */
static public void saveElement(File file, Element element) throws IOException {
    if (file.exists() && (!file.canWrite())) {
        throw new IOException("File is not writable: " + file.getPath());
    }
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(WaarpStringUtils.UTF8.name());
    XMLWriter writer = new XMLWriter(new FileWriter(file), format);
    writer.write(element);
    writer.flush();
    writer.close();
}

From source file:org.waarp.common.xml.XmlUtil.java

License:Open Source License

/**
 * Write the given XML document to filename using the encoding
 * /*  w w  w .j  av  a2s  . c o m*/
 * @param filename
 * @param encoding
 *            if null, default encoding UTF-8 will be used
 * @param document
 * @throws IOException
 */
public static void writeXML(String filename, String encoding, Document document) throws IOException {
    OutputFormat format = OutputFormat.createPrettyPrint();
    if (encoding != null) {
        format.setEncoding(encoding);
    } else {
        format.setEncoding(WaarpStringUtils.UTF8.name());
    }
    XMLWriter writer = null;
    writer = new XMLWriter(new FileWriter(filename), format);
    writer.write(document);
    try {
        writer.close();
    } catch (IOException e) {
    }
}