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