JHighlightColorer implements Colorer { @Override public String getColorizedContent(final String content, final String fileExtension, final String encoding) throws IOException { logger.debug("Colorizing content, file extension: " + fileExtension); if (content == null) { return ""; } final Renderer renderer = getRenderer(fileExtension); if (renderer == null) { return StringEscapeUtils.escapeXml(content); } final ByteArrayOutputStream out = new ByteArrayOutputStream(); final ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes(encoding)); renderer.highlight(null, in, out, encoding, true); return out.toString(encoding).substring(73).replace("<br />", "").trim(); }  @Override String getColorizedContent(final String content, final String fileExtension, final String encoding); void setRendererMappings(final Map<String, Renderer> rendererMappings);  }
@Test public void testGetColorizedContent() throws Exception { final JHighlightColorer colorer = new JHighlightColorer(); colorer.setRendererMappings(getRendererMappings()); assertEquals("<span class=\"java_keyword\">public</span><span class=\"java_plain\">&#160;</span>" + "<span class=\"java_keyword\">class</span><span class=\"java_plain\">&#160;</span><span class=\"java_type\">" + "HelloWorld</span><span class=\"java_plain\"></span>", (colorer.getColorizedContent("public class HelloWorld", "java", ENCODING))); assertEquals("<span class=\"csharp_keyword\">private</span><span class=\"csharp_plain\">&#160;</span>" + "<span class=\"csharp_type\">void</span><span class=\"csharp_plain\">&#160;Build</span>" + "<span class=\"csharp_separator\">(</span><span class=\"csharp_plain\">XmlNode&#160;node</span>" + "<span class=\"csharp_separator\">)</span><span class=\"csharp_plain\"></span>", colorer.getColorizedContent("private void Build(XmlNode node)", "cs", ENCODING)); assertEquals("public class HelloWorld", (colorer.getColorizedContent("public class HelloWorld", "testing", ENCODING))); assertEquals("public class HelloWorld", (colorer.getColorizedContent("public class HelloWorld", "", ENCODING))); try { assertEquals("public class HelloWorld", (colorer.getColorizedContent("public class HelloWorld", null, ENCODING))); fail("If filename is null, IAE is expected."); } catch (IllegalArgumentException iae) { } assertEquals("", (colorer.getColorizedContent(null, null, ENCODING))); assertEquals("", colorer.getColorizedContent(null, "java", ENCODING)); }