📄 exportxmlworker.java
字号:
/** the total record count */ int totalRecordCount = 0; /** the table's record count */ int recordCount = 0; // --------------------------- // --- define the XML tags --- // --------------------------- private final String rootElement = "schema"; private final String NAME = "name"; private final String schemaUrlAtt = "jdbcurl"; private final String schemaUserAtt = "user"; private final String tableNode = "table"; private final String rowAttNode = "rownum"; private final String rowNode = "row"; private final String newLine_s = "\n"; private final String attType1 = "CDATA"; private final String attType2 = "ID"; // ------------------------------- // --- define the line indents --- // ------------------------------- private final String indent_1 = "\n "; private final String indent_2 = "\n "; private final String indent_3 = "\n "; // temporary line indents private String indent_1a; private String indent_2a; private String indent_3a; public TableDataParser() { fileFormat = getParent().getMutlipleTableTransferType(); xmlFormat = getParent().getXMLFormat(); } public void parse(InputSource input) throws SAXException, IOException { if (!(input instanceof TableDataInputSource)) throw new SAXException("Parser can only accept a TableDataInputSource"); parse((TableDataInputSource)input); } public void parse(TableDataInputSource input) throws IOException, SAXException { ResultSet rs = null; try { if (handler == null) { throw new SAXException("No content handler"); } indent_1a = indent_1; indent_2a = indent_2; indent_3a = indent_3; // start xml document here to account // for multiple table loop handler.startDocument(); if (xmlFormat == ImportExportProcess.SCHEMA_ELEMENT) { atts.addAttribute(EMPTY, NAME, NAME, attType1, input.getSchemaName()); atts.addAttribute(EMPTY, schemaUrlAtt, schemaUrlAtt, attType1, input.getJDBCURL()); atts.addAttribute(EMPTY, schemaUserAtt, schemaUserAtt, attType1, input.getUserName()); handler.startElement(nsu, rootElement, rootElement, atts); handler.ignorableWhitespace(newLine_s.toCharArray(), 0, 1); atts.removeAttribute(atts.getIndex(NAME)); atts.removeAttribute(atts.getIndex(schemaUrlAtt)); atts.removeAttribute(atts.getIndex(schemaUserAtt)); } else if (xmlFormat == ImportExportProcess.TABLE_ELEMENT) { indent_3a = indent_2; indent_2a = indent_1; indent_1a = newLine_s; } int progressStatus = 0; int progressSet = 0; int progressCheck = 0; // the table column names String[] cols = null; for (int j = 0; j < tablesArray.length; j++) { String table = tablesArray[j]; // retrieve the record count int totalRecords = getTableRecordCount(table); setProgressBarMaximum(totalRecords); outputBuffer.append("---------------------------\nTable: "); outputBuffer.append(table); outputBuffer.append("\nRecords found: "); outputBuffer.append(totalRecords); outputBuffer.append("\nExport file: "); outputBuffer.append(currentExportFileName); appendProgressText(outputBuffer); // retrieve the columns to be exported (or all) Vector<ColumnData> columns = getColumns(table); rs = input.getTableData(table, columns); recordCount = 0; setProgressStatus(0); cols = new String[columns.size()]; for (int i = 0; i < cols.length; i++) { cols[i] = columns.elementAt(i).toString().toLowerCase(); } if (xmlFormat == ImportExportProcess.SCHEMA_ELEMENT) { handler.ignorableWhitespace(indent_1a.toCharArray(), 0, indent_1a.length()); } atts.addAttribute(EMPTY, NAME, NAME, attType1, tablesArray[j]); handler.startElement(nsu, tableNode, tableNode, atts); atts.removeAttribute(atts.getIndex(NAME)); appendProgressText("Exporting data..."); while (rs.next()) { if (Thread.interrupted()) { rs.close(); setProgressStatus(-1); throw new InterruptedException(); } totalRecordCount++; recordCount++; handler.ignorableWhitespace(newLine_s.toCharArray(), 0, 1); handler.ignorableWhitespace(indent_2a.toCharArray(), 0, indent_2a.length()); atts.addAttribute(EMPTY, rowAttNode, rowAttNode, attType2, Integer.toString(recordCount)); handler.startElement(nsu, rowNode, rowNode, atts); atts.removeAttribute(atts.getIndex(rowAttNode)); int type = -1; String value = null; for (int i = 0; i < cols.length; i++) { type = columns.get(i).getSQLType(); switch (type) { case Types.TINYINT: case Types.SMALLINT: case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: case Types.BIT: case Types.BOOLEAN: case Types.BIGINT: case Types.REAL: case Types.DECIMAL: case Types.NUMERIC: case Types.FLOAT: case Types.DOUBLE: case Types.INTEGER: case Types.DATE: case Types.TIMESTAMP: case Types.TIME: value = rs.getString(i+1); break; case Types.BLOB: case Types.CLOB: case Types.BINARY: case Types.LONGVARBINARY: value = Base64.encodeBytes( MiscUtils.inputStreamToBytes( rs.getBinaryStream(i+1))); break; default: value = rs.getString(i+1); break; } writeXML(cols[i], value, indent_3); } handler.ignorableWhitespace(indent_2a.toCharArray(), 0, indent_2a.length()); handler.endElement(nsu, rowNode, rowNode); setProgressStatus(recordCount); } rs.close(); handler.ignorableWhitespace(newLine_s.toCharArray(), 0, 1); handler.ignorableWhitespace(indent_1a.toCharArray(), 0, indent_1a.length()); handler.endElement(nsu, tableNode, tableNode); handler.ignorableWhitespace(newLine_s.toCharArray(), 0, 1); outputBuffer.append("Export successful for table: "); outputBuffer.append(table); appendProgressText(outputBuffer); } if (xmlFormat == ImportExportProcess.SCHEMA_ELEMENT) { handler.ignorableWhitespace(newLine_s.toCharArray(), 0, 1); handler.endElement(nsu, rootElement, rootElement); } handler.endDocument(); } catch (InterruptedException e) { input.cancelStatement(); processResult = CANCELLED; } catch (SQLException e) { logException(e); outputExceptionError("SQL error exporting table data to file", e); processResult = FAILED; } catch (OutOfMemoryError e) { processResult = FAILED; outputExceptionError("Error exporting table data to file", e); } catch (Exception e) { logException(e); outputExceptionError("Error exporting table data to file", e); processResult = FAILED; } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) {} } } } protected int getErrorCount() { return errorCount; } protected int getTotalRecordCount() { return totalRecordCount; } private void writeXML(String name, String line, String space) throws SAXException { if (line == null) { line = EMPTY; } int textLength = line.length(); handler.ignorableWhitespace(space.toCharArray(), 0, space.length()); handler.startElement(nsu, name, name, atts); handler.characters(line.toCharArray(), 0, textLength); handler.endElement(nsu, name, name); } public void setContentHandler(ContentHandler handler) { this.handler = handler; } public ContentHandler getContentHandler() { return this.handler; } public void setErrorHandler(ErrorHandler handler) {} public ErrorHandler getErrorHandler() { return null; } public void parse(String systemId) throws IOException, SAXException { } public DTDHandler getDTDHandler() { return null; } public EntityResolver getEntityResolver() { return null; } public void setEntityResolver(EntityResolver resolver) {} public void setDTDHandler(DTDHandler handler) {} public Object getProperty(String name) { return null; } public void setProperty(String name, java.lang.Object value) {} public void setFeature(String name, boolean value) {} public boolean getFeature(String name) { return false; } } // class ConnectionParser }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -