annotate java/src/uk/ac/qmul/eecs/ccmi/utils/CharEscaper.java @ 1:e3935c01cde2 tip

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