The Mudcat Café TM
Thread #112488 Message #2380640
Posted By: Artful Codger
03-Jul-08 - 10:15 PM
Thread Name: Tech: HtmlEsc.java: Convert special chars
Subject: RE: Tech: HtmlEsc.java: Convert special chars
Script updated 12 Feb 2011 to remove several mnemonics which aren't well-supported: hibar, Zcaron, zcaron and bdquote. The script supplies numeric escapes for these characters instead.
-Artful Codger- /* This program converts text on the clipboard to plain-text with all the non-ASCI and HTML special characters converted to HTML entities or character references (like é or ǀ). This includes a number of word processing characters (like quotes and dashes) which aren't ASCII, and thus get trashed when copied into postings. It is especially useful when posting text in languages other than English.
The character reference values correspond to Unicode values (specifically, to UTF-16 code points.)
Installation: (1) Save this script to a plain-text file named "HtmlEsc.java". (2) In a command window, change to the directory where the script file resides and compile it: javac HtmlEsc.java This produces a class file named "HtmlEsc.class". This is all the Java interpreter needs to run the program. See further compilation notes below.
Usage: (1) Copy the text you wish to convert to the clipboard. (2) In a command window, change to the directory where the class file resides and run: java HtmlEsc Do Not include the .class extension. The clipboard will now contain the converted text as plain text. (3) Paste the modified text from the clipboard to you post. Please preview your message (after any further editing) before you save it.
If you wish to execute the command from any directory, you will either have to supply a full path to the class file (either on the command line or in a shell script, expressed in UNIX forward-slash format) or place the class file in a directory you have defined in the CLASSPATH environment variable. See Java tutorials or documentation for details.
Compilation notes: This script can only be compiled on a system that has the Java Development Kit installed. The minimum version is v1.5 (JDK 5). You should be able to copy and run the resulting class file on any system that has an equivalent or later Java Runtime Environment (JRE). Both of these are available as free downloads from http://java.sun.com/
public class HtmlEsc { public static void main(String[] args) { // Get the current clipboard text. MyClipboard cb = new MyClipboard(); String cbText = cb.getText(); String outText = HtmlEscaper.convertText(cbText); // Append some text and write it back to the clipboard. cb.setText(outText); } } //----------------------------------------------------------------------------
class MyClipboard {
public String getText() { String rtn = null; Clipboard cb = getClipboard(); DataFlavor flavor = DataFlavor.stringFlavor; if (cb.isDataFlavorAvailable(flavor)) { try { rtn = (String) cb.getData(flavor); } catch (Exception e) {} // UnsupportedFlavor will never be thrwosn. } return rtn; }
public void setText(String text) { Clipboard cb = getClipboard(); StringSelection sel = new StringSelection(text); cb.setContents(sel, null); }