jcckit.util.FlatConfigData Maven / Gradle / Ivy
Show all versions of plantuml-gplv2 Show documentation
// THIS FILE HAS BEEN GENERATED BY A PREPROCESSOR.
/* +=======================================================================
* |
* | PlantUML : a free UML diagram generator
* |
* +=======================================================================
*
* (C) Copyright 2009-2024, Arnaud Roques
*
* Project Info: https://plantuml.com
*
* If you like this project or if you find it useful, you can support us at:
*
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/liberapay (only 1€ per month!)
* https://plantuml.com/paypal
*
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License V2.
*
* THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC
* LICENSE ("AGREEMENT"). [GNU General Public License V2]
*
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
*
* You may obtain a copy of the License at
*
* https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* PlantUML can occasionally display sponsored or advertising messages. Those
* messages are usually generated on welcome or error images and never on
* functional diagrams.
* See https://plantuml.com/professional if you want to remove them
*
* Images (whatever their format : PNG, SVG, EPS...) generated by running PlantUML
* are owned by the author of their corresponding sources code (that is, their
* textual description in PlantUML language). Those images are not covered by
* this GPL v2 license.
*
* The generated images can then be used without any reference to the GPL v2 license.
* It is not even necessary to stipulate that they have been generated with PlantUML,
* although this will be appreciated by the PlantUML team.
*
* There is an exception : if the textual description in PlantUML language is also covered
* by any license, then the generated images are logically covered
* by the very same license.
*
* This is the IGY distribution (Install GraphViz by Yourself).
* You have to install GraphViz and to setup the GRAPHVIZ_DOT environment variable
* (see https://plantuml.com/graphviz-dot )
*
* Icons provided by OpenIconic : https://useiconic.com/open
* Archimate sprites provided by Archi : http://www.archimatetool.com
* Stdlib AWS provided by https://github.com/milo-minderbinder/AWS-PlantUML
* Stdlib Icons provided https://github.com/tupadr3/plantuml-icon-font-sprites
* ASCIIMathML (c) Peter Jipsen http://www.chapman.edu/~jipsen
* ASCIIMathML (c) David Lippman http://www.pierce.ctc.edu/dlippman
* CafeUndZopfli ported by Eugene Klyuchnikov https://github.com/eustas/CafeUndZopfli
* Brotli (c) by the Brotli Authors https://github.com/google/brotli
* Themes (c) by Brett Schwarz https://github.com/bschwarz/puml-themes
* Twemoji (c) by Twitter at https://twemoji.twitter.com/
*
*/
package jcckit.util;
/**
* An implementation of ConfigData based on a flat
* representation of the hierachically organized key-value pairs.
* Concrete subclasses must implement the methods
* {@link #getValue} and {@link #createConfigData} in accordance
* with the Template Method pattern and Factory Method pattern,
* respectively.
*
* In a flat representation of hierachically organized key-value
* pairs all key-value pairs are stored in a single Hashtable.
* Its key is the full key of the configuration data (i.e. the key
* including its path).
*
* Example (using the notation for a .properties file):
*
* title = example
* symbolAttributes/className = jcckit.graphic.BasicDrawingAttributes
* symbolAttributes/fillColor = 0xcaffee
* symbolAttributes/lineColor = 0xff0000
*
* The following table shows the result of some method calls at a
* FlatConfigData instance prepared with
* this example:
*
*
*
* Method call Result
* get("title") example
* getNode("symbolAttributes").get("fillColor")
* 0xcaffee
*
*
*
* In addition FlatConfigData implements inheritance
* of key-value pairs.
* Basically a node in the tree of key-value pairs
* may extend another node in the tree.
* The extended node inherit all key-value pairs from the extending
* one including the key-value pairs of all descendants.
* The value of a inherited key-value pair may be overridden.
* Also new key-value pairs may be placed in the inherited node or
* anywhere in the subtree.
* Note, that the extending node has to be a node which is not a
* descendant of the extended node (otherwise a circulary chain
* of references occurs). As a consequence not more than 20 inheritance
* levels are allowed.
*
* The implementation of this kind of inheritance in a flat hashtable
* is done by an additional key-value pair of the form
*
* extending-node/ = extended-node/
*
* Example:
*
* A/a/priority = high
* A/a/alpha/hello = universe
* A/a/alpha/answer = 42
* A/b/1/ = A/a/
* A/b/1/alpha/hello = world
* A/b/1/alpha/question = 6 * 7
*
* The following table shows the result of various method calls
* applied at the node A/b/1/ of a FlatConfigData
* instance prepared with this example:
*
*
*
* Method call Result Comment
* get("priority") high inherited
* getNode("alpha").get("hello")
* world overridden
* getNode("alpha").get("question")
* 6 * 7 added
* getNode("alpha").get("answer")
* 42 inherited
*
*
*
* @author Franz-Josef Elmer
*/
public abstract class FlatConfigData implements ConfigData {
private final String _path;
/** Creates a new instance for the specified path. */
public FlatConfigData(String path) {
_path = path;
}
/**
* Returns the full key.
* @param key A (relative) key. null is not allowed.
* @return the path concatenated with key or key
* if the path is undefined.
*/
public String getFullKey(String key) {
return _path == null ? key : _path + key;
}
/**
* Returns the value associated with this key.
* @param key The relative key. null is not allowed.
* @return the associated value. Will be null if no value exists
* for key.
*/
public String get(String key) {
return get(_path, key, 0);
}
/**
* Obtains a value in accordance with hierarchy (path) and
* inheritance (recursive calls of this routine).
*/
private String get(String path, String key, int numberOfLevels) {
String result = null;
if (numberOfLevels < 20) {
String fullKey = path == null ? key : path + key;
result = getValue(fullKey);
if (result == null) {
// posAfterDelim is the index in path just after '/'
int posAfterDelim = path == null ? -1 : path.length();
String replacement;
while (posAfterDelim > 0) {
// look for a sub-tree
replacement = getValue(path.substring(0, posAfterDelim));
if (replacement != null) {
// sub-tree found, add last part of the original path
result = get(replacement + path.substring(posAfterDelim), key,
numberOfLevels + 1);
// break whether result is null or not.
break;
}
// remove last element from the path
posAfterDelim = path.lastIndexOf('/', posAfterDelim - 2) + 1;
}
}
}
return result;
}
/**
* Returns the ConfigData object associated with this key.
* @param key The relative key.
* @return the associated value. Will never return null.
* Instead an empty ConfigData is returned.
*/
public ConfigData getNode(String key) {
String path = (_path == null ? key : _path + key) + '/';
return createConfigData(path);
}
/**
* Returns the value for the specified full key from the flat
* representation of the hierarchically organized key-value pairs.
* @param fullKey The full key including path. null is not allowed.
* @return the value or null if not found.
*/
protected abstract String getValue(String fullKey);
/**
* Returns the FlatConfigData object for the specified full path.
* In general path will be used in the constructor with
* path argument.
* @param path The full path.
* @return a new instance in any case.
*/
protected abstract ConfigData createConfigData(String path);
}