List of usage examples for org.dom4j.io XMLWriter XMLWriter
public XMLWriter(OutputFormat format) throws UnsupportedEncodingException
From source file:InitializeDB.java
License:Open Source License
public static void clearEntryWithVulsoft(String filename) { try {/*www . ja v a 2s. co m*/ SAXReader saxReader = new SAXReader(); Document document = saxReader.read(filename); List soft = document .selectNodes("/*[local-name(.)='nvd']/*[local-name(.)='entry']/*[local-name(.)='vuln_soft']"); Iterator sft = soft.iterator(); Element nvd = (Element) document .selectSingleNode("/*[local-name(.)='nvd']"); while (sft.hasNext()) { Element vsft = (Element) sft.next(); nvd.remove(vsft.getParent()); XMLWriter output = new XMLWriter(new FileWriter(filename));// output.write(document); output.flush(); output.close(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:at.jabberwocky.impl.core.io.JabberwockyComponentConnection.java
public void connect() throws XMPPComponentException { if (logger.isLoggable(Level.FINER)) logger.log(Level.FINER, "Starting connection"); try {/*from w w w. j a v a 2 s.c om*/ connection = new Socket(config.getDomain(), config.getPort()); connection.setSoTimeout(0); } catch (IOException ex) { connection = null; logger.log(Level.SEVERE, "Cannnot connect to {0}:{1}", new Object[] { config.getDomain(), config.getPort() }); throw new XMPPComponentException("Creating socket connection to " + config.getDomain(), ex); } if (logger.isLoggable(Level.FINER)) { logger.log(Level.FINER, "Initializing XML parser"); } try { reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8")); } catch (IOException ex) { logger.log(Level.SEVERE, "Cannot create reader/writer", ex); close(); throw new XMPPComponentException("Cannot create reader/writer", ex); } try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); xmlParser = factory.newPullParser(); xmlParser.setInput(reader); xmlWriter = new XMLWriter(writer); } catch (XmlPullParserException ex) { logger.log(Level.SEVERE, "Cannot initialze XML parser", ex); close(); throw new XMPPComponentException("Cannot initialize XML parser", ex); } subdomainJID = new JID(config.getName() + "." + config.getDomain()); domainJID = new JID(config.getDomain()); try { if (!performHandshake(config.getName(), config.getSharedSecret())) { logger.log(Level.SEVERE, "Handshake error: {0}", errorTag); throw new XMPPComponentException("Handshake error: " + errorTag); } if (logger.isLoggable(Level.FINER)) logger.log(Level.FINER, "Handshake successful"); } catch (IOException | XmlPullParserException ex) { logger.log(Level.SEVERE, "Error during performHandshake()", ex); throw new XMPPComponentException("Error during performHandshake()", ex); } }
From source file:com.ah.be.admin.adminBackupUnit.AhBackupTool.java
private void backupDomainTable(String strPath, String strDomainName, String strTableName) { Connection con = null;//from w w w . j a va 2s . 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;//from ww w . j ava2s . c om 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;/* ww w .ja v a 2s. com*/ try { 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;//from ww w . ja va 2 s. co m 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 storeCertainDomainTable(String strTableName, String strPath, HmDomain oDomain, Connection conTable) {//w w w .j a v a 2 s.c om 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;/*w ww. j a v a 2 s. c om*/ ResultSet rsTable = null; 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()); } } } }
From source file:com.ah.be.admin.adminBackupUnit.AhBackupTool.java
private void storeSpecialTable(String strTableName, String strPath, HmDomain oDomain, Connection conTable) { XMLWriter output = null;//from ww w . j a v a2s. co m 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; } stTable = conTable.createStatement(); // String strSql = "select count(*) from " + strTableName // + ", hm_domain where " + strTableName // + ".owner=hm_domain.id and (" + strTableName + ".owner=" // + oDomain.getId().toString() // + " or hm_domain.domainname='global')"; // // begin = System.currentTimeMillis(); // // rsTable = stTable.executeQuery(strSql); // // dbtime += System.currentTimeMillis() - begin; // // rsTable.next(); // // int iRowCount = rsTable.getInt(1); // // rsTable.close(); String strSql = "select id from " + strTableName + " where owner=" + oDomain.getId().toString() + " order by id asc limit 1"; rsTable = stTable.executeQuery(strSql); long id = 0; if (rsTable.next()) id = rsTable.getLong(1) - 1; // else // return; rsTable.close(); int intFileCount = 0; String strId = String.valueOf(id); int intRecordNum; while (true) { intRecordNum = 0; // strSql = "select * from " + strTableName // + " where owner=" + oDomain.getId().toString() // + " and id>" + strId +" order by id asc "+ " limit " + 5000; strSql = "select * from " + strTableName + " where id > " + strId + " and owner=" + oDomain.getId().toString() + " order by id asc " + " limit " + 5000; 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 ("id".equalsIgnoreCase(rsmd.getColumnName(icol))) { strId = 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()); } } } }
From source file:com.amalto.workbench.utils.MDMServerHelper.java
License:Open Source License
private static boolean saveRootElement(Element rootElement) { try {//from ww w.j av a 2s .c om XMLWriter writer = new XMLWriter(new FileWriter(MDMServerHelper.workbenchConfigFile)); writer.write(rootElement.getDocument()); writer.close(); return true; } catch (IOException e) { log.error(e.getMessage(), e); return false; } }