view java/src/uk/ac/qmul/eecs/ccmi/utils/CharEscaper.java @ 8:ea7885bd9bff tip

fixed bug : render solid line as dotted/dashed when moving the stylus from dotted/dashed to solid
author ccmi-guest
date Thu, 03 Jul 2014 16:12:20 +0100
parents d66dd5880081
children
line wrap: on
line source
/*  
 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 {@code String} to remove new line characters from
	 * @return a {@code 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 the string to be restored
	 * @return the restored string
	 */
	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;
	}
	
	
}