All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cybergarage.xml.XML Maven / Gradle / Ivy

There is a newer version: 2.6.0
Show newest version
/******************************************************************
*
*	CyberXML for Java
*
*	Copyright (C) Satoshi Konno 2002-2003
*
*	File: XML.java
*
*	Revision;
*
*	01/05/03
*		- first revision.
*	12/15/03
*		- Terje Bakken
*		- Added escapeXMLChars()
*	
******************************************************************/

package org.cybergarage.xml;

public class XML 
{
	public final static String DEFAULT_CONTENT_TYPE = "text/xml; charset=\"utf-8\"";
	public final static String DEFAULT_CONTENT_LANGUAGE = "en";
	public final static String CHARSET_UTF8 = "utf-8";

	////////////////////////////////////////////////
	// escapeXMLChars
	////////////////////////////////////////////////
	
	private final static String escapeXMLChars(String input, boolean quote) 
	{
		if (input == null)
			return null;
		StringBuffer out = new StringBuffer();
		int oldsize=input.length();
		char[] old=new char[oldsize];
		input.getChars(0,oldsize,old,0);
		int selstart = 0;
		String entity=null;			
		for (int i=0;i': entity=">"; break;
			case '\'': if (quote) { entity="'"; break; } 
			case '"': if (quote) { entity="""; break; }
			}
			if (entity != null) {
				out.append(old,selstart,i-selstart);
				out.append(entity);
				selstart=i+1;
				entity=null;											 
			}
		}
		if (selstart == 0)
			return input;
		out.append(old,selstart,oldsize-selstart);
		return out.toString();	
	}

	public final static String escapeXMLChars(String input)
	{
		return escapeXMLChars(input, true);
	}
	
	////////////////////////////////////////////////
	// unescapeXMLChars
	////////////////////////////////////////////////

	public final static String unescapeXMLChars(String input)
	{
		if (input == null)
			return null;
		
		String outStr;

		outStr = input.replace("&", "&");
		outStr = outStr.replace("<", "<");
		outStr = outStr.replace(">", ">");
		outStr = outStr.replace("'", "\'");
		outStr = outStr.replace(""", "\"");
		
		return outStr;
	}
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy