2018-04-06 20:36:30 +00:00
|
|
|
/* ========================================================================
|
|
|
|
* PlantUML : a free UML diagram generator
|
|
|
|
* ========================================================================
|
|
|
|
*
|
2022-03-07 19:33:46 +00:00
|
|
|
* (C) Copyright 2009-2023, Arnaud Roques
|
2018-04-06 20:36:30 +00:00
|
|
|
*
|
|
|
|
* Project Info: http://plantuml.com
|
2022-08-17 17:34:24 +00:00
|
|
|
*
|
2018-04-06 20:36:30 +00:00
|
|
|
* If you like this project or if you find it useful, you can support us at:
|
2022-08-17 17:34:24 +00:00
|
|
|
*
|
2018-04-06 20:36:30 +00:00
|
|
|
* http://plantuml.com/patreon (only 1$ per month!)
|
|
|
|
* http://plantuml.com/paypal
|
2022-08-17 17:34:24 +00:00
|
|
|
*
|
2018-04-06 20:36:30 +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;
|
|
|
|
|
2021-09-13 04:37:17 +00:00
|
|
|
import static net.sourceforge.plantuml.utils.CharsetUtils.charsetOrDefault;
|
|
|
|
|
2020-05-30 15:20:23 +00:00
|
|
|
import java.io.BufferedInputStream;
|
2018-04-06 20:36:30 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.PrintStream;
|
|
|
|
import java.io.Reader;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
2021-09-13 04:37:17 +00:00
|
|
|
import java.nio.charset.Charset;
|
2018-04-06 20:36:30 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2021-06-27 16:50:40 +00:00
|
|
|
import net.sourceforge.plantuml.api.ImageDataSimple;
|
2022-09-12 20:08:34 +00:00
|
|
|
import net.sourceforge.plantuml.awt.geom.XDimension2D;
|
2018-04-06 20:36:30 +00:00
|
|
|
import net.sourceforge.plantuml.core.Diagram;
|
2019-05-24 19:59:31 +00:00
|
|
|
import net.sourceforge.plantuml.error.PSystemError;
|
2022-08-17 17:34:24 +00:00
|
|
|
import net.sourceforge.plantuml.log.Logme;
|
2021-09-11 09:02:36 +00:00
|
|
|
import net.sourceforge.plantuml.preproc.Defines;
|
2018-04-06 20:36:30 +00:00
|
|
|
import net.sourceforge.plantuml.preproc.FileWithSuffix;
|
2020-05-30 15:20:23 +00:00
|
|
|
import net.sourceforge.plantuml.security.SFile;
|
|
|
|
import net.sourceforge.plantuml.security.SecurityUtils;
|
2022-12-17 11:01:10 +00:00
|
|
|
import net.sourceforge.plantuml.utils.Log;
|
2018-04-06 20:36:30 +00:00
|
|
|
|
2021-06-27 16:50:40 +00:00
|
|
|
public abstract class SourceFileReaderAbstract implements ISourceFileReader {
|
2023-01-31 19:27:04 +00:00
|
|
|
// ::remove file when WASM
|
2018-04-06 20:36:30 +00:00
|
|
|
|
2022-09-20 20:35:41 +00:00
|
|
|
final private File file;
|
2018-04-06 20:36:30 +00:00
|
|
|
|
2022-09-20 20:35:41 +00:00
|
|
|
private FileFormatOption fileFormatOption;
|
2018-05-01 17:26:04 +00:00
|
|
|
private boolean checkMetadata;
|
2021-06-27 16:50:40 +00:00
|
|
|
private boolean noerror;
|
2018-05-01 17:26:04 +00:00
|
|
|
|
2022-09-20 20:35:41 +00:00
|
|
|
final private Charset charset;
|
|
|
|
|
|
|
|
private final BlockUmlBuilder builder;
|
|
|
|
private int cpt;
|
|
|
|
|
|
|
|
protected final SuggestedFile getSuggestedFile(File outputDirectory, String newName) {
|
|
|
|
final File outFile = new File(outputDirectory, newName);
|
|
|
|
return SuggestedFile.fromOutputFile(outFile, getFileFormatOption().getFileFormat(), cpt++);
|
|
|
|
}
|
|
|
|
|
2022-02-10 18:16:18 +00:00
|
|
|
public SourceFileReaderAbstract(File file, FileFormatOption fileFormatOption, Defines defines, List<String> config,
|
|
|
|
String charsetName) throws IOException {
|
|
|
|
|
2022-09-20 20:35:41 +00:00
|
|
|
if (file.exists() == false)
|
2021-09-11 09:02:36 +00:00
|
|
|
throw new IllegalArgumentException();
|
2021-09-13 04:37:17 +00:00
|
|
|
|
2021-09-11 09:02:36 +00:00
|
|
|
this.file = file;
|
2022-09-20 20:35:41 +00:00
|
|
|
this.charset = charsetOrDefault(charsetName);
|
2021-09-11 09:02:36 +00:00
|
|
|
this.fileFormatOption = fileFormatOption;
|
2022-09-20 20:35:41 +00:00
|
|
|
this.builder = new BlockUmlBuilder(config, charset, defines, getReader(charset),
|
|
|
|
SFile.fromFile(file.getAbsoluteFile().getParentFile()), FileWithSuffix.getFileName(file));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected final FileFormatOption getFileFormatOption() {
|
|
|
|
return fileFormatOption;
|
2021-09-11 09:02:36 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 17:26:04 +00:00
|
|
|
public void setCheckMetadata(boolean checkMetadata) {
|
|
|
|
this.checkMetadata = checkMetadata;
|
|
|
|
}
|
2018-04-06 20:36:30 +00:00
|
|
|
|
|
|
|
public boolean hasError() {
|
2022-09-20 20:35:41 +00:00
|
|
|
for (final BlockUml b : builder.getBlockUmls())
|
|
|
|
if (b.getDiagram() instanceof PSystemError)
|
2018-04-06 20:36:30 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<BlockUml> getBlocks() {
|
|
|
|
return builder.getBlockUmls();
|
|
|
|
}
|
|
|
|
|
2021-09-13 04:37:17 +00:00
|
|
|
protected Reader getReader(Charset charset) throws FileNotFoundException, UnsupportedEncodingException {
|
2020-05-30 15:20:23 +00:00
|
|
|
return new InputStreamReader(new BufferedInputStream(new FileInputStream(file)), charset);
|
2018-04-06 20:36:30 +00:00
|
|
|
}
|
|
|
|
|
2022-09-20 20:35:41 +00:00
|
|
|
public final Set<FileWithSuffix> getIncludedFiles() throws IOException {
|
2018-04-06 20:36:30 +00:00
|
|
|
return builder.getIncludedFiles();
|
|
|
|
}
|
|
|
|
|
2022-09-20 20:35:41 +00:00
|
|
|
@Override
|
|
|
|
public final ISourceFileReader setFileFormatOption(FileFormatOption fileFormatOption) {
|
2018-04-06 20:36:30 +00:00
|
|
|
this.fileFormatOption = fileFormatOption;
|
2022-09-20 20:35:41 +00:00
|
|
|
return this;
|
2018-04-06 20:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean endsWithSlashOrAntislash(String newName) {
|
|
|
|
return newName.endsWith("/") || newName.endsWith("\\");
|
|
|
|
}
|
|
|
|
|
2020-05-30 15:20:23 +00:00
|
|
|
private List<GeneratedImage> getCrashedImage(BlockUml blockUml, Throwable t, SFile outputFile) throws IOException {
|
2018-05-21 13:07:09 +00:00
|
|
|
final GeneratedImage image = new GeneratedImageImpl(outputFile, "Crash Error", blockUml, FileImageData.CRASH);
|
2021-09-10 14:52:29 +00:00
|
|
|
try (OutputStream os = outputFile.createBufferedOutputStream()) {
|
2018-04-06 20:36:30 +00:00
|
|
|
UmlDiagram.exportDiagramError(os, t, fileFormatOption, 42, null, blockUml.getFlashData(),
|
|
|
|
UmlDiagram.getFailureText2(t, blockUml.getFlashData()));
|
|
|
|
}
|
|
|
|
return Collections.singletonList(image);
|
|
|
|
}
|
|
|
|
|
2021-06-27 16:50:40 +00:00
|
|
|
protected void exportWarnOrErrIfWord(SFile f, Diagram system) throws FileNotFoundException {
|
|
|
|
if (OptionFlags.getInstance().isWord() && f != null) {
|
2018-04-06 20:36:30 +00:00
|
|
|
final String warnOrError = system.getWarningOrError();
|
|
|
|
if (warnOrError != null) {
|
|
|
|
final String name = f.getName().substring(0, f.getName().length() - 4) + ".err";
|
2020-05-30 15:20:23 +00:00
|
|
|
final SFile errorFile = f.getParentFile().file(name);
|
2021-09-10 14:52:29 +00:00
|
|
|
try (PrintStream ps = SecurityUtils.createPrintStream(errorFile.createFileOutputStream())) {
|
|
|
|
ps.print(warnOrError);
|
|
|
|
}
|
2018-04-06 20:36:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final public List<GeneratedImage> getGeneratedImages() throws IOException {
|
|
|
|
Log.info("Reading file: " + file);
|
|
|
|
|
|
|
|
cpt = 0;
|
2021-05-14 08:42:57 +00:00
|
|
|
final List<GeneratedImage> result = new ArrayList<>();
|
2018-04-06 20:36:30 +00:00
|
|
|
|
|
|
|
for (BlockUml blockUml : builder.getBlockUmls()) {
|
2018-07-27 21:56:46 +00:00
|
|
|
final SuggestedFile suggested = getSuggestedFile(blockUml);
|
2018-04-06 20:36:30 +00:00
|
|
|
|
|
|
|
final Diagram system;
|
|
|
|
try {
|
|
|
|
system = blockUml.getDiagram();
|
|
|
|
} catch (Throwable t) {
|
2022-08-17 17:34:24 +00:00
|
|
|
Logme.error(t);
|
2022-09-20 20:35:41 +00:00
|
|
|
if (OptionFlags.getInstance().isSilentlyCompletelyIgnoreErrors() || noerror)
|
2021-06-27 16:50:40 +00:00
|
|
|
continue;
|
2022-09-20 20:35:41 +00:00
|
|
|
|
2018-04-06 20:36:30 +00:00
|
|
|
return getCrashedImage(blockUml, t, suggested.getFile(0));
|
|
|
|
}
|
|
|
|
|
2022-09-20 20:35:41 +00:00
|
|
|
if (OptionFlags.getInstance().isSilentlyCompletelyIgnoreErrors() && system instanceof PSystemError)
|
2018-11-26 18:46:22 +00:00
|
|
|
continue;
|
|
|
|
|
2020-05-30 15:20:23 +00:00
|
|
|
OptionFlags.getInstance().logData(SFile.fromFile(file), system);
|
2021-06-27 16:50:40 +00:00
|
|
|
final List<FileImageData> exportDiagrams;
|
2021-07-25 10:41:36 +00:00
|
|
|
if (noerror && system instanceof PSystemError) {
|
2021-06-27 16:50:40 +00:00
|
|
|
exportDiagrams = new ArrayList<FileImageData>();
|
2022-09-20 20:35:41 +00:00
|
|
|
exportDiagrams
|
|
|
|
.add(new FileImageData(null, new ImageDataSimple(new XDimension2D(0, 0), FileImageData.ERROR)));
|
2021-06-27 16:50:40 +00:00
|
|
|
} else
|
|
|
|
exportDiagrams = PSystemUtils.exportDiagrams(system, suggested, fileFormatOption, checkMetadata);
|
|
|
|
|
2022-09-20 20:35:41 +00:00
|
|
|
if (exportDiagrams.size() > 1)
|
2018-04-06 20:36:30 +00:00
|
|
|
cpt += exportDiagrams.size() - 1;
|
|
|
|
|
|
|
|
for (FileImageData fdata : exportDiagrams) {
|
|
|
|
final String desc = "[" + file.getName() + "] " + system.getDescription();
|
2020-05-30 15:20:23 +00:00
|
|
|
final SFile f = fdata.getFile();
|
2018-04-06 20:36:30 +00:00
|
|
|
exportWarnOrErrIfWord(f, system);
|
2018-05-21 13:07:09 +00:00
|
|
|
final GeneratedImage generatedImage = new GeneratedImageImpl(f, desc, blockUml, fdata.getStatus());
|
2018-04-06 20:36:30 +00:00
|
|
|
result.add(generatedImage);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.info("Number of image(s): " + result.size());
|
|
|
|
|
|
|
|
return Collections.unmodifiableList(result);
|
|
|
|
}
|
|
|
|
|
2020-05-30 15:20:23 +00:00
|
|
|
abstract protected SuggestedFile getSuggestedFile(BlockUml blockUml) throws FileNotFoundException;
|
2018-04-06 20:36:30 +00:00
|
|
|
|
2021-06-27 16:50:40 +00:00
|
|
|
protected final void setNoerror(boolean noerror) {
|
|
|
|
this.noerror = noerror;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-20 20:35:41 +00:00
|
|
|
final protected String getFileName() {
|
|
|
|
return file.getName();
|
|
|
|
}
|
|
|
|
|
2018-04-06 20:36:30 +00:00
|
|
|
}
|