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

174 lines
5.9 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2013-12-10 19:36:50 +00:00
* (C) Copyright 2009-2013, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
* Project Info: http://plantuml.sourceforge.net
*
* 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.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* Original Author: Arnaud Roques
*
* Revision $Revision: 4041 $
*
*/
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.DiagramDescriptionImpl;
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;
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);
}
public SourceStringReader(Defines defines, String source, String charset, List<String> config) {
2010-11-15 20:35:36 +00:00
try {
2013-12-10 19:36:50 +00:00
final BlockUmlBuilder builder = new BlockUmlBuilder(config, charset, defines, new StringReader(source),
null);
2010-11-15 20:35:36 +00:00
this.blocks = builder.getBlockUmls();
} catch (IOException e) {
2013-12-10 19:36:50 +00:00
Log.error("error " + e);
throw new IllegalStateException(e);
2010-11-15 20:35:36 +00:00
}
}
2013-12-10 19:36:50 +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) {
final GraphicStrings error = new GraphicStrings(Arrays.asList("No @startuml found"));
2013-12-10 19:36:50 +00:00
error.writeImage(os, fileFormatOption, null);
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) {
//final CMapData cmap = new CMapData();
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;
}
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;
}
public DiagramDescription generateDiagramDescription(OutputStream os, FileFormatOption fileFormatOption) throws IOException {
return generateDiagramDescription(os, 0, fileFormatOption);
}
public DiagramDescription generateDiagramDescription(OutputStream os, int numImage) throws IOException {
return generateDiagramDescription(os, numImage, new FileFormatOption(FileFormat.PNG));
}
public DiagramDescription generateDiagramDescription(OutputStream os, int numImage, FileFormatOption fileFormatOption)
throws IOException {
if (blocks.size() == 0) {
final GraphicStrings error = new GraphicStrings(Arrays.asList("No @startuml found"));
error.writeImage(os, fileFormatOption, null);
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) {
// final CMapData cmap = new CMapData();
final ImageData imageData = system.exportDiagram(os, numImage, fileFormatOption);
if (imageData.containsCMapData()) {
return ((DiagramDescriptionImpl) system.getDescription()).withCMapData(imageData
.getCMapData("plantuml"));
}
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);
}
}