|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectsleep.bridges.Transliteration
public class Transliteration
This class provides a character translation utility similar to the UNIX tr command. Essentially a pattern is compiled defining characters and their appropriate substitutions. Once compiled a Transliteration pattern can be compared against a string. Each character in the string is compared to each character in the pattern. If a match is found the character is either replaced or deleted as specified in the patterns replacement.
Transliteration is not the same as regular expressions. Transliteration has a single character scope.
Example Usage:// A simple ROT13 Translator (for extra security run it twice...) Transliteration rot13 = Transliteration.compile("a-z", "n-za-m"); String ciphertext = rot13.translate("this is a mad cool test"); System.out.println("Cipher text: " + ciphertext); Sring plaintext = rot13.translate(ciphertext); System.out.println("Plain text: " + plaintext);
Replacement patterns and Matcher patterns may both contain ranges. Any range specified in either of these places will be
expanded to all of the characters. A range is specified as n-m
where n is the
starting character (A-Z, a-z, 0-9) and m is the ending character. Backwards ranges are allowed as well.
If an expanded replacement pattern is shorter than the matcher pattern, the last character of the replacement pattern
will be used to map to all remaining characters in the matcher pattern. The OPTION_DELETE
option changes this
behavior to delete those matches that don't have a replacement pattern character explicitly mapped to them.
Matcher patterns may contain the following character classes:
Sequence | Meaning |
---|---|
. | Matches any character |
\d | Matches any digit 0-9 |
\D | Matches any non-digit |
\s | Matches any whitespace character |
\S | Matches any non-whitespace character |
\w | Matches any letter |
\W | Matches any non-letter |
\\ | Matches a literal backslash |
\. | Matches a literal period |
\- | Matches a literal dash |
Any other escape sequence is considered an error and an exception will be thrown.
Transliteration patterns have several options that can change the behavior of the matcher/translator.
OPTION_DELETE
tells the translator to delete matching characters if there is no mapped character specified
in the replacement pattern.
OPTION_COMPLEMENT
negates the compiled pattern. When this flag is set all characters and meta-characters will
match their compliments.
OPTION_SQUEEZE
is used to force the translator to squeeze together matches right next to eachother. Essentially
this option will delete repeated characters that match a pattern character.
This class is released into the public domain. Do with it as you wish (but please give credit where credit is due). Created by Raphael Mudge for the Sleep scripting language.
Field Summary | |
---|---|
static int |
OPTION_COMPLEMENT
Negates the pattern |
static int |
OPTION_DELETE
Forces any matches of non-mapped pattern characters to be deleted |
static int |
OPTION_SQUEEZE
Deletes duplicates of all matched characters |
Constructor Summary | |
---|---|
Transliteration()
|
Method Summary | |
---|---|
static Transliteration |
compile(java.lang.String matches,
java.lang.String replacements)
Compiles the translation pattern. |
static Transliteration |
compile(java.lang.String matches,
java.lang.String replacements,
int options)
Compiles the translation pattern. |
java.lang.String |
toString()
Returns a string representation of this transliteration pattern... |
java.lang.String |
translate(java.lang.String text)
Applies this Transliteration to the specified text. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final int OPTION_DELETE
public static final int OPTION_COMPLEMENT
public static final int OPTION_SQUEEZE
Constructor Detail |
---|
public Transliteration()
Method Detail |
---|
public java.lang.String toString()
toString
in class java.lang.Object
public static Transliteration compile(java.lang.String matches, java.lang.String replacements) throws java.util.regex.PatternSyntaxException
java.util.regex.PatternSyntaxException
- caused by a bad pattern.public static Transliteration compile(java.lang.String matches, java.lang.String replacements, int options) throws java.util.regex.PatternSyntaxException
java.util.regex.PatternSyntaxException
- caused by a bad pattern.public java.lang.String translate(java.lang.String text)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |