All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.lwhite1.tablesaw.io.html.HtmlTableWriter Maven / Gradle / Ivy

There is a newer version: 0.7.7.3
Show newest version
package com.github.lwhite1.tablesaw.io.html;

import com.github.lwhite1.tablesaw.api.Table;
import com.github.lwhite1.tablesaw.columns.Column;
import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * Static utility that Writes outlier tables in html table format for display
 */
final public class HtmlTableWriter {

  /**
   * Private constructor to prevent instantiation
   */
  private HtmlTableWriter() {}

  public static String write(Table table, String missing) {

    StringBuilder builder = new StringBuilder();
    builder.append(header(table.columnNames()));
    builder.append("")
        .append('\n');
    for (int row : table.rows()) {
      builder.append(row(row, table, missing));
    }
    builder.append("");
    return builder.toString();
  }

  /**
   * Returns a string containing the html output of one table row
   */
  @VisibleForTesting
  static String row(int row, Table table, String missing) {
    StringBuilder builder = new StringBuilder()
        .append("");

    for (Column col : table.columns()) {
      builder
          .append("")
          .append(String.valueOf(col.getString(row)))
          .append("");
      }
    builder
        .append("")
        .append('\n');
    return builder.toString();
  }

  @VisibleForTesting
  static String header(List columnNames) {
    StringBuilder builder = new StringBuilder()
        .append("")
        .append('\n')
        .append("");
    for (String name : columnNames) {
      builder
          .append("")
          .append(splitCamelCase(splitOnUnderscore(name)))
          .append("");
    }
    builder
        .append("")
        .append('\n')
        .append("")
        .append('\n');

    return builder.toString();
  }

  // todo move to utils
  private static String splitCamelCase(String s) {
    return StringUtils.join(
        StringUtils.splitByCharacterTypeCamelCase(s),
        ' '
    );
  }

  // todo move to utils
  static String splitOnUnderscore(String s) {
    return StringUtils.join(
        StringUtils.split(s, '_'),
        ' '
    );
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy