001: /*
002: * HtmlRowDataConverter.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 workbench.storage.RowData;
015: import workbench.util.SqlUtil;
016: import workbench.util.StrBuffer;
017: import workbench.util.HtmlUtil;
018:
019: /**
020: *
021: * @author support@sql-workbench.net
022: */
023: public class HtmlRowDataConverter extends RowDataConverter {
024: private String pageTitle;
025: private boolean createFullPage = true;
026: private boolean escapeHtml = false;
027:
028: public HtmlRowDataConverter() {
029: super ();
030: }
031:
032: public StrBuffer getEnd(long totalRows) {
033: StrBuffer html = new StrBuffer("</table>\n");
034: if (createFullPage)
035: html.append("</body>\n</html>\n");
036: return html;
037: }
038:
039: public void setCreateFullPage(boolean flag) {
040: this .createFullPage = flag;
041: }
042:
043: public void setPageTitle(String title) {
044: this .pageTitle = title;
045: }
046:
047: public void setEscapeHtml(boolean flag) {
048: this .escapeHtml = flag;
049: }
050:
051: public StrBuffer convertRowData(RowData row, long rowIndex) {
052: int count = this .metaData.getColumnCount();
053: StrBuffer result = new StrBuffer(count * 30);
054: result.append(" <tr>\n ");
055: for (int c = 0; c < count; c++) {
056: String value = this .getValueAsFormattedString(row, c);
057: if (createFullPage) {
058: int type = this .metaData.getColumnType(c);
059: if (SqlUtil.isDateType(type)) {
060: result.append("<td class=\"date-cell\">");
061: } else if (SqlUtil.isNumberType(type)
062: || SqlUtil.isDateType(type)) {
063: result.append("<td class=\"number-cell\">");
064: } else {
065: result.append("<td class=\"text-cell\">");
066: }
067: } else {
068: result.append("<td>");
069: }
070:
071: if (value == null) {
072: result.append(" ");
073: } else {
074: if (this .escapeHtml) {
075: value = HtmlUtil.escapeHTML(value);
076: }
077: result.append(value);
078: }
079: result.append("</td>");
080: }
081: result.append("\n </tr>\n");
082: return result;
083: }
084:
085: public StrBuffer getStart() {
086: StrBuffer result = new StrBuffer(250);
087:
088: if (createFullPage) {
089: result.append("<html>\n");
090:
091: if (pageTitle != null && pageTitle.length() > 0) {
092: result.append("<head>\n<title>");
093: result.append(pageTitle);
094: result.append("</title>\n");
095: }
096: result.append("<style type=\"text/css\">\n");
097: result.append("<!--\n");
098: result
099: .append(" table { border-spacing:0; border-collapse:collapse}\n");
100: result
101: .append(" td { padding:2; border-style:solid;border-width:1px; vertical-align:top;}\n");
102: result
103: .append(" .number-cell { text-align:right; white-space:nowrap; } \n");
104: result.append(" .text-cell { text-align:left; } \n");
105: result
106: .append(" .date-cell { text-align:left; white-space:nowrap;} \n");
107: result.append("-->\n</style>\n");
108:
109: result.append("</head>\n<body>\n");
110: }
111: result.append("<table>\n");
112:
113: // table header with column names
114: result.append(" <tr>\n ");
115: for (int c = 0; c < this .metaData.getColumnCount(); c++) {
116: result.append("<td>");
117: if (createFullPage)
118: result.append("<b>");
119: result.append(this .metaData.getColumnName(c));
120: if (createFullPage)
121: result.append("</b>");
122: result.append("</td>");
123: }
124: result.append("\n </tr>\n");
125:
126: return result;
127: }
128:
129: }
|