|
|
|
@@ -7,6 +7,7 @@ import static de.srsoftware.tools.PathHandler.GET;
|
|
|
|
|
import static de.srsoftware.tools.PathHandler.POST;
|
|
|
|
|
import static de.srsoftware.tools.Strings.hex;
|
|
|
|
|
import static de.srsoftware.umbrella.core.Errors.INVALID_URL;
|
|
|
|
|
import static de.srsoftware.umbrella.core.ModuleRegistry.markdownService;
|
|
|
|
|
import static de.srsoftware.umbrella.core.constants.Constants.TIME_FORMATTER;
|
|
|
|
|
import static de.srsoftware.umbrella.core.constants.Field.*;
|
|
|
|
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.serverError;
|
|
|
|
@@ -35,14 +36,9 @@ import java.util.regex.Pattern;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
public class Util {
|
|
|
|
|
public static final System.Logger LOG = System.getLogger("Util");
|
|
|
|
|
private static final Pattern UML_PATTERN = Pattern.compile("@start(\\w+)(.*?)@end(\\1)",Pattern.DOTALL);
|
|
|
|
|
private static final Pattern SPREADSHEET_PATTERN = Pattern.compile("@startsheet(.*?)@endsheet",Pattern.DOTALL);
|
|
|
|
|
private static File plantumlJar = null;
|
|
|
|
|
private static final JParsedown MARKDOWN = new JParsedown();
|
|
|
|
|
public static final String SHA1 = "SHA-1";
|
|
|
|
|
public static final System.Logger LOG = System.getLogger("Util");
|
|
|
|
|
public static final String SHA1 = "SHA-1";
|
|
|
|
|
private static final MessageDigest SHA1_DIGEST;
|
|
|
|
|
private static final Map<Integer,String> umlCache = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
private static final String SCRIPT = """
|
|
|
|
|
<script src="http://127.0.0.1:8080/js/jspreadsheet-ce.js"></script>
|
|
|
|
@@ -111,71 +107,11 @@ jspreadsheet(document.getElementById('spreadsheet'), {
|
|
|
|
|
public static HashMap<String, Object> mapMarkdown(String source){
|
|
|
|
|
var map = new HashMap<String,Object>();
|
|
|
|
|
map.put(SOURCE,source);
|
|
|
|
|
map.put(RENDERED,markdown(source));
|
|
|
|
|
map.put(RENDERED,markdownService().markdown(source));
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String markdown(String source){
|
|
|
|
|
if (source == null) return source;
|
|
|
|
|
try {
|
|
|
|
|
var matcher = SPREADSHEET_PATTERN.matcher(source);
|
|
|
|
|
var count = 0;
|
|
|
|
|
while (matcher.find()){
|
|
|
|
|
count++;
|
|
|
|
|
var sheetData = matcher.group(0).trim();
|
|
|
|
|
var start = matcher.start(0);
|
|
|
|
|
var end = matcher.end(0);
|
|
|
|
|
source = source.substring(0, start)
|
|
|
|
|
+ "<div class=\"spreadsheet\" id=\"spreadsheet-"+count+"\">"
|
|
|
|
|
+ sheetData.substring(11,sheetData.length()-10)
|
|
|
|
|
+ "</div>"
|
|
|
|
|
+ source.substring(end);
|
|
|
|
|
matcher = SPREADSHEET_PATTERN.matcher(source);
|
|
|
|
|
}
|
|
|
|
|
if (plantumlJar != null && plantumlJar.exists()) {
|
|
|
|
|
matcher = UML_PATTERN.matcher(source);
|
|
|
|
|
while (matcher.find()) {
|
|
|
|
|
var uml = matcher.group(0).trim();
|
|
|
|
|
var start = matcher.start(0);
|
|
|
|
|
var end = matcher.end(0);
|
|
|
|
|
|
|
|
|
|
var umlHash = uml.hashCode();
|
|
|
|
|
LOG.log(DEBUG,"Hash of Plantuml code: {0}",umlHash);
|
|
|
|
|
var svg = umlCache.get(umlHash);
|
|
|
|
|
if (svg != null){
|
|
|
|
|
LOG.log(DEBUG,"Serving Plantuml generated SVG from cache…");
|
|
|
|
|
source = source.substring(0, start) + svg + source.substring(end);
|
|
|
|
|
matcher = UML_PATTERN.matcher(source);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG.log(DEBUG,"Cache miss. Generating SVG from plantuml code…");
|
|
|
|
|
ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", plantumlJar.getAbsolutePath(), "-tsvg", "-pipe");
|
|
|
|
|
var ignored = processBuilder.redirectErrorStream();
|
|
|
|
|
var process = processBuilder.start();
|
|
|
|
|
try (OutputStream os = process.getOutputStream()) {
|
|
|
|
|
os.write(uml.getBytes(UTF_8));
|
|
|
|
|
os.flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try (InputStream is = process.getInputStream()) {
|
|
|
|
|
byte[] out = is.readAllBytes();
|
|
|
|
|
LOG.log(DEBUG,"Generated SVG. Pushing to cache…");
|
|
|
|
|
svg = new String(out, UTF_8);
|
|
|
|
|
umlCache.put(umlHash,svg);
|
|
|
|
|
source = source.substring(0, start) + svg + source.substring(end);
|
|
|
|
|
matcher = UML_PATTERN.matcher(source);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return MARKDOWN.text(source);
|
|
|
|
|
} catch (Throwable e){
|
|
|
|
|
if (LOG.isLoggable(TRACE)){
|
|
|
|
|
LOG.log(TRACE,"Failed to render markdown, input was: \n{0}",source,e);
|
|
|
|
|
} else LOG.log(WARNING,"Failed to render markdown. Enable TRACE log level for details.");
|
|
|
|
|
return source;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static HttpURLConnection open(URL url) throws IOException {
|
|
|
|
|
var conn = (HttpURLConnection) url.openConnection();
|
|
|
|
@@ -249,11 +185,6 @@ jspreadsheet(document.getElementById('spreadsheet'), {
|
|
|
|
|
return new Hash(hex(bytes),SHA1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void setPlantUmlJar(File file){
|
|
|
|
|
LOG.log(INFO,"Using plantuml @ {0}",file.getAbsolutePath());
|
|
|
|
|
plantumlJar = file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String dateTimeOf(long epochMilis){
|
|
|
|
|
return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilis), ZoneId.systemDefault()).format(TIME_FORMATTER);
|
|
|
|
|
}
|
|
|
|
|