net.sf.jabb.util.text
Class StartWithMatcher

java.lang.Object
  extended by net.sf.jabb.util.text.StartWithMatcher
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
StringStartWithMatcher, UrlStartWithMatcher

public class StartWithMatcher
extends Object
implements Serializable

To test which regular expression among several others can be matched by a given string in the manner of "start with".
用来检查某一个字符串能同一组正则表达式中的哪一个符合“以此开头”形式的匹配。

If one of the regular expression can be matched from the first character of the string, they are considered to be matched.
For example:

匹配从字符串的第一个字符开始,只要开头一段完整匹配某个正则表达式,就算是匹配成功。
比如:

I like eating Chinese food --- matched by ---> I .*
I like eating Chinese food --- matched by ---> I like eating.*
I like eating Chinese food --- matched by ---> I
I like eating Chinese food --- matched by ---> I li
I like eating Chinese food --- matched by ---> I like
I like eating Chinese food --- matched by ---> I like eating
I like eating Chinese food --- matched by ---> .* like
I like eating Chinese food --- matched by ---> .* food
I like eating Chinese food --- matched by ---> I .* eating

Normally you don't need to use this class directly, using of its subclasses like StringStartWithMatcher or UrlStartWithMatcher is preferred.

一般来说不需要直接用这个类,应该使用它的子类,比如StringStartWithMatcherUrlStartWithMatcher

It's underlying implementation is based on dk.brics.automaton.RunAutomaton.

底层实现基于 dk.brics.automaton.RunAutomaton

Author:
Zhengmao HU (James)
See Also:
Serialized Form

Field Summary
protected  Object[] attachments
          runAutomation的每一个state对应一个attachment对象。
protected  dk.brics.automaton.RunAutomaton runAutomaton
          实现匹配状态机的引擎
 
Constructor Summary
StartWithMatcher(Collection<MatchingDefinition> definitionList)
          Create an instance according to matching definitions, when creating internal data structure, choose to consume more memory for better matching speed.
根据匹配方式定义创建一个新的对象实例; 在创建内部数据结构的时候,选择占用更多内存,而换取速度上的提升。
StartWithMatcher(Collection<MatchingDefinition> definitionList, boolean moreSpaceForSpeed)
          Create an instance according to matching definitions.
根据匹配方式定义创建一个新的对象实例。
StartWithMatcher(StartWithMatcher toBeCopied)
          Create a copy, the copy will have exactly the same matching definitions as the original copy.
创建一个副本,这个副本与原先的对象具有完全相同匹配方式。
 
Method Summary
protected  dk.brics.automaton.RunAutomaton copyRunAutomaton()
          Make a copy of the internal state machine (dk.brics.automaton.RunAutomaton) of this instance.
把自己内部的状态机(dk.brics.automaton.RunAutomaton)对象复制一份。
protected static String escapeForRegExp(String s)
          Escape special characters according to syntax of regular expression.
按正则表达式语法,对字符串进行escape。
protected  List<Integer> getAllAcceptedStates(CharSequence text, int startIndex)
          By utilizing the state machine (dk.brics.automaton.RunAutomaton), get all the accepted matching state.
protected  int getLastAcceptedState(CharSequence text)
          By utilizing the state machine (dk.brics.automaton.RunAutomaton), get the last accepted matching state.
利用状态机(dk.brics.automaton.RunAutomaton),取得最后一个匹配到的状态。
protected  int getLastAcceptedState(CharSequence text, int startIndex)
          By utilizing the state machine (dk.brics.automaton.RunAutomaton), get the last accepted matching state.
protected  void initialize(Collection<MatchingDefinition> definitionList, boolean moreSpaceForSpeed)
          Initialize the state machine (dk.brics.automaton.RunAutomaton).
初始化状态机(dk.brics.automaton.RunAutomaton)。
 Object match(CharSequence text)
          Test if the text string can be matched by any of the regular expression.
 Object match(CharSequence text, int startIndex)
          Begin from the specified position, test if the text string can be matched by any of the regular expression, return the attachment of the longest matching one.
从指定位置开始判断文本字符串是否可以被任意一个正则表达式所匹配, 返回所能匹配到的那个最长正则表达式的附件对象。
 List<Object> matchAll(CharSequence text)
          Test if the text string can be matched by any of the regular expression, return list of the attachments of all the matching regular expressions.
