📄 exportxmlworker.java
字号:
/* * ExportXMLWorker.java * * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */package org.executequery.gui.importexport;import java.io.FileOutputStream;import java.io.File;import java.io.IOException;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Types;import java.util.Vector;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.sax.SAXSource;import javax.xml.transform.stream.StreamResult;import org.xml.sax.ContentHandler;import org.xml.sax.DTDHandler;import org.xml.sax.EntityResolver;import org.xml.sax.ErrorHandler;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.AttributesImpl;import org.executequery.Constants;import org.executequery.databasemediators.MetaDataValues;import org.executequery.GUIUtilities;import org.executequery.gui.browser.ColumnData;import org.executequery.util.Base64;import org.executequery.util.Log;import org.underworldlabs.jdbc.DataSourceException;import org.underworldlabs.swing.util.SwingWorker;import org.underworldlabs.util.MiscUtils;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the * release of version 3.0.0beta1 has meant a * resetting of CVS revision numbers. * ---------------------------------------------------------- *//** <p>Performs the 'work' during the export XML process. * * @author Takis Diakoumis * @version $Revision: 1.10 $ * @date $Date: 2006/09/05 12:07:26 $ * @author Dragan Vasic */public class ExportXMLWorker extends AbstractImportExportWorker implements Constants { /** The thread worker process */ private SwingWorker worker; /** Utility class to retrieve DB metadata */ private MetaDataValues metaData; /** The tables to be exported */ private String[] tablesArray; /** The type of table transfer - single/multiple */ private int tableTransferType; /** the process result */ private String processResult; /** the current file in process */ private String currentExportFileName; /** * Constructs a new instance with the specified * parent object - an instance of <code>ImportExportXMLPanel</code>. * * @param the parent for this process */ public ExportXMLWorker(ImportExportProcess parent, ImportExportProgressPanel exportingDialog) { super(parent, exportingDialog); transferData(); } /** * Begins the transfer process setting up the <code>SwingWorker</code> * and creating the progress dialog. */ private void transferData() { reset(); worker = new SwingWorker() { public Object construct() { return doWork(); } public void finished() { String result = (String)get(); setResult(result); releaseResources(getParent().getDatabaseConnection()); printResults(); setProgressStatus(-1); getParent().setProcessComplete(result == SUCCESS); GUIUtilities.scheduleGC(); } }; worker.start(); } /** <p>Performs the actual processing for the worker. */ private Object doWork() { tableTransferType = parent.getTableTransferType(); // the custom parser TableDataParser parser = null; // the data input source TableDataInputSource tableInputSource = new TableDataInputSource(); // the transfer objects Vector transfers = getParent().getDataFileVector(); // single or multiple file for mutliple transfer int fileFormat = parent.getMutlipleTableTransferType(); // the size of the transfer int transfers_size = transfers.size(); try { // the output stream to file FileOutputStream os = null; // the stream result StreamResult streamResult; // the transformer factory to get the transformer TransformerFactory transFactory = TransformerFactory.newInstance(); // the actual transformer performing the process Transformer transformer = transFactory.newTransformer(); // the custom parser parser = new TableDataParser(); // the SAX source object SAXSource source = new SAXSource(parser, tableInputSource); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); appendProgressText("Beginning export to XML process..."); appendProgressText("Using connection: " + getParent().getDatabaseConnection().getName()); // record the start time start(); Log.debug("Transfers count: " + transfers_size); for (int i = 0; i < transfers_size; i++) { DataTransferObject obj = (DataTransferObject)transfers.elementAt(i); if (fileFormat == ImportExportProcess.SINGLE_FILE) { tablesArray = parent.getSelectedTables(); } else { tablesArray = new String[]{obj.getTableName()}; } File exportFile = new File(obj.getFileName()); currentExportFileName = exportFile.getName(); os = new FileOutputStream(exportFile); streamResult = new StreamResult(os); transformer.transform(source, streamResult); os.close(); } if (processResult == null) { return SUCCESS; } return processResult; } catch (Exception e) { logException(e); outputExceptionError("Error exporting table data to file", e); return FAILED; } finally { finish(); if (fileFormat == ImportExportProcess.SINGLE_FILE) { setTableCount(tablesArray.length); } else { setTableCount(transfers_size); } setRecordCount(parser.getTotalRecordCount() + parser.getErrorCount()); setErrorCount(parser.getErrorCount()); setRecordCountProcessed(parser.getTotalRecordCount()); } } private void logException(Throwable e) { if (Log.isDebugEnabled()) { Log.debug("Error on XML export.", e); } } public void cancelTransfer() { worker.interrupt(); getParent().cancelTransfer(); } public void finished() {} class TableDataInputSource extends InputSource { public TableDataInputSource() {} /** * Retrieves the records for the specified table as a * <code>ResultSet</code> object. * * @return the records returned from the query */ public ResultSet getTableData(String table, Vector<?> columns) { try { return getResultSet(table, columns); } catch (DataSourceException e) { outputExceptionError("Error retrieving table data", e); appendProgressErrorText(outputBuffer); return null; } catch (SQLException e) { outputExceptionError("Error retrieving table data", e); return null; } } public String getUserName() { return getParent().getMetaDataUtility().getUser(); } public String getJDBCURL() { return getParent().getMetaDataUtility().getURL(); } public void cancelStatement() { try { if (stmnt != null) { stmnt.cancel(); } } catch (SQLException e) { System.err.println("Exception closing statement at: " + e.getMessage()); } } public String getSchemaName() { return getParent().getSchemaName(); } } // class TableDataInputSource class TableDataParser implements XMLReader { /** The name space - empty string literal */ private String nsu = EMPTY; /** Attributes object */ private AttributesImpl atts = new AttributesImpl(); /** The process content handler */ private ContentHandler handler; /** Single or multiple file for mutliple transfer */ int fileFormat; /** the XML format style to use */ int xmlFormat; /** the total error count */ int errorCount = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -