00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.settings;
00023
00024 import java.util.regex.Matcher;
00025 import java.util.regex.Pattern;
00026 import org.jetbrains.annotations.NotNull;
00027
00035 public class Codec {
00036
00041 @NotNull
00042 private static final Pattern[] PATTERNS_ENCODE = {
00043 Pattern.compile("\\\\"),
00044 Pattern.compile("\r"),
00045 Pattern.compile("\n"),
00046 };
00047
00051 @NotNull
00052 private static final String[] REPLACEMENTS_ENCODE = {
00053 Matcher.quoteReplacement("\\\\"),
00054 Matcher.quoteReplacement("\\r"),
00055 Matcher.quoteReplacement("\\n"),
00056 };
00057
00062 @NotNull
00063 private static final Pattern[] PATTERNS_DECODE = {
00064 Pattern.compile("\\\\n"),
00065 Pattern.compile("\\\\r"),
00066 Pattern.compile("\\\\\\\\"),
00067 };
00068
00072 @NotNull
00073 private static final String[] REPLACEMENTS_DECODE = {
00074 Matcher.quoteReplacement("\n"),
00075 Matcher.quoteReplacement("\r"),
00076 Matcher.quoteReplacement("\\"),
00077 };
00078
00082 private Codec() {
00083 }
00084
00091 @NotNull
00092 public static String encode(@NotNull final String str) {
00093 assert PATTERNS_ENCODE.length == REPLACEMENTS_ENCODE.length;
00094 String tmp = str;
00095 for (int i = 0; i < PATTERNS_ENCODE.length; i++) {
00096 tmp = PATTERNS_ENCODE[i].matcher(tmp).replaceAll(REPLACEMENTS_ENCODE[i]);
00097 }
00098 return tmp;
00099 }
00100
00107 @NotNull
00108 public static String decode(@NotNull final String str) {
00109 assert PATTERNS_DECODE.length == REPLACEMENTS_DECODE.length;
00110 String tmp = str;
00111 for (int i = 0; i < PATTERNS_DECODE.length; i++) {
00112 tmp = PATTERNS_DECODE[i].matcher(tmp).replaceAll(REPLACEMENTS_DECODE[i]);
00113 }
00114 return tmp;
00115 }
00116
00117 }