提交 11aef576 编写于 作者: E ejf

XML data exporter.

Former-commit-id: 692e9c7f
上级 6a3c5f43
......@@ -153,6 +153,8 @@ command.org.jkiss.dbeaver.core.resultset.export.sql.property.escape.name=Escape
command.org.jkiss.dbeaver.core.resultset.export.sql.property.escape.description=Character which escapes single quote character
command.org.jkiss.dbeaver.core.resultset.export.sql.property.rowsInStatement.name=Data rows per statement
command.org.jkiss.dbeaver.core.resultset.export.sql.property.rowsInStatement.description=Number of data rows per single insert statement
command.org.jkiss.dbeaver.core.resultset.export.xml.name=XML
command.org.jkiss.dbeaver.core.resultset.export.xml.description=Export to XML file(s)
command.org.jkiss.dbeaver.core.edit.copy.adv_info=Copy Advanced Info
command.org.jkiss.dbeaver.core.edit.copy.special.name=Advanced Copy
......
......@@ -1721,6 +1721,14 @@
</extension>
<extension point="org.jkiss.dbeaver.dataExportProvider">
<export
id="org.jkiss.dbeaver.core.dataExport.xml"
class="org.jkiss.dbeaver.tools.data.impl.DataExporterXML"
description="%command.org.jkiss.dbeaver.core.resultset.export.xml.description"
extension="xml"
icon="icons/misc/xml.png"
label="%command.org.jkiss.dbeaver.core.resultset.export.xml.name">
</export>
<export
id="org.jkiss.dbeaver.core.dataExport.html"
class="org.jkiss.dbeaver.tools.data.impl.DataExporterHTML"
......
......@@ -150,6 +150,8 @@ command.org.jkiss.dbeaver.core.resultset.export.sql.property.escape.name=\u0421\
command.org.jkiss.dbeaver.core.resultset.export.sql.property.escape.description=\u0421\u0438\u043C\u0432\u043E\u043B, \u043A\u043E\u0442\u043E\u0440\u044B\u043C \u0431\u0443\u0434\u0443\u0442 \u044D\u043A\u0440\u0430\u043D\u0438\u0440\u043E\u0432\u0430\u0442\u044C\u0441\u044F \u043E\u0434\u0438\u043D\u0430\u0440\u043D\u044B\u0435 \u043A\u0430\u0432\u044B\u0447\u043A\u0438
command.org.jkiss.dbeaver.core.resultset.export.sql.property.rowsInStatement.name=\u0421\u0442\u0440\u043E\u043A \u0434\u0430\u043D\u043D\u044B\u0445 \u0432 \u0432\u044B\u0440\u0430\u0436\u0435\u043D\u0438\u0438
command.org.jkiss.dbeaver.core.resultset.export.sql.property.rowsInStatement.description=\u041A\u043E\u043B\u0438\u0447\u0435\u0441\u0442\u0432\u043E \u0441\u0442\u0440\u043E\u043A \u0434\u0430\u043D\u043D\u044B\u0445 \u0432 \u043E\u0434\u043D\u043E\u043C \u0432\u044B\u0440\u0430\u0436\u0435\u043D\u0438\u0438 \u0432\u0441\u0442\u0430\u0432\u043A\u0438
command.org.jkiss.dbeaver.core.resultset.export.xml.name=XML
command.org.jkiss.dbeaver.core.resultset.export.xml.description=\u042D\u043A\u0441\u043F\u043E\u0440\u0442 \u0432 XML \u0444\u0430\u0439\u043B(\u044B)
command.org.jkiss.dbeaver.core.edit.copy.special.name=\u0421\u043F\u0435\u0446\u0438\u0430\u043B\u044C\u043D\u0430\u044F \u043A\u043E\u043F\u0438\u044F
command.org.jkiss.dbeaver.core.edit.copy.special.description=\u0421\u043F\u0435\u0446\u0438\u0430\u043B\u044C\u043D\u0430\u044F \u043A\u043E\u043F\u0438\u044F
......
......@@ -41,7 +41,7 @@ import java.util.List;
*/
public class DataExporterHTML extends DataExporterAbstract {
public static final int IMAGE_FRAME_SIZE = 200;
private static final int IMAGE_FRAME_SIZE = 200;
private PrintWriter out;
private List<DBDAttributeBinding> columns;
......
/*
* Copyright (C) 2010-2012 Serge Rieder
* eugene.fradkin@gmail.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.jkiss.dbeaver.tools.data.impl;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.data.DBDAttributeBinding;
import org.jkiss.dbeaver.model.data.DBDContent;
import org.jkiss.dbeaver.model.data.DBDContentStorage;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.tools.data.IDataExporterSite;
import org.jkiss.dbeaver.utils.ContentUtils;
import javax.imageio.ImageIO;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.List;
/**
* CSV Exporter
*/
public class DataExporterXML extends DataExporterAbstract {
public static final int IMAGE_FRAME_SIZE = 200;
private PrintWriter out;
private List<DBDAttributeBinding> columns;
private int rowCount = 0;
@Override
public void init(IDataExporterSite site) throws DBException
{
super.init(site);
out = site.getWriter();
}
@Override
public void dispose()
{
out = null;
super.dispose();
}
@Override
public void exportHeader(DBRProgressMonitor monitor) throws DBException, IOException
{
columns = getSite().getAttributes();
printHeader();
}
private void printHeader()
{
out.write("<?xml version=\"1.0\" ?>\n");
String tableName = getSite().getSource().getName();
out.write("<!DOCTYPE " + tableName + " [\n");
out.write(" <!ELEMENT " + tableName + " (DATA_RECORD*)>\n");
out.write(" <!ELEMENT DATA_RECORD (");
int columnsSize = columns.size();
for (int i = 0; i < columnsSize; i++) {
out.write(columns.get(i).getAttribute().getName() + "?");
if (i < columnsSize - 1) {
out.write(",");
}
}
out.write(")+>\n");
for (int i = 0; i < columnsSize; i++) {
out.write(" <!ELEMENT " + columns.get(i).getAttribute().getName() + " (#PCDATA)>\n");
}
out.write("]>\n");
out.write("<" + tableName + ">\n");
}
@Override
public void exportRow(DBRProgressMonitor monitor, Object[] row) throws DBException, IOException
{
out.write(" <DATA_RECORD>\n");
for (int i = 0; i < row.length; i++) {
DBDAttributeBinding column = columns.get(i);
String columnName = column.getAttribute().getName();
out.write(" <" + columnName + ">");
if (DBUtils.isNullValue(row[i])) {
writeTextCell(null, false);
} else if (row[i] instanceof DBDContent) {
// Content
// Inline textual content and handle binaries in some special way
DBDContent content = (DBDContent)row[i];
try {
DBDContentStorage cs = content.getContents(monitor);
if (ContentUtils.isTextContent(content)) {
writeCellValue(cs.getContentReader());
} else {
getSite().writeBinaryData(cs.getContentStream(), cs.getContentLength());
}
}
finally {
content.release();
}
} else {
String stringValue = super.getValueDisplayString(column, row[i]);
boolean isImage = row[i] instanceof File && stringValue != null && stringValue.endsWith(".jpg");
if (isImage) {
writeImageCell((File) row[i]);
}
else {
writeTextCell(stringValue, false);
}
}
out.write("</" + columnName + ">\n");
}
out.write(" </DATA_RECORD>\n");
}
@Override
public void exportFooter(DBRProgressMonitor monitor) throws IOException
{
out.write("</" + getSite().getSource().getName() + ">\n");
}
private void writeTextCell(String value, boolean header)
{
if (value != null) {
value = value.replace("<", "&lt;").replace(">", "&gt;").replace("&", "&amp;");
out.write(value);
}
}
private void writeImageCell(File file) throws DBException
{
if (file != null && file.exists()) {
Image image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
throw new DBException("Can't read an exported image " + image, e);
}
if (image != null) {
String imagePath = file.getAbsolutePath();
imagePath = "files/" + imagePath.substring(imagePath.lastIndexOf(File.separator));
out.write(imagePath);
}
}
}
private void writeCellValue(Reader reader) throws IOException
{
try {
// Copy reader
char buffer[] = new char[2000];
for (;;) {
int count = reader.read(buffer);
if (count <= 0) {
break;
}
for (int i = 0; i < count; i++) {
if (buffer[i] == '<') {
out.write("&lt;");
}
else if (buffer[i] == '>') {
out.write("&gt;");
}
if (buffer[i] == '&') {
out.write("&amp;");
}
out.write(buffer[i]);
}
}
} finally {
ContentUtils.close(reader);
}
}
public boolean saveBinariesAsImages()
{
return true;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册