StackTraceColorizer implements MessageColorizer { protected Collection<MessageFragmentStyle> findExceptionNameAndMessage(final Style style, String subTextFragment) { final ArrayList<MessageFragmentStyle> result = new ArrayList<>(); Matcher matcherMessage = exceptionNameAndMessage.matcher(subTextFragment); while (matcherMessage.find()) { final int beginIndex = matcherMessage.start(1); final int endIndex = matcherMessage.end(1); final String msg = subTextFragment.substring(beginIndex, endIndex).replaceFirst("Caused by: ", ""); final Style style1 = styleContext.addStyle("exceptionMessage-" + msg, style); style1.addAttribute(STYLE_ATTRIBUTE_EXCEPTION_MSG, msg); result.add(new MessageFragmentStyle(beginIndex, endIndex - beginIndex, style1, false)); System.out.println("Setting style with exceptionMessage " + style1); } return result; } StackTraceColorizer(Theme theme); @Override boolean colorizingNeeded(String message); @Override Collection<MessageFragmentStyle> colorize(String message); @Override String getName(); @Override String getDescription(); @Override String getPluginableId(); @Override int getApiVersion(); static final String STYLE_ATTRIBUTE_EXCEPTION_MSG; static final String STYLE_ATTRIBUTE_LOCATION_INFO; }
@Test public void findExceptionNameAndMessage() throws IOException, BadLocationException { String stacktrace = IOUtils.toString(getSystemResourceAsStream("stacktrace/stacktrace3.txt"), UTF_8).replace("\r", ""); StackTraceColorizer colorizer = new StackTraceColorizer(new ThemeConfig(new BaseConfiguration())); colorizer.initStyles(); final Style style = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); final Collection<MessageFragmentStyle> colorize = colorizer.findExceptionNameAndMessage(style, stacktrace); final List<MessageFragmentStyle> exceptions = colorize.stream() .filter(msf -> msf.getStyle().getAttribute(STYLE_ATTRIBUTE_EXCEPTION_MSG) != null) .sorted((o1, o2) -> o1.getOffset() - o2.getOffset()) .collect(Collectors.toList()); assertEquals(exceptions.size(), 2); final MessageFragmentStyle msf0 = exceptions.get(0); assertEquals(stacktrace.substring(msf0.getOffset(), msf0.getOffset() + msf0.getLength()), "com.datastax.driver.core.exceptions.TransportException: [null] Cannot connect"); assertEquals(msf0.getOffset(), 97); assertEquals(msf0.getLength(), 77); assertEquals(msf0.getStyle().getAttribute(STYLE_ATTRIBUTE_EXCEPTION_MSG), "com.datastax.driver.core.exceptions.TransportException: [null] Cannot connect"); final MessageFragmentStyle msf1 = exceptions.get(1); assertEquals(stacktrace.substring(msf1.getOffset(), msf1.getOffset() + msf1.getLength()), "Caused by: java.nio.channels.UnresolvedAddressException: null"); assertEquals(msf1.getOffset(), 2540); assertEquals(msf1.getLength(), 61); assertEquals(msf1.getStyle().getAttribute(STYLE_ATTRIBUTE_EXCEPTION_MSG), "java.nio.channels.UnresolvedAddressException: null"); }