Java List Create createTable(List datas, String[] headers, int numColumns, int cellPadding, int border)

Here you can find the source of createTable(List datas, String[] headers, int numColumns, int cellPadding, int border)

Description

create Table

License

Open Source License

Declaration

public static String createTable(List<String> datas, String[] headers, int numColumns, int cellPadding,
            int border) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing./*w w w. j av a2  s .c  o  m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/

import java.util.List;

public class Main {
    private static final String begin = "<table ";

    public static String createTable(List<String> datas, String[] headers, int numColumns, int cellPadding,
            int border) {
        StringBuilder table = new StringBuilder(begin);

        if (datas == null) {
            throw new IllegalArgumentException("The data can not be null");
        }
        if (datas.size() % numColumns != 0) {
            throw new IllegalArgumentException(
                    "The table could not be created becuase the data does not match the column size");
        }
        if (border > 0) {
            table.append("border=\"" + border + "\"");
        }
        if (cellPadding > 0) {
            table.append("cellpadding=\"" + cellPadding + "\"");
        }
        table.append(">");

        if (headers != null && headers.length == numColumns) {
            table.append("<tr>");
            for (String header : headers) {
                table.append("<th>" + header + "</th>");
            }
            table.append("</tr>");
        }

        int colIndex = 0;
        for (String data : datas) {

            if (colIndex == 0) {
                table.append("<tr>");
            }
            table.append("<td>" + data + "</td>");
            colIndex++;

            if (colIndex == numColumns) {
                table.append("</tr>");
                colIndex = 0;
            }
        }
        return table.toString();
    }
}

Related

  1. createStringList(String spaceDelimitedString)
  2. createStringListFromStringArr(String[] stringArr)
  3. createSubList(Collection source, Class type)
  4. createSublist(final T[] elements, final Class listImplementation)
  5. createSymbolNameString_From_Parts(List descTagName_Parts)
  6. createTagList(List tags, String separator)
  7. createTarget(List labels)
  8. createTargetSorted(List labels, Comparator comp)
  9. createTuple(List> tuples, List entries, List> lists)