001package jmri.jmrit.logixng.expressions.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.*;
006import jmri.jmrit.logixng.expressions.ExpressionPower;
007import org.jdom2.Element;
008
009/**
010 * Handle XML configuration for ExpressionPower objects.
011 *
012 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
013 * @author Daniel Bergqvist Copyright (C) 2021
014 */
015public class ExpressionPowerXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
016
017    public ExpressionPowerXml() {
018    }
019
020    /**
021     * Default implementation for storing the contents of a ExpressionPower
022     *
023     * @param o Object to store, of type ExpressionPower
024     * @return Element containing the complete info
025     */
026    @Override
027    public Element store(Object o) {
028        ExpressionPower p = (ExpressionPower) o;
029
030        Element element = new Element("ExpressionPower");
031        element.setAttribute("class", this.getClass().getName());
032        element.addContent(new Element("systemName").addContent(p.getSystemName()));
033
034        storeCommon(p, element);
035
036        element.addContent(new Element("is_isNot").addContent(p.get_Is_IsNot().name()));
037
038        element.addContent(new Element("powerState").addContent(p.getBeanState().name()));
039
040        element.addContent(new Element("ignoreUnknownState").addContent(p.isIgnoreUnknownState() ? "yes" : "no"));
041
042        return element;
043    }
044
045    @Override
046    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
047        String sys = getSystemName(shared);
048        String uname = getUserName(shared);
049        ExpressionPower h = new ExpressionPower(sys, uname);
050
051        loadCommon(h, shared);
052
053        Element is_IsNot = shared.getChild("is_isNot");
054        if (is_IsNot != null) {
055            h.set_Is_IsNot(Is_IsNot_Enum.valueOf(is_IsNot.getTextTrim()));
056        }
057
058        Element powerState = shared.getChild("powerState");
059        if (powerState != null) {
060            String powerStateStr = powerState.getTextTrim();
061            if ("Other".equals(powerStateStr)) {
062                powerStateStr = "OnOrOff";
063                h.set_Is_IsNot(h.get_Is_IsNot().getOpposite());
064            }
065            h.setBeanState(ExpressionPower.PowerState.valueOf(powerStateStr));
066        }
067
068        Element ignoreUnknownState = shared.getChild("ignoreUnknownState");
069        if (ignoreUnknownState != null) {
070            h.setIgnoreUnknownState("yes".equals(ignoreUnknownState.getTextTrim()));
071        } else {
072            h.setIgnoreUnknownState(false);
073        }
074
075        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
076        return true;
077    }
078
079//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionPowerXml.class);
080}