use of top.hcode.hoj.pojo.entity.problem.Language in project HOJ by HimitZH.
the class ImportFpsController method parseFps.
private List<ProblemDto> parseFps(InputStream inputStream, String username) throws IOException {
Document document = XmlUtil.readXML(inputStream);
Element rootElement = XmlUtil.getRootElement(document);
String version = rootElement.getAttribute("version");
List<ProblemDto> problemDtoList = new ArrayList<>();
String fileDirId = IdUtil.simpleUUID();
String fileDir = Constants.File.TESTCASE_TMP_FOLDER.getPath() + File.separator + fileDirId;
int index = 1;
for (Element item : XmlUtil.getElements(rootElement, "item")) {
Problem problem = new Problem();
problem.setAuthor(username).setType(0).setIsUploadCase(true).setDifficulty(1).setIsRemoveEndBlank(true).setOpenCaseResult(true).setCodeShare(false).setIsRemote(false).setAuth(1).setProblemId(String.valueOf(System.currentTimeMillis()));
Element title = XmlUtil.getElement(item, "title");
// 标题
problem.setTitle(title.getTextContent());
HashMap<String, String> srcMapUrl = new HashMap<>();
List<Element> images = XmlUtil.getElements(item, "img");
for (Element img : images) {
Element srcElement = XmlUtil.getElement(img, "src");
if (srcElement == null) {
continue;
}
String src = srcElement.getTextContent();
String base64 = XmlUtil.getElement(img, "base64").getTextContent();
String[] split = src.split("\\.");
byte[] decode = Base64.getDecoder().decode(base64);
String fileName = IdUtil.fastSimpleUUID() + "." + split[split.length - 1];
FileUtil.writeBytes(decode, Constants.File.MARKDOWN_FILE_FOLDER.getPath() + File.separator + fileName);
srcMapUrl.put(src, Constants.File.IMG_API.getPath() + fileName);
}
Element descriptionElement = XmlUtil.getElement(item, "description");
String description = descriptionElement.getTextContent();
for (Map.Entry<String, String> entry : srcMapUrl.entrySet()) {
description = description.replaceAll(entry.getKey(), entry.getValue());
}
// 题目描述
problem.setDescription(description);
Element inputElement = XmlUtil.getElement(item, "input");
String input = inputElement.getTextContent();
for (Map.Entry<String, String> entry : srcMapUrl.entrySet()) {
input = input.replaceAll(entry.getKey(), entry.getValue());
}
// 输入描述
problem.setInput(input);
Element outputElement = XmlUtil.getElement(item, "output");
String output = outputElement.getTextContent();
for (Map.Entry<String, String> entry : srcMapUrl.entrySet()) {
output = output.replaceAll(entry.getKey(), entry.getValue());
}
// 输出描述
problem.setOutput(output);
// 提示
Element hintElement = XmlUtil.getElement(item, "hint");
String hint = hintElement.getTextContent();
for (Map.Entry<String, String> entry : srcMapUrl.entrySet()) {
hint = hint.replaceAll(entry.getKey(), entry.getValue());
}
problem.setHint(hint);
// 来源
Element sourceElement = XmlUtil.getElement(item, "source");
String source = sourceElement.getTextContent();
problem.setSource(source);
// ms
Integer timeLimit = getTimeLimit(version, item);
problem.setTimeLimit(timeLimit);
// mb
Integer memoryLimit = getMemoryLimit(item);
problem.setMemoryLimit(memoryLimit);
// 题面用例
List<Element> sampleInputs = XmlUtil.getElements(item, "sample_input");
List<Element> sampleOutputs = XmlUtil.getElements(item, "sample_output");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < sampleInputs.size(); i++) {
sb.append("<input>").append(sampleInputs.get(i).getTextContent()).append("</input>");
sb.append("<output>").append(sampleOutputs.get(i).getTextContent()).append("</output>");
}
problem.setExamples(sb.toString());
QueryWrapper<Language> languageQueryWrapper = new QueryWrapper<>();
languageQueryWrapper.eq("oj", "ME");
List<Language> languageList = languageService.list(languageQueryWrapper);
HashMap<String, Long> languageMap = new HashMap<>();
for (Language language : languageList) {
languageMap.put(language.getName(), language.getId());
}
// 题目模板
List<Element> templateNodes = XmlUtil.getElements(item, "template");
List<CodeTemplate> codeTemplates = new ArrayList<>();
for (Element templateNode : templateNodes) {
String templateLanguage = templateNode.getAttribute("language");
String templateCode = templateNode.getTextContent();
if (templateLanguage == null || templateCode == null) {
continue;
}
String lang = fpsMapHOJ.get(templateLanguage);
if (lang != null) {
codeTemplates.add(new CodeTemplate().setCode(templateCode).setLid(languageMap.get(lang)));
}
}
// spj
Element spjNode = XmlUtil.getElement(item, "spj");
if (spjNode != null) {
String spjLanguage = spjNode.getAttribute("language");
String spjCode = spjNode.getTextContent();
if (("C".equals(spjLanguage) || "C++".equals(spjLanguage)) && !StringUtils.isEmpty(spjCode)) {
problem.setSpjLanguage(spjLanguage).setSpjCode(spjCode);
}
}
// 题目评测数据
List<Element> testInputs = XmlUtil.getElements(item, "test_input");
List<Element> testOutputs = XmlUtil.getElements(item, "test_output");
List<ProblemCase> problemSamples = new LinkedList<>();
String problemTestCaseDir = fileDir + File.separator + index;
for (int i = 0; i < testInputs.size(); i++) {
String infileName = (i + 1) + ".in";
String outfileName = (i + 1) + ".out";
FileWriter infileWriter = new FileWriter(problemTestCaseDir + File.separator + infileName);
FileWriter outfileWriter = new FileWriter(problemTestCaseDir + File.separator + outfileName);
infileWriter.write(testInputs.get(i).getTextContent());
outfileWriter.write(testOutputs.get(i).getTextContent());
problemSamples.add(new ProblemCase().setInput(infileName).setOutput(outfileName));
}
String mode = Constants.JudgeMode.DEFAULT.getMode();
if (problem.getSpjLanguage() != null) {
mode = Constants.JudgeMode.SPJ.getMode();
}
ProblemDto problemDto = new ProblemDto();
problemDto.setSamples(problemSamples).setIsUploadTestCase(true).setUploadTestcaseDir(problemTestCaseDir).setLanguages(languageList).setTags(null).setJudgeMode(mode).setProblem(problem).setCodeTemplates(codeTemplates);
problemDtoList.add(problemDto);
index++;
}
return problemDtoList;
}
use of top.hcode.hoj.pojo.entity.problem.Language in project HOJ by HimitZH.
the class ImportQDUOJController method importQDOJProblem.
/**
* @param file
* @MethodName importQDOJProblem
* @Description zip文件导入题目 仅超级管理员可操作
* @Return
* @Since 2021/5/27
*/
@RequiresRoles("root")
@RequiresAuthentication
@ResponseBody
@Transactional(rollbackFor = Exception.class)
@PostMapping("/import-qdoj-problem")
public CommonResult importQDOJProblem(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
if (!"zip".toUpperCase().contains(suffix.toUpperCase())) {
return CommonResult.errorResponse("请上传zip格式的题目文件压缩包!");
}
String fileDirId = IdUtil.simpleUUID();
String fileDir = Constants.File.TESTCASE_TMP_FOLDER.getPath() + File.separator + fileDirId;
String filePath = fileDir + File.separator + file.getOriginalFilename();
// 文件夹不存在就新建
FileUtil.mkdir(fileDir);
try {
file.transferTo(new File(filePath));
} catch (IOException e) {
FileUtil.del(fileDir);
return CommonResult.errorResponse("服务器异常:qduoj题目上传失败!");
}
// 将压缩包压缩到指定文件夹
ZipUtil.unzip(filePath, fileDir);
// 删除zip文件
FileUtil.del(filePath);
// 检查文件是否存在
File testCaseFileList = new File(fileDir);
File[] files = testCaseFileList.listFiles();
if (files == null || files.length == 0) {
FileUtil.del(fileDir);
return CommonResult.errorResponse("评测数据压缩包里文件不能为空!");
}
HashMap<String, File> problemInfo = new HashMap<>();
for (File tmp : files) {
if (tmp.isDirectory()) {
File[] problemAndTestcase = tmp.listFiles();
if (problemAndTestcase == null || problemAndTestcase.length == 0) {
FileUtil.del(fileDir);
return CommonResult.errorResponse("编号为:" + tmp.getName() + "的文件夹为空!");
}
for (File problemFile : problemAndTestcase) {
if (problemFile.isFile()) {
// 检查文件是否时json文件
if (!problemFile.getName().endsWith("json")) {
FileUtil.del(fileDir);
return CommonResult.errorResponse("编号为:" + tmp.getName() + "的文件夹里面的题目数据格式错误,请使用json文件!");
}
problemInfo.put(tmp.getName(), problemFile);
}
}
}
}
// 读取json文件生成对象
HashMap<String, QDOJProblemDto> problemVoMap = new HashMap<>();
for (String key : problemInfo.keySet()) {
try {
FileReader fileReader = new FileReader(problemInfo.get(key));
JSONObject problemJson = JSONUtil.parseObj(fileReader.readString());
QDOJProblemDto qdojProblemDto = QDOJProblemToProblemVo(problemJson);
problemVoMap.put(key, qdojProblemDto);
} catch (Exception e) {
FileUtil.del(fileDir);
return CommonResult.errorResponse("请检查编号为:" + key + "的题目json文件的格式:" + e.getLocalizedMessage());
}
}
QueryWrapper<Language> languageQueryWrapper = new QueryWrapper<>();
languageQueryWrapper.eq("oj", "ME");
List<Language> languageList = languageService.list(languageQueryWrapper);
HashMap<String, Long> languageMap = new HashMap<>();
for (Language language : languageList) {
languageMap.put(language.getName(), language.getId());
}
// 获取当前登录的用户
HttpSession session = request.getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
List<Tag> tagList = tagService.list(new QueryWrapper<Tag>().eq("oj", "ME"));
HashMap<String, Tag> tagMap = new HashMap<>();
for (Tag tag : tagList) {
tagMap.put(tag.getName().toUpperCase(), tag);
}
List<ProblemDto> problemDtos = new LinkedList<>();
for (String key : problemInfo.keySet()) {
QDOJProblemDto qdojProblemDto = problemVoMap.get(key);
// 格式化题目语言
List<Language> languages = new LinkedList<>();
for (String lang : qdojProblemDto.getLanguages()) {
Long lid = languageMap.getOrDefault(lang, null);
languages.add(new Language().setId(lid).setName(lang));
}
// 格式化标签
List<Tag> tags = new LinkedList<>();
for (String tagStr : qdojProblemDto.getTags()) {
Tag tag = tagMap.getOrDefault(tagStr.toUpperCase(), null);
if (tag == null) {
tags.add(new Tag().setName(tagStr).setOj("ME"));
} else {
tags.add(tag);
}
}
Problem problem = qdojProblemDto.getProblem();
if (problem.getAuthor() == null) {
problem.setAuthor(userRolesVo.getUsername());
}
ProblemDto problemDto = new ProblemDto();
String mode = Constants.JudgeMode.DEFAULT.getMode();
if (qdojProblemDto.getIsSpj()) {
mode = Constants.JudgeMode.SPJ.getMode();
}
problemDto.setJudgeMode(mode).setProblem(problem).setCodeTemplates(qdojProblemDto.getCodeTemplates()).setTags(tags).setLanguages(languages).setUploadTestcaseDir(fileDir + File.separator + key + File.separator + "testcase").setIsUploadTestCase(true).setSamples(qdojProblemDto.getSamples());
problemDtos.add(problemDto);
}
for (ProblemDto problemDto : problemDtos) {
problemService.adminAddProblem(problemDto);
}
return CommonResult.successResponse(null, "导入题目成功");
}
use of top.hcode.hoj.pojo.entity.problem.Language in project HOJ by HimitZH.
the class StartupRunner method checkRemoteOJLanguage.
private void checkRemoteOJLanguage(Constants.RemoteOJ... remoteOJList) {
for (Constants.RemoteOJ remoteOJ : remoteOJList) {
QueryWrapper<Language> languageQueryWrapper = new QueryWrapper<>();
languageQueryWrapper.eq("oj", remoteOJ.getName());
int count = languageEntityService.count(languageQueryWrapper);
if (count == 0) {
List<Language> languageList = new LanguageContext(remoteOJ).buildLanguageList();
boolean isOk = languageEntityService.saveBatch(languageList);
if (!isOk) {
log.error("[Init System Config] [{}] Failed to initialize language list! Please check whether the language table corresponding to the database has the OJ language!", remoteOJ.getName());
}
}
}
}
use of top.hcode.hoj.pojo.entity.problem.Language in project HOJ by HimitZH.
the class StartupRunner method upsertHOJLanguage.
private void upsertHOJLanguage(String... languageList) {
/**
* 2022.02.25 新增js、pypy、php语言
*/
for (String language : languageList) {
QueryWrapper<Language> languageQueryWrapper = new QueryWrapper<>();
languageQueryWrapper.eq("oj", "ME").eq("name", language);
int count = languageEntityService.count(languageQueryWrapper);
if (count == 0) {
Language newLanguage = buildHOJLanguage(language);
boolean isOk = languageEntityService.save(newLanguage);
if (!isOk) {
log.error("[Init System Config] [HOJ] Failed to add new language [{}]! Please check whether the language table corresponding to the database has the language!", language);
}
}
}
}
use of top.hcode.hoj.pojo.entity.problem.Language in project HOJ by HimitZH.
the class ImportFpsProblemManager method parseFps.
private List<ProblemDto> parseFps(InputStream inputStream, String username) throws StatusFailException {
Document document = null;
try {
DocumentBuilderFactory documentBuilderFactory = XmlUtil.createDocumentBuilderFactory();
documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
document = documentBuilder.parse(inputStream);
} catch (ParserConfigurationException e) {
log.error("build DocumentBuilder error:", e);
} catch (IOException e) {
log.error("read xml file error:", e);
} catch (SAXException e) {
log.error("parse xml file error:", e);
}
if (document == null) {
throw new StatusFailException("读取xml失败,请检查FPS文件格式是否准确!");
}
Element rootElement = XmlUtil.getRootElement(document);
String version = rootElement.getAttribute("version");
List<ProblemDto> problemDtoList = new ArrayList<>();
String fileDirId = IdUtil.simpleUUID();
String fileDir = Constants.File.TESTCASE_TMP_FOLDER.getPath() + File.separator + fileDirId;
long time = System.currentTimeMillis();
int index = 1;
for (Element item : XmlUtil.getElements(rootElement, "item")) {
Problem problem = new Problem();
problem.setAuthor(username).setType(0).setIsUploadCase(true).setDifficulty(1).setIsRemoveEndBlank(true).setOpenCaseResult(true).setCodeShare(false).setIsRemote(false).setAuth(1).setIsGroup(false).setProblemId(String.valueOf(time + index));
Element title = XmlUtil.getElement(item, "title");
// 标题
problem.setTitle(title.getTextContent());
HashMap<String, String> srcMapUrl = new HashMap<>();
List<Element> images = XmlUtil.getElements(item, "img");
for (Element img : images) {
Element srcElement = XmlUtil.getElement(img, "src");
if (srcElement == null) {
continue;
}
String src = srcElement.getTextContent();
String base64 = XmlUtil.getElement(img, "base64").getTextContent();
String[] split = src.split("\\.");
byte[] decode = Base64.getDecoder().decode(base64);
String fileName = IdUtil.fastSimpleUUID() + "." + split[split.length - 1];
FileUtil.writeBytes(decode, Constants.File.MARKDOWN_FILE_FOLDER.getPath() + File.separator + fileName);
srcMapUrl.put(src, Constants.File.IMG_API.getPath() + fileName);
}
Element descriptionElement = XmlUtil.getElement(item, "description");
String description = descriptionElement.getTextContent();
for (Map.Entry<String, String> entry : srcMapUrl.entrySet()) {
description = description.replaceAll(entry.getKey(), entry.getValue());
}
// 题目描述
problem.setDescription(description);
Element inputElement = XmlUtil.getElement(item, "input");
String input = inputElement.getTextContent();
for (Map.Entry<String, String> entry : srcMapUrl.entrySet()) {
input = input.replaceAll(entry.getKey(), entry.getValue());
}
// 输入描述
problem.setInput(input);
Element outputElement = XmlUtil.getElement(item, "output");
String output = outputElement.getTextContent();
for (Map.Entry<String, String> entry : srcMapUrl.entrySet()) {
output = output.replaceAll(entry.getKey(), entry.getValue());
}
// 输出描述
problem.setOutput(output);
// 提示
Element hintElement = XmlUtil.getElement(item, "hint");
String hint = hintElement.getTextContent();
for (Map.Entry<String, String> entry : srcMapUrl.entrySet()) {
hint = hint.replaceAll(entry.getKey(), entry.getValue());
}
problem.setHint(hint);
// 来源
Element sourceElement = XmlUtil.getElement(item, "source");
String source = sourceElement.getTextContent();
problem.setSource(source);
// ms
Integer timeLimit = getTimeLimit(version, item);
problem.setTimeLimit(timeLimit);
// mb
Integer memoryLimit = getMemoryLimit(item);
problem.setMemoryLimit(memoryLimit);
// 题面用例
List<Element> sampleInputs = XmlUtil.getElements(item, "sample_input");
List<Element> sampleOutputs = XmlUtil.getElements(item, "sample_output");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < sampleInputs.size(); i++) {
sb.append("<input>").append(sampleInputs.get(i).getTextContent()).append("</input>");
sb.append("<output>").append(sampleOutputs.get(i).getTextContent()).append("</output>");
}
problem.setExamples(sb.toString());
QueryWrapper<Language> languageQueryWrapper = new QueryWrapper<>();
languageQueryWrapper.eq("oj", "ME");
List<Language> languageList = languageEntityService.list(languageQueryWrapper);
HashMap<String, Long> languageMap = new HashMap<>();
for (Language language : languageList) {
languageMap.put(language.getName(), language.getId());
}
// 题目模板
List<Element> templateNodes = XmlUtil.getElements(item, "template");
List<CodeTemplate> codeTemplates = new ArrayList<>();
for (Element templateNode : templateNodes) {
String templateLanguage = templateNode.getAttribute("language");
String templateCode = templateNode.getTextContent();
if (templateLanguage == null || templateCode == null) {
continue;
}
String lang = fpsMapHOJ.get(templateLanguage);
if (lang != null) {
codeTemplates.add(new CodeTemplate().setCode(templateCode).setLid(languageMap.get(lang)));
}
}
// spj
Element spjNode = XmlUtil.getElement(item, "spj");
if (spjNode != null) {
String spjLanguage = spjNode.getAttribute("language");
String spjCode = spjNode.getTextContent();
if (("C".equals(spjLanguage) || "C++".equals(spjLanguage)) && !StringUtils.isEmpty(spjCode)) {
problem.setSpjLanguage(spjLanguage).setSpjCode(spjCode);
}
}
// 题目评测数据
List<Element> testInputs = XmlUtil.getElements(item, "test_input");
List<Element> testOutputs = XmlUtil.getElements(item, "test_output");
boolean isNotOutputTestCase = CollectionUtils.isEmpty(testOutputs);
List<ProblemCase> problemSamples = new LinkedList<>();
String problemTestCaseDir = fileDir + File.separator + index;
for (int i = 0; i < testInputs.size(); i++) {
String infileName = (i + 1) + ".in";
String outfileName = (i + 1) + ".out";
FileWriter infileWriter = new FileWriter(problemTestCaseDir + File.separator + infileName);
FileWriter outfileWriter = new FileWriter(problemTestCaseDir + File.separator + outfileName);
infileWriter.write(testInputs.get(i).getTextContent());
outfileWriter.write(isNotOutputTestCase ? "" : testOutputs.get(i).getTextContent());
problemSamples.add(new ProblemCase().setInput(infileName).setOutput(outfileName));
}
if (CollectionUtils.isEmpty(problemSamples)) {
throw new StatusFailException("[" + problem.getTitle() + "] 题目的评测数据不能为空,请检查FPS文件内该题目是否有test_input和test_output!");
}
String mode = Constants.JudgeMode.DEFAULT.getMode();
if (problem.getSpjLanguage() != null) {
mode = Constants.JudgeMode.SPJ.getMode();
}
ProblemDto problemDto = new ProblemDto();
problemDto.setSamples(problemSamples).setIsUploadTestCase(true).setUploadTestcaseDir(problemTestCaseDir).setLanguages(languageList).setTags(null).setJudgeMode(mode).setProblem(problem).setCodeTemplates(codeTemplates);
problemDtoList.add(problemDto);
index++;
}
return problemDtoList;
}
Aggregations