All pastes #246125 Raw Edit

Stuff

public text v1 · immutable
#246125 ·published 2006-11-11 22:15 UTC
rendered paste body
        private static final char[] illegal = {  '<',    '>',    '"',     '\'',     '\\',     '&'};
	private static final String[] replace = {"&lt;", "&gt;", "&quot;", "&#039;", "&#092;", "&#38;"};
	/**
	 * This function isn't properly tested...
	 * @param in
	 * @return
	 */
	public static final String unescapeXml(String in) {
		StringBuilder ret = new StringBuilder();
		char c; 
		String z; 
		for(int i=0; i <in.length(); i++) {
			if((c=in.charAt(i))=='&') {
				z = in.substring(i, i+5);
//				System.out.println(z);
				if(z.indexOf("&amp;")==0) {
					ret.append('&');
					i+=4;
					
				}
				else {
					boolean cont = true; 
					for(int j=0; j<replace.length&&cont; j++) {
						if(z.indexOf(replace[j])==0) {
							ret.append(illegal[j]);
							i+= replace[j].length()-1;
							cont = false;
						}
					}
					if(cont) {
						System.out.println("Got unrecognized sequence: '" + z + "'");
						ret.append(c);
					}//else System.out.println("OK!");
				}
			}
			else ret.append(c);
		}
		
		return ret.toString();
	}