package com.appdev.bsf.common.client.util;
/**
?* @author wubin E-mail:wubin3347@gmail.com
?* @version 创建时间:2011-11-1 下午3:12:45
?*
?*/
public interface MatchResult {
??? int start();
??? int end();
}
?
?
package com.appdev.bsf.common.client.util;
/**
?* @author wubin E-mail:wubin3347@gmail.com
?* @version 创建时间:2011-11-1 下午3:12:13
?*
?*/
public class Matcher implements MatchResult {
??? private Pattern pat = null;
??? private String expression = null;
??? Matcher(Pattern pat, CharSequence cs) {
??? ??? this.pat = pat;
??? ??? this.expression = cs.toString();
??? }
??? public boolean find() {
??? ??? return pat.matches(expression);
??? }
??? public int start() {
??? ??? String[] s = pat.match(expression);
??? ??? int start = -1;
??? ??? for (String ss : s)
??? ??? ??? start = expression.indexOf(ss);
??? ??? return start;
??? }
??? public int end() {
??? ??? String[] s = pat.match(expression);
??? ??? int end = -1;
??? ??? for (String ss : s)
??? ??? ??? end = expression.indexOf(ss) + ss.length();
??? ??? return end;
??? }
}
?
?
?
package com.appdev.bsf.common.client.util;
import java.util.ArrayList;
import java.util.List;
import com.google.gwt.core.client.JavaScriptObject;
/**
?* @author wubin E-mail:wubin3347@gmail.com
?* @version 创建时间:2011-11-1 下午3:12:45
?*
?*/
public class Pattern {
??? public final static int MULTILINE = 1;
??? public final static int CASE_INSENSITIVE = 2;
??? private static native JavaScriptObject _createExpression(String pattern, String flags)
??? /*-{
??? ??? return new RegExp(pattern, flags);
??? }-*/;
??? public static Pattern compile(String pattern) {
??? ??? return new Pattern(pattern);
??? }
??? public static Pattern compile(String pattern, int flags) {
??? ??? return new Pattern(pattern, flags);
??? }
??? private static JavaScriptObject createExpression(String pattern, int flags) {
??? ??? String sFlags = "";
??? ??? if ((flags & MULTILINE) != 0)
??? ??? ??? sFlags += "m";
??? ??? if ((flags & CASE_INSENSITIVE) != 0)
??? ??? ??? sFlags += "i";
??? ??? return _createExpression(pattern, sFlags);
??? }
??? public static boolean matches(String regex, String input) {
??? ??? return new Pattern(regex).matches(input);
??? }
??? public static String quote(String input) {
??? ??? String output = "";
??? ??? for (int i = 0; i < input.length(); i++) {
??? ??? ??? output += "\\" + input.charAt(i);
??? ??? }
??? ??? return output;
??? }
??? private JavaScriptObject regExp;
??? public Pattern(String pattern) {
??? ??? this(pattern, 0);
??? }
??? public Pattern(String pattern, int flags) {
??? ??? regExp = createExpression(pattern, flags);
??? }
??? public Matcher matcher(CharSequence cs) {
??? ??? return new Matcher(this, cs);
??? }
??? private native void _match(String text, List<String> matches, JavaScriptObject regExp)
??? /*-{
??? ??? var result = text.match(regExp);
??? ??? if (result == null)
??? ??? ??? return;
??? ??? for ( var i = 0; i < result.length; i++)
??? ??? ??? matches.@java.util.ArrayList::add(Ljava/lang/Object;)(result[i]);
??? }-*/;
??? private native void _split(String input, List<String> results, JavaScriptObject regExp)
??? /*-{
??? ??? var parts = input.split(regExp);
??? ??? for ( var i = 0; i < parts.length; i++)
??? ??? ??? results.@java.util.ArrayList::add(Ljava/lang/Object;)(parts[i] );
??? }-*/;
??? public String[] match(String text) {
??? ??? List<String> matches = new ArrayList<String>();
??? ??? _match(text, matches, regExp);
??? ??? String arr[] = new String[matches.size()];
??? ??? for (int i = 0; i < matches.size(); i++)
??? ??? ??? arr[i] = matches.get(i).toString();
??? ??? return arr;
??? }
??? public boolean matches(String text) {
??? ??? return _matches(text, regExp);
??? }
??? private native boolean _matches(String text, JavaScriptObject regExp)
??? /*-{
??? ??? return regExp.test(text);
??? }-*/;
??? public String pattern() {
??? ??? return _pattern(regExp);
??? }
??? private native String _pattern(JavaScriptObject regExp)
??? /*-{
??? ??? return regExp.source;
??? }-*/;
??? public String[] split(String input) {
??? ??? List<String> results = new ArrayList<String>();
??? ??? _split(input, results, regExp);
??? ??? String[] parts = new String[results.size()];
??? ??? for (int i = 0; i < results.size(); i++)
??? ??? ??? parts[i] = (String) results.get(i);
??? ??? return parts;
??? }
??? public String toString() {
??? ??? return regExp.toString();
??? }
}