import java.net.*; import java.io.*; import java.util.*; /** * This class just provide one static function to do * a completely minimalistic encoding of html text. * * @author http://bumble.sf.net * @See Answer, FAQ, etc */ public class Html extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- private String text; //-------------------------------------------- public Html() { this.text = ""; } //-------------------------------------------- /** A completely inadequate encoding function. This * is just a place holder for a function form jtidy * or apache jakarta etc */ public static String encode(String sText) { StringBuffer sbReturn = new StringBuffer(sText.length() * 2); char cCurrentCharacter; for (int ii = 0; ii < sText.length(); ++ii) { cCurrentCharacter = sText.charAt(ii); if (cCurrentCharacter == '<') { sbReturn.append("<"); } else if (cCurrentCharacter == '>') { sbReturn.append(">"); } else if (cCurrentCharacter == '&') { sbReturn.append("&"); } else if (cCurrentCharacter == '"') { sbReturn.append("""); } else if (cCurrentCharacter == '"') { sbReturn.append("""); } else if (cCurrentCharacter == '"') { sbReturn.append("""); } else { sbReturn.append(cCurrentCharacter); } } //-- for return sbReturn.toString(); } //-- encode //-------------------------------------------- /** a main method for testing */ public static void main(String[] args) throws Exception { StringBuffer sbUsageMessage = new StringBuffer(""); sbUsageMessage.append("test usage: java Html text"); sbUsageMessage.append(NEWLINE); StringBuffer sbMessage = new StringBuffer(""); if (args.length == 0) { System.out.println(sbUsageMessage); System.exit(-1); } System.out.println(Html.encode(args[0])); System.out.println(NEWLINE); } //-- main() } //-- Html class