diff java/src/uk/ac/qmul/eecs/ccmi/utils/CharEscaper.java @ 0:9418ab7b7f3f

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