Last active
March 3, 2017 10:47
-
-
Save libetl/62007afc8b3798a5758d50e939500de2 to your computer and use it in GitHub Desktop.
XmlInColorsConverter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.IOException; | |
import java.util.Scanner; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
public class XmlInColorsConverter { | |
private static final Pattern NAME = Pattern.compile("(?:([a-zA-Z][a-zA-Z0-9]+):)?([a-zA-Z][a-zA-Z0-9._]+)"); | |
private static final Pattern VAL = Pattern.compile("\"((?:[^\"]|(?<=\\\\)\")*)\""); | |
private static final Pattern ATTR = Pattern.compile("(" + NAME + ")\\s*(=)\\s*(" + VAL + ")\\s*"); | |
private static final Pattern MARKUP = Pattern.compile("(\\s*)(<)(/|\\?)?" + NAME + "\\s*(?:" + ATTR + ")*(/|\\?)?>"); | |
private static final String NORMAL = "\033[0m"; | |
private static final String YELLOW = "\033[1;33m"; | |
private static final String BLUE = "\033[34m"; | |
private static final String PINK = "\033[35m"; | |
private static final String GREEN = "\033[32m"; | |
private static final String CYAN = "\033[36m"; | |
public static void main (String [] args) throws IOException { | |
Scanner scanner = new Scanner(new File(args[0])); | |
scanner.useDelimiter("\\A"); | |
System.out.println(new XmlInColorsConverter().convert(scanner.next())); | |
} | |
String convert(String text) { | |
return colorize(text, MARKUP.matcher(text)); | |
} | |
private static String colorize(String input, Matcher matcher) { | |
int lastEnd = 0; | |
int level = 0; | |
StringBuilder result = new StringBuilder(); | |
while (matcher.find()) { | |
String oneMarkup = matcher.group(); | |
Matcher attrMatcher = ATTR.matcher(oneMarkup); | |
String afterLastMatch = input.substring(lastEnd, matcher.start(2) == -1 ? 0 : matcher.start(2)); | |
level -= "/".equals(matcher.group(3)) ? 1 : 0; | |
if (afterLastMatch.matches("\\s*")) { | |
result.append("\n").append(IntStream.range(0, level).boxed().map(i -> " ").collect(Collectors.joining())); | |
} else { | |
result.append(afterLastMatch); | |
} | |
level += matcher.group(3) == null ? 1 : 0; | |
result.append(YELLOW).append("<").append((matcher.group(3) != null ? matcher.group(3) : "")).append((matcher.group(4) != null ? BLUE + matcher.group(4) + YELLOW + ":" : YELLOW)).append(matcher.group(5)).append(NORMAL); | |
while (attrMatcher.find()) { | |
result.append(" ").append((attrMatcher.group(2) != null ? BLUE + attrMatcher.group(2) + PINK + ":" : PINK)).append(attrMatcher.group(3)).append(GREEN).append("=").append(CYAN).append(attrMatcher.group(5)); | |
} | |
level -= "/".equals(matcher.group(12)) ? 1 : 0; | |
result.append(YELLOW).append((matcher.group(12) != null ? matcher.group(12) : "")).append(">").append(NORMAL); | |
lastEnd = input.charAt(matcher.end()) == '\n' ? matcher.end() + 1 : matcher.end(); | |
} | |
return result.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment