Example usage for org.dom4j.io XMLWriter write

List of usage examples for org.dom4j.io XMLWriter write

Introduction

In this page you can find the example usage for org.dom4j.io XMLWriter write.

Prototype

public void write(Object object) throws IOException 

Source Link

Document

Writes the given object which should be a String, a Node or a List of Nodes.

Usage

From source file:com.sofrecom.MybatisGenXmlHandler.java

public void saveDocument(Document document)
        throws FileNotFoundException, UnsupportedEncodingException, IOException {
    try (FileOutputStream fos = new FileOutputStream(Configuration.XMLTemplate)) {
        OutputFormat format = OutputFormat.createPrettyPrint();
        // Create the xml writer by passing outputstream and format
        XMLWriter writer = new XMLWriter(fos, format);
        // Write to the xml document
        writer.write(document);
        // Flush after done
        writer.flush();//  www. j  a va2  s .  c  om
        writer.close();
    }
}

From source file:com.sun.xml.ws.test.container.AbstractApplicationContainer.java

License:Open Source License

protected void updateWsitClient(WAR war, DeployedService deployedService, String id) throws Exception {
    File wsitClientFile = new File(deployedService.getResources(), "wsit-client.xml");
    if (wsitClientFile.exists()) {
        SAXReader reader = new SAXReader();
        Document document = reader.read(wsitClientFile);
        Element root = document.getRootElement();
        Element policy = root.element("Policy");
        Element sts = policy.element("ExactlyOne").element("All").element("PreconfiguredSTS");

        Attribute endpoint = sts.attribute("endpoint");
        endpoint.setValue(id);// w  w w  .  ja  v  a 2s .c  om

        Attribute wsdlLoc = sts.attribute("wsdlLocation");
        String x = deployedService.service.wsdl.get(0).wsdlFile.toURI().toString();
        wsdlLoc.setValue(x);

        XMLWriter writer = new XMLWriter(new FileWriter(wsitClientFile));
        writer.write(document);
        writer.close();
        war.copyWsit(wsitClientFile);
    } else {
        throw new RuntimeException("wsit-client.xml is absent. It is required. \n" + "Please check "
                + deployedService.getResources());
    }
}

From source file:com.suneee.core.util.XMLProperties.java

License:Open Source License

/**
 * Saves the properties to disk as an XML document. A temporary file is
 * used during the writing process for maximum safety.
 *//*from w  ww.  j ava 2 s.co m*/
private synchronized void saveProperties() {
    boolean error = false;
    // Write data out to a temporary file first.
    File tempFile = null;
    Writer writer = null;
    try {
        tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFile), "UTF-8"));
        OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
        XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
        xmlWriter.write(document);
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
        // There were errors so abort replacing the old property file.
        error = true;
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e1) {
                Log.error(e1.getMessage(), e1);
                error = true;
            }
        }
    }

    // No errors occured, so delete the main file.
    if (!error) {
        // Delete the old file so we can replace it.
        if (!file.delete()) {
            Log.error("Error deleting property file: " + file.getAbsolutePath());
            return;
        }
        // Copy new contents to the file.
        try {
            copy(tempFile, file);
        } catch (Exception e) {
            Log.error(e.getMessage(), e);
            // There were errors so abort replacing the old property file.
            error = true;
        }
        // If no errors, delete the temp file.
        if (!error) {
            tempFile.delete();
        }
    }
}

From source file:com.taobao.android.builder.tasks.library.publish.UpdatePomTask.java

License:Apache License

