1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-11-24 05:47:33 +00:00

Merge pull request #1472 from travkin79/patch/1470

Add reading remote style files from URLs (Fix #1470)
This commit is contained in:
PlantUML 2023-07-04 11:43:43 +02:00 committed by GitHub
commit ebeea26ecb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -37,6 +37,8 @@ package net.sourceforge.plantuml.style;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import net.sourceforge.plantuml.FileSystem; import net.sourceforge.plantuml.FileSystem;
import net.sourceforge.plantuml.TitledDiagram; import net.sourceforge.plantuml.TitledDiagram;
@ -47,6 +49,7 @@ import net.sourceforge.plantuml.regex.RegexConcat;
import net.sourceforge.plantuml.regex.RegexLeaf; import net.sourceforge.plantuml.regex.RegexLeaf;
import net.sourceforge.plantuml.regex.RegexResult; import net.sourceforge.plantuml.regex.RegexResult;
import net.sourceforge.plantuml.security.SFile; import net.sourceforge.plantuml.security.SFile;
import net.sourceforge.plantuml.security.SURL;
import net.sourceforge.plantuml.style.parser.StyleParser; import net.sourceforge.plantuml.style.parser.StyleParser;
import net.sourceforge.plantuml.style.parser.StyleParsingException; import net.sourceforge.plantuml.style.parser.StyleParsingException;
import net.sourceforge.plantuml.utils.BlocLines; import net.sourceforge.plantuml.utils.BlocLines;
@ -80,23 +83,32 @@ public class CommandStyleImport extends SingleLineCommand2<TitledDiagram> {
protected CommandExecutionResult executeArg(TitledDiagram diagram, LineLocation location, RegexResult arg) { protected CommandExecutionResult executeArg(TitledDiagram diagram, LineLocation location, RegexResult arg) {
final String path = arg.get("PATH", 0); final String path = arg.get("PATH", 0);
try { try {
final SFile f = FileSystem.getInstance().getFile(path);
BlocLines lines = null; BlocLines lines = null;
if (f.exists()) { if (path.startsWith("http")) {
lines = BlocLines.load(f, location); SURL url = SURL.create (path);
try (InputStream remoteInputStream = url.openStream()) {
lines = BlocLines.load(remoteInputStream, location);
}
} else { } else {
final InputStream internalIs = StyleLoader.class.getResourceAsStream("/skin/" + path); final SFile styleFile = FileSystem.getInstance().getFile(path);
if (internalIs != null) if (styleFile.exists()) {
lines = BlocLines.load(internalIs, location); lines = BlocLines.load(styleFile, location);
} else {
final InputStream internalIs = StyleLoader.class.getResourceAsStream("/skin/" + path);
if (internalIs != null)
lines = BlocLines.load(internalIs, location);
}
} }
if (lines == null)
if (lines == null || lines.size() == 0)
return CommandExecutionResult.error("Cannot read: " + path); return CommandExecutionResult.error("Cannot read: " + path);
final StyleBuilder styleBuilder = diagram.getSkinParam().getCurrentStyleBuilder(); final StyleBuilder styleBuilder = diagram.getSkinParam().getCurrentStyleBuilder();
for (Style modifiedStyle : StyleParser.parse(lines, styleBuilder)) for (Style modifiedStyle : StyleParser.parse(lines, styleBuilder))
diagram.getSkinParam().muteStyle(modifiedStyle); diagram.getSkinParam().muteStyle(modifiedStyle);
} catch (MalformedURLException e) {
return CommandExecutionResult.error("Invalid URL to style definition: " + e.getMessage());
} catch (StyleParsingException e) { } catch (StyleParsingException e) {
return CommandExecutionResult.error("Error in style definition: " + e.getMessage()); return CommandExecutionResult.error("Error in style definition: " + e.getMessage());
} catch (IOException e) { } catch (IOException e) {