fitnesse.slimTables.HtmlTableScanner.java Source code

Java tutorial

Introduction

Here is the source code for fitnesse.slimTables.HtmlTableScanner.java

Source

// Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the CPL Common Public License version 1.0.
package fitnesse.slimTables;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.lexer.Page;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import static org.apache.commons.lang.StringUtils.isEmpty;

public class HtmlTableScanner implements TableScanner {
    private List<Table> tables = new ArrayList<Table>();
    private NodeList htmlTree;

    public HtmlTableScanner(String page) throws IOException {
        if (isEmpty(page))
            page = "<i>This page intentionally left blank.</i>";

        Parser parser = new Parser(new Lexer(new Page(page)));
        try {
            htmlTree = parser.parse(null);
        } catch (ParserException e) {
            throw new IOException("Could not read HTML content", e);
        }
        scanForTables(htmlTree);
    }

    private void scanForTables(NodeList nodes) {
        for (int i = 0; i < nodes.size(); i++) {
            Node node = nodes.elementAt(i);
            if (node instanceof TableTag) {
                TableTag tableTag = (TableTag) node;
                guaranteeThatAllTablesAreUnique(tableTag);
                tables.add(new HtmlTable(tableTag));
            } else {
                NodeList children = node.getChildren();
                if (children != null)
                    scanForTables(children);
            }
        }
    }

    private void guaranteeThatAllTablesAreUnique(TableTag tagTable) {
        tagTable.setAttribute("_TABLENUMBER", "" + Math.abs((new Random()).nextLong()));
    }

    public int getTableCount() {
        return tables.size();
    }

    public Table getTable(int i) {
        return tables.get(i);
    }

    public Iterator<Table> iterator() {
        return tables.iterator();
    }

    public String toWikiText() {
        StringBuilder b = new StringBuilder();
        for (Table t : tables) {
            b.append("\n");
            for (int row = 0; row < t.getRowCount(); row++) {
                b.append("|");
                if (t.getColumnCountInRow(row) == 0)
                    b.append("|");
                for (int col = 0; col < t.getColumnCountInRow(row); col++) {
                    b.append(t.getCellContents(col, row));
                    b.append("|");
                }
                b.append("\n");
            }
        }
        return b.toString();
    }

    public String toHtml(Table startTable, Table endBeforeTable) {
        String allHtml = htmlTree.toHtml();

        int startIndex = 0;
        int endIndex = allHtml.length();
        if (startTable != null) {
            String startText = startTable.toHtml();
            int nodeIndex = allHtml.indexOf(startText);
            if (nodeIndex > 0) {
                startIndex = nodeIndex;
            }
        }

        if (endBeforeTable != null) {
            String stopText = endBeforeTable.toHtml();
            int nodeIndex = allHtml.indexOf(stopText);
            if (nodeIndex > 0) {
                endIndex = nodeIndex;
            }
        }
        return htmlTree.toHtml().substring(startIndex, endIndex);
    }

    public String toHtml() {
        return htmlTree.toHtml();
    }
}