001package jmri.jmrit.logixng.expressions.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.*;
006import jmri.jmrit.logixng.expressions.ExpressionConditional;
007import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml;
008import jmri.jmrit.logixng.util.parser.ParserException;
009
010import org.jdom2.Element;
011
012/**
013 * Handle XML configuration for ActionLightXml objects.
014 *
015 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
016 * @author Daniel Bergqvist Copyright (C) 2019
017 */
018public class ExpressionConditionalXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
019
020    public ExpressionConditionalXml() {
021    }
022
023    /**
024     * Default implementation for storing the contents of a SE8cSignalHead
025     *
026     * @param o Object to store, of type TripleLightSignalHead
027     * @return Element containing the complete info
028     */
029    @Override
030    public Element store(Object o) {
031        ExpressionConditional p = (ExpressionConditional) o;
032
033        Element element = new Element("ExpressionConditional");
034        element.setAttribute("class", this.getClass().getName());
035        element.addContent(new Element("systemName").addContent(p.getSystemName()));
036
037        storeCommon(p, element);
038
039        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Conditional>();
040        element.addContent(selectNamedBeanXml.store(p.getSelectNamedBean(), "namedBean"));
041
042        element.addContent(new Element("is_isNot").addContent(p.get_Is_IsNot().name()));
043
044        element.addContent(new Element("stateAddressing").addContent(p.getStateAddressing().name()));
045        element.addContent(new Element("conditionalState").addContent(p.getConditionalState().name()));
046        element.addContent(new Element("stateReference").addContent(p.getStateReference()));
047        element.addContent(new Element("stateLocalVariable").addContent(p.getStateLocalVariable()));
048        element.addContent(new Element("stateFormula").addContent(p.getStateFormula()));
049
050        return element;
051    }
052
053    @Override
054    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
055        String sys = getSystemName(shared);
056        String uname = getUserName(shared);
057        ExpressionConditional h = new ExpressionConditional(sys, uname);
058
059        loadCommon(h, shared);
060
061        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Conditional>();
062        selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean());
063        selectNamedBeanXml.loadLegacy(shared, h.getSelectNamedBean(), "conditional");
064
065        try {
066            Element is_IsNot = shared.getChild("is_isNot");
067            if (is_IsNot != null) {
068                h.set_Is_IsNot(Is_IsNot_Enum.valueOf(is_IsNot.getTextTrim()));
069            }
070
071            Element elem = shared.getChild("stateAddressing");
072            if (elem != null) {
073                h.setStateAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
074            }
075
076            Element conditionalState = shared.getChild("conditionalState");
077            if (conditionalState != null) {
078                h.setConditionalState(ExpressionConditional.ConditionalState.valueOf(conditionalState.getTextTrim()));
079            }
080
081            elem = shared.getChild("stateReference");
082            if (elem != null) h.setStateReference(elem.getTextTrim());
083
084            elem = shared.getChild("stateLocalVariable");
085            if (elem != null) h.setStateLocalVariable(elem.getTextTrim());
086
087            elem = shared.getChild("stateFormula");
088            if (elem != null) h.setStateFormula(elem.getTextTrim());
089
090        } catch (ParserException e) {
091            throw new JmriConfigureXmlException(e);
092        }
093
094        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
095        return true;
096    }
097
098//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionLightXml.class);
099}