String encode(String value)
	{
		StringBuffer sb = new StringBuffer();
		int len = value.length();
		for (int i = 0; i < len; i++)
		{
			char c = value.charAt(i);
			switch (c)
			{
				case '<':
					sb.append("&lt;");
					break;
				case '>':
					sb.append("&gt;");
					break;
				case '\'':
					sb.append("&apos;");
					break;
				case '\"':
					sb.append("&quot;");
					break;
				case '&':
					int nextSemi = value.indexOf(";", i);
					if (nextSemi < 0 || !isReference(value.substring(i, nextSemi + 1)))
					{
						sb.append("&amp;");
					}
					else
					{
						sb.append('&');
					}
					break;
				default:
					if (isLegalCharacter(c))
					{
						sb.append(c);
					}
					break;
			}
		}
		return sb.substring(0);
	}