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