Gridarta Editor
Codec.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.preferences;
21 
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24 import org.jetbrains.annotations.NotNull;
25 
32 public class Codec {
33 
38  @NotNull
39  private static final Pattern @NotNull [] PATTERNS_ENCODE = { Pattern.compile("\\\\"), Pattern.compile("\r"), Pattern.compile("\n"), };
40 
44  @NotNull
45  private static final String @NotNull [] REPLACEMENTS_ENCODE = { Matcher.quoteReplacement("\\\\"), Matcher.quoteReplacement("\\r"), Matcher.quoteReplacement("\\n"), };
46 
51  @NotNull
52  private static final Pattern @NotNull [] PATTERNS_DECODE = { Pattern.compile("\\\\n"), Pattern.compile("\\\\r"), Pattern.compile("\\\\\\\\"), };
53 
57  @NotNull
58  private static final String @NotNull [] REPLACEMENTS_DECODE = { Matcher.quoteReplacement("\n"), Matcher.quoteReplacement("\r"), Matcher.quoteReplacement("\\"), };
59 
63  private Codec() {
64  }
65 
72  @NotNull
73  public static String encode(@NotNull final String str) {
74  assert PATTERNS_ENCODE.length == REPLACEMENTS_ENCODE.length;
75  String tmp = str;
76  for (int i = 0; i < PATTERNS_ENCODE.length; i++) {
77  tmp = PATTERNS_ENCODE[i].matcher(tmp).replaceAll(REPLACEMENTS_ENCODE[i]);
78  }
79  return tmp;
80  }
81 
88  @NotNull
89  public static String decode(@NotNull final String str) {
90  assert PATTERNS_DECODE.length == REPLACEMENTS_DECODE.length;
91  String tmp = str;
92  for (int i = 0; i < PATTERNS_DECODE.length; i++) {
93  tmp = PATTERNS_DECODE[i].matcher(tmp).replaceAll(REPLACEMENTS_DECODE[i]);
94  }
95  return tmp;
96  }
97 
98 }
net.sf.gridarta.preferences.Codec.Codec
Codec()
Definition: Codec.java:63
net.sf.gridarta.preferences.Codec.encode
static String encode(@NotNull final String str)
Definition: Codec.java:73
net.sf.gridarta.preferences.Codec
Definition: Codec.java:32
net.sf.gridarta.preferences.Codec.REPLACEMENTS_ENCODE
static final String[] REPLACEMENTS_ENCODE
Definition: Codec.java:45
net.sf.gridarta.preferences.Codec.REPLACEMENTS_DECODE
static final String[] REPLACEMENTS_DECODE
Definition: Codec.java:58
net.sf.gridarta.preferences.Codec.PATTERNS_ENCODE
static final Pattern[] PATTERNS_ENCODE
Definition: Codec.java:39
net.sf.gridarta.preferences.Codec.PATTERNS_DECODE
static final Pattern[] PATTERNS_DECODE
Definition: Codec.java:52
net.sf.gridarta.preferences.Codec.decode
static String decode(@NotNull final String str)
Definition: Codec.java:89