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