/**
   * 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();
  }
Example #2
0
  /* (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");
  }
  /**
   * Converts the parsed source code to HTML by adding color information, adding line breaks and
   * replacing characters as needed for HTML. Also adds a table with line numbers etc.
   */
  public void convert(JavaSource source, JavaSourceConversionOptions options, BufferedWriter writer)
      throws IOException {
    if (source == null) {
      throw new IllegalStateException(
          "Trying to write out converted code without having source set.");
    }

    writer.write(BLOCK_HEADER);

    // 1) Header with filename if available
    if (options.isShowFileName() && source.getFileName() != null) {
      // TODO: Pretty print file name
    }

    writer.write("{");
    writer.newLine();
    writer.write("\\noindent \\ttfamily");
    writer.newLine();

    int lineCount = source.getLineCount();
    int lineNumber = 1;
    JavaSourceIterator iterator = source.getIterator();
    while (iterator.hasNext()) {
      JavaSourceRun run = iterator.getNext();
      if (run.isAtStartOfLine() && options.isShowLineNumbers()) {
        writeLineNumber(writer, lineNumber++, lineCount);
      }
      toTeX(run, writer);
      if (run.isAtEndOfLine()) {
        writer.write("\\\\");
        writer.newLine();
      }
    }

    writer.newLine();
    writer.write("}");
    writer.newLine();
  }
  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);
    }
  }
 public String getDocumentHeader(JavaSourceConversionOptions options, String title) {
   return DOCUMENT_HEADER + createFormatDefinition(options.getStyleTable());
 }