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

209 lines
7.0 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
*
* 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) {
this(new Defines(), source, Collections.<String> emptyList());
}
2013-12-10 19:36:50 +00:00
public SourceStringReader(String source, String charset) {
this(new Defines(), source, "UTF-8", Collections.<String> emptyList());
}
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) {
this(new Defines(), source, "UTF-8", Collections.<String> emptyList(), newCurrentDir);
}
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),
newCurrentDir, null);
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
2010-11-15 20:35:36 +00:00
public String generateImage(OutputStream os) throws IOException {
return generateImage(os, 0);
}
2011-01-05 18:23:06 +00:00
public String generateImage(File f) throws IOException {
2011-08-08 17:48:29 +00:00
final OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
2011-01-05 18:23:06 +00:00
final String result = generateImage(os, 0);
os.close();
return result;
}
public String generateImage(OutputStream os, FileFormatOption fileFormatOption) throws IOException {
return generateImage(os, 0, fileFormatOption);
2010-11-15 20:35:36 +00:00
}
public String generateImage(OutputStream os, int numImage) throws IOException {
2011-01-05 18:23:06 +00:00
return generateImage(os, numImage, new FileFormatOption(FileFormat.PNG));
2010-11-15 20:35:36 +00:00
}
2011-01-05 18:23:06 +00:00
public String generateImage(OutputStream os, int numImage, FileFormatOption fileFormatOption) throws IOException {
2010-11-15 20:35:36 +00:00
if (blocks.size() == 0) {
2015-04-07 18:18:37 +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);
if (imageData.containsCMapData()) {
return system.getDescription().getDescription() + "\n" + imageData.getCMapData("plantuml");
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
return system.getDescription().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;
}
2016-11-04 21:39:29 +00:00
public String getCMapData(int numImage, FileFormatOption fileFormatOption) throws IOException {
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(new NullOutputStream(), numImage, fileFormatOption);
if (imageData.containsCMapData()) {
return imageData.getCMapData("plantuml");
}
return null;
}
numImage -= nbInSystem;
}
return null;
}
2015-04-07 18:18:37 +00:00
private void noStartumlFound(OutputStream os, FileFormatOption fileFormatOption) throws IOException {
2016-11-18 21:12:09 +00:00
final TextBlockBackcolored error = GraphicStrings.createForError(Arrays.asList("No @startuml found"),
2015-04-07 18:18:37 +00:00
fileFormatOption.isUseRedForError());
final ImageBuilder imageBuilder = new ImageBuilder(new ColorMapperIdentity(), 1.0, error.getBackcolor(), null,
null, 0, 0, null, false);
2016-03-06 16:47:34 +00:00
imageBuilder.setUDrawable(error);
2015-05-31 18:56:03 +00:00
imageBuilder.writeImageTOBEMOVED(fileFormatOption, os);
2015-04-07 18:18:37 +00:00
}
2013-12-10 19:36:50 +00:00
public DiagramDescription generateDiagramDescription(OutputStream os) throws IOException {
return generateDiagramDescription(os, 0);
}
public DiagramDescription generateDiagramDescription(File f) throws IOException {
final OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
final DiagramDescription result = generateDiagramDescription(os, 0);
os.close();
return result;
}
2015-04-07 18:18:37 +00:00
public DiagramDescription generateDiagramDescription(OutputStream os, FileFormatOption fileFormatOption)
throws IOException {
2013-12-10 19:36:50 +00:00
return generateDiagramDescription(os, 0, fileFormatOption);
}
public DiagramDescription generateDiagramDescription(OutputStream os, int numImage) throws IOException {
return generateDiagramDescription(os, numImage, new FileFormatOption(FileFormat.PNG));
}
2015-04-07 18:18:37 +00:00
public DiagramDescription generateDiagramDescription(OutputStream os, int numImage,
FileFormatOption fileFormatOption) throws IOException {
2013-12-10 19:36:50 +00:00
if (blocks.size() == 0) {
2015-04-07 18:18:37 +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) {
2017-03-12 17:22:02 +00:00
// final ImageData imageData = system.exportDiagram(os, numImage, fileFormatOption);
// if (imageData.containsCMapData()) {
// return system.getDescription().withCMapData(imageData.getCMapData("plantuml"));
// }
2013-12-10 19:36:50 +00:00
return system.getDescription();
}
numImage -= nbInSystem;
}
2011-08-08 17:48:29 +00:00
Log.error("numImage is too big = " + numImage);
2010-11-15 20:35:36 +00:00
return null;
}
public final List<BlockUml> getBlocks() {
return Collections.unmodifiableList(blocks);
}
}