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:cn.ict.zyq.bestConf.util.ParseXMLToYaml.java

License:Open Source License

public static void appendXMLByDOM4J(HashMap target, String source, String destination) throws IOException {

    SAXReader reader = new SAXReader();
    try {/*from  w w  w . j a  v  a2  s.co  m*/

        Document document = reader.read(new File(source));
        Element configStore = document.getRootElement();
        Iterator iter = target.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = entry.getKey().toString();
            String val = entry.getValue().toString();
            Element property = configStore.addElement("property");
            Element name = property.addElement("name");
            name.setText(key);
            Element value = property.addElement("value");
            value.setText(val);
        }
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(new FileOutputStream(destination), format);
        writer.write(document);
        writer.close();

    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:cn.itcreator.android.reader.util.XMLUtil.java

License:Open Source License

/**
 * save the list to a xml file/*from  w ww .ja  v a  2s .  c om*/
 * @param list the list u wanna save
 * @return
 */
public boolean saveToFile(List<BookMark> list) {
    boolean result = true;
    Element root = null;
    root = mDocument.getRootElement();
    if (null == root) {
        root = mDocument.addElement("bookmark");
    }
    for (BookMark bookMark : list) {
        Element mark = root.addElement("mark");
        mark.addAttribute("markName", bookMark.getMarkName());
        mark.addAttribute("currentOffset", "" + bookMark.getCurrentOffset());
    }
    OutputFormat format = OutputFormat.createPrettyPrint();
    /** Give the xml file encode */
    format.setEncoding(Constant.UTF8);
    try {
        XMLWriter writer = new XMLWriter(new FileOutputStream(mFilePath), format);
        writer.write(mDocument);
        writer.close();
    } catch (IOException e) {
        result = false;
    }
    return result;
}

From source file:cn.mario256.blog.util.SystemUtils.java

License:Open Source License

/**
 * //from  w ww  .j  a v a  2  s  . c  om
 * 
 * @param setting
 *            
 */
@SuppressWarnings("unchecked")
public static void setSetting(Setting setting) {
    Assert.notNull(setting);

    try {
        File turingXmlFile = new ClassPathResource(CommonAttributes.TURING_XML_PATH).getFile();
        Document document = new SAXReader().read(turingXmlFile);
        List<org.dom4j.Element> elements = document.selectNodes("/turing/setting");
        for (org.dom4j.Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = BEAN_UTILS.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e.getMessage(), e);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }

        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            xmlWriter = new XMLWriter(new FileOutputStream(turingXmlFile), outputFormat);
            xmlWriter.write(document);
            xmlWriter.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        } finally {
            try {
                if (xmlWriter != null) {
                    xmlWriter.close();
                }
            } catch (IOException e) {
            }
        }
        Ehcache cache = CACHE_MANAGER.getEhcache(Setting.CACHE_NAME);
        String cacheKey = "setting";
        cache.put(new Element(cacheKey, setting));
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (DocumentException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.adobe.ac.pmd.metrics.engine.AbstractMetrics.java

License:Apache License

public void execute(final File outputFile) throws DocumentException, IOException {
    final String builtReport = buildReport(loadMetrics());
    final Document document = DocumentHelper.parseText(builtReport);
    final OutputFormat format = OutputFormat.createPrettyPrint();

    if (!outputFile.exists()) {
        if (outputFile.createNewFile() == false) {
            LOGGER.warning("Could not create a new output file");
        }//from w  w  w .  j  a v a  2  s .  c  om
    }

    final XMLWriter writer = new XMLWriter(new FileOutputStream(outputFile), format);
    writer.write(document);
    writer.close();
}

From source file:com.ah.be.admin.adminBackupUnit.AhBackupTool.java

private void backupDomainTable(String strPath, String strDomainName, String strTableName) {
    Connection con = null;/* w ww.jav  a  2  s . c o m*/

    XMLWriter output = null;

    ResultSet rsTable = null;

    Statement stTable = null;

    try {
        if (!(strPath.substring(strPath.length() - 1).equals(File.separator)
                || strPath.substring(strPath.length() - 1).equals("/"))) {
            strPath = strPath + File.separatorChar;
        }

        //         Class.forName("org.postgresql.Driver").newInstance();
        //
        //         String strUrl = "jdbc:postgresql://localhost:5432/hm";
        //
        //         String strUsr = "hivemanager";
        //
        //         String strPsd = "aerohive";

        AhBackupNewTool oTool = new AhBackupNewTool();

        con = oTool.initCon();

        try {
            stTable = con.createStatement();

            String strSql;
            int intFileCount = 0;
            int intRecordNum;

            while (true) {
                intRecordNum = 0;

                strSql = "select * from " + strTableName + " where domainname='" + strDomainName + "'"
                        + " limit " + 5000 + " offset " + intFileCount * 5000;

                if (!AhBackupNewTool.isValidCoon(con)) {
                    con = oTool.initCon();
                    stTable = con.createStatement();
                }

                rsTable = stTable.executeQuery(strSql);

                Document document = DocumentHelper.createDocument();

                Element table = document.addElement("table").addAttribute("schema", strTableName);

                ResultSetMetaData rsmd = rsTable.getMetaData();

                int iCount = rsmd.getColumnCount();

                while (rsTable.next()) {
                    intRecordNum++;
                    Element row = table.addElement("row");

                    for (int icol = 1; icol <= iCount; icol++) {
                        String newStr;

                        if (rsTable.getString(icol) == null) {
                            newStr = "NULL";
                        } else if ("null".equalsIgnoreCase(rsTable.getString(icol))) {
                            newStr = "_" + rsTable.getString(icol) + "_";
                        } else {
                            newStr = rsTable.getString(icol);
                        }

                        if (1 == intRecordNum) {
                            row.addElement("field").addAttribute("name", rsmd.getColumnName(icol))
                                    .addAttribute("value", newStr);
                        } else {
                            row.addElement("field").addAttribute("value", newStr);
                        }
                    }
                }

                if (intRecordNum <= 0 && 0 != intFileCount)
                    break;

                File file;

                if (intFileCount == 0) {
                    file = new File(strPath + strTableName.toLowerCase() + ".xml");

                } else {
                    file = new File(strPath + strTableName.toLowerCase() + "_" + intFileCount + ".xml");
                }

                intFileCount++;

                output = new XMLWriter(new FileOutputStream(file));

                output.write(document);

                output.close();

                if (5000 > intRecordNum)
                    break;
            }

            rsTable.close();

            stTable.close();
        } catch (Exception ex) {
            BeLogTools.restoreLog(BeLogTools.ERROR, ex.getMessage());
        }
    } catch (Exception ex) {
        // add the debug msg
        BeLogTools.restoreLog(BeLogTools.ERROR, ex.getMessage());
    } finally {
        if (null != con) {
            try {
                con.close();
            } catch (Exception conex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, conex.getMessage());
            }
        }

        if (null != output) {
            try {
                output.close();
            } catch (Exception outex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, outex.getMessage());
            }
        }

        if (null != rsTable) {
            try {
                rsTable.close();
            } catch (Exception rsex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, rsex.getMessage());
            }
        }

        if (null != stTable) {
            try {
                stTable.close();
            } catch (Exception stex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, stex.getMessage());
            }
        }
    }
}

From source file:com.ah.be.admin.adminBackupUnit.AhBackupTool.java

private void backup1table(String strPath, String strTableName) {
    Connection con = null;/*ww  w  .  j a  v  a  2 s .  co  m*/

    XMLWriter output = null;

    ResultSet rsTable = null;

    Statement stTable = null;

    try {
        if (!(strPath.substring(strPath.length() - 1).equals(File.separator)
                || strPath.substring(strPath.length() - 1).equals("/"))) {
            strPath = strPath + File.separatorChar;
        }

        //         Class.forName("org.postgresql.Driver").newInstance();
        //
        //         String strUrl = "jdbc:postgresql://localhost:5432/hm";
        //
        //         String strUsr = "hivemanager";
        //
        //         String strPsd = "aerohive";

        //         con = DriverManager
        //               .getConnection(strUrl, strUsr, strPsd);
        AhBackupNewTool oTool = new AhBackupNewTool();

        con = oTool.initCon();

        //String strTableName = AhBackupTool.ahLicenseHistory.toLowerCase();

        try {
            stTable = con.createStatement();

            //            String strSql = "select count(*) from " + strTableName;
            //
            //            rsTable = stTable.executeQuery(strSql);
            //
            //            rsTable = stTable.executeQuery(strSql);
            //
            //            rsTable.next();
            //
            //            int iRowCount = rsTable.getInt(1);
            //
            //            rsTable.close();

            //int intFileCount = 1;
            String strSql;
            int intFileCount = 0;
            int intRecordNum;

            //for (int i = 0; i < iRowCount || i == 0; i = i + 5000) {
            while (true) {
                intRecordNum = 0;

                strSql = "select * from " + strTableName + " limit " + 5000 + " offset " + intFileCount * 5000;
                if (!AhBackupNewTool.isValidCoon(con)) {
                    con = oTool.initCon();
                    stTable = con.createStatement();
                }

                rsTable = stTable.executeQuery(strSql);

                Document document = DocumentHelper.createDocument();

                Element table = document.addElement("table").addAttribute("schema", strTableName);

                ResultSetMetaData rsmd = rsTable.getMetaData();

                int iCount = rsmd.getColumnCount();

                while (rsTable.next()) {
                    intRecordNum++;
                    Element row = table.addElement("row");

                    for (int icol = 1; icol <= iCount; icol++) {
                        String newStr;

                        if (rsTable.getString(icol) == null) {
                            newStr = "NULL";
                        } else if ("null".equalsIgnoreCase(rsTable.getString(icol))) {
                            newStr = "_" + rsTable.getString(icol) + "_";
                        } else {
                            newStr = rsTable.getString(icol);
                        }

                        if (1 == intRecordNum) {
                            row.addElement("field").addAttribute("name", rsmd.getColumnName(icol))
                                    .addAttribute("value", newStr);
                        } else {
                            row.addElement("field").addAttribute("value", newStr);
                        }
                    }
                }

                if (intRecordNum <= 0 && 0 != intFileCount)
                    break;

                File file;

                if (intFileCount == 0) {
                    file = new File(strPath + strTableName.toLowerCase() + ".xml");

                } else {
                    file = new File(strPath + strTableName.toLowerCase() + "_" + intFileCount + ".xml");
                }

                intFileCount++;

                output = new XMLWriter(new FileOutputStream(file));

                output.write(document);

                output.close();

                if (5000 > intRecordNum)
                    break;
            }

            rsTable.close();

            stTable.close();
        } catch (Exception ex) {
            BeLogTools.restoreLog(BeLogTools.ERROR, ex.getMessage());
        }
    } catch (Exception ex) {
        // add the debug msg
        BeLogTools.restoreLog(BeLogTools.ERROR, ex.getMessage());
    } finally {
        if (null != con) {
            try {
                con.close();
            } catch (Exception conex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, conex.getMessage());
            }
        }

        if (null != output) {
            try {
                output.close();
            } catch (Exception outex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, outex.getMessage());
            }
        }

        if (null != rsTable) {
            try {
                rsTable.close();
            } catch (Exception rsex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, rsex.getMessage());
            }
        }

        if (null != stTable) {
            try {
                stTable.close();
            } catch (Exception stex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, stex.getMessage());
            }
        }
    }
}

