使用Java Prettify for Android应用程序语法高亮显示

时间:2014-03-02 06:44:55

标签: java android syntax highlighting prettify

我需要将语法突出显示集成到我的Android应用程序中。我尝试使用Java Prettify(https://code.google.com/p/java-prettify),但我没有将其集成。该应用程序正在崩溃。由于我的avd不工作,所以我无法从logcat获取有关崩溃的信息。

我关注此信息:https://code.google.com/p/java-prettify/wiki/UseTheParserAlone

我的代码到现在为止:

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import prettify.PrettifyParser;
import syntaxhighlight.ParseResult;
import syntaxhighlight.Parser;

public class PrettifyHighlighter {
Map<String, String> COLORS = buildColorsMap();
String FONT_PATTERN = "<font color=\"#%s\">%s</font>";
Parser parser = new PrettifyParser();

public String highlight(String fileExtension, String sourceCode) {
    StringBuilder highlighted = new StringBuilder();
    List<ParseResult> results = parser.parse(fileExtension, sourceCode);
    for(ParseResult result : results){
        String type = result.getStyleKeys().get(0);
        String content = sourceCode.substring(result.getOffset(), result.getOffset() + result.getLength());
        highlighted.append(String.format(FONT_PATTERN, getColor(type), content));
    }
    return highlighted.toString();
}

private String getColor(String type){
    return COLORS.containsKey(type) ? COLORS.get(type) : COLORS.get("pln");
}

private static Map<String, String> buildColorsMap() {
    Map<String, String> map = new HashMap<String, String>();
    map.put("typ", "87cefa");
    map.put("kwd", "00ff00");
    map.put("lit", "ffff00");
    map.put("com", "999999");
    map.put("str", "ff4500");
    map.put("pun", "eeeeee");
    map.put("pln", "ffffff");
    return map;
}

}

调试时,我发现当我评论该行时:     解析器解析器= new PrettifyParser();

并且它是相关的行,没有崩溃的应用程序。 我用谷歌搜索但未能得到解决方案。

我的logcat:

03-02 07:57:12.300: E/AndroidRuntime(1245): FATAL EXCEPTION: main
03-02 07:57:12.300: E/AndroidRuntime(1245): Process: com.himanshujaju.codeit, PID: 1245
03-02 07:57:12.300: E/AndroidRuntime(1245): java.lang.NoClassDefFoundError: prettify.PrettifyParser
03-02 07:57:12.300: E/AndroidRuntime(1245):     at com.himanshujaju.codeit.PrettifyHighlighter.<init>(PrettifyHighlighter.java:14)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at com.himanshujaju.codeit.codeArea$Run.onPostExecute(codeArea.java:193)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at com.himanshujaju.codeit.codeArea$Run.onPostExecute(codeArea.java:1)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at android.os.AsyncTask.finish(AsyncTask.java:632)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at android.os.Looper.loop(Looper.java:137)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at java.lang.reflect.Method.invokeNative(Native Method)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at java.lang.reflect.Method.invoke(Method.java:515)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-02 07:57:12.300: E/AndroidRuntime(1245):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

我尝试了你的代码并且效果很好。

  PrettifyHighlighter highlighter = new PrettifyHighlighter();
  String code = "for (int i = 0; i < 5; i++){System.out.println(i);}";
  String highlighted = highlighter.highlight("java", code);
  textView1.setText(Html.fromHtml(highlighted));

以下是模拟器上的输出Textview

highlight

对于NoClassDefFoundError异常,看起来eclipse在构建时不会导出jar库。试试this answer