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