package org.psjava.judgesubmit; import org.psjava.ds.map.MutableMap; import org.psjava.goods.GoodMutableMapFactory; import java.io.IOException; import java.util.HashMap; public class LightOJSubmitter implements Submitter { @Override public String submitAndGetId(JudgeHttpClient hc, String problemId, String userId, String password, Language language, String sourceCode) throws IOException, InvalidInputException, SiteParsingException, JudgeServiceException { LightOJLogin.login(hc, userId, password); submit(hc, problemId, language, sourceCode); return recvLastSubmitId(hc, problemId); } protected static void submit(JudgeHttpClient hc, String problemId, Language language, String sourceCode) throws IOException, JudgeServiceException, SiteParsingException, InvalidInputException { HashMap<String, String> param = new HashMap<String, String>(); param.put("sub_problem", problemId); param.put("language", getLanguageCodeMap().get(language)); param.put("code", sourceCode); String res = hc.receivePostBodyString("/volume_submit.php", param, LightOJ.ENCODING); String prefix = "<h3 class=\"bugreport\">"; if (res.contains("location.href='volume_usersubmissions.php'")) { // success } else if (res.contains(prefix)) { String msg = SiteParsingUtil.extractMiddleString(res, 0, prefix, "<"); if (msg.equals("Not a valid problem")) throw InvalidProblemIdException.create(problemId); else if (msg.equals("Please enter your code")) throw new InvalidInputException("Empty source code"); else if(msg.startsWith("You cannot submit a code which is more than")) throw new InvalidInputException("Too long code: " + msg); else throw new SiteParsingException("Unknown message from site: " + msg); } else { throw new SiteParsingException("Cannot parse response: " + res); } } public static org.psjava.ds.map.Map<Language, String> getLanguageCodeMap() { MutableMap<Language, String> map = GoodMutableMapFactory.getInstance().create(); map.put(Language.C, "C"); map.put(Language.CPP, "C++"); map.put(Language.JAVA, "JAVA"); return map; } protected static String recvLastSubmitId(JudgeHttpClient hc, String prblemId) throws IOException, JudgeServiceException, SiteParsingException { String body = hc.receiveGetBodyString("/volume_usersubmissions.php", LightOJ.ENCODING); String res = SiteParsingUtil.extractMiddleString(body, 0, "volume_showcode.php?sub_id=", "\""); String lastProblemId = SiteParsingUtil.extractMiddleString(body, 0, "volume_showproblem.php?problem=", "\""); if(!lastProblemId.equals(prblemId)) throw new SiteParsingException("Cannot extract submit id. last problem id is " + lastProblemId); return res; } }