1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-02 08:30:49 +00:00
plantuml/src/com/plantuml/wasm/Png.java
Arnaud Roques 62d819dae4 wip
2023-02-06 22:11:35 +01:00

108 lines
3.2 KiB
Java

/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2023, Arnaud Roques
*
* Project Info: http://plantuml.com
*
* 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
*
* 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
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* 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 com.plantuml.wasm;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Collections;
import java.util.List;
import net.sourceforge.plantuml.BlockUml;
import net.sourceforge.plantuml.BlockUmlBuilder;
import net.sourceforge.plantuml.ErrorUml;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.core.Diagram;
import net.sourceforge.plantuml.core.ImageData;
import net.sourceforge.plantuml.error.PSystemError;
import net.sourceforge.plantuml.preproc.Defines;
public class Png {
public static int convert(String pathOut, String text) {
WasmLog.start = System.currentTimeMillis();
WasmLog.log("Starting processing");
try {
final FileFormatOption format = new FileFormatOption(FileFormat.PNG);
text = cleanText(text);
final BlockUmlBuilder builder = new BlockUmlBuilder(Collections.<String>emptyList(), UTF_8,
Defines.createEmpty(), new StringReader(text), null, "string");
List<BlockUml> blocks = builder.getBlockUmls();
if (blocks.size() == 0)
return 43;
final Diagram system = blocks.get(0).getDiagram();
if (system instanceof PSystemError) {
final ErrorUml error = ((PSystemError) system).getFirstError();
WasmLog.log("[" + error.getPosition() + "] " + error.getError());
return 44;
}
WasmLog.log("...processing...");
final FileOutputStream fos = new FileOutputStream(new File(pathOut));
WasmLog.log("...loading data...");
final ImageData imageData = system.exportDiagram(fos, 0, format);
WasmLog.log("Done!");
fos.close();
return 0;
} catch (Throwable t) {
WasmLog.log("Fatal error " + t);
}
return 47;
}
private static String cleanText(String text) {
if (text.endsWith("\n") == false)
text = text + "\n";
if (text.endsWith("@start") == false)
text = "@startuml\n" + text + "@enduml\n";
return text;
}
}