devdaily home | apple | java | perl | unix | directory | blog

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

package wiki;

import org.stringtree.regex.Pattern;

import org.stringtree.factory.AbstractStringFetcher;
import org.stringtree.juicer.string.RegexSplitStringFilter;
import org.stringtree.juicer.string.StringFilter;
import org.stringtree.juicer.string.StringStringSource;
import org.stringtree.util.StringUtils;

public class TableRow
	extends AbstractStringFetcher
{
	private static final Pattern bar = Pattern.compile("(?<!\\\\)\\|");

	private void renderCell(StringBuffer buf, String cell, int blanks)
	{
		buf.append("<td");
		if (blanks > 0)
		{
			buf.append(" colspan='");
			buf.append(blanks+1);
			buf.append("'");
		}
		buf.append(">");
		buf.append(cell);
		buf.append("</td>");
	}

	public void renderRow(String row, StringBuffer buf)
	{
		row = row.trim();
		if (row.endsWith("|")) row = row.substring(0,row.length()-1);
		String[] cells = bar.split(row);
		int blanks = 0;
		String lastCell = null;

		buf.append("\n<tr>");

		for (int i = 0; i < cells.length; ++i)
		{
			String cell = cells[i];
			if (StringUtils.isBlank(cell))
			{
				++blanks;
			}
			else
			{
				if(lastCell != null)
				{
					renderCell(buf, lastCell, blanks);
					blanks = 0;
				}
				lastCell = cell;
			}
		}

		if(lastCell != null)
		{
			renderCell(buf, lastCell, blanks);
		}

		buf.append("</tr>");
	}

	public Object getObject(String content)
	{
		StringBuffer buf = new StringBuffer();
		String prev = null;

		StringFilter rows = new RegexSplitStringFilter("(^|\n)\\|");
		rows.connectSource(new StringStringSource(content));

		for (String row = rows.nextString(); row != null; row = rows.nextString())
		{
			if (row.length()==0)
			{
				prev = "|";
			}
			else
			{
				if (prev != null)
				{
					row = prev + row;
				}
				renderRow(row, buf);
			}
		}

		String ret = buf.toString();
		return ret;
	}
}




Copyright 1998-2008 Alvin Alexander
All Rights Reserved.
 
devdaily.com is based in louisville, kentucky, and this web site is hosted by godaddy.com