1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-10 20:22:26 +00:00
plantuml/src/net/sourceforge/plantuml/BlockUml.java

244 lines
7.4 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://plantuml.com
2022-08-17 17:34:24 +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:
2022-08-17 17:34:24 +00:00
*
2023-02-22 18:43:48 +00:00
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
2022-08-17 17:34:24 +00:00
*
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 static java.nio.charset.StandardCharsets.UTF_8;
import static net.sourceforge.plantuml.utils.CharsetUtils.charsetOrDefault;
2017-06-05 11:27:21 +00:00
import java.io.IOException;
import java.nio.charset.Charset;
2017-03-12 17:22:02 +00:00
import java.security.MessageDigest;
2010-11-15 20:35:36 +00:00
import java.util.ArrayList;
import java.util.Arrays;
2017-04-05 17:37:42 +00:00
import java.util.Collections;
2020-03-18 10:50:02 +00:00
import java.util.HashSet;
2010-11-15 20:35:36 +00:00
import java.util.List;
2020-03-18 10:50:02 +00:00
import java.util.Set;
2010-11-15 20:35:36 +00:00
2017-03-12 17:22:02 +00:00
import net.sourceforge.plantuml.code.AsciiEncoder;
2017-06-05 11:27:21 +00:00
import net.sourceforge.plantuml.code.Transcoder;
import net.sourceforge.plantuml.code.TranscoderUtil;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.core.Diagram;
2019-05-24 19:59:31 +00:00
import net.sourceforge.plantuml.error.PSystemErrorPreprocessor;
2022-08-17 17:34:24 +00:00
import net.sourceforge.plantuml.log.Logme;
2017-04-05 17:37:42 +00:00
import net.sourceforge.plantuml.preproc.Defines;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.preproc.FileWithSuffix;
2019-03-29 22:14:07 +00:00
import net.sourceforge.plantuml.preproc2.PreprocessorModeSet;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.regex.Matcher2;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.style.ISkinSimple;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.text.BackSlash;
import net.sourceforge.plantuml.text.StringLocated;
2019-03-29 22:14:07 +00:00
import net.sourceforge.plantuml.tim.TimLoader;
2022-12-17 11:01:10 +00:00
import net.sourceforge.plantuml.utils.LineLocationImpl;
2016-06-19 14:16:41 +00:00
import net.sourceforge.plantuml.utils.StartUtils;
2017-03-12 17:22:02 +00:00
import net.sourceforge.plantuml.version.Version;
2013-12-10 19:36:50 +00:00
2011-01-05 18:23:06 +00:00
public class BlockUml {
2023-02-28 21:22:51 +00:00
// ::remove file when __HAXE__
2010-11-15 20:35:36 +00:00
2020-05-17 21:15:50 +00:00
private final List<StringLocated> rawSource;
2019-03-29 22:14:07 +00:00
private final List<StringLocated> data;
2019-05-24 19:59:31 +00:00
private List<StringLocated> debug;
2013-12-10 19:36:50 +00:00
private Diagram system;
2017-04-05 17:37:42 +00:00
private final Defines localDefines;
2019-03-01 22:16:29 +00:00
private final ISkinSimple skinParam;
2021-05-14 08:42:57 +00:00
private final Set<FileWithSuffix> included = new HashSet<>();
2020-03-18 10:50:02 +00:00
public Set<FileWithSuffix> getIncluded() {
return Collections.unmodifiableSet(included);
}
2010-11-15 20:35:36 +00:00
2022-03-19 12:48:23 +00:00
@Deprecated
2022-09-18 17:08:06 +00:00
BlockUml(String... strings) {
this(convert(strings), Defines.createEmpty(), null, null, null);
2010-11-15 20:35:36 +00:00
}
2023-02-28 21:22:51 +00:00
// ::comment when __CORE__
2017-06-05 11:27:21 +00:00
public String getEncodedUrl() throws IOException {
final Transcoder transcoder = TranscoderUtil.getDefaultTranscoder();
final String source = getDiagram().getSource().getPlainString("\n");
2017-06-05 11:27:21 +00:00
final String encoded = transcoder.encode(source);
return encoded;
}
2023-02-22 18:43:48 +00:00
// ::done
2017-06-05 11:27:21 +00:00
2015-08-05 20:17:01 +00:00
public String getFlashData() {
final StringBuilder sb = new StringBuilder();
2019-03-29 22:14:07 +00:00
for (StringLocated line : data) {
sb.append(line.getString());
2015-08-05 20:17:01 +00:00
sb.append('\r');
2017-06-05 11:27:21 +00:00
sb.append(BackSlash.CHAR_NEWLINE);
2015-08-05 20:17:01 +00:00
}
return sb.toString();
}
2019-03-29 22:14:07 +00:00
public static List<StringLocated> convert(String... strings) {
2015-06-20 10:54:49 +00:00
return convert(Arrays.asList(strings));
}
2019-03-29 22:14:07 +00:00
public static List<StringLocated> convert(List<String> strings) {
2021-05-14 08:42:57 +00:00
final List<StringLocated> result = new ArrayList<>();
2015-06-20 10:54:49 +00:00
LineLocationImpl location = new LineLocationImpl("block", null);
for (String s : strings) {
location = location.oneLineRead();
2019-03-29 22:14:07 +00:00
result.add(new StringLocated(s, location));
2015-06-20 10:54:49 +00:00
}
return result;
}
2019-05-24 19:59:31 +00:00
private boolean preprocessorError;
/**
2022-02-10 18:16:18 +00:00
* @deprecated being kept for backwards compatibility, perhaps other projects
* are using this?
*/
@Deprecated
2019-03-29 22:14:07 +00:00
public BlockUml(List<StringLocated> strings, Defines defines, ISkinSimple skinParam, PreprocessorModeSet mode) {
2022-09-18 17:08:06 +00:00
this(strings, defines, skinParam, mode, charsetOrDefault(mode.getCharset()));
}
2022-02-10 18:16:18 +00:00
2022-09-18 17:08:06 +00:00
public BlockUml(List<StringLocated> strings, Defines defines, ISkinSimple skinParam, PreprocessorModeSet mode,
Charset charset) {
2021-05-14 08:42:57 +00:00
this.rawSource = new ArrayList<>(strings);
2017-04-05 17:37:42 +00:00
this.localDefines = defines;
2019-03-01 22:16:29 +00:00
this.skinParam = skinParam;
2019-06-26 19:24:49 +00:00
final String s0 = strings.get(0).getTrimmed().getString();
2022-02-10 18:16:18 +00:00
if (StartUtils.startsWithSymbolAnd("start", s0) == false)
2010-11-15 20:35:36 +00:00
throw new IllegalArgumentException();
2022-02-10 18:16:18 +00:00
2020-01-12 22:13:17 +00:00
if (mode == null) {
2021-05-14 08:42:57 +00:00
this.data = new ArrayList<>(strings);
2020-01-12 22:13:17 +00:00
} else {
final TimLoader timLoader = new TimLoader(mode.getImportedFiles(), defines, charset,
2019-06-26 19:24:49 +00:00
(DefinitionsContainer) mode);
2020-03-18 10:50:02 +00:00
this.included.addAll(timLoader.load(strings));
2019-08-26 17:07:21 +00:00
this.data = timLoader.getResultList();
2019-05-24 19:59:31 +00:00
this.debug = timLoader.getDebug();
this.preprocessorError = timLoader.isPreprocessorError();
2019-03-29 22:14:07 +00:00
}
2010-11-15 20:35:36 +00:00
}
2023-02-28 21:22:51 +00:00
// ::comment when __CORE__
2017-04-19 18:30:16 +00:00
public String getFileOrDirname() {
2022-02-10 18:16:18 +00:00
if (OptionFlags.getInstance().isWord())
2010-11-15 20:35:36 +00:00
return null;
2022-02-10 18:16:18 +00:00
2019-03-29 22:14:07 +00:00
final Matcher2 m = StartUtils.patternFilename.matcher(StringUtils.trin(data.get(0).getString()));
2010-11-15 20:35:36 +00:00
final boolean ok = m.find();
2022-02-10 18:16:18 +00:00
if (ok == false)
2010-11-15 20:35:36 +00:00
return null;
2022-02-10 18:16:18 +00:00
2015-04-07 18:18:37 +00:00
String result = m.group(1);
final int x = result.indexOf(',');
2022-02-10 18:16:18 +00:00
if (x != -1)
2015-04-07 18:18:37 +00:00
result = result.substring(0, x);
2022-02-10 18:16:18 +00:00
2011-04-19 16:50:40 +00:00
for (int i = 0; i < result.length(); i++) {
final char c = result.charAt(i);
2022-02-10 18:16:18 +00:00
if ("<>|".indexOf(c) != -1)
2011-04-19 16:50:40 +00:00
return null;
2022-02-10 18:16:18 +00:00
2011-04-19 16:50:40 +00:00
}
2022-02-10 18:16:18 +00:00
if (result.startsWith("file://"))
2015-08-05 20:17:01 +00:00
result = result.substring("file://".length());
2022-02-10 18:16:18 +00:00
2017-04-19 18:30:16 +00:00
result = result.replaceAll("\\.\\w\\w\\w$", "");
2011-04-19 16:50:40 +00:00
return result;
2010-11-15 20:35:36 +00:00
}
2023-02-22 18:43:48 +00:00
// ::done
2010-11-15 20:35:36 +00:00
2015-04-07 18:18:37 +00:00
public Diagram getDiagram() {
2011-04-19 16:50:40 +00:00
if (system == null) {
2022-02-10 18:16:18 +00:00
if (preprocessorError)
2019-05-24 19:59:31 +00:00
system = new PSystemErrorPreprocessor(data, debug);
2022-12-16 16:20:14 +00:00
else
2022-09-20 20:35:41 +00:00
system = new PSystemBuilder().createPSystem(data, rawSource,
skinParam == null ? Collections.<String, String>emptyMap() : skinParam.values());
2010-11-15 20:35:36 +00:00
}
return system;
}
2019-03-29 22:14:07 +00:00
public final List<StringLocated> getData() {
2015-09-28 20:42:17 +00:00
return data;
}
2023-02-28 21:22:51 +00:00
// ::comment when __CORE__
2017-03-12 17:22:02 +00:00
private String internalEtag() {
try {
final AsciiEncoder coder = new AsciiEncoder();
final MessageDigest msgDigest = MessageDigest.getInstance("MD5");
2022-02-10 18:16:18 +00:00
for (StringLocated s : data)
msgDigest.update(s.getString().getBytes(UTF_8));
2022-02-10 18:16:18 +00:00
2017-03-12 17:22:02 +00:00
final byte[] digest = msgDigest.digest();
return coder.encode(digest);
} catch (Exception e) {
2022-08-17 17:34:24 +00:00
Logme.error(e);
2017-03-12 17:22:02 +00:00
return "NOETAG";
}
}
public String etag() {
2017-03-15 19:13:31 +00:00
return Version.etag() + internalEtag();
2017-03-12 17:22:02 +00:00
}
2023-02-22 18:43:48 +00:00
// ::done
2017-03-12 17:22:02 +00:00
public long lastModified() {
return (Version.compileTime() / 1000L / 60) * 1000L * 60 + Version.beta() * 1000L * 3600;
}
2017-04-05 17:37:42 +00:00
public boolean isStartDef(String name) {
final String signature = "@startdef(id=" + name + ")";
2019-03-29 22:14:07 +00:00
return data.get(0).getString().equalsIgnoreCase(signature);
2017-04-05 17:37:42 +00:00
}
2019-03-29 22:14:07 +00:00
public List<String> getDefinition(boolean withHeader) {
2021-05-14 08:42:57 +00:00
final List<String> result = new ArrayList<>();
2022-02-10 18:16:18 +00:00
for (StringLocated s : data)
2020-01-12 22:13:17 +00:00
result.add(s.getString());
2022-02-10 18:16:18 +00:00
if (withHeader)
2020-01-12 22:13:17 +00:00
return Collections.unmodifiableList(result);
2022-02-10 18:16:18 +00:00
2020-01-12 22:13:17 +00:00
return Collections.unmodifiableList(result.subList(1, result.size() - 1));
2019-06-26 19:24:49 +00:00
}
2017-04-05 17:37:42 +00:00
public Defines getLocalDefines() {
return localDefines;
}
2010-11-15 20:35:36 +00:00
}