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

221 lines
6.6 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, 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;
2017-06-05 11:27:21 +00:00
import java.io.IOException;
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;
2010-11-15 20:35:36 +00:00
import java.util.List;
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;
2016-05-31 19:41:55 +00:00
import net.sourceforge.plantuml.command.regex.Matcher2;
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;
2017-04-05 17:37:42 +00:00
import net.sourceforge.plantuml.preproc.Defines;
2019-03-29 22:14:07 +00:00
import net.sourceforge.plantuml.preproc2.PreprocessorMode;
import net.sourceforge.plantuml.preproc2.PreprocessorModeSet;
import net.sourceforge.plantuml.tim.TimLoader;
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 {
2010-11-15 20:35:36 +00:00
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;
2010-11-15 20:35:36 +00:00
BlockUml(String... strings) {
2019-03-29 22:14:07 +00:00
this(convert(strings), Defines.createEmpty(), null, null);
2010-11-15 20:35:36 +00:00
}
2017-06-05 11:27:21 +00:00
public String getEncodedUrl() throws IOException {
final Transcoder transcoder = TranscoderUtil.getDefaultTranscoder();
final String source = getDiagram().getSource().getPlainString();
final String encoded = transcoder.encode(source);
return encoded;
}
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) {
final List<StringLocated> result = new ArrayList<StringLocated>();
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 PreprocessorMode pmode = PreprocessorMode.V1_LEGACY;
private boolean preprocessorError;
2019-03-29 22:14:07 +00:00
public BlockUml(List<StringLocated> strings, Defines defines, ISkinSimple skinParam, PreprocessorModeSet mode) {
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();
2016-06-19 14:16:41 +00:00
if (StartUtils.startsWithSymbolAnd("start", s0) == false) {
2010-11-15 20:35:36 +00:00
throw new IllegalArgumentException();
}
2019-03-29 22:14:07 +00:00
if (mode != null && mode.getPreprocessorMode() == PreprocessorMode.V2_NEW_TIM) {
2019-05-24 19:59:31 +00:00
this.pmode = mode.getPreprocessorMode();
2019-06-26 19:24:49 +00:00
final TimLoader timLoader = new TimLoader(mode.getImportedFiles(), defines, mode.getCharset(),
(DefinitionsContainer) mode);
2019-05-24 19:59:31 +00:00
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
} else {
this.data = new ArrayList<StringLocated>(strings);
}
2010-11-15 20:35:36 +00:00
}
2017-04-19 18:30:16 +00:00
public String getFileOrDirname() {
2010-11-15 20:35:36 +00:00
if (OptionFlags.getInstance().isWord()) {
return null;
}
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();
if (ok == false) {
return null;
}
2015-04-07 18:18:37 +00:00
String result = m.group(1);
final int x = result.indexOf(',');
if (x != -1) {
result = result.substring(0, x);
}
2011-04-19 16:50:40 +00:00
for (int i = 0; i < result.length(); i++) {
final char c = result.charAt(i);
if ("<>|".indexOf(c) != -1) {
return null;
}
}
2015-08-05 20:17:01 +00:00
if (result.startsWith("file://")) {
result = result.substring("file://".length());
}
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
}
2015-04-07 18:18:37 +00:00
public Diagram getDiagram() {
2011-04-19 16:50:40 +00:00
if (system == null) {
2019-05-24 19:59:31 +00:00
if (preprocessorError) {
system = new PSystemErrorPreprocessor(data, debug);
} else {
system = new PSystemBuilder().createPSystem(skinParam, data);
}
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;
}
2017-03-12 17:22:02 +00:00
private String internalEtag() {
try {
final AsciiEncoder coder = new AsciiEncoder();
final MessageDigest msgDigest = MessageDigest.getInstance("MD5");
2019-03-29 22:14:07 +00:00
for (StringLocated s : data) {
msgDigest.update(s.getString().getBytes("UTF-8"));
2017-03-12 17:22:02 +00:00
}
final byte[] digest = msgDigest.digest();
return coder.encode(digest);
} catch (Exception e) {
e.printStackTrace();
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
}
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) {
final List<String> data2 = new ArrayList<String>();
for (StringLocated s : data) {
data2.add(s.getString());
}
2018-07-27 21:56:46 +00:00
if (withHeader) {
2019-03-29 22:14:07 +00:00
return Collections.unmodifiableList(data2);
2017-04-05 17:37:42 +00:00
}
2019-03-29 22:14:07 +00:00
return Collections.unmodifiableList(data2.subList(1, data2.size() - 1));
2017-04-05 17:37:42 +00:00
}
2019-06-26 19:24:49 +00:00
public List<String> getDefinition2(boolean withHeader) {
final List<String> data2 = new ArrayList<String>();
for (StringLocated s : debug) {
data2.add(s.getString());
}
if (withHeader) {
return Collections.unmodifiableList(data2);
}
return Collections.unmodifiableList(data2.subList(1, data2.size() - 1));
}
2017-04-05 17:37:42 +00:00
public Defines getLocalDefines() {
return localDefines;
}
2010-11-15 20:35:36 +00:00
}