001package jmri.jmrit.logixng.expressions.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.*;
006import jmri.jmrit.logixng.expressions.ExpressionDispatcher;
007import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectEnumXml;
008import jmri.jmrit.logixng.util.parser.ParserException;
009
010import org.jdom2.Element;
011
012/**
013 * Handle XML configuration for ExpressionLightXml objects.
014 *
015 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
016 * @author Daniel Bergqvist Copyright (C) 2019
017 * @author Dave Sand Copyright (C) 2021
018 */
019public class ExpressionDispatcherXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
020
021    public ExpressionDispatcherXml() {
022    }
023
024    /**
025     * Default implementation for storing the contents of a SE8cSignalHead
026     *
027     * @param o Object to store ExpresssionDispatcher
028     * @return Element containing the complete info
029     */
030    @Override
031    public Element store(Object o) {
032        ExpressionDispatcher p = (ExpressionDispatcher) o;
033
034        Element element = new Element("ExpressionDispatcher");
035        element.setAttribute("class", this.getClass().getName());
036        element.addContent(new Element("systemName").addContent(p.getSystemName()));
037
038        storeCommon(p, element);
039
040        var selectEnumXml = new LogixNG_SelectEnumXml<ExpressionDispatcher.DispatcherState>();
041
042        String trainInfoFileName = p.getTrainInfoFileName();
043        if (trainInfoFileName != null) {
044            element.addContent(new Element("trainInfoFileName").addContent(trainInfoFileName));
045        }
046
047        element.addContent(new Element("addressing").addContent(p.getAddressing().name()));
048        element.addContent(new Element("reference").addContent(p.getReference()));
049        element.addContent(new Element("localVariable").addContent(p.getLocalVariable()));
050        element.addContent(new Element("formula").addContent(p.getFormula()));
051
052        element.addContent(new Element("is_isNot").addContent(p.get_Is_IsNot().name()));
053
054        element.addContent(selectEnumXml.store(p.getSelectEnum(), "state"));
055
056        return element;
057    }
058
059    @Override
060    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
061        String sys = getSystemName(shared);
062        String uname = getUserName(shared);
063        ExpressionDispatcher h = new ExpressionDispatcher(sys, uname);
064
065        loadCommon(h, shared);
066
067        var selectEnumXml = new LogixNG_SelectEnumXml<ExpressionDispatcher.DispatcherState>();
068
069        selectEnumXml.load(shared.getChild("state"), h.getSelectEnum());
070        selectEnumXml.loadLegacy(
071                shared, h.getSelectEnum(),
072                "stateAddressing",
073                "dispatcherState",
074                "stateReference",
075                "stateLocalVariable",
076                "stateFormula");
077
078        try {
079            Element elem = shared.getChild("trainInfoFileName");
080            if (elem != null) {
081                h.setTrainInfoFileName(elem.getTextTrim());
082            }
083
084            elem = shared.getChild("addressing");
085            if (elem != null) {
086                h.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
087            }
088
089            elem = shared.getChild("reference");
090            if (elem != null) h.setReference(elem.getTextTrim());
091
092            elem = shared.getChild("localVariable");
093            if (elem != null) h.setLocalVariable(elem.getTextTrim());
094
095            elem = shared.getChild("formula");
096            if (elem != null) h.setFormula(elem.getTextTrim());
097
098
099            Element is_IsNot = shared.getChild("is_isNot");
100            if (is_IsNot != null) {
101                h.set_Is_IsNot(Is_IsNot_Enum.valueOf(is_IsNot.getTextTrim()));
102            }
103        } catch (ParserException e) {
104            throw new JmriConfigureXmlException(e);
105        }
106
107        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
108        return true;
109    }
110
111//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionDispatcherXml.class);
112}