Example usage for org.dom4j.io XMLWriter close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the underlying Writer

Usage

From source file:com.sammyun.util.SettingUtils.java

License:Open Source License

/**
 * /*ww w  .  j  av  a  2s  . c  o m*/
 * 
 * @param setting 
 */
public static void set(Setting setting) {
    try {
        File preschoolEduXmlFile = new ClassPathResource(CommonAttributes.PRESCHOOLEDU_XML_PATH).getFile();
        Document document = new SAXReader().read(preschoolEduXmlFile);
        List<Element> elements = document.selectNodes("/preschoolEdu/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();
                logger.error(e.getMessage());
            } catch (InvocationTargetException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            }
        }

        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(preschoolEduXmlFile);
            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();
        logger.error(e.getMessage());
    }
}

From source file:com.sap.data.db.dao.StructureUtil.java

private void save(Document document, String filepath) throws IOException {
    document.addDocType(this.HIBERNATE_MAPPING, this.HIBERNATE_MAPPING_EN, this.HIBERNATE_MAPPING_DTD);
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");

    XMLWriter writer = new XMLWriter(new FileWriter(new File(filepath)), format);
    writer.write(document);/*from  www . ja v  a2s .c  o m*/
    writer.close();
}

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);// ww w . j a v a  2  s . com
        // Flush after done
        writer.flush();
        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 ww  . j  a 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.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 {/*  w  ww  .ja va  2s .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 w ww  .j  a v a2 s. c om
 *
 * @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;//from w  w  w .j  a va2s . co m
    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 {/*www .j  av a2s . co m*/
            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   www .  j av a  2 s .co 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;/*ww  w.j  a v  a2  s . com*/
    try {
        fos = new FileOutputStream(file);
        writer = new XMLWriter(fos, format);
        writer.write(document);
    } finally {
        if (null != writer) {
            writer.close();
        }
        IOUtils.closeQuietly(fos);
    }
}