private void okButtonActionPerformed(ActionEvent evt) { // SQL file selected? String sqlFilePath = sqlSourceTextField.getText(); if ((null == sqlFilePath) || (0 == sqlFilePath.length())) { JOptionPane.showMessageDialog( this, "Please select a source SQL file!", "Invalid Input", JOptionPane.ERROR_MESSAGE); return; } // destination XML file selected String xmlFilePath = xmlDestinationTextField.getText(); if ((null == xmlFilePath) || (0 == xmlFilePath.length())) { JOptionPane.showMessageDialog( this, "Please select a destination XML file!", "Invalid Input", JOptionPane.ERROR_MESSAGE); return; } // do the conversion boolean errorOccured = convert(sqlFilePath, xmlFilePath); if (errorOccured) { JOptionPane.showMessageDialog( this, "Conversion failed!", "Conversion Failure", JOptionPane.ERROR_MESSAGE); return; } boolean importIntoWorkspace = importCheckBox.isSelected(); if (importIntoWorkspace) { XMLDocument xmlDocument = null; try { xmlDocument = new XMLDocument(xmlFilePath); } catch (JDOMException e) { log.error(e.getMessage(), e); ErrorManager.getInstance().errorOccured(e.getMessage(), e); errorOccured = true; } catch (IOException e) { log.error(e.getMessage(), e); ErrorManager.getInstance().errorOccured(e.getMessage(), e); errorOccured = true; } Workspace.getInstance().addXMLDocument(xmlDocument); EventManager.getInstance().fireXMLDocumentAdded(xmlDocument); } if (errorOccured) { JOptionPane.showMessageDialog( this, "Import into workspace failed!", "Import Failure", JOptionPane.ERROR_MESSAGE); return; } // if (!errorOccured) { // dispose(); // } dispose(); }
/** returns true if error occurred, false if everything went fine. */ private boolean convert(String sqlFilePath, String xmlFilePath) { BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(sqlFilePath)); } catch (FileNotFoundException e) { log.error(e.getMessage(), e); ErrorManager.getInstance().errorOccured(e.getMessage(), e); return true; } // This is the parser creation! The whole parser is used on a // static basis, so no instance variable is needed. new ANSISQL92Parser(reader); File file = new File(sqlFilePath); XMLCreatorVisitor visitor = new XMLCreatorVisitor(file.getName()); int statementCount = 0; try { // direct_sql_statement stmt = eg2.direct_sql_statement(); getNextDirectSQLStatement stmt; stmt = ANSISQL92Parser.getNextDirectSQLStatement(); while (null != stmt) { // Every SQL direct statement or schema manipulation statement // has to be terminated by a semicolon ANSISQL92Parser.Semicolon(); // insert parsed objects into graph, the graph is managed by // the visitor. The visitor creates the Schema Element // instances stmt.accept(visitor); statementCount++; stmt = ANSISQL92Parser.getNextDirectSQLStatement(); switch (stmt.f0.which) { case 1: stmt = null; break; } } } catch (ParseException e) { log.error(e.getMessage(), e); ErrorManager.getInstance().errorOccured(e.getMessage(), e); return true; } Element root = visitor.getRoot(); Document document = new Document(root); // This is no DEBUG code! Do not comment it! It writes XML to a file XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); try { java.io.FileWriter writer; writer = new java.io.FileWriter(xmlFilePath); outputter.output(document, writer); writer.flush(); writer.close(); } catch (IOException e) { log.error(e.getMessage(), e); ErrorManager.getInstance().errorOccured(e.getMessage(), e); return true; } return false; }