Lieber Besucher, herzlich willkommen bei: Banana-Coding.com - Java Knuddels Emulator. Falls dies dein erster Besuch auf dieser Seite ist, lies bitte die Hilfe durch. Dort wird dir die Bedienung dieser Seite näher erläutert. Darüber hinaus solltest du dich registrieren, um alle Funktionen dieser Seite nutzen zu können. Benutze das Registrierungsformular, um dich zu registrieren oder informiere dich ausführlich über den Registrierungsvorgang. Falls du dich bereits zu einem früheren Zeitpunkt registriert hast, kannst du dich hier anmelden.
![]() |
Java-Quelltext |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
package jchat.tools; import java.awt.Color; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * @author uncopyable */ public class KCodeParser { private static String fontColor ="BLACK"; private static int fontSize = 12; private static StringBuilder code = new StringBuilder(); private static String style; public KCodeParser(String fontColor,int fontSize){ this.fontColor = fontColor; this.fontSize = fontSize; style = "<style type="text/css">" + "body {" + "font-family:Arial;" + "font-size:" + fontSize + "px;" + "color:" + fontColor + ";"; } public KCodeParser(){ style = null; fontColor ="BLACK"; fontSize = 12; } public static String parse(String text) { code = new StringBuilder(style); boolean escape = false; boolean bold = false; boolean italic = false; int b = 0; for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); switch (ch) { case '<': code.append("<"); break; case '>': code.append(">"); break; case '&': code.append("&"); break; case '\\': if (escape){ code.append(""); break; } break; case ':': // Smileys if (text.startsWith(":)", i) && (!escape)) { code.append("<img src="http://chat.knuddels.de/pics/sm_classic_00.gif" alt=":)"/>"); i += 1; } break; case '°': try { if (escape){ code.append("°"); break; } if (text.charAt(i + 1) == '°' && (!escape)) { String kcode = text.substring(i + 2); kcode = kcode.substring(0, kcode.indexOf('<')); if (kcode.split("\\|").length > 1){ String href = kcode.split("\\|")[0]; String a = kcode.split("\\|")[1]; code.append("<a href="").append(a).append("">").append(href).append("</a>"); if (kcode.split("\\|").length > 2){ // } }else { code.append("<a href="").append(kcode).append("">").append(kcode).append("</a>"); i += kcode.length() + 2; break; } i += kcode.length() + 2; break; }else if (isNumber(text.charAt(i + 1))) { // Schriftgröße String kcode = text.substring(i + 1); kcode = kcode.substring(0, kcode.indexOf('°')); if (kcode.isEmpty()){ break; } code.append("<span style="font-size:").append(kcode).append("px">"); i += kcode.length() + 1; break; }else if (text.charAt(i + 2) == '°') { // Farbe String kcode = text.substring(i + 1); kcode = kcode.substring(0, kcode.indexOf('°')); if (kcode.isEmpty()){ break; } code.append("<font color="").append(GetHtmlColor(kcode.charAt(0))).append("">"); i += kcode.length() + 1; }else if (text.charAt(i + 1) == '[' && (!escape)) { String kcode = text.substring(i + 1); kcode = kcode.substring(0, kcode.indexOf('°')); code.append("<font color="").append(parseColor(kcode)).append("">"); i += kcode.length() + 2; break; }else { String kcode = text.substring(i + 2); kcode = kcode.substring(0, kcode.indexOf('<')); if (kcode.split("\\|").length > 1){ String href = kcode.split("\\|")[0]; String a = kcode.split("\\|")[1]; code.append("<a href="").append(a).append("">").append(href).append("</a>"); if (kcode.split("\\|").length > 2){ // } }else { code.append("<a href="").append(kcode).append("">").append(kcode).append("</a>"); i += kcode.length() + 2; break; } i += kcode.length() + 2; break; } } catch (Exception ex) { } break; case '§': escape = false; bold = false; italic = false; code.append("<span style="font-size:").append(fontSize).append("px">"); break; case '_': if (escape){ code.append("_"); break; } if (bold){ code.append("</b>"); bold = false; break; } code.append("<b>"); bold = !bold; break; case '"': if (escape){ code.append("""); break; } if (italic){ code.append("</i>"); break; } code.append("<i>"); italic = !italic; break; case '#': if (escape){ code.append("#"); break; } code.append("<br>"); break; default: code.append(ch); } if (ch == '\\') { code.append(""); escape = true; }else { escape = false; } } return code.toString(); } private static String GetHtmlColor(char s) { if (s == 'A') { return "GRAY"; } else if (s == 'B') { return "BLUE"; } else if (s == 'C') { return "CYAN"; } else if (s == 'D') { return "DARKGRAY"; } else if (s == 'E') { return "DARKGREEN"; } else if (s == 'G') { return "GREEN"; } else if (s == 'K') { return "BLACK"; } else if (s == 'Y') { return "YELLOW"; } else if (s == 'L') { return "LIGHTBLUE"; } else if (s == 'M') { return "VIOLET"; } else if (s == 'N') { return "BROWN"; } else if (s == 'O') { return "ORANGE"; } else if (s == 'P') { return "PINK"; } else if (s == 'R') { return "RED"; } else if (s == 'W') { return "WHITE"; } else if (s == 'Y') { return "YELLOW"; } else if (s == 'r') { return fontColor; } else { return "BLACK"; } } public static String parseColor(String input) { Pattern p = Pattern.compile("(\\d{1,3}),(\\d{1,3}),(\\d{1,3})"); Matcher matcher = p.matcher(input); if(matcher.find()) { String colors = matcher.group(); int c1 = Integer.parseInt(colors.split(",")[0]); int c2 = Integer.parseInt(colors.split(",")[1]); int c3 = Integer.parseInt(colors.split(",")[2]); return Integer.toString((new Color(c1,c2,c3)).getRGB() & 0xffffff, 16); } return null; } private static boolean isNumber(int character) { return character >= '0' && character <= '9'; } } |
Benutzer, die sich für diesen Beitrag bedankt haben:
![]() |
Java-Quelltext |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
package jchat.tools; import java.awt.Color; import java.awt.Font; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * @author uncopyable * * Version 1.2 Dienstag, 29. Mai 2012 * * Last Modified: Mittwoch, 30. Mai 2012 * - kleine Schönheitskorrekturen * */ public class KCodeParser { private static Color fontColor; private static int fontSize; private static StringBuilder code; private static Font font; public KCodeParser(Color fontColor, Font font) { this.code = new StringBuilder(); this.font = font; this.fontColor = fontColor; this.fontSize = font.getSize(); } public KCodeParser() { this.code = new StringBuilder(); this.font = new Font("Arial", Font.PLAIN, 12); this.fontColor = new Color(0, 0, 0); this.fontSize = 12; } public static String parse(String text) { code = new StringBuilder(); boolean escape = false; boolean bold = false; boolean italic = false; // Style code.append("<span style="font-size:").append(fontSize).append("px">"); code.append("<font color="").append(Integer.toString(fontColor.getRGB() & 0x00ffffff, 16)).append("">"); for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); switch (ch) { case '<': code.append("<"); break; case '>': code.append(">"); break; case '&': code.append("&"); break; case '\\': i++; break; // Smileys case ':': if (text.startsWith(":)", i) && (!escape)) { code.append("<img src="http://chat.knuddels.de/pics/sm_classic_00.gif" alt=":)"/>"); i++; } else { code.append(":"); } break; // Reset Style case '§': escape = false; bold = false; italic = false; code.append("<span style="font-size:").append(fontSize).append("px">"); code.append("<font color="").append(Integer.toString(fontColor.getRGB() & 0x00ffffff, 16)).append("">"); break; // Bold case '_': if (escape) { code.append("_"); break; } if (bold) { code.append("</b>"); bold = false; break; } code.append("<b>"); bold = !bold; break; // Italic case '"': if (escape) { code.append("""); break; } if (italic) { code.append("</i>"); break; } code.append("<i>"); italic = !italic; break; // Line break case '#': if (escape) { code.append("#"); break; } code.append("<br>"); break; // KCode case '°': try { if (escape) { code.append("°"); break; } if (text.charAt(i + 1) == '°' && (!escape)) { break; } else if (isNumber(text.charAt(i + 1))) { // Schriftgröße if (text.charAt(i + 3) == '>') { // Link int length = 0; StringBuilder size = new StringBuilder(); size.append(text.charAt(i + 1)); size.append(text.charAt(i + 2)); code.append("<span style="font-size:").append(size.toString()).append("px">"); length += size.toString().length(); String link = text.substring(i + 4); link = link.substring(0, link.indexOf('<')); if (link.split("\\|").length > 1) { String href = link.split("\\|")[0]; String a = link.split("\\|")[1]; code.append("<a href="").append(href).append("">").append(a).append("</a>"); } else { code.append("<a href="").append(link).append("">").append(link).append("</a>"); } length += link.length() + 2; i += length; break; } else if ((isNumber(text.charAt(i + 2)))) { StringBuilder size = new StringBuilder(); size.append(text.charAt(i + 1)); size.append(text.charAt(i + 2)); code.append("<span style="font-size:").append(size.toString()).append("px">"); i += size.toString().length(); break; } else if (text.charAt(i + 2) == '°') { String size = text.substring(i + 1); size = size.substring(0, size.indexOf('°')); code.append("<span style="font-size:").append(size).append("px">"); i += size.length() + 1; break; } else if (text.charAt(i + 2) == '>') { // Link int length = 0; StringBuilder size = new StringBuilder(); size.append(text.charAt(i + 1)); code.append("<span style="font-size:").append(size.toString()).append("px">"); length += size.toString().length(); String link = text.substring(i + 3); link = link.substring(0, link.indexOf('<')); if (link.split("\\|").length > 1) { String href = link.split("\\|")[0]; String a = link.split("\\|")[1]; code.append("<a href="").append(href).append("">").append(a).append("</a>"); } else { code.append("<a href="").append(link).append("">").append(link).append("</a>"); } length += link.length() + 2; i += length; break; } else { break; } } else if ((text.charAt(i + 1) == '>')) { // Link String kcode = text.substring(i + 2); kcode = kcode.substring(0, kcode.indexOf('<')); if ((text.charAt(i + 2) == '_') && (text.charAt(i + 3) == 'h')) { if (kcode.split("\\|").length > 1) { String href = kcode.replace("_h", "").split("\\|")[0]; String a = kcode.split("\\|")[1]; a = a.replace(""", href); code.append("<a href="").append(a).append("">").append(href).append("</a>"); } i += kcode.length() + 2; break; } if (kcode.split("\\|").length > 1) { String href = kcode.split("\\|")[0]; String a = kcode.split("\\|")[1]; code.append("<a href="").append(href).append("">").append(a).append("</a>"); i += kcode.length() + 2; break; } if (isImage(kcode)) { // Image code.append("<img src="http://chat.knuddels.de/pics/"); code.append(kcode); code.append(""/>"); i += kcode.length() + 2; break; } code.append("<a href="").append(kcode).append("">").append(kcode).append("</a>"); i += kcode.length() + 2; break; } else if (isColor(text.charAt(i + 1)) && (text.charAt(i + 2) == '>')) { // Schriftgröße int length = 0; String color = String.valueOf(text.charAt(i + 1)); code.append("<font color="").append(GetHtmlColor(color.charAt(0))).append("">"); length += color.length() + 1; if (text.charAt(i + 2) == '>') { // Link String link = text.substring(i + 3); link = link.substring(0, link.indexOf('<')); if (link.split("\\|").length > 1) { String href = link.split("\\|")[0]; String a = link.split("\\|")[1]; code.append("<a href="").append(href).append("">").append(a).append("</a>"); } else { code.append("<a href="").append(link).append("">").append(link).append("</a>"); } length += link.length() + 2; } i += length; break; } else if (text.charAt(i + 2) == '°') { // Farbe String color = text.substring(i + 1); color = color.substring(0, color.indexOf('°')); code.append("<font color="").append(GetHtmlColor(color.charAt(0))).append("">"); i += color.length() + 1; } else if (text.charAt(i + 1) == '[') { // rgb String rgbColor = text.substring(i + 1); rgbColor = rgbColor.substring(0, rgbColor.indexOf('°')); rgbColor = parseColor(rgbColor); code.append("<font color="").append(rgbColor).append("">"); i += rgbColor.length() + 3; break; } else { } } catch (Exception ex) { } break; default: code.append(ch); } } return code.toString(); } private static boolean isColor(int character) { switch (character) { case 'A': case 'B': case 'C': case 'D': case 'E': case 'G': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'R': case 'r': case 'W': case 'Y': return true; default: return false; } } private static String GetHtmlColor(char s) { if (s == 'A') { return "GRAY"; } else if (s == 'B') { return "BLUE"; } else if (s == 'C') { return "CYAN"; } else if (s == 'D') { return "DARKGRAY"; } else if (s == 'E') { return "DARKGREEN"; } else if (s == 'G') { return "GREEN"; } else if (s == 'K') { return "BLACK"; } else if (s == 'Y') { return "YELLOW"; } else if (s == 'L') { return "LIGHTBLUE"; } else if (s == 'M') { return "VIOLET"; } else if (s == 'N') { return "BROWN"; } else if (s == 'O') { return "ORANGE"; } else if (s == 'P') { return "PINK"; } else if (s == 'R') { return "RED"; } else if (s == 'W') { return "WHITE"; } else if (s == 'Y') { return "YELLOW"; } else if (s == 'r') { return Integer.toString(fontColor.getRGB() & 0x00ffffff, 16); } else { return "BLACK"; } } private static boolean isNumber(int character) { return character >= '0' && character <= '9'; } private static boolean isImage(String s) { return s.endsWith(".gif") | s.endsWith(".png") | s.endsWith(".jpg"); } public static String parseColor(String input) { Pattern p = Pattern.compile("(\\d{1,3}),(\\d{1,3}),(\\d{1,3})"); Matcher matcher = p.matcher(input); if (matcher.find()) { String colors = matcher.group(); int c1 = Integer.parseInt(colors.split(",")[0]); int c2 = Integer.parseInt(colors.split(",")[1]); int c3 = Integer.parseInt(colors.split(",")[2]); return Integer.toString((new Color(c1, c2, c3)).getRGB() & 0xffffff, 16); } return null; } } |
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »uncopyable« (30.05.2012, 13:12)
Benutzer, die sich für diesen Beitrag bedankt haben: