plantuml/src/net/sourceforge/plantuml/SourceStringReader.java

244 lines
7.9 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://plantuml.com
2010-11-15 20:35:36 +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:
*
2023-02-22 18:43:48 +00:00
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
2017-03-15 19:13:31 +00:00
*
2010-11-15 20:35:36 +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
2013-12-10 19:36:50 +00:00
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
2010-11-15 20:35:36 +00:00
* 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.nio.charset.StandardCharsets.UTF_8;
2023-02-22 18:43:48 +00:00
import static net.atmp.ImageBuilder.plainImageBuilder;
import static net.sourceforge.plantuml.utils.CharsetUtils.charsetOrDefault;
2021-05-23 15:35:13 +00:00
2010-11-15 20:35:36 +00:00
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.nio.charset.Charset;
2010-11-15 20:35:36 +00:00
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.core.Diagram;
import net.sourceforge.plantuml.core.DiagramDescription;
import net.sourceforge.plantuml.core.ImageData;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.shape.GraphicStrings;
2023-02-26 18:51:17 +00:00
import net.sourceforge.plantuml.klimt.shape.TextBlock;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.preproc.Defines;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.SFile;
2022-12-17 11:01:10 +00:00
import net.sourceforge.plantuml.utils.Log;
2010-11-15 20:35:36 +00:00
public class SourceStringReader {
final private List<BlockUml> blocks;
public SourceStringReader(String source) {
this(Defines.createEmpty(), source, Collections.<String>emptyList());
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
public SourceStringReader(String source, String charset) {
this(Defines.createEmpty(), source, UTF_8.name(), Collections.<String>emptyList());
}
public SourceStringReader(String source, Charset charset) {
this(Defines.createEmpty(), source, charset.name(), Collections.<String>emptyList());
2013-12-10 19:36:50 +00:00
}
2010-11-15 20:35:36 +00:00
public SourceStringReader(Defines defines, String source, List<String> config) {
this(defines, source, UTF_8.name(), config);
2013-12-10 19:36:50 +00:00
}
2019-09-22 17:20:16 +00:00
public SourceStringReader(Defines defines, String source) {
this(defines, source, UTF_8.name(), Collections.<String>emptyList());
2019-09-22 17:20:16 +00:00
}
2020-05-30 15:20:23 +00:00
public SourceStringReader(String source, SFile newCurrentDir) {
this(Defines.createEmpty(), source, UTF_8, Collections.<String>emptyList(), newCurrentDir);
2016-06-19 14:16:41 +00:00
}
2013-12-10 19:36:50 +00:00
public SourceStringReader(Defines defines, String source, String charset, List<String> config) {
2018-10-21 19:44:14 +00:00
this(defines, source, charset, config, FileSystem.getInstance().getCurrentDir());
2016-06-19 14:16:41 +00:00
}
2022-02-10 18:16:18 +00:00
public SourceStringReader(Defines defines, String source, String charset, List<String> config,
SFile newCurrentDir) {
this(defines, source, charsetOrDefault(charset), config, newCurrentDir);
}
2022-02-10 18:16:18 +00:00
public SourceStringReader(Defines defines, String source, Charset charset, List<String> config,
2020-12-06 21:43:09 +00:00
SFile newCurrentDir) {
2018-10-21 19:44:14 +00:00
try {
2022-09-18 17:08:06 +00:00
final BlockUmlBuilder builder = new BlockUmlBuilder(config, charset, defines, new StringReader(source),
newCurrentDir, "string");
2018-10-21 19:44:14 +00:00
this.blocks = builder.getBlockUmls();
} catch (IOException e) {
Log.error("error " + e);
throw new IllegalStateException(e);
2010-11-15 20:35:36 +00:00
}
}
2015-04-07 18:18:37 +00:00
2017-04-19 18:30:16 +00:00
@Deprecated
public String generateImage(OutputStream os) throws IOException {
return outputImage(os).getDescription();
2010-11-15 20:35:36 +00:00
}
2017-04-19 18:30:16 +00:00
public DiagramDescription outputImage(OutputStream os) throws IOException {
return outputImage(os, 0);
}
2023-02-22 18:43:48 +00:00
// ::comment when CORE
2017-04-19 18:30:16 +00:00
@Deprecated
2020-05-30 15:20:23 +00:00
public String generateImage(SFile f) throws IOException {
2017-04-19 18:30:16 +00:00
return outputImage(f).getDescription();
}
2020-05-30 15:20:23 +00:00
public DiagramDescription outputImage(SFile f) throws IOException {
try (OutputStream os = f.createBufferedOutputStream()) {
return outputImage(os, 0);
2017-04-19 18:30:16 +00:00
}
2011-01-05 18:23:06 +00:00
}
2023-02-22 18:43:48 +00:00
// ::done
2011-01-05 18:23:06 +00:00
2017-04-19 18:30:16 +00:00
@Deprecated
public String generateImage(OutputStream os, FileFormatOption fileFormatOption) throws IOException {
2022-09-18 17:08:06 +00:00
return outputImage(os, fileFormatOption).getDescription();
2017-04-19 18:30:16 +00:00
}
public DiagramDescription outputImage(OutputStream os, FileFormatOption fileFormatOption) throws IOException {
2022-09-18 17:08:06 +00:00
return outputImage(os, 0, fileFormatOption);
2010-11-15 20:35:36 +00:00
}
2017-04-19 18:30:16 +00:00
@Deprecated
public String generateImage(OutputStream os, int numImage) throws IOException {
return outputImage(os, numImage).getDescription();
2010-11-15 20:35:36 +00:00
}
2017-04-19 18:30:16 +00:00
public DiagramDescription outputImage(OutputStream os, int numImage) throws IOException {
return outputImage(os, numImage, new FileFormatOption(FileFormat.PNG));
}
@Deprecated
public String generateImage(OutputStream os, int numImage, FileFormatOption fileFormatOption) throws IOException {
2022-09-18 17:08:06 +00:00
return outputImage(os, numImage, fileFormatOption).getDescription();
2017-04-19 18:30:16 +00:00
}
public DiagramDescription outputImage(OutputStream os, int numImage, FileFormatOption fileFormatOption)
2017-03-21 21:37:59 +00:00
throws IOException {
2010-11-15 20:35:36 +00:00
if (blocks.size() == 0) {
2021-03-23 13:06:33 +00:00
noStartumlFound(os, fileFormatOption);
2010-11-15 20:35:36 +00:00
return null;
}
2013-12-10 19:36:50 +00:00
for (BlockUml b : blocks) {
final Diagram system = b.getDiagram();
final int nbInSystem = system.getNbImages();
if (numImage < nbInSystem) {
2015-04-07 18:18:37 +00:00
// final CMapData cmap = new CMapData();
2013-12-10 19:36:50 +00:00
final ImageData imageData = system.exportDiagram(os, numImage, fileFormatOption);
2017-03-21 21:37:59 +00:00
// if (imageData.containsCMapData()) {
// return system.getDescription().getDescription() + BackSlash.BS_N +
// imageData.getCMapData("plantuml");
2017-03-21 21:37:59 +00:00
// }
return system.getDescription();
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
numImage -= nbInSystem;
}
Log.error("numImage is too big = " + numImage);
return null;
}
2017-04-19 18:30:16 +00:00
public DiagramDescription generateDiagramDescription(int numImage, FileFormatOption fileFormatOption) {
2022-09-20 20:35:41 +00:00
if (blocks.size() == 0)
2016-11-04 21:39:29 +00:00
return null;
2022-09-20 20:35:41 +00:00
2016-11-04 21:39:29 +00:00
for (BlockUml b : blocks) {
final Diagram system = b.getDiagram();
final int nbInSystem = system.getNbImages();
if (numImage < nbInSystem) {
// final ImageData imageData = system.exportDiagram(os, numImage,
// fileFormatOption);
2017-04-19 18:30:16 +00:00
// if (imageData.containsCMapData()) {
// return
// system.getDescription().withCMapData(imageData.getCMapData("plantuml"));
2017-04-19 18:30:16 +00:00
// }
return system.getDescription();
2016-11-04 21:39:29 +00:00
}
numImage -= nbInSystem;
}
2017-04-19 18:30:16 +00:00
Log.error("numImage is too big = " + numImage);
2016-11-04 21:39:29 +00:00
return null;
2015-04-07 18:18:37 +00:00
}
2013-12-10 19:36:50 +00:00
2017-03-21 21:37:59 +00:00
public DiagramDescription generateDiagramDescription() {
return generateDiagramDescription(0);
2013-12-10 19:36:50 +00:00
}
2017-03-21 21:37:59 +00:00
public DiagramDescription generateDiagramDescription(FileFormatOption fileFormatOption) {
2022-09-18 17:08:06 +00:00
return generateDiagramDescription(0, fileFormatOption);
2013-12-10 19:36:50 +00:00
}
2017-03-21 21:37:59 +00:00
public DiagramDescription generateDiagramDescription(int numImage) {
2022-09-18 17:08:06 +00:00
return generateDiagramDescription(numImage, new FileFormatOption(FileFormat.PNG));
2013-12-10 19:36:50 +00:00
}
2017-04-19 18:30:16 +00:00
public String getCMapData(int numImage, FileFormatOption fileFormatOption) throws IOException {
2022-09-20 20:35:41 +00:00
if (blocks.size() == 0)
2010-11-15 20:35:36 +00:00
return null;
2022-09-20 20:35:41 +00:00
2013-12-10 19:36:50 +00:00
for (BlockUml b : blocks) {
final Diagram system = b.getDiagram();
final int nbInSystem = system.getNbImages();
if (numImage < nbInSystem) {
2017-04-19 18:30:16 +00:00
final ImageData imageData = system.exportDiagram(new NullOutputStream(), numImage, fileFormatOption);
2022-09-20 20:35:41 +00:00
if (imageData.containsCMapData())
2017-04-19 18:30:16 +00:00
return imageData.getCMapData("plantuml");
2022-09-20 20:35:41 +00:00
2017-04-19 18:30:16 +00:00
return null;
2013-12-10 19:36:50 +00:00
}
numImage -= nbInSystem;
}
2010-11-15 20:35:36 +00:00
return null;
}
2021-03-23 13:06:33 +00:00
public ImageData noStartumlFound(OutputStream os, FileFormatOption fileFormatOption) throws IOException {
2023-02-26 18:51:17 +00:00
final TextBlock error = GraphicStrings.createForError(Arrays.asList("No @startuml/@enduml found"),
2017-04-19 18:30:16 +00:00
fileFormatOption.isUseRedForError());
2022-02-10 18:16:18 +00:00
return plainImageBuilder(error, fileFormatOption).write(os);
2017-04-19 18:30:16 +00:00
}
2010-11-15 20:35:36 +00:00
public final List<BlockUml> getBlocks() {
return Collections.unmodifiableList(blocks);
}
}