All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codingame.gameengine.runner.StatementSplitter Maven / Gradle / Ivy

Go to download

Runner / Testbed to locally run Games created using the CodinGame engine toolkit.

There is a newer version: 4.5.0
Show newest version
package com.codingame.gameengine.runner;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.gson.JsonObject;

/***
 * Parse an HTML file and remove blocks between <!-- BEGIN keyword1 keyword2 --> and <!-- END -->
 * Must contain a block <!-- LEAGUES level1 level2 level3 level4 -->
*
* The HTML file(s) must be named "statement_<language>.html.tpl" and placed in "config" directory */ class StatementSplitter { private static final Pattern LEAGUE_MARKER = Pattern.compile("\\s*\\s*"); private static final Pattern BEGIN_MARKER = Pattern.compile("\\s*\\s*"); private static final Pattern END_MARKER = Pattern.compile("\\s*\\s*"); public static void generateSplitStatement(Path sourceFolderPath, File statementFile, ExportReport exportReport) { //Retrieve content from the file List lines = getLines(statementFile); List leagues = getLeagues(lines); if (leagues.isEmpty()) { exportReport.addItem(ReportItemType.WARNING, statementFile.getName() + ": Statement splitter did not find leagues."); return; } for (String league : leagues) { sourceFolderPath.resolve("config/" + league).toFile().mkdir(); if (!sourceFolderPath.resolve("config/" + league).toFile().isDirectory()) { throw new RuntimeException("Unable to access or create config/" + league + " directory."); } writeStatement(sourceFolderPath, statementFile, lines, league, exportReport); } } public static JsonObject generateSplitStatementInMemory(List lines, ExportReport exportReport) throws IOException { List leagues = getLeagues(lines); JsonObject result = new JsonObject(); for (String league : leagues) { StringWriter writer = new StringWriter(); writeStatement(lines, league, exportReport, writer, String.format("statement_%s", league)); result.addProperty(league, writer.toString()); } return result; } private static void writeStatement(Path sourceFolderPath, File statementFile, List lines, String league, ExportReport exportReport) { Writer writer = null; try { //Substring to remove ".tpl" from the new file String newStatementFilename = statementFile.getName().substring(0, statementFile.getName().length() - 4); File newFile = sourceFolderPath.resolve("config/" + league + "/" + newStatementFilename).toFile(); if (!newFile.exists()) { newFile.createNewFile(); } writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(newFile) ) ); writeStatement(lines, league, exportReport, writer, newStatementFilename); } catch (IOException e) { throw new RuntimeException("Cannot generate statement file.", e); } finally { try { writer.close(); } catch (IOException e) { throw new RuntimeException("Cannot close statement file.", e); } } } private static void writeStatement(List lines, String league, ExportReport exportReport, Writer writer, String newStatementFilename) throws IOException { writer.write(""); boolean visible = true; Stack stack = new Stack<>(); for (String line : lines) { Matcher beginMatcher = BEGIN_MARKER.matcher(line); if (beginMatcher.matches()) { stack.push(beginMatcher.group("leagues")); visible = Arrays.asList(beginMatcher.group("leagues").split(" ")).contains(league); } else if (END_MARKER.matcher(line).matches()) { if (stack.isEmpty()) { exportReport.addItem(ReportItemType.WARNING, league + "/" + newStatementFilename + ": \"" + line + "\" unexpected"); writer.close(); return; } stack.pop(); visible = stack.isEmpty() ? true : Arrays.asList(stack.peek().split(" ")).contains(league); } else if (visible) { writer.write(line); } } } private static List getLeagues(List lines) { Matcher leagueMatcher; List leagues = new ArrayList<>(); for (String line : lines) { leagueMatcher = LEAGUE_MARKER.matcher(line); if (leagueMatcher.matches()) { leagues.addAll(Arrays.asList(leagueMatcher.group("leagues").split(" "))); break; } } return leagues; } private static List getLines(File statementFile) { List lines = new ArrayList<>(); try { BufferedReader bufferedReader = new BufferedReader(new FileReader(statementFile)); String statementContent = null; while ((statementContent = bufferedReader.readLine()) != null) { lines.add(statementContent + '\n'); } bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } return lines; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy