1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-09-22 12:09:03 +00:00
plantuml/src/net/sourceforge/plantuml/style/StyleBuilder.java

164 lines
4.6 KiB
Java
Raw Normal View History

2019-07-14 20:09:26 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2020, 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 net.sourceforge.plantuml.style;
import java.util.EnumMap;
import java.util.LinkedHashMap;
2019-08-26 17:07:21 +00:00
import java.util.LinkedHashSet;
2019-07-14 20:09:26 +00:00
import java.util.Map;
import java.util.Map.Entry;
2019-08-26 17:07:21 +00:00
import java.util.Set;
2019-07-14 20:09:26 +00:00
2019-08-26 17:07:21 +00:00
import net.sourceforge.plantuml.Log;
2019-07-14 20:09:26 +00:00
import net.sourceforge.plantuml.SkinParam;
public class StyleBuilder implements AutomaticCounter {
2022-02-01 20:21:45 +00:00
private final Map<StyleSignature, Style> stylesMap = new LinkedHashMap<StyleSignature, Style>();
2019-08-26 17:07:21 +00:00
private final Set<StyleSignature> printedForLog;
2019-07-14 20:09:26 +00:00
private final SkinParam skinParam;
private int counter;
2021-05-03 20:11:48 +00:00
public void printMe() {
2022-02-01 20:21:45 +00:00
for (Entry<StyleSignature, Style> ent : stylesMap.entrySet())
2021-05-03 20:11:48 +00:00
ent.getValue().printMe();
2022-01-28 21:45:34 +00:00
2021-05-03 20:11:48 +00:00
}
2019-08-26 17:07:21 +00:00
private StyleBuilder(SkinParam skinParam, Set<StyleSignature> printedForLog) {
2019-07-14 20:09:26 +00:00
this.skinParam = skinParam;
2021-05-14 08:42:57 +00:00
this.printedForLog = new LinkedHashSet<>();
2019-08-26 17:07:21 +00:00
}
2019-07-14 20:09:26 +00:00
2019-08-26 17:07:21 +00:00
public StyleBuilder(SkinParam skinParam) {
this(skinParam, new LinkedHashSet<StyleSignature>());
2019-07-14 20:09:26 +00:00
}
2019-08-26 17:07:21 +00:00
public final SkinParam getSkinParam() {
return skinParam;
2019-07-14 20:09:26 +00:00
}
public Style createStyle(String name) {
2022-01-28 21:45:34 +00:00
if (name.contains("*"))
2021-08-30 17:13:54 +00:00
throw new IllegalArgumentException();
2022-01-28 21:45:34 +00:00
2019-08-26 17:07:21 +00:00
name = name.toLowerCase();
final StyleSignature signature = new StyleSignature(name);
2022-02-01 20:21:45 +00:00
final Style result = stylesMap.get(signature);
2022-01-28 21:45:34 +00:00
if (result == null)
2019-08-26 17:07:21 +00:00
return new Style(signature, new EnumMap<PName, Value>(PName.class));
2022-01-28 21:45:34 +00:00
2019-07-14 20:09:26 +00:00
return result;
}
public StyleBuilder muteStyle(Style modifiedStyle) {
2022-02-01 20:21:45 +00:00
final Map<StyleSignature, Style> copy = new LinkedHashMap<StyleSignature, Style>(stylesMap);
2019-08-26 17:07:21 +00:00
final StyleSignature signature = modifiedStyle.getSignature();
final Style orig = copy.get(signature);
2019-07-14 20:09:26 +00:00
if (orig == null) {
2019-08-26 17:07:21 +00:00
copy.put(signature, modifiedStyle);
2019-07-14 20:09:26 +00:00
} else {
2022-02-01 20:21:45 +00:00
final Style tmp = orig.mergeWith(modifiedStyle);
copy.put(signature, tmp);
2019-07-14 20:09:26 +00:00
}
2019-08-26 17:07:21 +00:00
final StyleBuilder result = new StyleBuilder(skinParam, this.printedForLog);
2022-02-01 20:21:45 +00:00
result.stylesMap.putAll(copy);
2019-07-14 20:09:26 +00:00
result.counter = this.counter;
return result;
}
2022-02-01 20:21:45 +00:00
public void loadInternal(StyleSignature signature, Style newStyle) {
if (signature.isStarred())
2021-08-30 17:13:54 +00:00
throw new IllegalArgumentException();
2022-01-28 21:45:34 +00:00
2022-02-01 20:21:45 +00:00
final Style orig = this.stylesMap.get(signature);
if (orig == null) {
this.stylesMap.put(signature, newStyle);
} else {
final Style tmp = orig.mergeWith(newStyle);
this.stylesMap.put(signature, tmp);
}
2019-07-14 20:09:26 +00:00
}
public int getNextInt() {
return ++counter;
}
2019-08-26 17:07:21 +00:00
public Style getMergedStyle(StyleSignature signature) {
boolean added = this.printedForLog.add(signature);
2022-01-28 21:45:34 +00:00
if (added)
2019-08-26 17:07:21 +00:00
Log.info("Using style " + signature);
2022-01-28 21:45:34 +00:00
2019-08-26 17:07:21 +00:00
Style result = null;
2022-02-01 20:21:45 +00:00
for (Entry<StyleSignature, Style> ent : stylesMap.entrySet()) {
2019-08-26 17:07:21 +00:00
final StyleSignature key = ent.getKey();
2022-01-28 21:45:34 +00:00
if (key.matchAll(signature) == false)
2019-07-14 20:09:26 +00:00
continue;
2022-01-28 21:45:34 +00:00
if (result == null)
2019-08-26 17:07:21 +00:00
result = ent.getValue();
2022-01-28 21:45:34 +00:00
else
2019-07-14 20:09:26 +00:00
result = result.mergeWith(ent.getValue());
}
return result;
}
2021-08-30 17:13:54 +00:00
public Style getMergedStyleSpecial(StyleSignature signature, int deltaPriority) {
boolean added = this.printedForLog.add(signature);
2022-01-28 21:45:34 +00:00
if (added)
2021-08-30 17:13:54 +00:00
Log.info("Using style " + signature);
2022-01-28 21:45:34 +00:00
2021-08-30 17:13:54 +00:00
Style result = null;
2022-02-01 20:21:45 +00:00
for (Entry<StyleSignature, Style> ent : stylesMap.entrySet()) {
2021-08-30 17:13:54 +00:00
final StyleSignature key = ent.getKey();
2022-01-28 21:45:34 +00:00
if (key.matchAll(signature) == false)
2021-08-30 17:13:54 +00:00
continue;
2022-01-28 21:45:34 +00:00
2021-08-30 17:13:54 +00:00
Style tmp = ent.getValue();
2022-01-28 21:45:34 +00:00
if (key.isStarred())
2021-08-30 17:13:54 +00:00
tmp = tmp.deltaPriority(deltaPriority);
2022-01-28 21:45:34 +00:00
if (result == null)
2021-08-30 17:13:54 +00:00
result = tmp;
2022-01-28 21:45:34 +00:00
else
2021-08-30 17:13:54 +00:00
result = result.mergeWith(tmp);
}
return result;
}
2019-07-14 20:09:26 +00:00
}