Example #1
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;
    }
  }
  /**
   * 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);
    }
  }