package com.onaro.util.jfc;
import javax.swing.table.TableModel;
import org.apache.commons.lang3.StringUtils;
import com.netapp.oci.server.annotation.data.AnnotationValue;
import com.onaro.client.annotations.ui.AnnotationInfo;
import com.onaro.client.annotations.ui.AnnotationsUtils;
import com.onaro.commons.lang.CommonStringUtils;
import com.onaro.sanscreen.client.view.tabular.value.NumberValue;
import com.onaro.util.enumeration.EnumPresentation;
public class ExportUtils {
private static final String NEW_LINE = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
private static final char COMMA = ',';
private static final char DOUBLE_QUOTE = '"';
private static final char LINEFEED = '\n';
private static final char CARRIAGE_RETURN = '\r';
public static String exportTableToCSV(TableModel exportableTableModel, boolean includeHeader) {
int rowCount = exportableTableModel.getRowCount();
int columnCount = exportableTableModel.getColumnCount();
StringBuilder resultSB = new StringBuilder();
if (includeHeader) {
for (int columnIndex = 0; columnIndex < exportableTableModel.getColumnCount(); columnIndex++) {
String columnName = exportableTableModel.getColumnName(columnIndex);
if (columnIndex > 0) {
resultSB.append(',');
}
resultSB.append(columnName);
}
resultSB.append(NEW_LINE);
}
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
Object value = exportableTableModel.getValueAt(rowIndex, columnIndex);
if (columnIndex > 0) {
resultSB.append(',');
}
resultSB.append(getExportableRepresentation(value, true, true));
}
resultSB.append(NEW_LINE);
}
return resultSB.toString();
}
public static String exportTableToHTML(TableModel exportableTableModel, boolean includeHeader) {
int rowCount = exportableTableModel.getRowCount();
int columnCount = exportableTableModel.getColumnCount();
StringBuilder htmlBuf = new StringBuilder();
htmlBuf.append("").append(NEW_LINE).append("
").append(NEW_LINE).append(""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
if (includeHeader) {
// Add the header row
htmlBuf.append("").append(NEW_LINE); //$NON-NLS-1$
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
String columnName = exportableTableModel.getColumnName(columnIndex);
htmlBuf.append("").append(columnName).append(" | ").append(NEW_LINE); //$NON-NLS-1$ //$NON-NLS-2$
}
htmlBuf.append("
").append(NEW_LINE); //$NON-NLS-1$
}
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
htmlBuf.append("").append(NEW_LINE); //$NON-NLS-1$
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
Object value = exportableTableModel.getValueAt(rowIndex, columnIndex);
String htmlRepresentation = getExportableRepresentation(value, false, false);
htmlBuf.append("").append(htmlRepresentation).append(" | ").append(NEW_LINE); //$NON-NLS-1$ //$NON-NLS-2$
}
htmlBuf.append("
").append(NEW_LINE); //$NON-NLS-1$
}
htmlBuf.append("
").append(NEW_LINE).append("").append(NEW_LINE).append(""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return htmlBuf.toString();
}
public static String getExportableRepresentation(Object value, boolean stripHTML, boolean formatCSV) {
if (value == null) {
return StringUtils.EMPTY;
}
if (value instanceof AnnotationInfo) {
value = AnnotationsUtils.formatValue(((AnnotationInfo)value).getValue());
} else if (value instanceof AnnotationValue) {
value = AnnotationsUtils.formatValue((AnnotationValue)value);
} else if (value instanceof EnumPresentation.Attributes) {
EnumPresentation.Attributes attributes = (EnumPresentation.Attributes)value;
if (StringUtils.isBlank(attributes.name)) {
value = attributes.value;
} else {
value = attributes.name;
}
}
// Don't export units in encoded numbers. Convert to String without locale formatting (no commas).
if (value instanceof NumberValue) {
return ((NumberValue)value).getNumberObject().toString();
}
String valueStr = String.valueOf(value);
if (stripHTML) {
// Strip out HTML from the value. Convert HTML escape sequences to Unicode.
valueStr = CommonStringUtils.getTextFromHtml(valueStr);
}
if (formatCSV) {
// Determine if there are commas or other characters that must be quoted in CSV format.
boolean isQuoteRequired = false;
for (int i = 0; i < valueStr.length(); i++) {
char chr = valueStr.charAt(i);
if (chr == COMMA || chr == DOUBLE_QUOTE || chr == LINEFEED || chr == CARRIAGE_RETURN) {
isQuoteRequired = true;
break;
}
}
// Quote value if necessary.
if (isQuoteRequired) {
// Typically just need to add 2 characters: begin and end quote.
StringBuilder buf = new StringBuilder(valueStr.length() + 2);
buf.append (DOUBLE_QUOTE);
for (int i = 0; i < valueStr.length(); i++) {
char chr = valueStr.charAt(i);
buf.append(chr);
if (chr == DOUBLE_QUOTE) {
// Quotes are escaped by another quote. See RFC 4180.
buf.append(DOUBLE_QUOTE);
}
}
buf.append(DOUBLE_QUOTE);
valueStr = buf.toString();
}
}
return valueStr;
}
}