alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Friki example source code file (TableRow.java)

This example Friki source code file (TableRow.java) 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.

Java - Friki tags/keywords

abstractstringfetcher, abstractstringfetcher, object, object, pattern, regexsplitstringfilter, string, string, stringbuffer, stringbuffer, stringfilter, stringstringsource, tablerow

The Friki TableRow.java 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;
	}
}

Other Friki examples (source code examples)

Here is a short list of links related to this Friki TableRow.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.