Better Language detection

This commit is contained in:
Nico Mexis 2021-06-22 12:45:39 +02:00
parent 93b60b8490
commit 87fc6c8435
No known key found for this signature in database
GPG key ID: 27D6E17CE092AB78
2 changed files with 77 additions and 67 deletions

View file

@ -1,6 +1,5 @@
package the.bytecode.club.bytecodeviewer.gui; package the.bytecode.club.bytecodeviewer.gui;
import the.bytecode.club.bytecodeviewer.gui.hexviewer.JHexEditor;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Color; import java.awt.Color;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
@ -22,12 +21,13 @@ import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter; import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent; import javax.swing.text.JTextComponent;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rtextarea.RTextScrollPane; import org.fife.ui.rtextarea.RTextScrollPane;
import org.imgscalr.Scalr; import org.imgscalr.Scalr;
import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.Resources; import the.bytecode.club.bytecodeviewer.Resources;
import the.bytecode.club.bytecodeviewer.gui.hexviewer.JHexEditor;
import the.bytecode.club.bytecodeviewer.util.FileContainer; import the.bytecode.club.bytecodeviewer.util.FileContainer;
import the.bytecode.club.bytecodeviewer.util.Language;
/*************************************************************************** /***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite * * Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
@ -127,71 +127,8 @@ public class FileViewer extends Viewer {
} }
} }
if (name.endsWith(".xml") || contentsS.startsWith("<?xml") || contentsS.startsWith(("<xml"))) { panelArea.setSyntaxEditingStyle(Language.detectLanguage(name, contentsS).getSyntaxConstant());
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_XML);
panelArea.setText(contentsS); panelArea.setText(contentsS);
} else if (name.endsWith(".py") || name.endsWith(".python")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_PYTHON);
panelArea.setText(contentsS);
} else if (name.endsWith(".rb") || name.endsWith(".ruby")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_RUBY);
panelArea.setText(contentsS);
} else if (name.endsWith(".java")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
panelArea.setText(contentsS);
} else if (name.endsWith(".html")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_HTML);
panelArea.setText(contentsS);
} else if (name.endsWith(".css")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_CSS);
panelArea.setText(contentsS);
} else if (name.endsWith(".properties") || name.endsWith(".mf") || name.endsWith(".sf")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_PROPERTIES_FILE);
panelArea.setText(contentsS);
} else if (name.endsWith(".php") || contentsS.startsWith("<?php")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_PHP);
panelArea.setText(contentsS);
} else if (name.endsWith(".js")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVASCRIPT);
panelArea.setText(contentsS);
} else if (name.endsWith(".bat")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_WINDOWS_BATCH);
panelArea.setText(contentsS);
} else if (name.endsWith(".sh")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_UNIX_SHELL);
panelArea.setText(contentsS);
} else if (name.endsWith(".c")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_C);
panelArea.setText(contentsS);
} else if (name.endsWith(".cpp")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_CPLUSPLUS);
panelArea.setText(contentsS);
} else if (name.endsWith(".scala")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SCALA);
panelArea.setText(contentsS);
} else if (name.endsWith(".clojure")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_CLOJURE);
panelArea.setText(contentsS);
} else if (name.endsWith(".groovy")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_GROOVY);
panelArea.setText(contentsS);
} else if (name.endsWith(".lua")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_LUA);
panelArea.setText(contentsS);
} else if (name.endsWith(".sql")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SQL);
panelArea.setText(contentsS);
} else if (name.endsWith(".json")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JSON);
panelArea.setText(contentsS);
} else if (name.endsWith(".jsp")) {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JSP);
panelArea.setText(contentsS);
} else {
panelArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE);
panelArea.setText(contentsS);
}
panelArea.setCaretPosition(0); panelArea.setCaretPosition(0);
scrollPane.setColumnHeaderView(panel); scrollPane.setColumnHeaderView(panel);
panel2.add(scrollPane); panel2.add(scrollPane);

View file

@ -0,0 +1,73 @@
package the.bytecode.club.bytecodeviewer.util;
import java.util.function.BiFunction;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
public enum Language {
XML(SyntaxConstants.SYNTAX_STYLE_XML, (n, c) -> n.endsWith(".xml")
|| c.startsWith("<?xml") || c.startsWith("<xml")),
PYTHON(SyntaxConstants.SYNTAX_STYLE_PYTHON, (n, c) -> n.endsWith(".py") || n.endsWith(".python")),
RUBY(SyntaxConstants.SYNTAX_STYLE_RUBY, (n, c) -> n.endsWith(".rb") || n.endsWith(".ruby")),
JAVA(SyntaxConstants.SYNTAX_STYLE_JAVA, (n, c) -> n.endsWith(".java")),
HTML(SyntaxConstants.SYNTAX_STYLE_HTML, (n, c) -> n.endsWith(".html")),
CSS(SyntaxConstants.SYNTAX_STYLE_CSS, (n, c) -> n.endsWith(".css")),
PROPERTIES(SyntaxConstants.SYNTAX_STYLE_PROPERTIES_FILE, (n, c) -> n.endsWith(".properties")
|| n.endsWith(".mf") || n.endsWith(".sf")),
PHP(SyntaxConstants.SYNTAX_STYLE_PHP, (n, c) -> n.endsWith(".php") || c.startsWith("<?php")),
JS(SyntaxConstants.SYNTAX_STYLE_JAVASCRIPT, (n, c) -> n.endsWith(".js")),
BATCH(SyntaxConstants.SYNTAX_STYLE_WINDOWS_BATCH, (n, c) -> n.endsWith(".bat")),
SHELL(SyntaxConstants.SYNTAX_STYLE_UNIX_SHELL, (n, c) -> n.endsWith(".sh")),
C(SyntaxConstants.SYNTAX_STYLE_C, (n, c) -> n.endsWith(".c") || n.endsWith(".h")),
CPP(SyntaxConstants.SYNTAX_STYLE_CPLUSPLUS, (n, c) -> n.endsWith(".cpp") || n.endsWith(".hpp")),
SCALA(SyntaxConstants.SYNTAX_STYLE_SCALA, (n, c) -> n.endsWith(".scala")),
CLOJURE(SyntaxConstants.SYNTAX_STYLE_CLOJURE, (n, c) -> n.endsWith(".clojure")),
GROOVY(SyntaxConstants.SYNTAX_STYLE_GROOVY, (n, c) -> n.endsWith(".groovy") || n.endsWith(".gradle")),
LUA(SyntaxConstants.SYNTAX_STYLE_LUA, (n, c) -> n.endsWith(".lua")),
SQL(SyntaxConstants.SYNTAX_STYLE_SQL, (n, c) -> n.endsWith(".sql")),
JSON(SyntaxConstants.SYNTAX_STYLE_JSON, (n, c) -> n.endsWith(".json")),
JSP(SyntaxConstants.SYNTAX_STYLE_JSP, (n, c) -> n.endsWith(".jsp")),
YAML(SyntaxConstants.SYNTAX_STYLE_YAML, (n, c) -> n.endsWith(".yml") || n.endsWith(".yaml")),
CS(SyntaxConstants.SYNTAX_STYLE_CSHARP, (n, c) -> n.endsWith(".cs")),
CSV(SyntaxConstants.SYNTAX_STYLE_CSV, (n, c) -> n.endsWith(".csv")),
DOCKER(SyntaxConstants.SYNTAX_STYLE_DOCKERFILE, (n, c) -> n.endsWith(".dockerfile")),
DART(SyntaxConstants.SYNTAX_STYLE_DART, (n, c) -> n.endsWith(".dart")),
GO(SyntaxConstants.SYNTAX_STYLE_GO, (n, c) -> n.endsWith(".go")),
HTACCESS(SyntaxConstants.SYNTAX_STYLE_HTACCESS, (n, c) -> n.endsWith(".htaccess")),
INI(SyntaxConstants.SYNTAX_STYLE_INI, (n, c) -> n.endsWith(".ini")),
KOTLIN(SyntaxConstants.SYNTAX_STYLE_KOTLIN, (n, c) -> n.endsWith(".kt") || n.endsWith(".kts")),
LATEX(SyntaxConstants.SYNTAX_STYLE_LATEX, (n, c) -> n.endsWith(".tex")),
MARKDOWN(SyntaxConstants.SYNTAX_STYLE_MARKDOWN, (n, c) -> n.endsWith(".md")),
PERL(SyntaxConstants.SYNTAX_STYLE_PERL, (n, c) -> n.endsWith(".pl")),
TYPESCRIPT(SyntaxConstants.SYNTAX_STYLE_TYPESCRIPT, (n, c) -> n.endsWith(".ts")),
NONE(SyntaxConstants.SYNTAX_STYLE_NONE, (n, c) -> false);
public static final Language[] VALUES = values();
private final BiFunction<String, String, Boolean> criteria;
private final String syntaxConstant;
Language(String syntaxConstant, BiFunction<String, String, Boolean> criteria) {
this.criteria = criteria;
this.syntaxConstant = syntaxConstant;
}
public boolean isLanguage(String fileName, String content) {
return criteria.apply(fileName, content);
}
public String getSyntaxConstant() {
return syntaxConstant;
}
public static Language detectLanguage(String fileName, String content) {
for (Language lang : VALUES) {
if (lang.isLanguage(fileName, content)) {
return lang;
}
}
return NONE;
}
}