devdaily home | career | drupal | java | mac | mysql | perl | php | uml | unix

Friki example source code file (a.txt)

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

characteriterator, matcher, matcher, pattern, pattern, resultgenerator, string, string, stringbuffer, stringbuffer, stringcharacteriterator, text

The Friki a.txt source code

package org.stringtree.regex;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

public class Pattern
{
	public static String processor="java.util.regex";
	protected java.util.regex.Pattern real;

	public Pattern(java.util.regex.Pattern real)
	{
		this.real = real;
	}

	public static Pattern compile(String text)
	{
		return new Pattern(java.util.regex.Pattern.compile(text, java.util.regex.Pattern.MULTILINE));
	}

	public String replaceAll(String input, ResultGenerator generator)
	{
		StringBuffer buf = new StringBuffer();
		Matcher matcher = matcher(input);

		while (matcher.real.find())
		{
			matcher.real.appendReplacement(buf, escapeBackslashes(generator.result(matcher)));
		}
		matcher.real.appendTail(buf);

		return buf.toString();
	}

	public Matcher matcher(String input)
	{
		java.util.regex.Matcher realMatcher = real.matcher(input);
		return new Matcher(realMatcher);
	}

	public String replaceAll(String input, String value)
	{
		java.util.regex.Matcher matcher = real.matcher(input);
 		return matcher.replaceAll(escapeBackslashes(value));
	}

	public String[] split(String input)
	{
		return real.split(input, -1);
	}

	private String escapeBackslashes(String input)
	{
		StringBuffer buf = new StringBuffer();
		CharacterIterator ci = new StringCharacterIterator(input);
		for(char c = ci.first(); c != CharacterIterator.DONE; c = ci.next())
		{
			if (c=='\\')
			{
				buf.append('\\');
			}
			buf.append(c);
		}
		return buf.toString();
	}
}

Other Friki examples (source code examples)

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

new blog posts

 

Other websites by Alvin Alexander:  
Life/living in Alaska (OneMansAlaska.com)
How I Sold My Business (HowISoldMyBusiness.com)

Copyright 1998-2011 Alvin Alexander, devdaily.com
All Rights Reserved.