判断文本字符串是否可以被任意一个正则表达式所匹配, 返回所有能匹配到的正则表达式的附件对象的列表。
 List<Object> matchAll(CharSequence text, int startIndex)
          Begin from the specified position, test if the text string can be matched by any of the regular expression, return list of the attachments of all the matching regular expressions.
从指定位置开始判断文本字符串是否可以被任意一个正则表达式所匹配, 返回所有能匹配到的正则表达式的附件对象的列表。
protected  void setAttachmentByExample(String example, Object att, String exp)
          Set attachment object for each state, by testing which state the example string can run into.
根据示范字符串,设置各状态所对应的附件对象。
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

runAutomaton

protected dk.brics.automaton.RunAutomaton runAutomaton
实现匹配状态机的引擎


attachments

protected Object[] attachments
runAutomation的每一个state对应一个attachment对象。

Constructor Detail

StartWithMatcher

public StartWithMatcher(StartWithMatcher toBeCopied)
Create a copy, the copy will have exactly the same matching definitions as the original copy.
创建一个副本,这个副本与原先的对象具有完全相同匹配方式。

Parameters:
toBeCopied - The original copy.
原本。

StartWithMatcher

public StartWithMatcher(Collection<MatchingDefinition> definitionList)
Create an instance according to matching definitions, when creating internal data structure, choose to consume more memory for better matching speed.
根据匹配方式定义创建一个新的对象实例; 在创建内部数据结构的时候,选择占用更多内存,而换取速度上的提升。

Parameters:
definitionList - Matching definitions
一组匹配方式定义。

StartWithMatcher

public StartWithMatcher(Collection<MatchingDefinition> definitionList,
                        boolean moreSpaceForSpeed)
Create an instance according to matching definitions.
根据匹配方式定义创建一个新的对象实例。

Parameters:
definitionList - Matching definitions
一组匹配方式定义。
moreSpaceForSpeed - Whether or not to consume more memory for better matching speed.
是否占用更多内存,而换取速度上的提升。
Method Detail

initialize

protected void initialize(Collection<MatchingDefinition> definitionList,
                          boolean moreSpaceForSpeed)
Initialize the state machine (dk.brics.automaton.RunAutomaton).
初始化状态机(dk.brics.automaton.RunAutomaton)。

Parameters:
definitionList - Matching definitions
一组匹配方式定义。
moreSpaceForSpeed - Whether or not to consume more memory for better matching speed.
是否占用更多内存,而换取速度上的提升。

setAttachmentByExample

protected void setAttachmentByExample(String example,
                                      Object att,
                                      String exp)
Set attachment object for each state, by testing which state the example string can run into.
根据示范字符串,设置各状态所对应的附件对象。

Parameters:
example - Example string.
示范字符串
att - Attachment object.
附件对象
exp - Regular expression which will only be used within the exception message.
正则表达式字符串(仅被用在抛出的异常消息中)

getLastAcceptedState

protected int getLastAcceptedState(CharSequence text)
By utilizing the state machine (dk.brics.automaton.RunAutomaton), get the last accepted matching state.
利用状态机(dk.brics.automaton.RunAutomaton),取得最后一个匹配到的状态。

Parameters:
text - The text to be tested.
待进行匹配检查的文本。
Returns:
The state number that matched, return -1 if no expression can match the text.
匹配到的状态编号,返回-1表示没有与任何一个正则表达式相匹配。

getLastAcceptedState

protected int getLastAcceptedState(CharSequence text,
                                   int startIndex)
By utilizing the state machine (dk.brics.automaton.RunAutomaton), get the last accepted matching state. The matching test starts at specified position.
利用状态机(dk.brics.automaton.RunAutomaton),取得最后一个匹配到的状态。从指定位置开始进行匹配检查。

Parameters:
text - The text to be tested.
待进行匹配检查的文本。
startIndex - The position to start matching test.
从文本的这个位置开始匹配。
Returns:
The state number that matched, return -1 if no expression can match the text.
匹配到的状态编号,返回-1表示没有与任何一个正则表达式相匹配。

getAllAcceptedStates

protected List<Integer> getAllAcceptedStates(CharSequence text,
                                             int startIndex)
By utilizing the state machine (dk.brics.automaton.RunAutomaton), get all the accepted matching state. The matching test starts at specified position.
利用状态机(dk.brics.automaton.RunAutomaton),取得所有匹配到的状态。从指定位置开始进行匹配检查。

Parameters:
text - The text to be tested.
待进行匹配检查的文本。
startIndex - The position to start matching test.
从文本的这个位置开始匹配。
Returns:
List of the state numbers that are matched, return null if no expression can match the text.
匹配到的所有状态编号的列表,返回null表示没有与任何一个正则表达式相匹配。

match

public Object match(CharSequence text,
                    int startIndex)
Begin from the specified position, test if the text string can be matched by any of the regular expression, return the attachment of the longest matching one.
从指定位置开始判断文本字符串是否可以被任意一个正则表达式所匹配, 返回所能匹配到的那个最长正则表达式的附件对象。

The test starts from the specified position. Caution: if the beginning part matches, the whole text is considered to match.

匹配从指定的位置开始。 注意:只要开头匹配了就算匹配。

Parameters:
text - Text string to be tested for matching.
对这个字符串进行匹配判断。
startIndex - The starting position of the testing.
从文本的这个位置开始匹配。
Returns:
Attachment object of the regular expression that matches the text string; null is returned if no matching can be found.
匹配到的那个正则表达式所对应的附件;返回null表示没有匹配到任何一个正则表达式。

matchAll

public List<Object> matchAll(CharSequence text,
                             int startIndex)
Begin from the specified position, test if the text string can be matched by any of the regular expression, return list of the attachments of all the matching regular expressions.
从指定位置开始判断文本字符串是否可以被任意一个正则表达式所匹配, 返回所有能匹配到的正则表达式的附件对象的列表。

The test starts from the specified position. Caution: if the beginning part matches, the whole text is considered to match.

匹配从指定的位置开始。 注意:只要开头匹配了就算匹配。

Parameters:
text - Text string to be tested for matching.
对这个字符串进行匹配判断。
startIndex - The starting position of the testing.
从文本的这个位置开始匹配。
Returns:
List of attachment objects of the regular expressions that matche the text string; null is returned if no matching can be found.
匹配到的那些正则表达式所对应的附件的列表;返回null表示没有匹配到任何一个正则表达式。

match

public Object match(CharSequence text)
Test if the text string can be matched by any of the regular expression.
判断文本字符串是否可以被任意一个正则表达式所匹配。

The test starts from the beginning. Caution: if the beginning part matches, the whole text is considered to match.

匹配从头开始。 注意:只要开头匹配了就算匹配。

Parameters:
text - Text string to be tested for matching.
对这个字符串进行匹配判断。
Returns:
Attachment object of the regular expression that matches the text string; null is returned if no matching can be found.
匹配到的那个正则表达式所对应的附件。返回null表示没有匹配到任何一个正则表达式。

matchAll

public List<Object> matchAll(CharSequence text)
Test if the text string can be matched by any of the regular expression, return list of the attachments of all the matching regular expressions.
判断文本字符串是否可以被任意一个正则表达式所匹配, 返回所有能匹配到的正则表达式的附件对象的列表。

The test starts from the beginning. Caution: if the beginning part matches, the whole text is considered to match.

匹配从头开始。 注意:只要开头匹配了就算匹配。

Parameters:
text - Text string to be tested for matching.
对这个字符串进行匹配判断。
Returns:
List of attachment objects of the regular expressions that matche the text string; null is returned if no matching can be found.
匹配到的那些正则表达式所对应的附件的列表;返回null表示没有匹配到任何一个正则表达式。

escapeForRegExp

protected static String escapeForRegExp(String s)
Escape special characters according to syntax of regular expression.
按正则表达式语法,对字符串进行escape。

Parameters:
s - The string to be escaped
Returns:
The result of escaping

copyRunAutomaton

protected dk.brics.automaton.RunAutomaton copyRunAutomaton()
                                                    throws IOException,
                                                           ClassNotFoundException
Make a copy of the internal state machine (dk.brics.automaton.RunAutomaton) of this instance.
把自己内部的状态机(dk.brics.automaton.RunAutomaton)对象复制一份。

Returns:
a copy made by serializing and then de-serializing
Throws:
IOException
ClassNotFoundException


Copyright © 2012. All Rights Reserved.