private void updatePomXml(File xml) throws IOException, DocumentException {

    Map<String, DependencyExtraInfo> extraInfoMap = getExtraMap();

    File backupFile = new File(xml.getParentFile(), "pom-backup.xml");
    FileUtils.deleteQuietly(backupFile);

    FileUtils.moveFile(xml, backupFile);

    XMLWriter writer = null;// XML
    SAXReader reader = new SAXReader();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");// XML??
    FileOutputStream fos = new FileOutputStream(xml);

    try {//from   w w w  .  j  a v  a  2 s  .c  o m
        Document document = reader.read(backupFile);// ?XML

        Element dependencies = document.getRootElement().element("dependencies");

        if ((null != dependencies) && null != dependencies.elements()) {
            List<Element> list = dependencies.elements();

            for (Element element : list) {

                List<Element> itemList = element.elements();

                String group = "";
                String name = "";
                String type;
                String scope;

                Element scopeEl = null;
                Element typeEl = null;

                for (Element element1 : itemList) {
                    if ("groupId".equals(element1.getQName().getName())) {
                        group = element1.getStringValue();
                    } else if ("artifactId".equals(element1.getQName().getName())) {
                        name = element1.getStringValue();
                    } else if ("scope".equals(element1.getQName().getName())) {
                        scope = element1.getStringValue();
                        scopeEl = element1;
                    } else if ("type".equals(element1.getQName().getName())) {
                        type = element1.getStringValue();
                        typeEl = element1;
                    }
                }

                DependencyExtraInfo dependencyExtraInfo = extraInfoMap.get(group + ":" + name);
                if (null == dependencyExtraInfo) {
                    continue;
                }

                //update scope
                if (StringUtils.isNotEmpty(dependencyExtraInfo.scope)) {
                    if (null != scopeEl) {
                        scopeEl.setText(dependencyExtraInfo.scope);
                    } else {
                        Element newEl = element.addElement("scope");
                        newEl.setText(dependencyExtraInfo.scope);
                    }
                }

                if (StringUtils.isNotEmpty(dependencyExtraInfo.type)) {
                    if (null != typeEl) {
                        typeEl.setText(dependencyExtraInfo.type);
                    } else {
                        Element newEl = element.addElement("type");
                        newEl.setText(dependencyExtraInfo.type);
                    }
                }

            }
        }

        writer = new XMLWriter(fos, format);
        writer.write(document);

    } finally {
        if (null != writer) {
            writer.close();
        }
        IOUtils.closeQuietly(fos);
    }

}

From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java

License:Apache License

/**
 * manifest???/*from   ww  w . j  av  a 2  s . co  m*/
 *
 * @param mainManifest
 * @param libManifestMap
 * @param baseBunfleInfoFile
 * @param manifestOptions
 */
public static void postProcessManifests(File mainManifest, Map<String, File> libManifestMap,
        Multimap<String, File> libDependenciesMaps, File baseBunfleInfoFile, ManifestOptions manifestOptions,
        boolean addMultiDex, Set<String> remoteBundles) throws IOException, DocumentException {
    File backupFile = new File(mainManifest.getParentFile(), "AndroidManifest-backup.xml");
    FileUtils.deleteQuietly(backupFile);
    FileUtils.moveFile(mainManifest, backupFile);

    XMLWriter writer = null;// XML
    SAXReader reader = new SAXReader();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");// XML??
    FileOutputStream fos = new FileOutputStream(mainManifest);
    if (mainManifest.exists()) {
        try {
            Document document = reader.read(backupFile);// ?XML
            if (null != baseBunfleInfoFile && baseBunfleInfoFile.exists()) {
                addApplicationMetaData(document, libManifestMap, baseBunfleInfoFile, manifestOptions,
                        remoteBundles);
            }
            if (null != manifestOptions && manifestOptions.isAddBundleLocation()) {
                addBundleLocationToDestManifest(document, libManifestMap, libDependenciesMaps, manifestOptions);
            }
            if (null != manifestOptions && manifestOptions.isReplaceApplication()) {
                replaceManifestApplicationName(document);
            }
            if ((null != manifestOptions && manifestOptions.isAddMultiDexMetaData()) || addMultiDex) {
                addMultiDexMetaData(document);
            }
            if (null != manifestOptions && manifestOptions.isRemoveProvider()) {
                removeProvider(document);
            }
            removeCustomLaunches(document, manifestOptions);
            updatePermission(document, manifestOptions);
            removeComments(document);

            writer = new XMLWriter(fos, format);
            writer.write(document);
        } finally {
            if (null != writer) {
                writer.close();
            }
            IOUtils.closeQuietly(fos);
        }
    }
}

From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java

License:Apache License

private static void saveFile(Document document, OutputFormat format, File file) throws IOException {

    XMLWriter writer = null;// XML
    FileOutputStream fos = null;//w  w  w  . ja  v a2  s .  c  om
    try {
        fos = new FileOutputStream(file);
        writer = new XMLWriter(fos, format);
        writer.write(document);
    } finally {
        if (null != writer) {
            writer.close();
        }
        IOUtils.closeQuietly(fos);
    }
}

From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java

License:Apache License

public static void minifyManifest(File mainManifest, File destManifest) throws IOException, DocumentException {

    XMLWriter writer = null;// XML
    SAXReader reader = new SAXReader();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");// XML??
    FileOutputStream fos = new FileOutputStream(destManifest);
    if (mainManifest.exists()) {
        try {// w w  w. j  a  v a  2  s .  com
            Document document = reader.read(mainManifest);// ?XML

            //                removeComments(document);

            Element element = document.getRootElement();

            element.clearContent();

            writer = new XMLWriter(fos, format);
            writer.write(document);
        } finally {
            if (null != writer) {
                writer.close();
            }
            IOUtils.closeQuietly(fos);
        }
    }
}

