/* (non-Javadoc) * @see de.jwic.sourceviewer.viewer.IObjectViewer#setNavigationElement(de.jwic.sourceviewer.model.NavigationElement) */ public void setNavigationElement(NavigationElement element) { if (element == null) { htmlCode = ""; return; // direct exit } if (false) { htmlCode = "test"; return; } FileElement fe = (FileElement) element; try { FileReader reader = new FileReader(fe.getFile()); JavaSource source = new JavaSourceParser().parse(reader); JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter(); StringWriter writer = new StringWriter(); JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault(); // options.setShowLineNumbers(true); converter.convert(source, options, writer); htmlCode = writer.toString(); reader.close(); } catch (Exception e) { log.error("Error creating HTML code", e); htmlCode = "Error creating html code: " + e; } }
// TODO Sep 13, 2004 (Markus Gebhard): Convert this into JDemo demos public static void main(String args[]) throws IOException { long time0 = System.currentTimeMillis(); JavaSource source = (new JavaSourceParser()).parse(new java.io.File("JavaSourceParser.java")); long time1 = System.currentTimeMillis(); JavaSource2TeXConverter conn1 = new JavaSource2TeXConverter(); conn1.convert(source, JavaSourceConversionOptions.getDefault(), new StringWriter()); long time2 = System.currentTimeMillis(); JavaSource2HTMLConverter conn2 = new JavaSource2HTMLConverter(); conn2.convert(source, JavaSourceConversionOptions.getDefault(), new StringWriter()); long time3 = System.currentTimeMillis(); System.out.println("Parse: " + (time1 - time0) + "ms"); System.out.println("toTeX: " + (time2 - time1) + "ms"); System.out.println("toHTML: " + (time3 - time2) + "ms"); }
/** * Highlights the specified source and returns the result as an HTML string. * * @param file the source file to highlight * @return the source as an HTML string * @throws IOException */ public final String highlightSource(final InputStream file) throws IOException { JavaSource source = new JavaSourceParser().parse(new InputStreamReader(file)); JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter(); StringWriter writer = new StringWriter(); JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault(); options.setShowLineNumbers(true); options.setAddLineAnchors(true); converter.convert(source, options, writer); return writer.toString(); }
private void loadSourceCode(String rawFile) { try { InputStream stream = this.getResources() .openRawResource(getResources().getIdentifier(rawFile, "raw", getPackageName())); byte[] buffer = new byte[stream.available()]; while (stream.read(buffer) != -1) ; stream.close(); JavaSource source = new JavaSourceParser().parse(new String(buffer)); StringWriter writer = new StringWriter(); JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter(); converter.convert(source, JavaSourceConversionOptions.getDefault(), writer); this.codeVisualizer.loadData(writer.toString(), "text/html", "UTF-8"); } catch (Exception e) { Log.e("Examples", "Could not read example source.", e); } }