1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-05-29 06:30:48 +00:00
plantuml/src/net/sourceforge/plantuml/SourceStringReader.java

238 lines
7.8 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://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:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
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 java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
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;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.graphic.GraphicStrings;
import net.sourceforge.plantuml.preproc.Defines;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.SFile;
2016-11-18 21:12:09 +00:00
import net.sourceforge.plantuml.svek.TextBlockBackcolored;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.ugraphic.ImageBuilder;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.ColorMapperIdentity;
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", 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) {
2013-12-10 19:36:50 +00:00
this(defines, source, "UTF-8", config);
}
2019-09-22 17:20:16 +00:00
public SourceStringReader(Defines defines, String source) {
this(defines, source, "UTF-8", 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
}
2020-05-30 15:20:23 +00:00
public SourceStringReader(Defines defines, String source, String charset, List<String> config, SFile newCurrentDir) {
2018-10-21 19:44:14 +00:00
// // WARNING GLOBAL LOCK HERE
// synchronized (SourceStringReader.class) {
try {
final BlockUmlBuilder builder = new BlockUmlBuilder(config, charset, defines, new StringReader(source),
newCurrentDir, "string");
this.blocks = builder.getBlockUmls();
} catch (IOException e) {
Log.error("error " + e);
throw new IllegalStateException(e);
2010-11-15 20:35:36 +00:00
}
2018-10-21 19:44:14 +00:00
// }
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);
}
@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 {
final OutputStream os = f.createBufferedOutputStream();
2017-04-19 18:30:16 +00:00
DiagramDescription result = null;
try {
result = outputImage(os, 0);
} finally {
os.close();
}
2011-01-05 18:23:06 +00:00
return result;
}
2017-04-19 18:30:16 +00:00
@Deprecated
public String generateImage(OutputStream os, FileFormatOption fileFormatOption) throws IOException {
return outputImage(os, fileFormatOption).getDescription();
}
public DiagramDescription outputImage(OutputStream os, FileFormatOption fileFormatOption) throws IOException {
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 {
return outputImage(os, numImage, fileFormatOption).getDescription();
}
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) {
2017-04-19 18:30:16 +00:00
noStartumlFound(os, fileFormatOption, 42);
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) {
2016-11-04 21:39:29 +00:00
if (blocks.size() == 0) {
return null;
}
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) {
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) {
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 {
2013-12-10 19:36:50 +00:00
if (blocks.size() == 0) {
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) {
2017-04-19 18:30:16 +00:00
final ImageData imageData = system.exportDiagram(new NullOutputStream(), numImage, fileFormatOption);
if (imageData.containsCMapData()) {
return imageData.getCMapData("plantuml");
}
return null;
2013-12-10 19:36:50 +00:00
}
numImage -= nbInSystem;
}
2010-11-15 20:35:36 +00:00
return null;
}
2017-04-19 18:30:16 +00:00
private void noStartumlFound(OutputStream os, FileFormatOption fileFormatOption, long seed) throws IOException {
2019-12-10 21:45:49 +00:00
final TextBlockBackcolored error = GraphicStrings.createForError(Arrays.asList("No @startuml/@enduml found"),
2017-04-19 18:30:16 +00:00
fileFormatOption.isUseRedForError());
2020-05-30 15:20:23 +00:00
final ImageBuilder imageBuilder = ImageBuilder.buildA(new ColorMapperIdentity(), false, null, null, null, 1.0,
error.getBackcolor());
2017-04-19 18:30:16 +00:00
imageBuilder.setUDrawable(error);
imageBuilder.writeImageTOBEMOVED(fileFormatOption, seed, os);
}
2010-11-15 20:35:36 +00:00
public final List<BlockUml> getBlocks() {
return Collections.unmodifiableList(blocks);
}
}