Java tutorial
//package com.java2s; //License from project: Apache License import java.io.*; import java.util.*; public class Main { private static void __toColoredHtml(String xmlText, HashMap<String, String> tagColorScheme, Writer writer) throws IOException { BufferedReader br = new BufferedReader(new StringReader(xmlText)); String line; // String[] lines = xmlText.split("\r\n"); while ((line = br.readLine()) != null) { __toColoredHtmlLine(line, tagColorScheme, writer); } } private static void __toColoredHtmlLine(String line, HashMap<String, String> tagColorScheme, Writer writer) throws IOException { if (line.trim().length() == 0) return; line = line.replace("\t", " "); line = line.replace("<", "|<|"); line = line.replace(">", "|>|"); while (true) { int pos = line.indexOf("|<|"); if (pos < 0) break; String tagName = __getNextWord(line, pos + 3); String color = null; if (tagName != null) { color = tagColorScheme.get(tagName); } if (color != null) { line = line.substring(0, pos) + "<span style=\"color: " + color + "; font-weight: bold;\"><" + line.substring(pos + 3); } else { line = line.substring(0, pos) + "<span style=\"color: #808080;\"><" + line.substring(pos + 3); } } line = line.replace("|>|", "></span>"); int n = line.length(); line = line.trim(); n -= line.length(); StringBuilder sb = new StringBuilder(); for (int j = 0; j < n; j++) { sb.append(" "); } writer.append(sb.toString()); writer.append(line); writer.append("<br />\r\n"); } private static String __getNextWord(String s, int pos) { int i = 0; StringBuilder sb = new StringBuilder(); while (pos + i < s.length()) { char c = s.charAt(pos + i); if (Character.isLetter(c)) { sb.append(c); } else { break; } i++; } if (sb.length() == 0) return null; return sb.toString(); } }