上传部分漏掉的java文件

This commit is contained in:
Jered Ye 2024-05-09 22:29:56 +08:00
parent f515ccd590
commit 77e8b31392
7 changed files with 226 additions and 38 deletions

View File

@ -13,7 +13,7 @@ import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Properties; import java.util.Properties;
import log.Log; import log.Log;
import java.nio.file.Paths;
public class Config { public class Config {
public static int DEBUG; public static int DEBUG;
@ -100,7 +100,12 @@ public class Config {
//临时代码输出路径 若要改回配置文件读取 从const里获取字段 //临时代码输出路径 若要改回配置文件读取 从const里获取字段
public static String getSourcePath() { public static String getSourcePath() {
return "./test"; // String curDir = Paths.get(".").toAbsolutePath().normalize().toString();
return curDir+"/test";
}
public static String getLogPath(){
String curDir = Paths.get(".").toAbsolutePath().normalize().toString();
return curDir+"/log";
} }
//临时代码输出路径 //临时代码输出路径
@ -108,7 +113,8 @@ public class Config {
// if (isTestRelative) { // if (isTestRelative) {
// return System.getProperty("user.dir") + prop.getProperty("relativeExeDir"); // return System.getProperty("user.dir") + prop.getProperty("relativeExeDir");
// } else { // } else {
return "./test"; String curDir = Paths.get(".").toAbsolutePath().normalize().toString();
return curDir+"/test";
//} //}
} }
@ -116,28 +122,29 @@ public class Config {
public static String getCompilerDir(String language,String compiler) { public static String getCompilerDir(String language,String compiler) {
String dir = null; String dir = null;
//todo //todo
if (language.equals("c") || language.equals("cpp") || language.equals("c++")) { dir=LangSelector.getCompilerPath(language,compiler);
dir=LangSelector.getCompilerPath("C++", compiler); //if (language.equals("c") || language.equals("cpp") || language.equals("c++")) {
// dir=LangSelector.getCompilerPath("C++", compiler);
//dir = prop.getProperty(Const.MinGWDir); //dir = prop.getProperty(Const.MinGWDir);
} else if (language.equals("java")) { //} else if (language.equals("java")) {
dir=LangSelector.getCompilerPath("Java", compiler); // dir=LangSelector.getCompilerPath("Java", compiler);
//dir = prop.getProperty(Const.JavaCompilerDir); //dir = prop.getProperty(Const.JavaCompilerDir);
} //}
return dir; return dir;
} }
public static String getCompilerDir(String language) { //public static String getCompilerDir(String language) {
String dir = null; // String dir = null;
//todo //todo
if (language.equals("c") || language.equals("cpp") || language.equals("c++")) { // if (language.equals("c") || language.equals("cpp") || language.equals("c++")) {
dir=LangSelector.getCompilerPath("C++", null); // dir=LangSelector.getCompilerPath("C++", null);
//dir = prop.getProperty(Const.MinGWDir); // //dir = prop.getProperty(Const.MinGWDir);
} else if (language.equals("java")) { // } else if (language.equals("java")) {
dir=LangSelector.getCompilerPath("Java", null); // dir=LangSelector.getCompilerPath("Java", null);
//dir = prop.getProperty(Const.JavaCompilerDir); // //dir = prop.getProperty(Const.JavaCompilerDir);
} // }
return dir; // return dir;
} // }
public static String CompilerDir(String language,String compiler) { public static String CompilerDir(String language,String compiler) {
String dir = null; String dir = null;
@ -151,17 +158,17 @@ public class Config {
} }
return dir; return dir;
} }
public static String CompilerDir(String language) { // public static String CompilerDir(String language) {
String dir = null; // String dir = null;
//
if (language.equals("c") || language.equals("cpp") || language.equals("c++")) //返回各种语言的编译器地址 // if (language.equals("c") || language.equals("cpp") || language.equals("c++")) //返回各种语言的编译器地址
{ // {
//dir = System.getProperty("user.dir") + Const.MinGWDir; // //dir = System.getProperty("user.dir") + Const.MinGWDir;
dir=LangSelector.getCompilerPath("C++",null); // dir = LangSelector.getCompilerPath("C++", compiler);
} else if (language.equals("java")) { // } else if (language.equals("java")) {
//dir = System.getProperty("user.dir") + Const.JavaCompilerDir; // //dir = System.getProperty("user.dir") + Const.JavaCompilerDir;
dir=LangSelector.getCompilerPath("Java",null); // dir = LangSelector.getCompilerPath("Java", null);
} // }
return dir; // return dir;
} // }
} }

