Update of /cvsroot/squirrel-sql/sql12/fw/src/net/sourceforge/squirrel_sql/fw/gui/action
In directory sfp-cvsdas-3.v30.ch3.sourceforge.com:/tmp/cvs-serv2561/fw/src/net/sourceforge/squirrel_sql/fw/gui/action
Modified Files:
TableExportCsvCommand.java I18NStrings.properties
TableExportCsvDlg.java TableExportCsvController.java
Log Message:
XML Export:
Tables in SQuirreL's GUI now allow to export data as XML (besides CSV and MS-Excel export)
Thanks to mironsadziak for the patch (Patch ID 2905595)
Index: TableExportCsvController.java
===================================================================
RCS file: /cvsroot/squirrel-sql/sql12/fw/src/net/sourceforge/squirrel_sql/fw/gui/action/TableExportCsvController.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** TableExportCsvController.java 9 Jul 2008 22:51:05 -0000 1.12
--- TableExportCsvController.java 1 Dec 2009 23:22:57 -0000 1.13
***************
*** 37,40 ****
--- 37,41 ----
private static final String PREF_KEY_FORMAT_CSV = "SquirrelSQL.csvexport.formatCSV";
private static final String PREF_KEY_FORMAT_XLS = "SquirrelSQL.csvexport.formatXLS";
+ private static final String PREF_KEY_FORMAT_XML = "SquirrelSQL.csvexport.formatXML";
private static final StringManager s_stringMgr =
***************
*** 47,50 ****
--- 48,52 ----
public static final int EXPORT_FORMAT_CSV = 0;
public static final int EXPORT_FORMAT_XLS = 1;
+ public static final int EXPORT_FORMAT_XML = 2;
TableExportCsvController()
***************
*** 125,128 ****
--- 127,138 ----
}
});
+
+ _dlg.radFormatXML.addActionListener(new ActionListener()
+ {
+ public void actionPerformed(ActionEvent e)
+ {
+ onFormat(true);
+ }
+ });
***************
*** 202,205 ****
--- 212,227 ----
}
}
+ else if (_dlg.radFormatXML.isSelected())
+ {
+ _dlg.lblSeparator.setEnabled(false);
+ _dlg.lblCharset.setEnabled(false);
+ _dlg.chkSeparatorTab.setEnabled(false);
+ _dlg.txtSeparatorChar.setEnabled(false);
+ _dlg.charsets.setEnabled(false);
+ if(replaceEnding)
+ {
+ replaceFileEnding();
+ }
+ }
else
{
***************
*** 220,223 ****
--- 242,249 ----
newEnding = "xls";
}
+ else if (_dlg.radFormatXML.isSelected())
+ {
+ newEnding = "xml";
+ }
else
{
***************
*** 377,380 ****
--- 403,407 ----
Preferences.userRoot().putBoolean(PREF_KEY_FORMAT_CSV, _dlg.radFormatCSV.isSelected());
Preferences.userRoot().putBoolean(PREF_KEY_FORMAT_XLS, _dlg.radFormatXLS.isSelected());
+ Preferences.userRoot().putBoolean(PREF_KEY_FORMAT_XML, _dlg.radFormatXML.isSelected());
Preferences.userRoot().putBoolean(PREF_KEY_SEPERATOR_TAB, _dlg.chkSeparatorTab.isSelected());
Preferences.userRoot().put(PREF_KEY_SEPERATOR_CHAR, _dlg.txtSeparatorChar.getText());
***************
*** 410,413 ****
--- 437,444 ----
_dlg.radFormatXLS.setSelected(true);
}
+ else if(userRoot.getBoolean(PREF_KEY_FORMAT_XML, false))
+ {
+ _dlg.radFormatXML.setSelected(true);
+ }
else
{
***************
*** 573,576 ****
--- 604,611 ----
return EXPORT_FORMAT_XLS;
}
+ else if(_dlg.radFormatXML.isSelected())
+ {
+ return EXPORT_FORMAT_XML;
+ }
else
{
Index: TableExportCsvDlg.java
===================================================================
RCS file: /cvsroot/squirrel-sql/sql12/fw/src/net/sourceforge/squirrel_sql/fw/gui/action/TableExportCsvDlg.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** TableExportCsvDlg.java 9 Jul 2008 11:47:54 -0000 1.6
--- TableExportCsvDlg.java 1 Dec 2009 23:22:57 -0000 1.7
***************
*** 34,37 ****
--- 34,38 ----
JCheckBox chkWithHeaders;
JRadioButton radFormatXLS;
+ JRadioButton radFormatXML;
JRadioButton radFormatCSV;
JLabel lblSeparator;
***************
*** 75,79 ****
super(GUIUtils.getMainFrame(), true);
! // i18n[TableExportCSVDlg.exportTitleNew=CSV / MS Excel export]
setTitle(s_stringMgr.getString("TableExportCSVDlg.exportTitleNew"));
--- 76,80 ----
super(GUIUtils.getMainFrame(), true);
! // i18n[TableExportCSVDlg.exportTitleNew=CSV / MS Excel / XML export]
setTitle(s_stringMgr.getString("TableExportCSVDlg.exportTitleNew"));
***************
*** 138,143 ****
gbc = new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
ret.add(radFormatXLS, gbc);
! gbc = new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0);
ret.add(new JPanel(), gbc);
--- 139,149 ----
gbc = new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
ret.add(radFormatXLS, gbc);
+
+ // i18n[TableExportCsvDlg.formatXML=Export XML file]
+ radFormatXML = new JRadioButton(s_stringMgr.getString("TableExportCsvDlg.formatXML"));
+ gbc = new GridBagConstraints(2, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
+ ret.add(radFormatXML, gbc);
! gbc = new GridBagConstraints(2, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0);
ret.add(new JPanel(), gbc);
***************
*** 146,149 ****
--- 152,156 ----
bg.add(radFormatCSV);
bg.add(radFormatXLS);
+ bg.add(radFormatXML);
return ret;
Index: I18NStrings.properties
===================================================================
RCS file: /cvsroot/squirrel-sql/sql12/fw/src/net/sourceforge/squirrel_sql/fw/gui/action/I18NStrings.properties,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** I18NStrings.properties 9 Jul 2008 11:47:54 -0000 1.12
--- I18NStrings.properties 1 Dec 2009 23:22:57 -0000 1.13
***************
*** 12,16 ****
TableExportCsvDlg.defaultLabel=Default
! TableExportCSVDlg.exportTitleNew=CSV / MS Excel export
TableExportCsvDlg.exportCsvFile=Export to file:
TableExportCsvDlg.columnSeparator=Column Separator:
--- 12,16 ----
TableExportCsvDlg.defaultLabel=Default
! TableExportCSVDlg.exportTitleNew=CSV / MS Excel / XML export
TableExportCsvDlg.exportCsvFile=Export to file:
TableExportCsvDlg.columnSeparator=Column Separator:
***************
*** 45,48 ****
--- 45,49 ----
TableExportCsvDlg.formatCSV=Export CSV file
TableExportCsvDlg.formatXLS=Export MS Excel (XLS) file
+ TableExportCsvDlg.formatXML=Export XML file
TableExportCsvDlg.useGlobalPrefsFormatingExcel=Use formatting as configured in Global Prefs (recommended for MS Excel)
\ No newline at end of file
Index: TableExportCsvCommand.java
===================================================================
RCS file: /cvsroot/squirrel-sql/sql12/fw/src/net/sourceforge/squirrel_sql/fw/gui/action/TableExportCsvCommand.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -d -r1.16 -r1.17
*** TableExportCsvCommand.java 1 Dec 2009 22:59:31 -0000 1.16
--- TableExportCsvCommand.java 1 Dec 2009 23:22:57 -0000 1.17
***************
*** 3,9 ****
--- 3,11 ----
import java.io.BufferedWriter;
import java.io.File;
+ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
+ import java.io.PrintStream;
import java.sql.Types;
import java.util.Calendar;
***************
*** 11,14 ****
--- 13,28 ----
import javax.swing.JOptionPane;
import javax.swing.JTable;
+ import javax.xml.parsers.DocumentBuilder;
+ import javax.xml.parsers.DocumentBuilderFactory;
+ import javax.xml.parsers.ParserConfigurationException;
+ import javax.xml.transform.OutputKeys;
+ import javax.xml.transform.Transformer;
+ import javax.xml.transform.TransformerException;
+ import javax.xml.transform.TransformerFactory;
+ import javax.xml.transform.dom.DOMSource;
+ import javax.xml.transform.stream.StreamResult;
+
+ import org.w3c.dom.Document;
+ import org.w3c.dom.Element;
import jxl.Workbook;
***************
*** 208,211 ****
--- 222,229 ----
return writeXLS(file, includeHeaders, nbrSelCols, selCols, nbrSelRows, ctrl, selRows);
}
+ else if(TableExportCsvController.EXPORT_FORMAT_XML == ctrl.getExportFormat())
+ {
+ return writeXML(file, includeHeaders, nbrSelCols, selCols, nbrSelRows, ctrl, selRows);
+ }
else
{
***************
*** 234,238 ****
JOptionPane.showMessageDialog(GUIUtils.getMainFrame(), msg);
return false;
! }
}
--- 252,270 ----
JOptionPane.showMessageDialog(GUIUtils.getMainFrame(), msg);
return false;
! } catch (ParserConfigurationException e) {
! Object[] params = new Object[]{file, e.getMessage()};
! // i18n[TableExportCsvCommand.failedToWriteFile=Failed to write file\n{0}\nError message\n{1}\nSee last log entry for details.]
! String msg = s_stringMgr.getString("TableExportCsvCommand.failedToWriteFile", params);
! s_log.error(msg, e);
! JOptionPane.showMessageDialog(GUIUtils.getMainFrame(), msg);
! return false;
! } catch (TransformerException e) {
! Object[] params = new Object[]{file, e.getMessage()};
! // i18n[TableExportCsvCommand.failedToWriteFile=Failed to write file\n{0}\nError message\n{1}\nSee last log entry for details.]
! String msg = s_stringMgr.getString("TableExportCsvCommand.failedToWriteFile", params);
! s_log.error(msg, e);
! JOptionPane.showMessageDialog(GUIUtils.getMainFrame(), msg);
! return false;
! }
}
***************
*** 286,289 ****
--- 318,439 ----
}
+ /**
+ * Writes the selected table data to XML file.
+ *
+ * <p>
+ * Uses DOM for output
+ * </p>
+ *
+ * @param file
+ * File to output to
+ * @param includeHeaders
+ * Set to true if header info (column names) should be included
+ * @param nbrSelCols
+ * Number of selected columns
+ * @param selCols
+ * Selected columns
+ * @param nbrSelRows
+ * Number of selected rows
+ * @param ctrl
+ * Export controller
+ * @param selRows
+ * Selected rows
+ * @return If everything went well returns true
+ * @throws ParserConfigurationException
+ * If the XML DocumentBuilder cannnot be created
+ * @throws FileNotFoundException
+ * When failing to create the file
+ * @throws TransformerException
+ * When failing to output the XML structure to the file
+ */
+ private boolean writeXML(File file, boolean includeHeaders, int nbrSelCols,
+ int[] selCols, int nbrSelRows, TableExportCsvController ctrl,
+ int[] selRows) throws ParserConfigurationException,
+ FileNotFoundException, TransformerException {
+
+ // Using a factory to get DocumentBuilder for creating XML's
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder builder = factory.newDocumentBuilder();
+
+ // Here instead of parsing an existing document we want to
+ // create a new one.
+ Document testDoc = builder.newDocument();
+
+ // 'table' is the main tag in the XML.
+ Element root = testDoc.createElement("table");
+ testDoc.appendChild(root);
+
+ // 'columns' tag will contain informations about columns
+ Element columns = testDoc.createElement("columns");
+ root.appendChild(columns);
+ int curRow = 0;
+ if (includeHeaders) {
+ for (int colIdx = 0; colIdx < nbrSelCols; ++colIdx) {
+ String columnName = _table.getColumnName(selCols[colIdx]);
+
+ Element columnEl = testDoc.createElement("column");
+ columnEl.setAttribute("number", String.valueOf(colIdx));
+ columns.appendChild(columnEl);
+
+ Element columnNameEl = testDoc.createElement("name");
+ columnNameEl.setTextContent(columnName);
+ columnEl.appendChild(columnNameEl);
+ }
+ curRow++;
+ }
+
+ // 'rows' tag contains the data extracted from the table
+ Element rows = testDoc.createElement("rows");
+ root.appendChild(rows);
+
+ for (int rowIdx = 0; rowIdx < nbrSelRows; ++rowIdx) {
+ Element row = testDoc.createElement("row");
+ row.setAttribute("rowNumber", String.valueOf(rowIdx));
+ rows.appendChild(row);
+ for (int colIdx = 0; colIdx < nbrSelCols; ++colIdx) {
+
+ Element value = testDoc.createElement("value");
+ Object cellValue = _table.getValueAt(selRows[rowIdx], selCols[colIdx]);
+ String strCellValue = "";
+ if(null != cellValue)
+ {
+ strCellValue = cellValue.toString();
+ }
+
+ value.setAttribute("columnNumber", String.valueOf(colIdx));
+ value.setTextContent(strCellValue);
+ row.appendChild(value);
+
+ }
+ curRow++;
+ }
+
+ // The XML document we created above is still in memory
+ // so we have to output it to a real file.
+ // In order to do it we first have to create
+ // an instance of DOMSource
+ DOMSource source = new DOMSource(testDoc);
+
+ // PrintStream will be responsible for writing
+ // the text data to the file
+ PrintStream ps = new PrintStream(file);
+ StreamResult result = new StreamResult(ps);
+
+ // Once again we are using a factory of some sort,
+ // this time for getting a Transformer instance,
+ // which we use to output the XML
+ TransformerFactory transformerFactory = TransformerFactory
+ .newInstance();
+ Transformer transformer = transformerFactory.newTransformer();
+
+ // Indenting the XML
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+
+ // The actual output to a file goes here
+ transformer.transform(source, result);
+
+ return true;
+ }
+
private WritableCell getXlsCell(ExtTableColumn col, int colIdx, int curRow, Object cellObj)
{
|