From 77e8b31392c9ead3af12ac58e674c0e783949f8c Mon Sep 17 00:00:00 2001 From: Jered Ye <87924680+JeredYe@users.noreply.github.com> Date: Thu, 9 May 2024 22:29:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E9=83=A8=E5=88=86=E6=BC=8F?= =?UTF-8?q?=E6=8E=89=E7=9A=84java=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/src/common/Config.java | 71 ++++++++------- Common/src/common/Const.java | 2 + Common/src/common/LangSelector.java | 64 ++++++++++++- Common/src/common/LogLevel.java | 20 +++++ Common/src/common/Logger.java | 89 +++++++++++++++++++ .../persistence/oj_beans/SolutionBean.java | 12 +++ client/src/client/view/panel/CodePanel.java | 6 +- 7 files changed, 226 insertions(+), 38 deletions(-) create mode 100644 Common/src/common/LogLevel.java create mode 100644 Common/src/common/Logger.java diff --git a/Common/src/common/Config.java b/Common/src/common/Config.java index 66a1900..2bb577a 100644 --- a/Common/src/common/Config.java +++ b/Common/src/common/Config.java @@ -13,7 +13,7 @@ import java.util.List; import java.util.Map.Entry; import java.util.Properties; import log.Log; - +import java.nio.file.Paths; public class Config { public static int DEBUG; @@ -100,7 +100,12 @@ public class Config { //临时代码输出路径 若要改回配置文件读取 从const里获取字段 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) { // return System.getProperty("user.dir") + prop.getProperty("relativeExeDir"); // } 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) { String dir = null; //todo - if (language.equals("c") || language.equals("cpp") || language.equals("c++")) { - dir=LangSelector.getCompilerPath("C++", compiler); + dir=LangSelector.getCompilerPath(language,compiler); + //if (language.equals("c") || language.equals("cpp") || language.equals("c++")) { + // dir=LangSelector.getCompilerPath("C++", compiler); //dir = prop.getProperty(Const.MinGWDir); - } else if (language.equals("java")) { - dir=LangSelector.getCompilerPath("Java", compiler); + //} else if (language.equals("java")) { + // dir=LangSelector.getCompilerPath("Java", compiler); //dir = prop.getProperty(Const.JavaCompilerDir); - } + //} return dir; } - public static String getCompilerDir(String language) { - String dir = null; + //public static String getCompilerDir(String language) { + // String dir = null; //todo - if (language.equals("c") || language.equals("cpp") || language.equals("c++")) { - dir=LangSelector.getCompilerPath("C++", null); - //dir = prop.getProperty(Const.MinGWDir); - } else if (language.equals("java")) { - dir=LangSelector.getCompilerPath("Java", null); - //dir = prop.getProperty(Const.JavaCompilerDir); - } - return dir; - } + // if (language.equals("c") || language.equals("cpp") || language.equals("c++")) { + // dir=LangSelector.getCompilerPath("C++", null); + // //dir = prop.getProperty(Const.MinGWDir); + // } else if (language.equals("java")) { + // dir=LangSelector.getCompilerPath("Java", null); + // //dir = prop.getProperty(Const.JavaCompilerDir); + // } + // return dir; + // } public static String CompilerDir(String language,String compiler) { String dir = null; @@ -151,17 +158,17 @@ public class Config { } return dir; } - public static String CompilerDir(String language) { - String dir = null; - - if (language.equals("c") || language.equals("cpp") || language.equals("c++")) //返回各种语言的编译器地址 - { - //dir = System.getProperty("user.dir") + Const.MinGWDir; - dir=LangSelector.getCompilerPath("C++",null); - } else if (language.equals("java")) { - //dir = System.getProperty("user.dir") + Const.JavaCompilerDir; - dir=LangSelector.getCompilerPath("Java",null); - } - return dir; - } +// public static String CompilerDir(String language) { + // String dir = null; +// + // if (language.equals("c") || language.equals("cpp") || language.equals("c++")) //返回各种语言的编译器地址 + // { + // //dir = System.getProperty("user.dir") + Const.MinGWDir; + // dir = LangSelector.getCompilerPath("C++", compiler); + // } else if (language.equals("java")) { + // //dir = System.getProperty("user.dir") + Const.JavaCompilerDir; + // dir = LangSelector.getCompilerPath("Java", null); + // } + // return dir; + // } } \ No newline at end of file diff --git a/Common/src/common/Const.java b/Common/src/common/Const.java index 9ccb743..5b5d6f2 100644 --- a/Common/src/common/Const.java +++ b/Common/src/common/Const.java @@ -37,6 +37,8 @@ public class Const { return ".cpp"; } else if (language.equals("java")) { return ".java"; + }else if(language.equals("python")){ + return ".py"; } return null; } diff --git a/Common/src/common/LangSelector.java b/Common/src/common/LangSelector.java index 8f0f417..9819999 100644 --- a/Common/src/common/LangSelector.java +++ b/Common/src/common/LangSelector.java @@ -5,12 +5,14 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; @@ -24,12 +26,22 @@ import org.w3c.dom.NodeList; */ public abstract class LangSelector { // + private static MapcustomDefaultCompiler; + + 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 Document Data; public static boolean Loaded=false; private static XPath xpath = XPathFactory.newInstance().newXPath(); static { try { + customDefaultCompiler=new HashMap<>(); init(); Loaded=true; } catch (Exception e) { @@ -69,8 +81,48 @@ public abstract class LangSelector { public static String getConfigPath(){ 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){ + languageName=parseStandardLanguageName(languageName); + if(customDefaultCompiler.containsKey(languageName)) + return customDefaultCompiler.get(languageName); //if(languageName.charAt(0)>='a')languageName try{ String exp="/languages/language"+ "[@id='" +languageName+ "']"+"/compiler[1]/@name"; @@ -98,6 +150,8 @@ public abstract class LangSelector { return doc; } public static List getCompilerNames(String languageName){ + languageName=parseStandardLanguageName(languageName); + ArrayListarrayListStr=new ArrayList<>(); try { String exp="/languages/language"+ "[@id='" +languageName+ "']"+"//compiler"; @@ -117,6 +171,7 @@ public abstract class LangSelector { } public static String getCompilerPath(String languageName,String compilerName) { + languageName=parseStandardLanguageName(languageName); if(compilerName==null)compilerName=getDefaultCompilerName(languageName); try{ 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){ + languageName=parseStandardLanguageName(languageName); if(compilerName==null)compilerName=getDefaultCompilerName(languageName); try{ 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){ + languageName=parseStandardLanguageName(languageName); if(compilerName==null)compilerName=getDefaultCompilerName(languageName); try{ 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){ + languageName=parseStandardLanguageName(languageName); if(compilerName==null)compilerName=getDefaultCompilerName(languageName); try{ 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, HashMapmap){ + if(src==null||src.trim().isEmpty())return null; for (java.util.Map.Entry Entry : map.entrySet()) { src = src.replace(Entry.getKey(), Entry.getValue()); - System.out.println(Entry.getKey() + "已替换:" + Entry.getValue()); + //System.out.println(Entry.getKey() + "已替换:" + Entry.getValue()); } return src; } - } diff --git a/Common/src/common/LogLevel.java b/Common/src/common/LogLevel.java new file mode 100644 index 0000000..d043e9b --- /dev/null +++ b/Common/src/common/LogLevel.java @@ -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; + } +} \ No newline at end of file diff --git a/Common/src/common/Logger.java b/Common/src/common/Logger.java new file mode 100644 index 0000000..c325f6d --- /dev/null +++ b/Common/src/common/Logger.java @@ -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(); + } + } + } +} + diff --git a/Common/src/persistence/oj_beans/SolutionBean.java b/Common/src/persistence/oj_beans/SolutionBean.java index 49b902a..7ff4164 100644 --- a/Common/src/persistence/oj_beans/SolutionBean.java +++ b/Common/src/persistence/oj_beans/SolutionBean.java @@ -18,6 +18,9 @@ public class SolutionBean { Integer userId; Date submitTime; String language; + //NEW + String compiler; + // String SourceCode; Integer codelength; String status; @@ -27,6 +30,7 @@ public class SolutionBean { Float score; String remark; + public SolutionBean() { this.id = 1; this.examId = 1; @@ -34,6 +38,7 @@ public class SolutionBean { this.userId = 1; this.submitTime = new Date(); this.language = ""; + this.compiler=""; this.SourceCode = ""; this.codelength = 3; this.status = ""; @@ -43,6 +48,13 @@ public class SolutionBean { this.score = 1f; this.remark = ""; } + + public void setCompiler(String compiler){ + this.compiler=compiler; + } + public String getCompiler(){ + return compiler; + } public Integer getId() { return id; diff --git a/client/src/client/view/panel/CodePanel.java b/client/src/client/view/panel/CodePanel.java index f07f4a8..3824a65 100644 --- a/client/src/client/view/panel/CodePanel.java +++ b/client/src/client/view/panel/CodePanel.java @@ -1090,7 +1090,7 @@ public class CodePanel extends JPanel implements ActionListener { private boolean checkForCompile() { String tmp = null; 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")) { //ñ Object t = this.getParent(); @@ -1104,7 +1104,7 @@ public class CodePanel extends JPanel implements ActionListener { } } 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")) { //ñ Object t = this.getParent(); @@ -1119,7 +1119,7 @@ public class CodePanel extends JPanel implements ActionListener { } 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")) { //ñ Object t = this.getParent();