View File

@ -37,6 +37,8 @@ public class Const {
return ".cpp"; return ".cpp";
} else if (language.equals("java")) { } else if (language.equals("java")) {
return ".java"; return ".java";
}else if(language.equals("python")){
return ".py";
} }
return null; return null;
} }

View File

@ -5,12 +5,14 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.xml.xpath.XPath; import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory; import javax.xml.xpath.XPathFactory;
@ -24,12 +26,22 @@ import org.w3c.dom.NodeList;
*/ */
public abstract class LangSelector { public abstract class LangSelector {
// //
private static Map<String,String>customDefaultCompiler;
public static String getDefaultCompiler(String language) {
return customDefaultCompiler.get(language);
}
public static void setDefaultCompiler(String language,String compiler) {
customDefaultCompiler.put(language, compiler);
}
private static String ConfigPath=""; private static String ConfigPath="";
private static Document Data; private static Document Data;
public static boolean Loaded=false; public static boolean Loaded=false;
private static XPath xpath = XPathFactory.newInstance().newXPath(); private static XPath xpath = XPathFactory.newInstance().newXPath();
static { static {
try { try {
customDefaultCompiler=new HashMap<>();
init(); init();
Loaded=true; Loaded=true;
} catch (Exception e) { } catch (Exception e) {
@ -69,8 +81,48 @@ public abstract class LangSelector {
public static String getConfigPath(){ public static String getConfigPath(){
return ConfigPath; return ConfigPath;
} }
public static String parseStandardLanguageName(String languageName) {
try {
// 创建 XPath 对象
XPath xpath = XPathFactory.newInstance().newXPath();
// 编译 XPath 表达式
XPathExpression expr = xpath.compile("//language");
// 评估 XPath 表达式
Object result = expr.evaluate(Data, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
// 遍历每个语言节点
for (int i = 0; i < nodes.getLength(); i++) {
String id = nodes.item(i).getAttributes().getNamedItem("id").getNodeValue();
String aliasString = nodes.item(i).getAttributes().getNamedItem("alias").getNodeValue();
String[] aliases = aliasString.split(",");
// 检查 languageName 是否与 ID 匹配
if (id.equalsIgnoreCase(languageName)) {
return id;
}
// 检查 languageName 是否与别名匹配
for (String alias : aliases) {
if (alias.equalsIgnoreCase(languageName)) {
return id;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
// 如果没有匹配项则返回原始 languageName
return languageName;
}
public static String getDefaultCompilerName(String languageName){ public static String getDefaultCompilerName(String languageName){
languageName=parseStandardLanguageName(languageName);
if(customDefaultCompiler.containsKey(languageName))
return customDefaultCompiler.get(languageName);
//if(languageName.charAt(0)>='a')languageName //if(languageName.charAt(0)>='a')languageName
try{ try{
String exp="/languages/language"+ "[@id='" +languageName+ "']"+"/compiler[1]/@name"; String exp="/languages/language"+ "[@id='" +languageName+ "']"+"/compiler[1]/@name";
@ -98,6 +150,8 @@ public abstract class LangSelector {
return doc; return doc;
} }
public static List<String> getCompilerNames(String languageName){ public static List<String> getCompilerNames(String languageName){
languageName=parseStandardLanguageName(languageName);
ArrayList<String>arrayListStr=new ArrayList<>(); ArrayList<String>arrayListStr=new ArrayList<>();
try { try {
String exp="/languages/language"+ "[@id='" +languageName+ "']"+"//compiler"; String exp="/languages/language"+ "[@id='" +languageName+ "']"+"//compiler";
@ -117,6 +171,7 @@ public abstract class LangSelector {
} }
public static String getCompilerPath(String languageName,String compilerName) { public static String getCompilerPath(String languageName,String compilerName) {
languageName=parseStandardLanguageName(languageName);
if(compilerName==null)compilerName=getDefaultCompilerName(languageName); if(compilerName==null)compilerName=getDefaultCompilerName(languageName);
try{ try{
String exp="/languages/language"+ "[@id='" +languageName+ "']"+"//compiler[@name='"+compilerName+"']//path/text()"; String exp="/languages/language"+ "[@id='" +languageName+ "']"+"//compiler[@name='"+compilerName+"']//path/text()";
@ -127,6 +182,7 @@ public abstract class LangSelector {
} }
} }
public static String getCompileCommand(String languageName,String compilerName){ public static String getCompileCommand(String languageName,String compilerName){
languageName=parseStandardLanguageName(languageName);
if(compilerName==null)compilerName=getDefaultCompilerName(languageName); if(compilerName==null)compilerName=getDefaultCompilerName(languageName);
try{ try{
String exp="/languages/language"+ "[@id='" +languageName+ "']"+"//compiler[@name='"+compilerName+"']//compileCmd/text()"; String exp="/languages/language"+ "[@id='" +languageName+ "']"+"//compiler[@name='"+compilerName+"']//compileCmd/text()";
@ -137,6 +193,7 @@ public abstract class LangSelector {
} }
} }
public static String getLinkCommand(String languageName,String compilerName){ public static String getLinkCommand(String languageName,String compilerName){
languageName=parseStandardLanguageName(languageName);
if(compilerName==null)compilerName=getDefaultCompilerName(languageName); if(compilerName==null)compilerName=getDefaultCompilerName(languageName);
try{ try{
String exp="/languages/language"+ "[@id='" +languageName+ "']"+"//compiler[@name='"+compilerName+"']//linkCmd/text()"; String exp="/languages/language"+ "[@id='" +languageName+ "']"+"//compiler[@name='"+compilerName+"']//linkCmd/text()";
@ -147,6 +204,7 @@ public abstract class LangSelector {
} }
} }
public static String getRunCommand(String languageName,String compilerName){ public static String getRunCommand(String languageName,String compilerName){
languageName=parseStandardLanguageName(languageName);
if(compilerName==null)compilerName=getDefaultCompilerName(languageName); if(compilerName==null)compilerName=getDefaultCompilerName(languageName);
try{ try{
String exp="/languages/language"+ "[@id='" +languageName+ "']"+"//compiler[@name='"+compilerName+"']//runCmd/text()"; String exp="/languages/language"+ "[@id='" +languageName+ "']"+"//compiler[@name='"+compilerName+"']//runCmd/text()";
@ -171,12 +229,12 @@ public abstract class LangSelector {
} }
} }
public static String matchPlaceHolder(String src, HashMap<String,String>map){ public static String matchPlaceHolder(String src, HashMap<String,String>map){
if(src==null||src.trim().isEmpty())return null;
for (java.util.Map.Entry<String, String> Entry : map.entrySet()) { for (java.util.Map.Entry<String, String> Entry : map.entrySet()) {
src = src.replace(Entry.getKey(), Entry.getValue()); src = src.replace(Entry.getKey(), Entry.getValue());
System.out.println(Entry.getKey() + "已替换:" + Entry.getValue()); //System.out.println(Entry.getKey() + "已替换:" + Entry.getValue());
} }
return src; return src;
} }
} }

View File

@ -0,0 +1,20 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package common;
public enum LogLevel{
INFO("INFO"),
WARNING("WARNING"),
ERROR("ERROR");
private final String info;
private LogLevel(String info){
this.info=info;
}
public String getString(){
return info;
}
}

View File

@ -0,0 +1,89 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package common;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Logger {
private static Logger instance;
private PrintWriter writer;
private String logFileName;
private static final String LOG_FILE_PREFIX = "log";
private static final String LOG_FILE_SUFFIX = ".txt";
private static final long HOUR_IN_MILLISECONDS = 3600000L; // 1 hour in milliseconds
private static String Logdir = Config.getLogPath();
private Logger() {
createLogFile();
startFileUpdateThread();
}
public static synchronized Logger getInstance() {
if (instance == null) {
instance = new Logger();
}
return instance;
}
private void createLogFile() {
LocalDateTime now = LocalDateTime.now();
logFileName = Logdir+File.separator+LOG_FILE_PREFIX + "_" + now.format(DateTimeFormatter.ofPattern("yyyyMMddHH")) + LOG_FILE_SUFFIX;
System.out.println("日志文件"+logFileName+"已创建!");
try {
writer = new PrintWriter(new FileWriter(logFileName, true));
} catch (IOException e) {
e.printStackTrace();
}
}
private void startFileUpdateThread() {
Thread fileUpdateThread = new Thread(() -> {
while (true) {
try {
Thread.sleep(HOUR_IN_MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
updateLogFile();
}
});
fileUpdateThread.setDaemon(true);
fileUpdateThread.start();
}
private void updateLogFile() {
writer.close(); // Close the current log file
createLogFile(); // Create a new log file
}
public void log(String msg, LogLevel level) {
String txt = getTimeStamp() + "[" + level.getString() + "]" + msg;
System.out.println(txt);
writer.println(txt);
writer.flush();
}
private String getTimeStamp() {
LocalDateTime now = LocalDateTime.now();
return now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
public static void main(String[] args) {
Logger logger = Logger.getInstance();
for (int i = 0; i < 10; i++) {
logger.log("Log message " + i, LogLevel.INFO);
try {
Thread.sleep(50); // Sleep for 5 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

View File

@ -18,6 +18,9 @@ public class SolutionBean {
Integer userId; Integer userId;
Date submitTime; Date submitTime;
String language; String language;
//NEW
String compiler;
//
String SourceCode; String SourceCode;
Integer codelength; Integer codelength;
String status; String status;
@ -27,6 +30,7 @@ public class SolutionBean {
Float score; Float score;
String remark; String remark;
public SolutionBean() { public SolutionBean() {
this.id = 1; this.id = 1;
this.examId = 1; this.examId = 1;
@ -34,6 +38,7 @@ public class SolutionBean {
this.userId = 1; this.userId = 1;
this.submitTime = new Date(); this.submitTime = new Date();
this.language = ""; this.language = "";
this.compiler="";
this.SourceCode = ""; this.SourceCode = "";
this.codelength = 3; this.codelength = 3;
this.status = ""; this.status = "";
@ -43,6 +48,13 @@ public class SolutionBean {
this.score = 1f; this.score = 1f;
this.remark = ""; this.remark = "";
} }
public void setCompiler(String compiler){
this.compiler=compiler;
}
public String getCompiler(){
return compiler;
}
public Integer getId() { public Integer getId() {
return id; return id;

View File

@ -1090,7 +1090,7 @@ public class CodePanel extends JPanel implements ActionListener {
private boolean checkForCompile() { private boolean checkForCompile() {
String tmp = null; String tmp = null;
if (language.toLowerCase().equals("c") || language.toLowerCase().equals("cpp") || language.toLowerCase().equals("c++")) { if (language.toLowerCase().equals("c") || language.toLowerCase().equals("cpp") || language.toLowerCase().equals("c++")) {
tmp = Config.getCompilerDir("c"); tmp = Config.getCompilerDir("c",null);
if (tmp == null || "".equals(tmp) || !FileFinder.isExistFile(tmp + File.separator + "gcc.exe") || !FileFinder.isExistFile(tmp + File.separator + "g++.exe")) { if (tmp == null || "".equals(tmp) || !FileFinder.isExistFile(tmp + File.separator + "gcc.exe") || !FileFinder.isExistFile(tmp + File.separator + "g++.exe")) {
//µ¯´°ÉèÖñ£´æ //µ¯´°ÉèÖñ£´æ
Object t = this.getParent(); Object t = this.getParent();
@ -1104,7 +1104,7 @@ public class CodePanel extends JPanel implements ActionListener {
} }
} }
if (language.toLowerCase().equals("java")) { if (language.toLowerCase().equals("java")) {
tmp = Config.getCompilerDir("java"); tmp = Config.getCompilerDir("java",null);
if (tmp == null || "".equals(tmp) || !FileFinder.isExistFile(tmp + File.separator + "javac.exe")) { if (tmp == null || "".equals(tmp) || !FileFinder.isExistFile(tmp + File.separator + "javac.exe")) {
//µ¯´°ÉèÖñ£´æ //µ¯´°ÉèÖñ£´æ
Object t = this.getParent(); Object t = this.getParent();
@ -1119,7 +1119,7 @@ public class CodePanel extends JPanel implements ActionListener {
} }
if (language.toLowerCase().equals("python")) { if (language.toLowerCase().equals("python")) {
tmp = Config.getCompilerDir("python"); tmp = Config.getCompilerDir("python",null);
if (tmp == null || "".equals(tmp) || !FileFinder.isExistFile(tmp + File.separator + "javac.exe")) { if (tmp == null || "".equals(tmp) || !FileFinder.isExistFile(tmp + File.separator + "javac.exe")) {
//µ¯´°ÉèÖñ£´æ //µ¯´°ÉèÖñ£´æ
Object t = this.getParent(); Object t = this.getParent();