From source file:com.ah.be.admin.adminBackupUnit.AhBackupTool.java

private void storeBriefDomainInfo(String strXmlPath, HmDomain oDomain) {
    String strDomainTable = AhBackupTool.ahDomain.toLowerCase();

    Document document = DocumentHelper.createDocument();

    Element table = document.addElement("table").addAttribute("schema", strDomainTable);

    File fDomain = new File(strXmlPath + strDomainTable.toLowerCase() + ".xml");

    XMLWriter output = null;

    try {/*w w w .  java  2 s .c  om*/
        output = new XMLWriter(new FileOutputStream(fDomain));

        Element row = table.addElement("row");

        String strDomainId = oDomain.getId().toString();

        String strDomainName = oDomain.getDomainName();

        row.addElement("field").addAttribute("name", "id").addAttribute("value", strDomainId);

        row.addElement("field").addAttribute("name", "domainname").addAttribute("value", strDomainName);

        output.write(document);

        output.close();
    } catch (Exception ex) {
        BeLogTools.restoreLog(BeLogTools.ERROR, ex.getMessage());
    } finally {

        if (null != output) {
            try {
                output.close();
            } catch (Exception outex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, outex.getMessage());
            }
        }
    }
}

From source file:com.ah.be.admin.adminBackupUnit.AhBackupTool.java

