Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Thursday, November 22, 2007

java code for Custom Editor

/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/


///////////////////////////////////////////////////////////////////////////////////////
//File:custom.xml














(dog|fox)


The quick brown fox jumped over the lazy dog.





///////////////////////////////////////////////////////////////////////////////////////

import java.beans.PropertyEditorSupport;
import java.util.regex.Pattern;

public class PatternPropertyEditor extends PropertyEditorSupport {

public void setAsText(String text) throws IllegalArgumentException {
Pattern pattern = Pattern.compile(text);
setValue(pattern);
}
}

///////////////////////////////////////////////////////////////////////////////////////
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class CustomEditorExample {

private Pattern searchPattern;

private String textToSearch;

public static void main(String[] args) {
ConfigurableListableBeanFactory factory = new XmlBeanFactory(
new FileSystemResource("build/custom.xml"));

CustomEditorConfigurer config = (CustomEditorConfigurer) factory
.getBean("customEditorConfigurer");

config.postProcessBeanFactory(factory);

CustomEditorExample bean = (CustomEditorExample) factory
.getBean("exampleBean");

System.out.println(bean.getMatchCount());
}

public void setSearchPattern(Pattern searchPattern) {
this.searchPattern = searchPattern;
}

public void setTextToSearch(String textToSearch) {
this.textToSearch = textToSearch;
}

public int getMatchCount() {
Matcher m = searchPattern.matcher(textToSearch);

int count = 0;
while (m.find()) {
count++;
}

return count;
}
}

No comments: