Top Description Fields Constructors Methods
com.ibm.icu.text

pack-priv Class TransliterationRule

extends Object
Class Inheritance
Imports
com.ibm.icu.impl.Utility

A transliteration rule used by RuleBasedTransliterator. TransliterationRule is an immutable object.

A rule consists of an input pattern and an output string. When the input pattern is matched, the output string is emitted. The input pattern consists of zero or more characters which are matched exactly (the key) and optional context. Context must match if it is specified. Context may be specified before the key, after the key, or both. The key, preceding context, and following context may contain variables. Variables represent a set of Unicode characters, such as the letters a through z. Variables are detected by looking up each character in a supplied variable list to see if it has been so defined.

A rule may contain segments in its input string and segment references in its output string. A segment is a substring of the input pattern, indicated by an offset and limit. The segment may be in the preceding or following context. It may not span a context boundary. A segment reference is a special character in the output string that causes a segment of the input string (not the input pattern) to be copied to the output string. The range of special characters that represent segment references is defined by RuleBasedTransliterator.Data.

Example: The rule "([a-z]) . ([0-9]) > $2 . $1" will change the input string "abc.123" to "ab1.c23".

Copyright © IBM Corporation 1999. All rights reserved.

Author
Alan Liu

Field Summary

Modifier and TypeField and Description
pack-priv static final int
pack-priv static final int
ANCHOR_START

Flag attributes.

private StringMatcher
anteContext

The match that must occur before the key, or null if there is no preceding context.

private int
anteContextLength

The length of the string that must match before the key.

private final RuleBasedTransliterator.Data
data

An alias pointer to the data for this rule.

pack-priv byte
flags

Miscellaneous attributes.

private StringMatcher
key

The matcher object for the key.

private int
keyLength

The length of the key.

private UnicodeReplacer
output

The object that performs the replacement if the key, anteContext, and postContext are matched.

private String
pattern

The string that must be matched, consisting of the anteContext, key, and postContext, concatenated together, in that order.

private StringMatcher
postContext

The match that must occur after the key, or null if there is no following context.

pack-priv UnicodeMatcher[]
segments

An array of matcher objects corresponding to the input pattern segments.

Constructor Summary

AccessConstructor and Description
public
TransliterationRule(String
input string, including key and optional ante and post context
input
,
int
offset into input to end of ante context, or -1 if none. Must be <= input.length() if not -1.
anteContextPos
,
int
offset into input to start of post context, or -1 if none. Must be <= input.length() if not -1, and must be >= anteContextPos.
postContextPos
,
String
output string
output
,
int
offset into output at which cursor is located, or -1 if none. If less than zero, then the cursor is placed after the output; that is, -1 is equivalent to output.length(). If greater than output.length() then an exception is thrown.
cursorPos
,
int
an offset to be added to cursorPos to position the cursor either in the ante context, if < 0, or in the post context, if > 0. For example, the rule "abc{def} > | @@@ xyz;" changes "def" to "xyz" and moves the cursor to before "a". It would have a cursorOffset of -3.
cursorOffset
,
UnicodeMatcher[]
array of UnicodeMatcher corresponding to input pattern segments, or null if there are none
segs
,
boolean
true if the the rule is anchored on the left to the context start
anchorStart
,
boolean
true if the rule is anchored on the right to the context limit
anchorEnd
,
RuleBasedTransliterator.Data theData)

Construct a new rule with the given input, output text, and other attributes.

Method Summary

Modifier and TypeMethod and Description
pack-priv void
addSourceTargetSet(UnicodeSet filter, UnicodeSet sourceSet, UnicodeSet targetSet, UnicodeSet revisiting)

Find the source and target sets, subject to the input filter.

public int
getAnteContextLength()

Return the preceding context length.

pack-priv final int
getIndexValue()

Internal method.

public boolean
masks(TransliterationRule r2)

Return true if this rule masks another rule.

public int

Returns:

one of U_MISMATCH, U_PARTIAL_MATCH, or U_MATCH. If incremental is false then U_PARTIAL_MATCH will not be returned.
matchAndReplace
(Replaceable
the text
text
,
Transliterator.Position
the position indices
pos
,
boolean
if true, test for partial matches that may be completed by additional text inserted at pos.limit.
incremental
)

Attempt a match and replacement at the given position.

pack-priv final boolean
matchesIndexValue(int v)

Internal method.

pack-priv static final int
posAfter(Replaceable str, int pos)

pack-priv static final int
posBefore(Replaceable str, int pos)

public String
toRule(boolean escapeUnprintable)

Create a source string that represents this rule.

public String

Returns:

string representation of this object
toString
()

Overrides java.lang.Object.toString.

Return a string representation of this object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

ANCHOR_ENDback to summary
pack-priv static final int ANCHOR_END
ANCHOR_STARTback to summary
pack-priv static final int ANCHOR_START

Flag attributes.

anteContextback to summary
private StringMatcher anteContext

The match that must occur before the key, or null if there is no preceding context.

anteContextLengthback to summary
private int anteContextLength

The length of the string that must match before the key. If zero, then there is no matching requirement before the key. Substring [0,anteContextLength) of pattern is the anteContext.

databack to summary
private final RuleBasedTransliterator.Data data

An alias pointer to the data for this rule. The data provides lookup services for matchers and segments.

flagsback to summary
pack-priv byte flags

Miscellaneous attributes.

keyback to summary
private StringMatcher key

The matcher object for the key. If null, then the key is empty.

keyLengthback to summary
private int keyLength

The length of the key. Substring [anteContextLength, anteContextLength + keyLength) is the key.

outputback to summary
private UnicodeReplacer output

The object that performs the replacement if the key, anteContext, and postContext are matched. Never null.

patternback to summary
private String pattern

The string that must be matched, consisting of the anteContext, key, and postContext, concatenated together, in that order. Some components may be empty (zero length).

See Also
anteContextLength, keyLength
postContextback to summary
private StringMatcher postContext

The match that must occur after the key, or null if there is no following context.

segmentsback to summary
pack-priv UnicodeMatcher[] segments

An array of matcher objects corresponding to the input pattern segments. If there are no segments this is null. N.B. This is a UnicodeMatcher for generality, but in practice it is always a StringMatcher. In the future we may generalize this, but for now we sometimes cast down to StringMatcher.

Constructor Detail

TransliterationRuleback to summary
public TransliterationRule(String input, int anteContextPos, int postContextPos, String output, int cursorPos, int cursorOffset, UnicodeMatcher[] segs, boolean anchorStart, boolean anchorEnd, RuleBasedTransliterator.Data theData)

Construct a new rule with the given input, output text, and other attributes. A cursor position may be specified for the output text.

Parameters
input:String

input string, including key and optional ante and post context

anteContextPos:int

offset into input to end of ante context, or -1 if none. Must be <= input.length() if not -1.

postContextPos:int

offset into input to start of post context, or -1 if none. Must be <= input.length() if not -1, and must be >= anteContextPos.

output:String

output string

cursorPos:int

offset into output at which cursor is located, or -1 if none. If less than zero, then the cursor is placed after the output; that is, -1 is equivalent to output.length(). If greater than output.length() then an exception is thrown.

cursorOffset:int

an offset to be added to cursorPos to position the cursor either in the ante context, if < 0, or in the post context, if > 0. For example, the rule "abc{def} > | @@@ xyz;" changes "def" to "xyz" and moves the cursor to before "a". It would have a cursorOffset of -3.

segs:UnicodeMatcher[]

array of UnicodeMatcher corresponding to input pattern segments, or null if there are none

anchorStart:boolean

true if the the rule is anchored on the left to the context start

anchorEnd:boolean

true if the rule is anchored on the right to the context limit

Method Detail

addSourceTargetSetback to summary
pack-priv void addSourceTargetSet(UnicodeSet filter, UnicodeSet sourceSet, UnicodeSet targetSet, UnicodeSet revisiting)

Find the source and target sets, subject to the input filter. There is a known issue with filters containing multiple characters.

getAnteContextLengthback to summary
public int getAnteContextLength()

Return the preceding context length. This method is needed to support the Transliterator method getMaximumContextLength().

getIndexValueback to summary
pack-priv final int getIndexValue()

Internal method. Returns 8-bit index value for this rule. This is the low byte of the first character of the key, unless the first character of the key is a set. If it's a set, or otherwise can match multiple keys, the index value is -1.

masksback to summary
public boolean masks(TransliterationRule r2)

Return true if this rule masks another rule. If r1 masks r2 then r1 matches any input string that r2 matches. If r1 masks r2 and r2 masks r1 then r1 == r2. Examples: "a>x" masks "ab>y". "a>x" masks "a[b]>y". "[c]a>x" masks "[dc]a>y".

matchAndReplaceback to summary
public int matchAndReplace(Replaceable text, Transliterator.Position pos, boolean incremental)

Attempt a match and replacement at the given position. Return the degree of match between this rule and the given text. The degree of match may be mismatch, a partial match, or a full match. A mismatch means at least one character of the text does not match the context or key. A partial match means some context and key characters match, but the text is not long enough to match all of them. A full match means all context and key characters match. If a full match is obtained, perform a replacement, update pos, and return U_MATCH. Otherwise both text and pos are unchanged.

Parameters
text:Replaceable

the text

pos:Transliterator.Position

the position indices

incremental:boolean

if true, test for partial matches that may be completed by additional text inserted at pos.limit.

Returns:int

one of U_MISMATCH, U_PARTIAL_MATCH, or U_MATCH. If incremental is false then U_PARTIAL_MATCH will not be returned.

matchesIndexValueback to summary
pack-priv final boolean matchesIndexValue(int v)

Internal method. Returns true if this rule matches the given index value. The index value is an 8-bit integer, 0..255, representing the low byte of the first character of the key. It matches this rule if it matches the first character of the key, or if the first character of the key is a set, and the set contains any character with a low byte equal to the index value. If the rule contains only ante context, as in foo)>bar, then it will match any key.

posAfterback to summary
pack-priv static final int posAfter(Replaceable str, int pos)
posBeforeback to summary
pack-priv static final int posBefore(Replaceable str, int pos)
toRuleback to summary
public String toRule(boolean escapeUnprintable)

Create a source string that represents this rule. Append it to the given string.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Return a string representation of this object.

Returns:String

string representation of this object

Annotations
@Override