From 1fbe0806837bda83c90ec47d18797bb3a9a10cf1 Mon Sep 17 00:00:00 2001 From: Arnaud Roques Date: Wed, 10 Nov 2021 19:18:02 +0100 Subject: [PATCH] wip --- .../plantuml/jsondiagram/StyleExtractor.java | 2 + .../plantuml/theme/ThemeUtils.java | 7 +- .../sourceforge/plantuml/tim/EaterTheme.java | 32 +++++-- .../sourceforge/plantuml/tim/TContext.java | 2 +- .../plantuml/tim/stdlib/LoadJson.java | 94 ++++++++++--------- .../sourceforge/plantuml/version/Version.java | 2 +- 6 files changed, 84 insertions(+), 55 deletions(-) diff --git a/src/net/sourceforge/plantuml/jsondiagram/StyleExtractor.java b/src/net/sourceforge/plantuml/jsondiagram/StyleExtractor.java index 28fd3fc85..67d0d47f0 100644 --- a/src/net/sourceforge/plantuml/jsondiagram/StyleExtractor.java +++ b/src/net/sourceforge/plantuml/jsondiagram/StyleExtractor.java @@ -62,6 +62,8 @@ public class StyleExtractor { break; line = data.next(); } + } else if (line.getString().trim().startsWith("!pragma ")) { + // Ignore } else if (line.getString().trim().startsWith("title ")) { this.title = line.getString().trim().substring("title ".length()).trim(); } else if (line.getString().trim().startsWith("skinparam ")) { diff --git a/src/net/sourceforge/plantuml/theme/ThemeUtils.java b/src/net/sourceforge/plantuml/theme/ThemeUtils.java index 2d2557081..72442e838 100644 --- a/src/net/sourceforge/plantuml/theme/ThemeUtils.java +++ b/src/net/sourceforge/plantuml/theme/ThemeUtils.java @@ -86,6 +86,11 @@ public class ThemeUtils { if (from.endsWith("/") == false) { sb.append("/"); } - return sb + THEME_FILE_PREFIX + filename + THEME_FILE_SUFFIX; + return sb + getFilename(filename); } + + public static String getFilename(String filename) { + return THEME_FILE_PREFIX + filename + THEME_FILE_SUFFIX; + } + } diff --git a/src/net/sourceforge/plantuml/tim/EaterTheme.java b/src/net/sourceforge/plantuml/tim/EaterTheme.java index 56e268b15..9d65a8122 100644 --- a/src/net/sourceforge/plantuml/tim/EaterTheme.java +++ b/src/net/sourceforge/plantuml/tim/EaterTheme.java @@ -36,11 +36,14 @@ package net.sourceforge.plantuml.tim; import static java.nio.charset.StandardCharsets.UTF_8; +import java.io.BufferedReader; import java.io.IOException; import java.io.UnsupportedEncodingException; +import net.sourceforge.plantuml.AFile; import net.sourceforge.plantuml.StringLocated; import net.sourceforge.plantuml.preproc.FileWithSuffix; +import net.sourceforge.plantuml.preproc.ImportedFiles; import net.sourceforge.plantuml.preproc.ReadLine; import net.sourceforge.plantuml.preproc.ReadLineReader; import net.sourceforge.plantuml.preproc2.PreprocessorUtils; @@ -53,9 +56,11 @@ public class EaterTheme extends Eater { private String name; private String from; private TContext context; + private final ImportedFiles importedFiles; - public EaterTheme(StringLocated s) { + public EaterTheme(StringLocated s, ImportedFiles importedFiles) { super(s); + this.importedFiles = importedFiles; } @Override @@ -79,13 +84,26 @@ public class EaterTheme extends Eater { public final ReadLine getTheme() throws EaterException { if (from == null) { final ReadLine reader = ThemeUtils.getReaderTheme(realName); - return reader; - } - if (from.startsWith("http://") || from.startsWith("https://")) { - final SURL url = SURL.create(ThemeUtils.getFullPath(from, realName)); - if (url == null) { - throw EaterException.located("Cannot open URL"); + if (reader != null) + return reader; + + try { + final AFile localFile = importedFiles.getAFile(ThemeUtils.getFilename(realName)); + if (localFile != null && localFile.isOk()) { + final BufferedReader br = localFile.getUnderlyingFile().openBufferedReader(); + if (br != null) + return ReadLineReader.create(br, "theme " + realName); + } + } catch (IOException e) { + e.printStackTrace(); } + throw EaterException.located("Cannot load " + realName); + + } else if (from.startsWith("http://") || from.startsWith("https://")) { + final SURL url = SURL.create(ThemeUtils.getFullPath(from, realName)); + if (url == null) + throw EaterException.located("Cannot open URL"); + try { return PreprocessorUtils.getReaderInclude(url, getLineLocation(), UTF_8); } catch (UnsupportedEncodingException e) { diff --git a/src/net/sourceforge/plantuml/tim/TContext.java b/src/net/sourceforge/plantuml/tim/TContext.java index 78e798a19..3ad874aee 100644 --- a/src/net/sourceforge/plantuml/tim/TContext.java +++ b/src/net/sourceforge/plantuml/tim/TContext.java @@ -585,7 +585,7 @@ public class TContext { } private void executeTheme(TMemory memory, StringLocated s) throws EaterException, EaterExceptionLocated { - final EaterTheme eater = new EaterTheme(s.getTrimmed()); + final EaterTheme eater = new EaterTheme(s.getTrimmed(), importedFiles); eater.analyze(this, memory); final ReadLine reader = eater.getTheme(); if (reader == null) { diff --git a/src/net/sourceforge/plantuml/tim/stdlib/LoadJson.java b/src/net/sourceforge/plantuml/tim/stdlib/LoadJson.java index 299b4a6f5..17445548c 100644 --- a/src/net/sourceforge/plantuml/tim/stdlib/LoadJson.java +++ b/src/net/sourceforge/plantuml/tim/stdlib/LoadJson.java @@ -34,6 +34,13 @@ */ package net.sourceforge.plantuml.tim.stdlib; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.List; +import java.util.Map; +import java.util.Set; + import net.sourceforge.plantuml.FileSystem; import net.sourceforge.plantuml.FileUtils; import net.sourceforge.plantuml.LineLocation; @@ -42,25 +49,23 @@ import net.sourceforge.plantuml.json.JsonValue; import net.sourceforge.plantuml.json.ParseException; import net.sourceforge.plantuml.security.SFile; import net.sourceforge.plantuml.security.SURL; -import net.sourceforge.plantuml.tim.*; +import net.sourceforge.plantuml.tim.EaterException; +import net.sourceforge.plantuml.tim.EaterExceptionLocated; +import net.sourceforge.plantuml.tim.TContext; +import net.sourceforge.plantuml.tim.TFunctionSignature; +import net.sourceforge.plantuml.tim.TMemory; import net.sourceforge.plantuml.tim.expression.TValue; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.util.List; -import java.util.Map; -import java.util.Set; - /** * Loads JSON data from file or URL source. *

- * Supports three parameters for datasource, default JSON value and charset. The datasource will be checked against - * the security rules. + * Supports three parameters for datasource, default JSON value and charset. The + * datasource will be checked against the security rules. *

* Examples:
+ * *

- *     @ startuml
+ *     @ startuml
  *     ' loads a local file
  *     !$JSON_LOCAL_RELATIVE=%loadJSON("file.json")
  *
@@ -81,8 +86,9 @@ import java.util.Set;
  *     !$STATUS_NO_CONNECTION={"status": "No connection"}
  *     !$JSON_REMOTE_DEF=%loadJSON("https://localhost:7778/management/health", $STATUS_NO_CONNECTION)
  *     status -> $JSON_REMOTE_DEF.status
- *     @ enduml
+ *     @ enduml
  * 
+ * * @author Aljoscha Rittner */ public class LoadJson extends SimpleReturnFunction { @@ -92,7 +98,7 @@ public class LoadJson extends SimpleReturnFunction { private static final String VALUE_DEFAULT_DEFAULT = "{}"; public TFunctionSignature getSignature() { - return new TFunctionSignature("%loadJSON", 3 ); + return new TFunctionSignature("%loadJSON", 3); } public boolean canCover(int nbArg, Set namedArgument) { @@ -101,52 +107,54 @@ public class LoadJson extends SimpleReturnFunction { public TValue executeReturnFunction(TContext context, TMemory memory, LineLocation location, List values, Map named) throws EaterException, EaterExceptionLocated { - String path = values.get(0).toString(); + final String path = values.get(0).toString(); try { - String data = loadStringData ( path, getCharset (values) ); - if ( data == null ) { + String data = loadStringData(path, getCharset(values)); + if (data == null) data = getDefaultJson(values); - } + JsonValue jsonValue = Json.parse(data); return TValue.fromJson(jsonValue); } catch (ParseException pe) { - pe.printStackTrace (); - throw EaterException.unlocated ( "JSON parse issue in source " - + path + " on location " + pe.getLocation () ); + pe.printStackTrace(); + throw EaterException.unlocated("JSON parse issue in source " + path + " on location " + pe.getLocation()); } catch (UnsupportedEncodingException e) { - e.printStackTrace (); - throw EaterException.unlocated ( "JSON encoding issue in source " - + path + ": " + e.getMessage () ); + e.printStackTrace(); + throw EaterException.unlocated("JSON encoding issue in source " + path + ": " + e.getMessage()); } } /** * Returns the JSON default, if the data source contains no data. + * * @param values value parameters * @return the defined default JSON or {@code "{}"} */ private String getDefaultJson(List values) { - if (values.size() > 1 ) { - return values.get(1).toString (); - } + if (values.size() > 1) + return values.get(1).toString(); + return VALUE_DEFAULT_DEFAULT; } /** * Returns the charset name (if set) + * * @param values value parameters * @return defined charset or {@code "UTF-8"} */ private String getCharset(List values) { - if ( values.size() == 3) { - return values.get (2).toString (); - } + if (values.size() == 3) + return values.get(2).toString(); + return VALUE_CHARSET_DEFAULT; } /** - * Loads String data from a data source {@code path} (file or URL) and expects the data encoded in {@code charset}. - * @param path path to data source (http(s)-URL or file). + * Loads String data from a data source {@code path} (file or URL) and expects + * the data encoded in {@code charset}. + * + * @param path path to data source (http(s)-URL or file). * @param charset character set to encode the string data * @return the decoded String from the data source * @throws EaterException if something went wrong on reading data @@ -156,31 +164,27 @@ public class LoadJson extends SimpleReturnFunction { byte[] byteData = null; if (path.startsWith("http://") || path.startsWith("https://")) { final SURL url = SURL.create(path); - if (url == null) { + if (url == null) throw EaterException.located("load JSON: Invalid URL " + path); - } byteData = url.getBytes(); - if (byteData != null && byteData.length == 0) { - // no length, no data (we want the default) - byteData = null; - } } else { try { - SFile file = FileSystem.getInstance().getFile(path); + final SFile file = FileSystem.getInstance().getFile(path); if (file != null && file.exists() && file.canRead() && !file.isDirectory()) { - ByteArrayOutputStream out = new ByteArrayOutputStream(1024 * 8); + final ByteArrayOutputStream out = new ByteArrayOutputStream(1024 * 8); FileUtils.copyToStream(file, out); byteData = out.toByteArray(); } } catch (IOException e) { e.printStackTrace(); - throw EaterException.located("load JSON: Cannot read file " - + path + ". " + e.getMessage()); + throw EaterException.located("load JSON: Cannot read file " + path + ". " + e.getMessage()); } } - if (byteData != null) { - return new String(byteData, charset); - } - return null; + + if (byteData == null || byteData.length == 0) + return null; // no length, no data (we want the default) + + return new String(byteData, charset); + } } diff --git a/src/net/sourceforge/plantuml/version/Version.java b/src/net/sourceforge/plantuml/version/Version.java index 0ae1dd2f1..b2e7617d1 100644 --- a/src/net/sourceforge/plantuml/version/Version.java +++ b/src/net/sourceforge/plantuml/version/Version.java @@ -80,7 +80,7 @@ public class Version { } public static int beta() { - final int beta = 4; + final int beta = 5; return beta; }