public static void highlightFile(String name, InputStream in, OutputStream out, String encoding) throws IOException { Renderer renderer = XhtmlRendererFactory.getRenderer(FileUtils.getExtension(name)); if (renderer == null) { renderer = XhtmlRendererFactory.getRenderer("java"); } renderer.highlight(name, in, out, encoding, false); }
/** * Highlight stream. * * @param name the name * @param input the input * @param rendererName the renderer name * @param encoding the encoding * @return the string * @throws IOException Signals that an I/O exception has occurred. */ public static String highlightStream( String name, InputStream input, String rendererName, String encoding) throws IOException { Renderer jspRenderer = XhtmlRendererFactory.getRenderer(rendererName); if (jspRenderer != null) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); jspRenderer.highlight(name, input, bos, encoding, true); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); Tokenizer tokenizer = new Tokenizer(new InputStreamReader(bis, encoding)); tokenizer.addSymbol(new TokenizerSymbol("EOL", "\n", null, false, false, true, false)); tokenizer.addSymbol(new TokenizerSymbol("EOL", "\r\n", null, false, false, true, false)); // // JHighlight adds HTML comment as the first line, so if // we number the lines we could end up with a line number and no line // to avoid that we just ignore the first line alltogether. // StringBuilder buffer = new StringBuilder(); long counter = 0; while (tokenizer.hasMore()) { Token tk = tokenizer.nextToken(); if ("EOL".equals(tk.getName())) { counter++; buffer.append(tk.getText()); } else if (counter > 0) { buffer.append("<span class=\"codeline\">"); buffer.append("<span class=\"linenum\">"); buffer.append(leftPad(Long.toString(counter), 6, " ")); buffer.append("</span>"); buffer.append(tk.getText()); buffer.append("</span>"); } } return buffer.toString(); } return null; }