private void storeDomainTable(String strTableName, String strPath, HmDomain oDomain, Connection conTable) {
    String strSql = "select * from " + strTableName;

    XMLWriter output = null;

    ResultSet rsTable = null;/*from w  ww .j  a  va 2s  . c  om*/

    Statement stTable = null;

    try {
        stTable = conTable.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

        rsTable = stTable.executeQuery(strSql);

        Document document = DocumentHelper.createDocument();

        Element table = document.addElement("table").addAttribute("schema", strTableName);

        File file = new File(strPath + strTableName.toLowerCase() + ".xml");

        output = new XMLWriter(new FileOutputStream(file));

        ResultSetMetaData rsmd = rsTable.getMetaData();

        int iCount = rsmd.getColumnCount();

        while (rsTable.next()) {
            Element row = table.addElement("row");

            for (int icol = 1; icol <= iCount; icol++) {
                String newStr;

                if (rsTable.getString(icol) == null) {
                    newStr = "NULL";
                } else if ("null".equalsIgnoreCase(rsTable.getString(icol))) {
                    newStr = "_" + rsTable.getString(icol) + "_";
                } else {
                    newStr = rsTable.getString(icol);
                }

                row.addElement("field").addAttribute("name", rsmd.getColumnName(icol)).addAttribute("value",
                        newStr);
            }
        }

        output.write(document);

        output.close();

        rsTable.close();

        stTable.close();
    } catch (Exception ex) {
        BeLogTools.restoreLog(BeLogTools.ERROR, ex.getMessage());
    } finally {
        if (null != output) {
            try {
                output.close();
            } catch (Exception outex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, outex.getMessage());
            }
        }

        if (null != rsTable) {
            try {
                rsTable.close();
            } catch (Exception rsex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, rsex.getMessage());
            }
        }

        if (null != stTable) {
            try {
                stTable.close();
            } catch (Exception stex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, stex.getMessage());
            }
        }
    }
}

From source file:com.ah.be.admin.adminBackupUnit.AhBackupTool.java

private void storeCertainDomainTable(String strTableName, String strPath, HmDomain oDomain,
        Connection conTable) {/*from  w  ww. ja  va 2s  . c  o m*/
    String strSql = "select * from " + strTableName + " where domainname=" + "'" + oDomain.getDomainName()
            + "'";

    XMLWriter output = null;

    ResultSet rsTable = null;

    Statement stTable = null;

    try {
        stTable = conTable.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

        rsTable = stTable.executeQuery(strSql);

        Document document = DocumentHelper.createDocument();

        Element table = document.addElement("table").addAttribute("schema", strTableName);

        File file = new File(strPath + strTableName.toLowerCase() + ".xml");

        output = new XMLWriter(new FileOutputStream(file));

        ResultSetMetaData rsmd = rsTable.getMetaData();

        int iCount = rsmd.getColumnCount();

        while (rsTable.next()) {
            Element row = table.addElement("row");

            for (int icol = 1; icol <= iCount; icol++) {
                String newStr;

                if (rsTable.getString(icol) == null) {
                    newStr = "NULL";
                } else if ("null".equalsIgnoreCase(rsTable.getString(icol))) {
                    newStr = "_" + rsTable.getString(icol) + "_";
                } else {
                    newStr = rsTable.getString(icol);
                }

                row.addElement("field").addAttribute("name", rsmd.getColumnName(icol)).addAttribute("value",
                        newStr);
            }
        }

        output.write(document);

        output.close();

        rsTable.close();

        stTable.close();
    } catch (Exception ex) {
        BeLogTools.restoreLog(BeLogTools.ERROR, ex.getMessage());
    } finally {
        if (null != output) {
            try {
                output.close();
            } catch (Exception outex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, outex.getMessage());
            }
        }

        if (null != rsTable) {
            try {
                rsTable.close();
            } catch (Exception rsex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, rsex.getMessage());
            }
        }

        if (null != stTable) {
            try {
                stTable.close();
            } catch (Exception stex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, stex.getMessage());
            }
        }
    }
}

From source file:com.ah.be.admin.adminBackupUnit.AhBackupTool.java

private void storeCommonTable(String strTableName, String strPath, HmDomain oDomain, Connection conTable) {
    XMLWriter output = null;

    ResultSet rsTable = null;//w ww.j av  a  2  s .c om

    Statement stTable = null;

    try {
        stTable = conTable.createStatement();

        boolean bOwnerFlag = false;

        String strSql = "select * from " + strTableName + " limit 1";

        rsTable = stTable.executeQuery(strSql);

        rsTable.next();

        ResultSetMetaData rsmd = rsTable.getMetaData();

        int iColumnCount = rsmd.getColumnCount();

        for (int i = 1; i <= iColumnCount; ++i) {
            if (rsmd.getColumnName(i).equalsIgnoreCase("owner")) {
                bOwnerFlag = true;

                break;
            }
        }

        rsTable.close();

        //         if (!bOwnerFlag) {
        //            strSql = "select count(*) from " + strTableName;
        //         } else {
        //            strSql = "select count(*) from " + strTableName
        //                  + ", hm_domain where " + strTableName
        //                  + ".owner=hm_domain.id and (" + strTableName
        //                  + ".owner=" + oDomain.getId().toString()
        //                  + " or hm_domain.domainname='global')";
        //         }

        //         rsTable = stTable.executeQuery(strSql);

        //         rsTable.next();

        //         int iRowCount = rsTable.getInt(1);

        //         rsTable.close();

        int intFileCount = 0;
        int intRecordNum;

        strSql = "select id from hm_domain where domainname='global'";
        rsTable = stTable.executeQuery(strSql);
        long global_id = 0;

        if (rsTable.next())
            global_id = rsTable.getLong(1);

        String strId = String.valueOf(global_id);

        while (true) {

            intRecordNum = 0;
            //for (int i = 0; i < iRowCount || i == 0; i = i + 5000) {

            //            if (!bOwnerFlag) {
            //               strSql = "select * from " + strTableName + " limit " + 5000
            //                     + " offset " + i;
            //            } else {
            //               strSql = "select " + strTableName + ".* from "
            //                     + strTableName + ", hm_domain where "
            //                     + strTableName + ".owner=hm_domain.id and ("
            //                     + strTableName + ".owner="
            //                     + oDomain.getId().toString()
            //                     + " or hm_domain.domainname='global')" + " limit "
            //                     + 5000 + " offset " + i;
            //            }

            if (!bOwnerFlag) {
                strSql = "select * from " + strTableName + " limit " + 5000 + " offset " + intFileCount * 5000;
            } else {
                strSql = "select * from " + strTableName + " where owner=" + oDomain.getId().toString()
                        + " or owner=" + strId + " limit " + 5000 + " offset " + intFileCount * 5000;
            }

            rsTable = stTable.executeQuery(strSql);

            Document document = DocumentHelper.createDocument();

            Element table = document.addElement("table").addAttribute("schema", strTableName);

            rsmd = rsTable.getMetaData();

            int iCount = rsmd.getColumnCount();

            while (rsTable.next()) {
                intRecordNum++;
                Element row = table.addElement("row");

                for (int icol = 1; icol <= iCount; icol++) {
                    String newStr;

                    if (rsTable.getString(icol) == null) {
                        newStr = "NULL";
                    } else if ("null".equalsIgnoreCase(rsTable.getString(icol))) {
                        newStr = "_" + rsTable.getString(icol) + "_";
                    } else {
                        newStr = rsTable.getString(icol);
                    }

                    //                  row.addElement("field").addAttribute("name",
                    //                        rsmd.getColumnName(icol)).addAttribute("value",
                    //                        newStr);
                    if (1 == intRecordNum) {
                        row.addElement("field").addAttribute("name", rsmd.getColumnName(icol))
                                .addAttribute("value", newStr);
                    } else {
                        row.addElement("field").addAttribute("value", newStr);
                    }
                }
            }

            if (intRecordNum <= 0 && 0 != intFileCount)
                break;

            File file;

            if (intFileCount == 0) {
                file = new File(strPath + strTableName.toLowerCase() + ".xml");

            } else {
                file = new File(strPath + strTableName.toLowerCase() + "_" + intFileCount + ".xml");
            }

            intFileCount++;

            output = new XMLWriter(new FileOutputStream(file));

            output.write(document);

            output.close();

            if (5000 > intRecordNum)
                break;
        }

        rsTable.close();

        stTable.close();
    } catch (Exception ex) {
        BeLogTools.restoreLog(BeLogTools.ERROR, ex.getMessage());
    } finally {
        if (null != output) {
            try {
                output.close();
            } catch (Exception outex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, outex.getMessage());
            }
        }

        if (null != rsTable) {
            try {
                rsTable.close();
            } catch (Exception rsex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, rsex.getMessage());
            }
        }

        if (null != stTable) {
            try {
                stTable.close();
            } catch (Exception stex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, stex.getMessage());
            }
        }
    }
}