sleep.bridges
Class Transliteration

java.lang.Object
  extended by sleep.bridges.Transliteration

public class Transliteration
extends java.lang.Object

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:

SequenceMeaning
.Matches any character
\dMatches any digit 0-9
\DMatches any non-digit
\sMatches any whitespace character
\SMatches any non-whitespace character
\wMatches any letter
\WMatches 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

OPTION_DELETE

public static final int OPTION_DELETE
Forces any matches of non-mapped pattern characters to be deleted

See Also:
Constant Field Values

OPTION_COMPLEMENT

public static final int OPTION_COMPLEMENT
Negates the pattern

See Also:
Constant Field Values

OPTION_SQUEEZE

public static final int OPTION_SQUEEZE
Deletes duplicates of all matched characters

See Also:
Constant Field Values
Constructor Detail

Transliteration

public Transliteration()
Method Detail

toString

public java.lang.String toString()
Returns a string representation of this transliteration pattern...

Overrides:
toString in class java.lang.Object

compile

public static Transliteration compile(java.lang.String matches,
                                      java.lang.String replacements)
                               throws java.util.regex.PatternSyntaxException
Compiles the translation pattern. The matches pattern is what the translator engine looks for. Each character in the expanded matches pattern is mapped to the corresponding character in the expanded replacements pattern. In theory when a string is applied to this Transliteration all characters that match something in the matches pattern will be replaced with the corresponding character from the replacements pattern.

Throws:
java.util.regex.PatternSyntaxException - caused by a bad pattern.

compile

public static Transliteration compile(java.lang.String matches,
                                      java.lang.String replacements,
                                      int options)
                               throws java.util.regex.PatternSyntaxException
Compiles the translation pattern. The matches pattern is what the translator engine looks for. Each character in the expanded matches pattern is mapped to the corresponding character in the expanded replacements pattern. In theory when a string is applied to this Transliteration all characters that match something in the matches pattern will be replaced with the corresponding character from the replacements pattern.

Throws:
java.util.regex.PatternSyntaxException - caused by a bad pattern.

translate

public java.lang.String translate(java.lang.String text)
Applies this Transliteration to the specified text.