001package gudusoft.gsqlparser.dlineage.graph.utils; 002 003import java.io.IOException; 004import java.io.StringWriter; 005import java.io.Writer; 006import java.util.Locale; 007 008public abstract class CharSequenceTranslator { 009 static final char[] HEX_DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 010 011 public CharSequenceTranslator() { 012 } 013 014 public abstract int translate(CharSequence var1, int var2, Writer var3) throws IOException; 015 016 public final String translate(CharSequence input) { 017 if (input == null) { 018 return null; 019 } else { 020 try { 021 StringWriter writer = new StringWriter(input.length() * 2); 022 this.translate(input, writer); 023 return writer.toString(); 024 } catch (IOException var3) { 025 throw new RuntimeException(var3); 026 } 027 } 028 } 029 030 public final void translate(CharSequence input, Writer out) throws IOException { 031 if (out == null) { 032 throw new IllegalArgumentException("The Writer must not be null"); 033 } else if (input != null) { 034 int pos = 0; 035 int len = input.length(); 036 037 while(true) { 038 while(pos < len) { 039 int consumed = this.translate(input, pos, out); 040 if (consumed == 0) { 041 char c1 = input.charAt(pos); 042 out.write(c1); 043 ++pos; 044 if (Character.isHighSurrogate(c1) && pos < len) { 045 char c2 = input.charAt(pos); 046 if (Character.isLowSurrogate(c2)) { 047 out.write(c2); 048 ++pos; 049 } 050 } 051 } else { 052 for(int pt = 0; pt < consumed; ++pt) { 053 pos += Character.charCount(Character.codePointAt(input, pos)); 054 } 055 } 056 } 057 058 return; 059 } 060 } 061 } 062 063 public final CharSequenceTranslator with(CharSequenceTranslator... translators) { 064 CharSequenceTranslator[] newArray = new CharSequenceTranslator[translators.length + 1]; 065 newArray[0] = this; 066 System.arraycopy(translators, 0, newArray, 1, translators.length); 067 return new AggregateTranslator(newArray); 068 } 069 070 public static String hex(int codepoint) { 071 return Integer.toHexString(codepoint).toUpperCase(Locale.ENGLISH); 072 } 073}