Commit 594515b2 authored by Nico Mack's avatar Nico Mack

Added HtmlTemplate utility class

parent 336e9f8c
/**
* Copyright Luxembourg Institute of Science and Technology, 2017. All rights reserved. If you wish
* to use this code for any purpose, please contact the author(s).
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package lu.list.itis.dkd.tui.utility;
import lu.list.itis.dkd.tui.cps.system.VariableBased;
import lu.list.itis.dkd.tui.cps.variable.Variable;
import lu.list.itis.dkd.tui.utility.interpolation.Chunk;
import lu.list.itis.dkd.tui.utility.interpolation.Interpolator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Nico Mack [nico.mack@list.lu]
* @since 2.5
* @version 2.5.0
*/
// ***************************************************************************
// * Class Definition and Members *
// ***************************************************************************
public class HtmlTemplate implements VariableBased {
private String template;
private List<Chunk<?>> chunks;
private List<String> identifiers;
private Map<String, Variable<?>> parameters;
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
// ***************************************************************************
// ---------------------------------------------------------------------------
public HtmlTemplate(String template) {
this.parameters = new HashMap<>();
this.template = template;
this.identifiers = Interpolator.extractVariableIdentifiers(this.template);
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body *
// ***************************************************************************
// ---------------------------------------------------------------------------
public boolean dependsOn(String identifier) {
return this.identifiers.contains(identifier);
}
// ---------------------------------------------------------------------------
public String render() {
return Interpolator.render(chunks);
}
// ---------------------------------------------------------------------------
@Override
public List<Variable<?>> connectWithSystemVariables(Map<String, Variable<?>> systemVariables) {
List<Variable<?>> connected = new ArrayList<>();
for (String identifier : identifiers) {
if ((systemVariables != null) && systemVariables.containsKey(identifier)) {
Variable<?> variable = systemVariables.get(identifier);
this.parameters.put(identifier, variable);
connected.add(variable);
}
}
if (!connected.isEmpty()) {
chunks = Interpolator.decompose(this.template, this.parameters);
}
return connected;
}
// ---------------------------------------------------------------------------
@Override
public List<Variable<?>> getDeclaredVariables() {
List<Variable<?>> declared = new ArrayList<>();
for (Variable<?> parameter : this.parameters.values()) {
declared.add(parameter);
}
return declared;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * End of Class *
// ***************************************************************************
// ---------------------------------------------------------------------------
}
......@@ -101,7 +101,6 @@ public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChan
if (!connected.isEmpty()) {
htmlChunks = Interpolator.decompose(this.htmlTemplate, this.parameters);
}
return connected;
}
......
......@@ -29,6 +29,7 @@ import lu.list.itis.dkd.tui.exception.BuildException;
import lu.list.itis.dkd.tui.utility.CpsNamespace;
import lu.list.itis.dkd.tui.utility.Point;
import lu.list.itis.dkd.tui.widget.corona.HtmlBox;
import lu.list.itis.dkd.tui.widget.corona.VariableHtmlBox;
import org.jdom2.Element;
......@@ -131,6 +132,6 @@ public abstract class BaseVariableHtmlBoxBuilder<B extends BaseVariableHtmlBoxBu
/** {@inheritDoc} */
@Override
public abstract HtmlBox build();
public abstract VariableHtmlBox build();
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment