⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 importxmlworker.java

📁 eq跨平台查询工具源码 eq跨平台查询工具源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * ImportXMLWorker.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.CharArrayWriter;import java.io.File;import java.sql.BatchUpdateException;import java.sql.SQLException;import java.sql.Types;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.HashMap;import java.util.Map;import java.util.Vector;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.executequery.Constants;import org.executequery.GUIUtilities;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;import org.executequery.datasource.ConnectionDataSource;import org.executequery.datasource.ConnectionManager;import org.executequery.gui.browser.ColumnData;import org.executequery.util.Log;import org.underworldlabs.swing.util.SwingWorker;import org.xml.sax.SAXParseException;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the  *           release of version 3.0.0beta1 has meant a  *           resetting of CVS revision numbers. * ---------------------------------------------------------- *//**  * Performs the 'work' during the import XML process. * * @author   Takis Diakoumis * @version  $Revision: 1.9 $ * @date     $Date: 2006/09/05 12:07:26 $ */public class ImportXMLWorker extends AbstractImportExportWorker                              implements Constants {        /** The worker object performing the process */    private SwingWorker worker;    /** The tables to be imported */    private String[] tablesArray;        /** The type of table transfer - single/multiple */    private int tableTransferType;        /** the current file in process */    private String currentImportFileName;        /** the process result */    private String processResult;    /** the specified rollback size */    private int rollbackSize = 0;    /** Whether we are halting on errors */    private boolean haltOnError;    /** the number of errors */    private int errorCount = 0;    // ---------------------------------------    // table specific counters    /** the record count per table */    private int tableRowCount = 0;        /** the table commit count */    private int tableCommitCount = 0;    /** the table statement result */    private int tableInsertCount = 0;    // ---------------------------------------    // total import process counters    /** the current commit block size */    private int commitCount = 0;    /** the total record count */    private int totalRecordCount = 0;    /** the total number of records inserted */    private int totalInsertCount = 0;    /** the file format - single or multiple */    private int fileFormat;        // ---------------------------------------    /** SAX exception message not to be printed */    private final String SAX_NO_PRINT_EXCEPTION = "SAX_NO_PRINT";        public ImportXMLWorker(ImportExportProcess parent,                           ImportExportProgressPanel progress) {        super(parent, progress);        tableTransferType = parent.getTableTransferType();        transferData();    }        // initialise results Hashtable and start the worker    private void transferData() {        setIndeterminateProgress(true);        reset();                worker = new SwingWorker() {            public Object construct() {                return doWork();            }            public void finished() {                String result = (String)get();                setResult(result);                printResults();                setProgressStatus(-1);                setIndeterminateProgress(false);                getParent().setProcessComplete(result == SUCCESS);                GUIUtilities.scheduleGC();            }        };        worker.start();    }        // retrieve the file, create the parser and start parseing    private Object doWork() {                haltOnError = (getParent().getOnError() == ImportExportProcess.STOP_TRANSFER);        // the XML handler        ImportXMLHandler handler = null;                // the parser factory for the process        SAXParserFactory factory = null;                // the parser for the process        SAXParser parser = null;                // the transfer objects        Vector transfers = getParent().getDataFileVector();                // single or multiple file for mutliple transfer        fileFormat = getParent().getMutlipleTableTransferType();        // the rollback/commit block size        rollbackSize = getParent().getRollbackSize();                appendProgressText("Beginning import from XML process...");        appendProgressText("Using connection: " +                 getParent().getDatabaseConnection().getName());        // record the start time        start();        try {                       // the size of the transfer            int transfers_size = transfers.size();            // the complete path to the file            String fullPath = null;            // the current import file            File importFile = null;                        factory = SAXParserFactory.newInstance();            factory.setNamespaceAware(true);            handler = new ImportXMLHandler();            parser = factory.newSAXParser();                        for (int i = 0; i < transfers_size; i++) {                DataTransferObject obj = (DataTransferObject)transfers.get(i);                if (fileFormat == ImportExportProcess.SINGLE_FILE) {                    tablesArray = getParent().getSelectedTables();                }                else {                    tablesArray = new String[]{obj.getTableName()};                }                importFile = new File(obj.getFileName());                currentImportFileName = importFile.getName();                if (i > 0) {                    parser.reset();                    handler.reset();                }                                parser.parse(importFile, handler);            }            // commit the last remaining block or where             // set to commit at the end of all files            if (rollbackSize != ImportExportProcess.COMMIT_END_OF_FILE) {                setProgressStatus(-1);                try {                    if (conn != null) {                        conn.commit();                        totalInsertCount += commitCount;                    }                }                catch (SQLException e) {                    errorCount++;                    processResult = FAILED;                    outputExceptionError("Error committing last transaction block", e);                }            }            if (processResult == null) {                return SUCCESS;            }            return processResult;                    }        catch (SAXException e) {            if (e.getMessage() != SAX_NO_PRINT_EXCEPTION) {                outputExceptionError("Error parsing XML data file", e);            }            if (processResult != CANCELLED) {                return FAILED;            }            return processResult;        }        catch (Exception e) {            logException(e);            outputExceptionError("Error importing table data from XML", e);            return FAILED;        }         finally {            finish();            setRecordCount(totalRecordCount);            setRecordCountProcessed(totalInsertCount);            setErrorCount(errorCount);        }    }       private void logException(Throwable e) {        if (Log.isDebugEnabled()) {            Log.debug("Error on XML import.", e);        }    }    /**     * Cancels the current in-process transfer.      */    public void cancelTransfer() {        worker.interrupt();        getParent().cancelTransfer();    }        /**      * Indicates that the process has completed.      */    public void finished() {}        private String lastTableResultsPrinted;        /**     * Prints the table specific execution results to the output buffer.     */    private void printTableResult(int tableRowCount,                                   int tableInsertCount,                                   String tableName) {                if (tableName == lastTableResultsPrinted) {            return;        }                // update the progress display        outputBuffer.append("Records processed: ");        outputBuffer.append(tableRowCount);        outputBuffer.append("\nRecords inserted: ");        outputBuffer.append(tableInsertCount);        outputBuffer.append("\nImport complete for table: ");        outputBuffer.append(tableName);        appendProgressText(outputBuffer);        lastTableResultsPrinted = tableName;    }    class ImportXMLHandler extends DefaultHandler {                private CharArrayWriter contents = new CharArrayWriter();                // ----------------------------------        // --- reuseable string constants ---        // ----------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -