1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-04 17:40:47 +00:00
plantuml/src/net/sourceforge/plantuml/command/CommandSpriteFile.java

150 lines
4.8 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, Arnaud Roques
2013-12-10 19:36:50 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2013-12-10 19:36:50 +00:00
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
2013-12-10 19:36:50 +00:00
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
*
*
*/
package net.sourceforge.plantuml.command;
import java.io.File;
2016-11-04 21:39:29 +00:00
import java.io.FileInputStream;
2013-12-10 19:36:50 +00:00
import java.io.IOException;
2016-01-09 12:15:40 +00:00
import java.io.InputStream;
2016-11-04 21:39:29 +00:00
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
2013-12-10 19:36:50 +00:00
import javax.imageio.ImageIO;
import net.sourceforge.plantuml.FileSystem;
2016-11-04 21:39:29 +00:00
import net.sourceforge.plantuml.FileUtils;
2019-03-29 22:14:07 +00:00
import net.sourceforge.plantuml.LineLocation;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.Log;
import net.sourceforge.plantuml.UmlDiagram;
2019-06-26 19:24:49 +00:00
import net.sourceforge.plantuml.command.regex.IRegex;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.command.regex.RegexConcat;
import net.sourceforge.plantuml.command.regex.RegexLeaf;
import net.sourceforge.plantuml.command.regex.RegexResult;
2018-10-21 19:44:14 +00:00
import net.sourceforge.plantuml.preproc.FileWithSuffix;
2019-08-26 17:07:21 +00:00
import net.sourceforge.plantuml.sprite.Sprite;
import net.sourceforge.plantuml.sprite.SpriteImage;
import net.sourceforge.plantuml.sprite.SpriteSvg;
2013-12-10 19:36:50 +00:00
public class CommandSpriteFile extends SingleLineCommand2<UmlDiagram> {
public CommandSpriteFile() {
super(getRegexConcat());
}
2019-06-26 19:24:49 +00:00
private static IRegex getRegexConcat() {
return RegexConcat.build(CommandSpriteFile.class.getName(), RegexLeaf.start(), //
new RegexLeaf("sprite"), //
RegexLeaf.spaceOneOrMore(), //
new RegexLeaf("\\$?"), //
new RegexLeaf("NAME", "([\\p{L}0-9_]+)"), //
RegexLeaf.spaceOneOrMore(), //
new RegexLeaf("FILE", "(.*)"), RegexLeaf.end());
2013-12-10 19:36:50 +00:00
}
@Override
2019-03-29 22:14:07 +00:00
protected CommandExecutionResult executeArg(UmlDiagram system, LineLocation location, RegexResult arg) {
2013-12-10 19:36:50 +00:00
final String src = arg.get("FILE", 0);
2016-11-04 21:39:29 +00:00
final Sprite sprite;
2013-12-10 19:36:50 +00:00
try {
2016-01-09 12:15:40 +00:00
if (src.startsWith("jar:")) {
final String inner = src.substring(4) + ".png";
final InputStream is = SpriteImage.getInternalSprite(inner);
if (is == null) {
return CommandExecutionResult.error("No such internal sprite: " + inner);
}
2016-11-04 21:39:29 +00:00
sprite = new SpriteImage(ImageIO.read(is));
} else if (src.contains("~")) {
final int idx = src.lastIndexOf("~");
final File f = FileSystem.getInstance().getFile(src.substring(0, idx));
if (f.exists() == false) {
return CommandExecutionResult.error("File does not exist: " + src);
}
final String name = src.substring(idx + 1);
sprite = getImageFromZip(f, name);
if (sprite == null) {
2018-10-21 19:44:14 +00:00
return CommandExecutionResult.error("No image " + name + " in " + FileWithSuffix.getFileName(f));
2016-11-04 21:39:29 +00:00
}
2016-01-09 12:15:40 +00:00
} else {
final File f = FileSystem.getInstance().getFile(src);
if (f.exists() == false) {
return CommandExecutionResult.error("File does not exist: " + src);
}
2016-11-04 21:39:29 +00:00
if (isSvg(f.getName())) {
sprite = new SpriteSvg(f);
} else {
2017-12-11 21:02:10 +00:00
sprite = new SpriteImage(FileUtils.ImageIO_read(f));
2016-11-04 21:39:29 +00:00
}
2013-12-10 19:36:50 +00:00
}
} catch (IOException e) {
Log.error("Error reading " + src + " " + e);
return CommandExecutionResult.error("Cannot read: " + src);
}
2016-11-04 21:39:29 +00:00
system.addSprite(arg.get("NAME", 0), sprite);
2013-12-10 19:36:50 +00:00
return CommandExecutionResult.ok();
}
2016-11-04 21:39:29 +00:00
private Sprite getImageFromZip(File f, String name) throws IOException {
ZipInputStream zis = null;
try {
zis = new ZipInputStream(new FileInputStream(f));
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
final String fileName = ze.getName();
if (ze.isDirectory()) {
} else if (fileName.equals(name)) {
if (isSvg(name)) {
return new SpriteSvg(FileUtils.readSvg(zis));
} else {
return new SpriteImage(ImageIO.read(zis));
}
}
ze = zis.getNextEntry();
}
} finally {
if (zis != null) {
zis.closeEntry();
zis.close();
}
}
return null;
}
private boolean isSvg(String name) {
return name.toLowerCase().endsWith(".svg");
}
2013-12-10 19:36:50 +00:00
}