1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-11-11 16:15:50 +00:00
This commit is contained in:
Arnaud Roques 2021-11-10 19:18:02 +01:00
parent 735038fdf3
commit 1fbe080683
6 changed files with 84 additions and 55 deletions

View File

@ -62,6 +62,8 @@ public class StyleExtractor {
break; break;
line = data.next(); line = data.next();
} }
} else if (line.getString().trim().startsWith("!pragma ")) {
// Ignore
} else if (line.getString().trim().startsWith("title ")) { } else if (line.getString().trim().startsWith("title ")) {
this.title = line.getString().trim().substring("title ".length()).trim(); this.title = line.getString().trim().substring("title ".length()).trim();
} else if (line.getString().trim().startsWith("skinparam ")) { } else if (line.getString().trim().startsWith("skinparam ")) {

View File

@ -86,6 +86,11 @@ public class ThemeUtils {
if (from.endsWith("/") == false) { if (from.endsWith("/") == false) {
sb.append("/"); 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;
}
} }

View File

@ -36,11 +36,14 @@ package net.sourceforge.plantuml.tim;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import net.sourceforge.plantuml.AFile;
import net.sourceforge.plantuml.StringLocated; import net.sourceforge.plantuml.StringLocated;
import net.sourceforge.plantuml.preproc.FileWithSuffix; import net.sourceforge.plantuml.preproc.FileWithSuffix;
import net.sourceforge.plantuml.preproc.ImportedFiles;
import net.sourceforge.plantuml.preproc.ReadLine; import net.sourceforge.plantuml.preproc.ReadLine;
import net.sourceforge.plantuml.preproc.ReadLineReader; import net.sourceforge.plantuml.preproc.ReadLineReader;
import net.sourceforge.plantuml.preproc2.PreprocessorUtils; import net.sourceforge.plantuml.preproc2.PreprocessorUtils;
@ -53,9 +56,11 @@ public class EaterTheme extends Eater {
private String name; private String name;
private String from; private String from;
private TContext context; private TContext context;
private final ImportedFiles importedFiles;
public EaterTheme(StringLocated s) { public EaterTheme(StringLocated s, ImportedFiles importedFiles) {
super(s); super(s);
this.importedFiles = importedFiles;
} }
@Override @Override
@ -79,13 +84,26 @@ public class EaterTheme extends Eater {
public final ReadLine getTheme() throws EaterException { public final ReadLine getTheme() throws EaterException {
if (from == null) { if (from == null) {
final ReadLine reader = ThemeUtils.getReaderTheme(realName); final ReadLine reader = ThemeUtils.getReaderTheme(realName);
return reader; if (reader != null)
} return reader;
if (from.startsWith("http://") || from.startsWith("https://")) {
final SURL url = SURL.create(ThemeUtils.getFullPath(from, realName)); try {
if (url == null) { final AFile localFile = importedFiles.getAFile(ThemeUtils.getFilename(realName));
throw EaterException.located("Cannot open URL"); 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 { try {
return PreprocessorUtils.getReaderInclude(url, getLineLocation(), UTF_8); return PreprocessorUtils.getReaderInclude(url, getLineLocation(), UTF_8);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {

View File

@ -585,7 +585,7 @@ public class TContext {
} }
private void executeTheme(TMemory memory, StringLocated s) throws EaterException, EaterExceptionLocated { 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); eater.analyze(this, memory);
final ReadLine reader = eater.getTheme(); final ReadLine reader = eater.getTheme();
if (reader == null) { if (reader == null) {

View File

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

View File

@ -80,7 +80,7 @@ public class Version {
} }
public static int beta() { public static int beta() {
final int beta = 4; final int beta = 5;
return beta; return beta;
} }