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

233 lines
7.6 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2016-01-09 12:15:40 +00:00
* (C) Copyright 2009-2017, 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;
2011-08-08 17:48:29 +00:00
import java.io.BufferedOutputStream;
2011-01-05 18:23:06 +00:00
import java.io.File;
import java.io.FileOutputStream;
2010-11-15 20:35:36 +00:00
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;
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.ColorMapperIdentity;
import net.sourceforge.plantuml.ugraphic.ImageBuilder;
2010-11-15 20:35:36 +00:00
public class SourceStringReader {
final private List<BlockUml> blocks;
public SourceStringReader(String source) {
2017-04-05 17:37:42 +00:00
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) {
2017-04-05 17:37:42 +00:00
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);
}
2016-06-19 14:16:41 +00:00
public SourceStringReader(String source, File newCurrentDir) {
2017-04-05 17:37:42 +00:00
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) {
2016-06-19 14:16:41 +00:00
this(defines, source, charset, config, null);
}
public SourceStringReader(Defines defines, String source, String charset, List<String> config, File newCurrentDir) {
2015-04-07 18:18:37 +00:00
// WARNING GLOBAL LOCK HERE
synchronized (SourceStringReader.class) {
try {
2016-06-19 14:16:41 +00:00
final BlockUmlBuilder builder = new BlockUmlBuilder(config, charset, defines, new StringReader(source),
2018-06-12 20:50:45 +00:00
newCurrentDir, "string");
2015-04-07 18:18:37 +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);
}
@Deprecated
public String generateImage(File f) throws IOException {
return outputImage(f).getDescription();
}
public DiagramDescription outputImage(File f) throws IOException {
2011-08-08 17:48:29 +00:00
final OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
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()) {
2017-06-05 11:27:21 +00:00
// 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) {
2017-04-19 18:30:16 +00:00
// final ImageData imageData = system.exportDiagram(os, numImage, fileFormatOption);
// if (imageData.containsCMapData()) {
// return system.getDescription().withCMapData(imageData.getCMapData("plantuml"));
// }
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 {
final TextBlockBackcolored error = GraphicStrings.createForError(Arrays.asList("No @startuml found"),
fileFormatOption.isUseRedForError());
final ImageBuilder imageBuilder = new ImageBuilder(new ColorMapperIdentity(), 1.0, error.getBackcolor(), null,
null, 0, 0, null, false);
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);
}
}