001: /*
002: * XmlRowDataConverter.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db.exporter;
013:
014: import java.text.SimpleDateFormat;
015: import java.util.Date;
016: import workbench.storage.RowData;
017: import workbench.util.EncodingUtil;
018: import workbench.util.StrBuffer;
019: import workbench.util.StringUtil;
020:
021: /**
022: * Convert row data to our own XML format.
023: *
024: * @author support@sql-workbench.net
025: */
026: public class XlsXMLRowDataConverter extends RowDataConverter {
027: private SimpleDateFormat tsFormat = new SimpleDateFormat(
028: "yyyy-MM-dd'T'HH:mm:ss");
029:
030: public XlsXMLRowDataConverter() {
031: super ();
032: }
033:
034: public StrBuffer getStart() {
035: StrBuffer out = new StrBuffer(5000);
036: out.append("<?xml version=\"1.0\" encoding=\""
037: + EncodingUtil.cleanupEncoding(getEncoding())
038: + "\"?>\n");
039: out.append("<?mso-application progid=\"Excel.Sheet\"?>\n");
040: out
041: .append("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n");
042: out
043: .append("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">\n");
044: out.append("<LastAuthor>SQL Workbench/J</LastAuthor>\n");
045: out.append("<Created>");
046: out.append(tsFormat.format(new Date()));
047: out.append("</Created>\n");
048: out.append("</DocumentProperties>\n");
049:
050: out.append("<Styles>\n");
051: if (writeHeader) {
052: out
053: .append(" <Style ss:ID=\"wbHeader\"><Font ss:Bold=\"1\"/></Style>\n");
054: }
055: out.append(" <Style ss:ID=\"wbTS\"><NumberFormat ss:Format=\""
056: + getDateFormat() + "\"/></Style>\n");
057: out
058: .append(" <Style ss:ID=\"wbML\"><Alignment ss:Vertical=\"Top\" ss:WrapText=\"1\"/></Style>\n");
059: out.append("</Styles>\n");
060:
061: int colCount = metaData.getColumnCount();
062:
063: out.append("<Worksheet ss:Name=\"" + getPageTitle("Export")
064: + "\">\n");
065: out.append("<Table ss:ExpandedColumnCount=\"" + colCount
066: + "\" x:FullColumns=\"1\" x:FullRows=\"1\">\n");
067:
068: for (int i = 0; i < colCount; i++) {
069: if (!this .includeColumnInExport(i))
070: continue;
071: out.append("<Column ss:AutoFitWidth=\"1\"/>\n");
072: }
073:
074: if (writeHeader) {
075: out.append("<Row>\n");
076: for (int i = 0; i < colCount; i++) {
077: if (!this .includeColumnInExport(i))
078: continue;
079: out
080: .append(" <Cell ss:StyleID=\"wbHeader\"><Data ss:Type=\"String\">");
081: out.append(metaData.getColumnName(i));
082: out.append("</Data></Cell>\n");
083: }
084: out.append("</Row>");
085: }
086:
087: out.append('\n');
088: return out;
089: }
090:
091: public StrBuffer getEnd(long totalRows) {
092: StrBuffer out = new StrBuffer(250);
093: out.append("</Table>\n");
094: out.append("</Worksheet>\n");
095: out.append("</Workbook>\n");
096: return out;
097: }
098:
099: public StrBuffer convertRowData(RowData row, long rowIndex) {
100: int colCount = row.getColumnCount();
101: StrBuffer xml = new StrBuffer(colCount * 50);
102: xml.append("<Row>\n");
103: for (int i = 0; i < colCount; i++) {
104: if (!this .includeColumnInExport(i)) {
105: continue;
106: }
107: boolean isDate = (row.getValue(i) instanceof Date);
108: String value = null;
109:
110: if (isDate) {
111: Date d = (Date) row.getValue(i);
112: value = tsFormat.format(d);
113: } else {
114: value = getValueAsFormattedString(row, i);
115: }
116: boolean isMultiline = (value == null ? false : value
117: .indexOf('\n') > 0);
118:
119: if (isDate) {
120: xml.append(" <Cell ss:StyleID=\"wbTS\">");
121: } else if (isMultiline) {
122: xml.append(" <Cell ss:StyleID=\"wbML\">");
123: } else {
124: xml.append(" <Cell>");
125: }
126: xml.append("<Data ss:Type=\"");
127: xml.append(getDataType(row.getValue(i)));
128: xml.append("\">");
129:
130: writeEscapedXML(xml, value, false);
131:
132: xml.append("</Data></Cell>\n");
133: }
134: xml.append("</Row>\n\n");
135:
136: return xml;
137: }
138:
139: private String getDateFormat() {
140: String javaFormat = this .defaultTimestampFormatter != null ? this .defaultTimestampFormatter
141: .toPattern()
142: : "yyyy\\-mm\\-dd\\ hh:mm:ss";
143: String excelFormat = StringUtil.replace(javaFormat, "-", "\\-");
144: excelFormat = StringUtil.replace(excelFormat, " ", "\\ ");
145: excelFormat = StringUtil.replace(excelFormat, "/", "\\/");
146: return excelFormat.toLowerCase();
147: }
148:
149: private String getDataType(Object data) {
150: if (data instanceof Number) {
151: return "Number";
152: } else if (data instanceof java.util.Date) {
153: return "DateTime";
154: }
155: return "String";
156: }
157: }
|