fiore@0
|
1 /*
|
fiore@0
|
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
|
fiore@0
|
3
|
fiore@0
|
4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
|
fiore@0
|
5
|
fiore@0
|
6 This program is free software: you can redistribute it and/or modify
|
fiore@0
|
7 it under the terms of the GNU General Public License as published by
|
fiore@0
|
8 the Free Software Foundation, either version 3 of the License, or
|
fiore@0
|
9 (at your option) any later version.
|
fiore@0
|
10
|
fiore@0
|
11 This program is distributed in the hope that it will be useful,
|
fiore@0
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
fiore@0
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
fiore@0
|
14 GNU General Public License for more details.
|
fiore@0
|
15
|
fiore@0
|
16 You should have received a copy of the GNU General Public License
|
fiore@0
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
fiore@0
|
18 */
|
fiore@0
|
19
|
fiore@0
|
20 package uk.ac.qmul.eecs.ccmi.utils;
|
fiore@0
|
21
|
fiore@0
|
22 /**
|
fiore@0
|
23 * A utility class providing static methods to escape one or more characters.
|
fiore@0
|
24 *
|
fiore@0
|
25 */
|
fiore@0
|
26 public class CharEscaper {
|
fiore@0
|
27 /**
|
fiore@0
|
28 * Replaces the new line character with a '|' in the {@code String} passed as argument.
|
fiore@0
|
29 * The original {@code String} can be restored by passing the returned {@code String} to
|
fiore@0
|
30 * {@link #restoreNewline(String)}. Existing '|' characters will be escaped in order not to miss
|
fiore@0
|
31 * them in the restore process.
|
fiore@5
|
32 * @param s the {@code String} to remove new line characters from
|
fiore@5
|
33 * @return a {@code String} where all new line characters have been replaced by '|'
|
fiore@0
|
34 */
|
fiore@0
|
35 public static String replaceNewline(String s){
|
fiore@0
|
36 String result = s.replace("|", "'|");
|
fiore@0
|
37 return result.replace('\n', '|');
|
fiore@0
|
38 }
|
fiore@0
|
39
|
fiore@0
|
40 /**
|
fiore@0
|
41 * Restores a {@code String} whose new line characters have been previously replaced by
|
fiore@0
|
42 * {@link #replaceNewline(String)}, to the original form.
|
fiore@5
|
43 * @param s the string to be restored
|
fiore@5
|
44 * @return the restored string
|
fiore@0
|
45 */
|
fiore@0
|
46 public static String restoreNewline(String s){
|
fiore@0
|
47 String result = s.replaceAll("([^'])\\|","$1\n");
|
fiore@0
|
48 return result.replace("'|", "|");
|
fiore@0
|
49 }
|
fiore@0
|
50
|
fiore@0
|
51 /**
|
fiore@0
|
52 * Escapes a set of character with another character.
|
fiore@0
|
53 * @param s The {@code String} whose characters must be escaped.
|
fiore@0
|
54 * @param charsToEscape The set of characters to escape
|
fiore@0
|
55 * @param escapeChar The escape character
|
fiore@0
|
56 * @return a new {@code String} where characters in {@code charsToEscape} have been escaped
|
fiore@0
|
57 * by {@code escapeChar}.
|
fiore@0
|
58 */
|
fiore@0
|
59 public static String escapeCharSequence(String s, CharSequence charsToEscape, char escapeChar ){
|
fiore@0
|
60 String result = s;
|
fiore@0
|
61 for(int i=0;i< charsToEscape.length();i++){
|
fiore@0
|
62 char c = charsToEscape.charAt(i);
|
fiore@0
|
63 if(c == escapeChar)
|
fiore@0
|
64 throw new IllegalArgumentException("escape character cannot be in chars to escape sequence");
|
fiore@0
|
65 for(int j=0;j<i;j++)
|
fiore@0
|
66 if(charsToEscape.charAt(j) == c)
|
fiore@0
|
67 throw new IllegalArgumentException("chars to escape sequence can only have unique characters");
|
fiore@0
|
68 result = result.replace(""+c, ""+escapeChar+c);
|
fiore@0
|
69 }
|
fiore@0
|
70 return result;
|
fiore@0
|
71 }
|
fiore@0
|
72
|
fiore@0
|
73 /**
|
fiore@0
|
74 * Removes an escape character preceding a set of character.
|
fiore@0
|
75 * @param s The {@code String} containing the characters to un-escape
|
fiore@0
|
76 * @param charsToUnescape The set of characters to the which must be un-escaped
|
fiore@0
|
77 * @param escapeChar The escape character
|
fiore@0
|
78 * @return A {@code String} where the escape character preceding the characters
|
fiore@0
|
79 * in charsToUnescape have been removed.
|
fiore@0
|
80 */
|
fiore@0
|
81 public static String unescapeCharSequence(String s, CharSequence charsToUnescape, char escapeChar ){
|
fiore@0
|
82 String result = s;
|
fiore@0
|
83 for(int i=0;i< charsToUnescape.length();i++){
|
fiore@0
|
84 char c = charsToUnescape.charAt(i);
|
fiore@0
|
85 if(c == escapeChar)
|
fiore@0
|
86 throw new IllegalArgumentException("escape character cannot be in chars to escape sequence");
|
fiore@0
|
87 for(int j=0;j<i;j++)
|
fiore@0
|
88 if(charsToUnescape.charAt(j) == c)
|
fiore@0
|
89 throw new IllegalArgumentException("chars to escape sequence can only have unique characters");
|
fiore@0
|
90 result = result.replace(""+escapeChar+c,""+c);
|
fiore@0
|
91 }
|
fiore@0
|
92 return result;
|
fiore@0
|
93 }
|
fiore@0
|
94
|
fiore@0
|
95
|
fiore@0
|
96 }
|