From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java

License:Apache License

public static void removeProvider(File androidManifestFile) throws IOException, DocumentException {
    File backupFile = new File(androidManifestFile.getParentFile(), "AndroidManifest-backup.xml");
    FileUtils.deleteQuietly(backupFile);
    FileUtils.moveFile(androidManifestFile, backupFile);

    XMLWriter writer = null;// XML
    SAXReader reader = new SAXReader();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");// XML??
    FileOutputStream fos = new FileOutputStream(androidManifestFile);

    if (androidManifestFile.exists()) {
        try {/*from w ww  .  ja  v a2s.c  o m*/
            Document document = reader.read(backupFile);// ?XML
            Element root = document.getRootElement();// 
            List<? extends Node> nodes = root.selectNodes("//provider");
            for (Node node : nodes) {
                Element element = (Element) node;
                String name = element.attributeValue("name");
                logger.info("[Remove Provider]" + name);
                element.getParent().remove(element);
            }
            writer = new XMLWriter(fos, format);
            writer.write(document);
        } finally {
            if (null != writer) {
                writer.close();
            }
            IOUtils.closeQuietly(fos);
        }
    }
}

From source file:com.taobao.android.builder.tools.xml.XmlHelper.java

License:Apache License

public static void saveFile(Document document, OutputFormat format, File file) throws IOException {

    XMLWriter writer = null;// XML
    FileOutputStream fos = null;//from  ww w  . j  a v  a2s  .c om
    try {
        fos = new FileOutputStream(file);
        writer = new XMLWriter(fos, format);
        writer.write(document);
    } finally {
        if (null != writer) {
            writer.close();
        }
        IOUtils.closeQuietly(fos);
    }
}

From source file:com.taobao.datax.engine.tools.JobConfGenDriver.java

License:Open Source License

private static int genXmlFile(String filename, ClassNode reader, ClassNode writer) throws IOException {

    Document document = DocumentHelper.createDocument();
    Element jobsElement = document.addElement("jobs");
    Element jobElement = jobsElement.addElement("job");
    String id = reader.getName() + "_to_" + writer.getName() + "_job";
    jobElement.addAttribute("id", id);

    /**/*w  w  w  .  j  av a  2  s .com*/
     * ?readerxml
     */
    Element readerElement = jobElement.addElement("reader");
    Element plugin_Element = readerElement.addElement("plugin");
    plugin_Element.setText(reader.getName());

    ClassNode readerNode = reader;
    Element tempElement = null;

    List<ClassMember> members = readerNode.getAllMembers();
    for (ClassMember member : members) {
        StringBuilder command = new StringBuilder("\n");

        Set<String> set = member.getAllKeys();
        String value = "";
        for (String key : set) {
            value = member.getAttr("default");
            command.append(key).append(":").append(member.getAttr(key)).append("\n");
        }
        readerElement.addComment(command.toString());

        String keyName = member.getName();
        keyName = keyName.substring(1, keyName.length() - 1);
        tempElement = readerElement.addElement("param");
        tempElement.addAttribute("key", keyName);

        if (value == null || "".equals(value)) {
            value = "?";
        }
        tempElement.addAttribute("value", value);
    }

    /**
     * ?writerxml
     */
    Element writerElement = jobElement.addElement("writer");
    plugin_Element = writerElement.addElement("plugin");
    plugin_Element.setText(writer.getName());

    members = writer.getAllMembers();
    for (ClassMember member : members) {
        StringBuilder command = new StringBuilder("\n");
        Set<String> set = member.getAllKeys();

        String value = "";
        for (String key : set) {
            value = member.getAttr("default");
            command.append(key).append(":").append(member.getAttr(key)).append("\n");
        }
        writerElement.addComment(command.toString());

        String keyName = member.getName();
        keyName = keyName.substring(1, keyName.length() - 1);
        tempElement = writerElement.addElement("param");
        tempElement.addAttribute("key", keyName);

        if (value == null || "".equals(value)) {
            value = "?";
        }
        tempElement.addAttribute("value", value);
    }

    try {
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        XMLWriter writerOfXML = new XMLWriter(new FileWriter(new File(filename)), format);
        writerOfXML.write(document);
        writerOfXML.close();
    } catch (Exception ex) {
        throw new IOException(ex.getCause());
    }

    return 0;
}