From b865c6b453e53ba2c5861fa0f78c812675f9058e Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Fri, 23 Jan 2004 12:21:01 +0000 Subject: [PATCH] Initial import. --- TextToHTML.java | 183 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 TextToHTML.java diff --git a/TextToHTML.java b/TextToHTML.java new file mode 100644 index 0000000..22af5dc --- /dev/null +++ b/TextToHTML.java @@ -0,0 +1,183 @@ +/* + * @(#)TextToHTML.java + * + * Copyright (C) 2002-2003 by Erik C. Thauvin (erik@thauvin.net) + * All rights reserved. + * + * Donated to the Roller Weblogger project for publication under + * the terms of the Roller Software License. + */ + +//package com.imacination.jtalk; + +/** + * Converts/encodes text to HTML or XML. + * + * @author Erik C. Thauvin + * + * @created October 31, 2002 + */ +public class TextToHTML +{ + /** + * The main program usage. + */ + private static final String USAGE = "Usage: java " + TextToHTML.class.getName() + " [-xml] "; + + /** + * Converts a character to HTML or XML entity. + * + * @param ch The character to convert. + * @param xml Convert the character to XML if set to true. + * + * @return The converted string. + */ + public static final String charToHTML(char ch, boolean xml) + { + int c; + + // Convert left bracket + if (ch == '<') + { + return ("<"); + } + + // Convert left bracket + else if (ch == '>') + { + return (">"); + } + + // Convert ampersand + else if (ch == '&') + { + return ("&"); + } + + // High-ASCII character + else if (ch >= 128) + { + c = ch; + + return ("&#" + c + ';'); + } + + // Convert double quote + else if (xml && (ch == '"')) + { + return ("""); + } + + // Convert single quote + else if (xml && (ch == '\'')) + { + return ("'"); + } + + // No conversion + else + { + // Return character as string + return (String.valueOf(ch)); + } + } + + /** + * The main program for the TextToHtml class. + * + * @param args The command line arguments. + */ + public static final void main(String[] args) + { + if (args.length > 0) + { + boolean xml = false; + int i = 0; + + if (args[0].startsWith("-x")) + { + if (args.length == 1) + { + usage(); + } + + xml = true; + i++; + } + + final StringBuffer cmdline = new StringBuffer(); + + for (; i < args.length; i++) + { + if (cmdline.length() != 0) + { + cmdline.append(' '); + } + + cmdline.append(args[i]); + } + + System.out.println(textToHTML(cmdline.toString(), xml)); + } + else + { + usage(); + } + } + + /** + * Converts a text string to HTML or XML entities. + * + * @param text The string to convert. + * @param xml Convert the string to XML if set to true. + * + * @return The converted string. + */ + public static final String textToHTML(String text, boolean xml) + { + final StringBuffer html = new StringBuffer(); + + // Loop thru each characters of the text + for (int i = 0; i < text.length(); i++) + { + // Convert character to HTML/XML + html.append(charToHTML(text.charAt(i), xml)); + } + + // Return HTML/XML string + return html.toString(); + } + + /** + * Converts a text string to HTML or XML entities. + * + * @param text The string to convert. + * + * @return The converted string. + */ + public static final String textToHTML(String text) + { + return textToHTML(text, false); + } + + /** + * Converts a text string to XML entities. + * + * @param text The string to convert. + * + * @return The converted string. + */ + public static final String textToXML(String text) + { + return textToHTML(text, true); + } + + /** + * Prints the usage and exits. + */ + private static final void usage() + { + System.err.println(USAGE); + System.exit(1); + } +}