1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-05-28 22:20:49 +00:00
plantuml/src/net/sourceforge/plantuml/preproc/Defines.java

259 lines
6.9 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.preproc;
2017-04-05 17:37:42 +00:00
import java.io.File;
2016-11-04 21:39:29 +00:00
import java.text.SimpleDateFormat;
2018-11-26 18:46:22 +00:00
import java.util.ArrayList;
2015-04-07 18:18:37 +00:00
import java.util.Arrays;
2018-11-26 18:46:22 +00:00
import java.util.Collection;
2016-11-04 21:39:29 +00:00
import java.util.Date;
2018-11-26 18:46:22 +00:00
import java.util.HashSet;
2015-04-07 18:18:37 +00:00
import java.util.LinkedHashMap;
import java.util.List;
2010-11-15 20:35:36 +00:00
import java.util.Map;
2018-11-26 18:46:22 +00:00
import java.util.Set;
2013-12-10 19:36:50 +00:00
import java.util.regex.Matcher;
2016-11-04 21:39:29 +00:00
import java.util.regex.Pattern;
2010-11-15 20:35:36 +00:00
2018-12-22 11:11:40 +00:00
import net.sourceforge.plantuml.AParentFolder;
2016-12-01 20:29:25 +00:00
import net.sourceforge.plantuml.Log;
2017-04-05 17:37:42 +00:00
import net.sourceforge.plantuml.api.ApiWarning;
2017-06-05 11:27:21 +00:00
import net.sourceforge.plantuml.version.Version;
2016-12-01 20:29:25 +00:00
public class Defines implements Truth {
2010-11-15 20:35:36 +00:00
2017-04-05 17:37:42 +00:00
private final Map<String, String> environment = new LinkedHashMap<String, String>();
2016-11-04 21:39:29 +00:00
private final Map<String, Define> values = new LinkedHashMap<String, Define>();
private final Map<String, Define> savedState = new LinkedHashMap<String, Define>();
2010-11-15 20:35:36 +00:00
2017-04-05 17:37:42 +00:00
@Deprecated
@ApiWarning(willBeRemoved = "in next major release")
public Defines() {
2017-06-05 11:27:21 +00:00
environment.put("PLANTUML_VERSION", "" + Version.versionString());
2017-04-05 17:37:42 +00:00
}
@Override
public String toString() {
2018-11-26 18:46:22 +00:00
return values.keySet().toString() + " " + environment.keySet();
2017-04-05 17:37:42 +00:00
}
public static Defines createEmpty() {
2018-01-04 22:32:45 +00:00
return new Defines();
2017-12-11 21:02:10 +00:00
}
2018-01-04 22:32:45 +00:00
public void overrideFilename(String filename) {
2017-12-11 21:02:10 +00:00
if (filename != null) {
2018-01-04 22:32:45 +00:00
environment.put("filename", filename);
environment.put("filenameNoExtension", nameNoExtension(filename));
2017-12-11 21:02:10 +00:00
}
2017-04-05 17:37:42 +00:00
}
public void importFrom(Defines other) {
this.environment.putAll(other.environment);
this.values.putAll(other.values);
2018-11-26 18:46:22 +00:00
magic = null;
2017-04-05 17:37:42 +00:00
}
public Defines cloneMe() {
final Defines result = new Defines();
result.importFrom(this);
return result;
}
public static Defines createWithFileName(File file) {
if (file == null) {
throw new IllegalArgumentException();
}
2018-01-04 22:32:45 +00:00
final Defines result = createEmpty();
result.overrideFilename(file.getName());
2017-04-26 17:48:37 +00:00
result.environment.put("filedate", new Date(file.lastModified()).toString());
2017-04-05 17:37:42 +00:00
result.environment.put("dirpath", file.getAbsoluteFile().getParentFile().getAbsolutePath().replace('\\', '/'));
return result;
}
2019-09-22 17:20:16 +00:00
public static Defines createWithMap(Map<String, String> init) {
final Defines result = createEmpty();
for (Map.Entry<String, String> ent : init.entrySet()) {
result.environment.put(ent.getKey(), ent.getValue());
}
return result;
}
2019-04-21 20:40:01 +00:00
public String getEnvironmentValue(String key) {
return this.environment.get(key);
}
2017-04-05 17:37:42 +00:00
2017-12-11 21:02:10 +00:00
private static String nameNoExtension(String name) {
2017-07-03 17:59:53 +00:00
final int x = name.lastIndexOf('.');
if (x == -1) {
return name;
}
return name.substring(0, x);
}
2018-12-22 11:11:40 +00:00
public void define(String name, List<String> value, boolean emptyParentheses, AParentFolder currentDir) {
values.put(name, new Define(name, value, emptyParentheses, currentDir));
2018-11-26 18:46:22 +00:00
magic = null;
2015-04-07 18:18:37 +00:00
}
2016-12-01 20:29:25 +00:00
public boolean isDefine(String expression) {
try {
final EvalBoolean eval = new EvalBoolean(expression, this);
return eval.eval();
} catch (IllegalArgumentException e) {
Log.info("Error in " + expression);
return false;
}
}
public boolean isTrue(String name) {
2015-04-07 18:18:37 +00:00
for (String key : values.keySet()) {
if (key.equals(name) || key.startsWith(name + "(")) {
return true;
}
}
return false;
}
public void undefine(String name) {
values.remove(name);
2018-11-26 18:46:22 +00:00
magic = null;
2015-04-07 18:18:37 +00:00
}
public List<String> applyDefines(String line) {
2018-11-26 18:46:22 +00:00
// System.err.println("line=" + line + " " + values.size());
2016-11-04 21:39:29 +00:00
line = manageDate(line);
2017-04-05 17:37:42 +00:00
line = manageEnvironment(line);
2018-11-26 18:46:22 +00:00
line = method1(line);
// line = values.size() < 10 ? method1(line) : method2(line);
return Arrays.asList(line.split("\n"));
}
private String method1(String line) {
for (Define def : values.values()) {
2016-11-04 21:39:29 +00:00
line = def.apply(line);
2010-11-15 20:35:36 +00:00
}
2018-11-26 18:46:22 +00:00
return line;
}
private Map<String, Collection<Define>> getAll() {
final Map<String, Collection<Define>> result = new LinkedHashMap<String, Collection<Define>>();
for (Define def : values.values()) {
Collection<Define> tmp = result.get(def.getFunctionName());
if (tmp == null) {
tmp = new ArrayList<Define>();
result.put(def.getFunctionName(), tmp);
}
tmp.add(def);
}
return result;
}
private Map<String, Collection<Define>> magic;
private String method2(String line) {
final Set<String> words = words(line);
if (magic == null) {
magic = getAll();
}
for (String w : words) {
Collection<Define> tmp = magic.get(w);
if (tmp == null) {
continue;
}
for (Define def : tmp) {
line = def.apply(line);
}
}
return line;
}
private Set<String> words(String line) {
final String ID = "[A-Za-z_][A-Za-z_0-9]*";
Pattern p = Pattern.compile(ID);
Matcher m = p.matcher(line);
final Set<String> words = new HashSet<String>();
while (m.find()) {
words.add(m.group(0));
}
return words;
2015-04-07 18:18:37 +00:00
}
2017-04-05 17:37:42 +00:00
private String manageEnvironment(String line) {
for (Map.Entry<String, String> ent : environment.entrySet()) {
final String key = Pattern.quote("%" + ent.getKey() + "%");
line = line.replaceAll(key, ent.getValue());
}
return line;
}
2016-11-04 21:39:29 +00:00
private static final String DATE = "(?i)%date(\\[(.+?)\\])?%";
private final static Pattern datePattern = Pattern.compile(DATE);
2015-09-06 17:28:59 +00:00
2016-11-04 21:39:29 +00:00
private String manageDate(String line) {
final Matcher m = datePattern.matcher(line);
if (m.find()) {
final String format = m.group(2);
String replace;
if (format == null) {
replace = new Date().toString();
} else {
try {
replace = new SimpleDateFormat(format).format(new Date());
} catch (Exception e) {
replace = "(BAD DATE PATTERN:" + format + ")";
}
2015-09-06 17:28:59 +00:00
}
2016-11-04 21:39:29 +00:00
line = line.replaceAll(DATE, replace);
2015-09-06 17:28:59 +00:00
}
return line;
}
2018-11-26 18:46:22 +00:00
public void saveState1() {
2015-04-07 18:18:37 +00:00
this.savedState.putAll(values);
}
2018-11-26 18:46:22 +00:00
public void restoreState1() {
2015-04-07 18:18:37 +00:00
this.values.clear();
this.values.putAll(savedState);
2018-11-26 18:46:22 +00:00
magic = null;
2010-11-15 20:35:36 +00:00
}
}