/** * Returns the duplicate source code fragment as formatted HTML string. * * @return the duplicate source code fragment */ public String getFormattedSourceCode() { try { JavaSource source = new JavaSourceParser().parse(new StringReader(sourceCode)); JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter(); StringWriter writer = new StringWriter(); JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault(); options.setShowLineNumbers(false); options.setAddLineAnchors(false); converter.convert(source, options, writer); return writer.toString(); } catch (IllegalConfigurationException exception) { return sourceCode; } catch (IOException exception) { return sourceCode; } }
/** * 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 * if the source code could not be read */ public final String highlightSource(final InputStream file) throws IOException { JavaSource source = new JavaSourceParser().parse( new InputStreamReader(file, EncodingValidator.defaultCharset(defaultEncoding))); 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(); }
/** * 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 * if the source code could not be read */ public final String highlightSource(final InputStream file) throws IOException { JavaSource source = new JavaSourceParser().parse( new InputStreamReader(file, EncodingValidator.defaultCharset(defaultEncoding))); 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(); }