001package jmri.jmrit.logixng.expressions.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.DigitalExpressionManager;
006import jmri.jmrit.logixng.expressions.ExpressionLocalVariable;
007import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml;
008import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectTableXml;
009import jmri.util.CompareUtil;
010
011import org.jdom2.Element;
012
013/**
014 * Handle XML configuration for ExpressionLocalVariable objects.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
017 * @author Daniel Bergqvist Copyright (C) 2019
018 */
019public class ExpressionLocalVariableXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
020
021    public ExpressionLocalVariableXml() {
022    }
023
024    /**
025     * Default implementation for storing the contents of a ExpressionLocalVariable
026     *
027     * @param o Object to store, of type ExpressionLocalVariable
028     * @return Element containing the complete info
029     */
030    @Override
031    public Element store(Object o) {
032        ExpressionLocalVariable p = (ExpressionLocalVariable) o;
033
034        LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
035
036        Element element = new Element("ExpressionLocalVariable");
037        element.setAttribute("class", this.getClass().getName());
038        element.addContent(new Element("systemName").addContent(p.getSystemName()));
039
040        storeCommon(p, element);
041
042        String variableName = p.getLocalVariable();
043        if (variableName != null) {
044            element.addContent(new Element("variable").addContent(variableName));
045        }
046        String otherVariableName = p.getOtherLocalVariable();
047        if (otherVariableName != null) {
048            element.addContent(new Element("otherVariable").addContent(otherVariableName));
049        }
050
051        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>();
052        element.addContent(selectNamedBeanXml.store(p.getSelectMemoryNamedBean(), "memoryNamedBean"));
053
054        element.addContent(new Element("compareTo").addContent(p.getCompareTo().name()));
055        element.addContent(new Element("variableOperation").addContent(p.getVariableOperation().name()));
056        element.addContent(new Element("compareType").addContent(p.getCompareType().name()));
057        element.addContent(new Element("caseInsensitive").addContent(p.getCaseInsensitive() ? "yes" : "no"));
058
059        element.addContent(new Element("constant").addContent(p.getConstantValue()));
060        element.addContent(new Element("regEx").addContent(p.getRegEx()));
061
062        element.addContent(selectTableXml.store(p.getSelectTable(), "table"));
063
064        return element;
065    }
066
067    @Override
068    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
069        String sys = getSystemName(shared);
070        String uname = getUserName(shared);
071        ExpressionLocalVariable h = new ExpressionLocalVariable(sys, uname);
072
073        LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
074
075        loadCommon(h, shared);
076
077        Element variableName = shared.getChild("variable");
078        if (variableName != null) {
079            h.setLocalVariable(variableName.getTextTrim());
080        }
081
082        Element otherVariableName = shared.getChild("otherVariable");
083        if (otherVariableName != null) {
084            h.setOtherLocalVariable(otherVariableName.getTextTrim());
085        }
086
087        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>();
088        selectNamedBeanXml.load(shared.getChild("memoryNamedBean"), h.getSelectMemoryNamedBean());
089        selectNamedBeanXml.loadLegacy(shared, h.getSelectMemoryNamedBean(), "memory", null, null, null, null);
090
091        Element constant = shared.getChild("constant");
092        if (constant != null) {
093            h.setConstantValue(constant.getText());
094        }
095
096        Element regEx = shared.getChild("regEx");
097        if (regEx != null) {
098            h.setRegEx(regEx.getText());
099        }
100
101        Element compareTo = shared.getChild("compareTo");
102        if (compareTo != null) {
103            h.setCompareTo(ExpressionLocalVariable.CompareTo.valueOf(compareTo.getTextTrim()));
104        }
105
106        Element variableOperation = shared.getChild("variableOperation");
107        if (variableOperation != null) {
108            h.setVariableOperation(ExpressionLocalVariable.VariableOperation.valueOf(variableOperation.getTextTrim()));
109        }
110
111        Element compareType = shared.getChild("compareType");
112        if (compareType != null) {
113            h.setCompareType(CompareUtil.CompareType.valueOf(compareType.getTextTrim()));
114        }
115
116        Element caseInsensitive = shared.getChild("caseInsensitive");
117        if (caseInsensitive != null) {
118            h.setCaseInsensitive("yes".equals(caseInsensitive.getTextTrim()));
119        } else {
120            h.setCaseInsensitive(false);
121        }
122
123        selectTableXml.load(shared.getChild("table"), h.getSelectTable());
124
125        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
126        return true;
127    }
128
129//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionLocalVariableXml.class);
130}