1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-07 19:00:53 +00:00
plantuml/src/net/sourceforge/plantuml/PSystemUtils.java

221 lines
7.7 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, 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;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
2013-12-10 19:36:50 +00:00
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
2022-11-07 19:27:11 +00:00
import net.sourceforge.plantuml.baraye.CucaDiagram;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.core.Diagram;
import net.sourceforge.plantuml.core.ImageData;
import net.sourceforge.plantuml.html.CucaDiagramHtmlMaker;
import net.sourceforge.plantuml.png.PngSplitter;
2020-02-18 21:24:31 +00:00
import net.sourceforge.plantuml.project.GanttDiagram;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.SFile;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.sequencediagram.SequenceDiagram;
2022-08-19 16:34:21 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColors;
2022-12-17 11:01:10 +00:00
import net.sourceforge.plantuml.utils.Log;
2013-12-10 19:36:50 +00:00
public class PSystemUtils {
// :: remove file when WASM
2013-12-10 19:36:50 +00:00
2018-05-01 17:26:04 +00:00
public static List<FileImageData> exportDiagrams(Diagram system, SuggestedFile suggested,
2017-03-12 17:22:02 +00:00
FileFormatOption fileFormatOption) throws IOException {
2018-05-01 17:26:04 +00:00
return exportDiagrams(system, suggested, fileFormatOption, false);
}
public static List<FileImageData> exportDiagrams(Diagram system, SuggestedFile suggestedFile,
FileFormatOption fileFormatOption, boolean checkMetadata) throws IOException {
// ::comment when WASM
2020-05-30 15:20:23 +00:00
final SFile existingFile = suggestedFile.getFile(0);
2022-12-04 18:59:49 +00:00
if (checkMetadata && fileFormatOption.getFileFormat().doesSupportMetadata() && existingFile.exists()) {
// && system.getNbImages() == 1) {
2019-09-14 18:12:04 +00:00
final boolean sameMetadata = fileFormatOption.getFileFormat().equalsMetadata(system.getMetadata(),
existingFile);
2018-05-01 17:26:04 +00:00
if (sameMetadata) {
2020-05-30 15:20:23 +00:00
Log.info("Skipping " + existingFile.getPrintablePath() + " because metadata has not changed.");
2019-09-14 18:12:04 +00:00
return Arrays.asList(new FileImageData(existingFile, null));
2018-05-01 17:26:04 +00:00
}
}
// ::done
2018-05-01 17:26:04 +00:00
2022-12-04 18:59:49 +00:00
if (system instanceof NewpagedDiagram)
2016-12-01 20:29:25 +00:00
return exportDiagramsNewpaged((NewpagedDiagram) system, suggestedFile, fileFormatOption);
2022-12-04 18:59:49 +00:00
if (system instanceof SequenceDiagram)
2016-12-01 20:29:25 +00:00
return exportDiagramsSequence((SequenceDiagram) system, suggestedFile, fileFormatOption);
2022-12-04 18:59:49 +00:00
// ::comment when WASM
2022-12-04 18:59:49 +00:00
if (system instanceof CucaDiagram && fileFormatOption.getFileFormat() == FileFormat.HTML)
return createFilesHtml((CucaDiagram) system, suggestedFile);
// ::done
return exportDiagramsDefault(system, suggestedFile, fileFormatOption);
2013-12-10 19:36:50 +00:00
}
2017-04-19 18:30:16 +00:00
private static List<FileImageData> exportDiagramsNewpaged(NewpagedDiagram system, SuggestedFile suggestedFile,
2013-12-10 19:36:50 +00:00
FileFormatOption fileFormat) throws IOException {
2021-05-14 08:42:57 +00:00
final List<FileImageData> result = new ArrayList<>();
2013-12-10 19:36:50 +00:00
final int nbImages = system.getNbImages();
for (int i = 0; i < nbImages; i++) {
2020-05-30 15:20:23 +00:00
final SFile f = suggestedFile.getFile(i);
2022-12-04 18:59:49 +00:00
if (canFileBeWritten(f) == false)
2013-12-10 19:36:50 +00:00
return result;
2022-12-04 18:59:49 +00:00
2020-05-30 15:20:23 +00:00
final OutputStream fos = f.createBufferedOutputStream();
2017-03-12 17:22:02 +00:00
ImageData cmap = null;
2013-12-10 19:36:50 +00:00
try {
2017-03-12 17:22:02 +00:00
system.exportDiagram(fos, i, fileFormat);
2013-12-10 19:36:50 +00:00
} finally {
fos.close();
}
// if (system.hasUrl() && cmap != null && cmap.containsCMapData()) {
// system.exportCmap(suggestedFile, cmap);
// }
Log.info("File size : " + f.length());
2017-03-12 17:22:02 +00:00
result.add(new FileImageData(f, cmap));
2013-12-10 19:36:50 +00:00
}
return result;
}
2020-05-30 15:20:23 +00:00
public static boolean canFileBeWritten(final SFile f) {
Log.info("Creating file: " + f.getAbsolutePath());
2013-12-10 19:36:50 +00:00
if (f.exists() && f.canWrite() == false) {
if (OptionFlags.getInstance().isOverwrite()) {
Log.info("Overwrite " + f);
f.setWritable(true);
f.delete();
return true;
}
2020-05-30 15:20:23 +00:00
Log.error("Cannot write to file " + f.getAbsolutePath());
2013-12-10 19:36:50 +00:00
return false;
}
return true;
}
2017-04-19 18:30:16 +00:00
private static List<FileImageData> exportDiagramsSequence(SequenceDiagram system, SuggestedFile suggestedFile,
2013-12-10 19:36:50 +00:00
FileFormatOption fileFormat) throws IOException {
2021-05-14 08:42:57 +00:00
final List<FileImageData> result = new ArrayList<>();
2013-12-10 19:36:50 +00:00
final int nbImages = system.getNbImages();
for (int i = 0; i < nbImages; i++) {
2020-05-30 15:20:23 +00:00
final SFile f = suggestedFile.getFile(i);
2022-12-04 18:59:49 +00:00
if (PSystemUtils.canFileBeWritten(suggestedFile.getFile(i)) == false)
2013-12-10 19:36:50 +00:00
return result;
2022-12-04 18:59:49 +00:00
2020-05-30 15:20:23 +00:00
final OutputStream fos = f.createBufferedOutputStream();
2013-12-10 19:36:50 +00:00
ImageData cmap = null;
try {
cmap = system.exportDiagram(fos, i, fileFormat);
} finally {
fos.close();
}
// ::comment when SPAM
2022-12-04 18:59:49 +00:00
if (cmap != null && cmap.containsCMapData())
2017-04-19 18:30:16 +00:00
system.exportCmap(suggestedFile, i, cmap);
2022-12-04 18:59:49 +00:00
2013-12-10 19:36:50 +00:00
Log.info("File size : " + f.length());
// ::done
2017-03-12 17:22:02 +00:00
result.add(new FileImageData(f, cmap));
2013-12-10 19:36:50 +00:00
}
return result;
}
2017-04-19 18:30:16 +00:00
private static List<FileImageData> createFilesHtml(CucaDiagram system, SuggestedFile suggestedFile)
throws IOException {
2013-12-10 19:36:50 +00:00
final String name = suggestedFile.getName();
final int idx = name.lastIndexOf('.');
2020-05-30 15:20:23 +00:00
final SFile dir = suggestedFile.getParentFile().file(name.substring(0, idx));
2013-12-10 19:36:50 +00:00
final CucaDiagramHtmlMaker maker = new CucaDiagramHtmlMaker(system, dir);
return maker.create();
}
2022-11-04 17:36:03 +00:00
private static List<FileImageData> splitPng(TitledDiagram diagram, SuggestedFile pngFile, ImageData imageData,
FileFormatOption fileFormatOption) throws IOException {
2022-11-04 17:36:03 +00:00
final List<SFile> files = new PngSplitter(fileFormatOption.getColorMapper(), pngFile,
diagram.getSplitPagesHorizontal(), diagram.getSplitPagesVertical(),
fileFormatOption.isWithMetadata() ? diagram.getMetadata() : null, diagram.getSkinParam().getDpi(),
diagram instanceof GanttDiagram ? new SplitParam(HColors.BLACK, null, 5) // for backwards compatibility
: diagram.getSkinParam().getSplitParam()).getFiles();
final List<FileImageData> result = new ArrayList<>();
2022-12-04 18:59:49 +00:00
for (SFile f : files)
result.add(new FileImageData(f, imageData));
2022-12-04 18:59:49 +00:00
return result;
}
private static List<FileImageData> exportDiagramsDefault(Diagram system, SuggestedFile suggestedFile,
FileFormatOption fileFormatOption) throws IOException {
final SFile outputFile = suggestedFile.getFile(0);
2022-12-04 18:59:49 +00:00
if (outputFile.isDirectory())
throw new IllegalArgumentException("File is a directory " + suggestedFile);
2022-12-04 18:59:49 +00:00
if (!canFileBeWritten(outputFile))
return emptyList();
final ImageData imageData;
try (OutputStream os = outputFile.createBufferedOutputStream()) {
imageData = system.exportDiagram(os, 0, fileFormatOption);
}
2022-12-04 18:59:49 +00:00
if (imageData == null)
return emptyList();
// ::comment when SPAM
2022-12-04 18:59:49 +00:00
if (imageData.containsCMapData() && system instanceof UmlDiagram)
((UmlDiagram) system).exportCmap(suggestedFile, 0, imageData);
// ::done
2022-12-04 18:59:49 +00:00
if (system instanceof TitledDiagram && fileFormatOption.getFileFormat() == FileFormat.PNG)
return splitPng((TitledDiagram) system, suggestedFile, imageData, fileFormatOption);
return singletonList(new FileImageData(outputFile, imageData));
}
2013-12-10 19:36:50 +00:00
}