Java tutorial
//package com.java2s; /* * IRClib -- A Java Internet Relay Chat library -- class IRCUtil * Copyright (C) 2002, 2003 Christoph Schwering <ch@schwering.org> * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ public class Main { /** * This is part of the mIRC code and shows that a color-code starts / ends. * Here it is as the ASCII decimal int 3. */ public static char colorIndicator = 3; /** * This is part of the mIRC code and shows that bold starts / ends. * Here it is as the ASCII decimal int 32. */ public static char boldIndicator = 31; /** * This is part of the mIRC code and shows that bold starts / ends. * Here it is as the ASCII decimal int 2. */ public static char underlinedIndicator = 2; /** * This is part of the mIRC code and shows that bold, underline and colors * end. * Here it is as the ASCII decimal int 15. */ public static char colorEndIndicator = 15; /** * This is part of the mIRC code and indicates that the client's colors are * reversed (background -> foreground and foreground -> background). * Here it is as the ASCII decimal int 1. */ public static char colorReverseIndicator = 22; /** * This is part of the mIRC code and shows that a PRIVMSG is an ACTION * (<code>/me</code>). * Here it is as the ASCII decimal int 22. */ public static char actionIndicator = 1; /** * Erases the mIRC colorcodes from a String. * The documentation of the evil color codes is available on * <a href="http://www.mirc.co.uk/help/color.txt" * target="_blank">http://www.mirc.co.uk/help/color.txt</a>. * This method links to the <code>parseColors(StringBuffer)</code> method. * @param str The line which should be parsed. * @return A line cleaned from any mIRC colorcodes. * @see #parseColors(StringBuffer) */ public static String parseColors(String str) { return parseColors(new StringBuffer(str)).toString(); } /** * Erases the mIRC colorcodes from a String. * The documentation of the evil color codes is available on * <a href="http://www.mirc.co.uk/help/color.txt" * target="_blank">http://www.mirc.co.uk/help/color.txt</a>. * @param buf The line which should be parsed. * @return A line as <code>StringBuffer</code> object which is cleaned from * any mIRC colorcodes. * @see #parseColors(String) */ public static StringBuffer parseColors(StringBuffer buf) { int len = buf.length(); for (int i = 0, j = 0, c; i < len; i++, j = i) { c = buf.charAt(i); try { // COLORS Beginning // (format: <colorIndicator><int>[<int>][[,<int>[<int>]] if (c == colorIndicator) { c = buf.charAt(++j); if ('0' <= c && c <= '9') { // first int c = buf.charAt(++j); if ('0' <= c && c <= '9') c = buf.charAt(++j); // second int } if (c == ',') c = buf.charAt(++j); // comma if ('0' <= c && c <= '9') { // first int c = buf.charAt(++j); if ('0' <= c && c <= '9') c = buf.charAt(++j); // second int } // ACTION / BOLD / UNDERLINE / COLOR END // (format: <actionIndicator> / <boldIndicator> etc.) } else if (c == actionIndicator || c == boldIndicator || c == underlinedIndicator || c == colorEndIndicator || c == colorReverseIndicator) { j++; } } catch (StringIndexOutOfBoundsException exc) { // we got the end of the string with a call to charAt(++iIndexEnd) // nothing } if (j > i) { buf = buf.delete(i, j); // remove the cars len -= (j - i); i -= (j - i); } } return buf; } }