fiore@0: /* fiore@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool fiore@0: fiore@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) fiore@0: fiore@0: This program is free software: you can redistribute it and/or modify fiore@0: it under the terms of the GNU General Public License as published by fiore@0: the Free Software Foundation, either version 3 of the License, or fiore@0: (at your option) any later version. fiore@0: fiore@0: This program is distributed in the hope that it will be useful, fiore@0: but WITHOUT ANY WARRANTY; without even the implied warranty of fiore@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fiore@0: GNU General Public License for more details. fiore@0: fiore@0: You should have received a copy of the GNU General Public License fiore@0: along with this program. If not, see . fiore@0: */ fiore@0: fiore@0: package uk.ac.qmul.eecs.ccmi.utils; fiore@0: fiore@0: /** fiore@0: * A utility class providing static methods to escape one or more characters. fiore@0: * fiore@0: */ fiore@0: public class CharEscaper { fiore@0: /** fiore@0: * Replaces the new line character with a '|' in the {@code String} passed as argument. fiore@0: * The original {@code String} can be restored by passing the returned {@code String} to fiore@0: * {@link #restoreNewline(String)}. Existing '|' characters will be escaped in order not to miss fiore@0: * them in the restore process. fiore@5: * @param s the {@code String} to remove new line characters from fiore@5: * @return a {@code String} where all new line characters have been replaced by '|' fiore@0: */ fiore@0: public static String replaceNewline(String s){ fiore@0: String result = s.replace("|", "'|"); fiore@0: return result.replace('\n', '|'); fiore@0: } fiore@0: fiore@0: /** fiore@0: * Restores a {@code String} whose new line characters have been previously replaced by fiore@0: * {@link #replaceNewline(String)}, to the original form. fiore@5: * @param s the string to be restored fiore@5: * @return the restored string fiore@0: */ fiore@0: public static String restoreNewline(String s){ fiore@0: String result = s.replaceAll("([^'])\\|","$1\n"); fiore@0: return result.replace("'|", "|"); fiore@0: } fiore@0: fiore@0: /** fiore@0: * Escapes a set of character with another character. fiore@0: * @param s The {@code String} whose characters must be escaped. fiore@0: * @param charsToEscape The set of characters to escape fiore@0: * @param escapeChar The escape character fiore@0: * @return a new {@code String} where characters in {@code charsToEscape} have been escaped fiore@0: * by {@code escapeChar}. fiore@0: */ fiore@0: public static String escapeCharSequence(String s, CharSequence charsToEscape, char escapeChar ){ fiore@0: String result = s; fiore@0: for(int i=0;i< charsToEscape.length();i++){ fiore@0: char c = charsToEscape.charAt(i); fiore@0: if(c == escapeChar) fiore@0: throw new IllegalArgumentException("escape character cannot be in chars to escape sequence"); fiore@0: for(int j=0;j