001package jmri.jmrit.logixng.expressions.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.*;
006import jmri.jmrit.logixng.expressions.ExpressionAudio;
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 ExpressionAudioXml objects.
014 *
015 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
016 * @author Daniel Bergqvist Copyright (C) 2023
017 */
018public class ExpressionAudioXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
019
020    public ExpressionAudioXml() {
021    }
022
023    /**
024     * Default implementation for storing the contents of a ExpressionAudio
025     *
026     * @param o Object to store, of type TripleAudioSignalHead
027     * @return Element containing the complete info
028     */
029    @Override
030    public Element store(Object o) {
031        ExpressionAudio p = (ExpressionAudio) o;
032
033        Element element = new Element("ExpressionAudio");
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<Audio>();
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("audioState").addContent(p.getBeanState().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        element.addContent(new Element("checkOnlyOnChange").addContent(p.isCheckOnlyOnChange()? "yes" : "no"));
051
052        return element;
053    }
054
055    @Override
056    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {     // Test class that inherits this class throws exception
057        String sys = getSystemName(shared);
058        String uname = getUserName(shared);
059        ExpressionAudio h = new ExpressionAudio(sys, uname);
060
061        loadCommon(h, shared);
062
063        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Audio>();
064        selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean());
065
066        try {
067            Element is_IsNot = shared.getChild("is_isNot");
068            if (is_IsNot != null) {
069                h.set_Is_IsNot(Is_IsNot_Enum.valueOf(is_IsNot.getTextTrim()));
070            }
071
072            Element elem = shared.getChild("stateAddressing");
073            if (elem != null) {
074                h.setStateAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
075            }
076
077            Element audioState = shared.getChild("audioState");
078            if (audioState != null) {
079                h.setBeanState(ExpressionAudio.AudioState.valueOf(audioState.getTextTrim()));
080            }
081
082            elem = shared.getChild("stateReference");
083            if (elem != null) h.setStateReference(elem.getTextTrim());
084
085            elem = shared.getChild("stateLocalVariable");
086            if (elem != null) h.setStateLocalVariable(elem.getTextTrim());
087
088            elem = shared.getChild("stateFormula");
089            if (elem != null) h.setStateFormula(elem.getTextTrim());
090
091            Element checkOnlyOnChangeElem = shared.getChild("checkOnlyOnChange");
092            if (checkOnlyOnChangeElem != null) {
093                h.setCheckOnlyOnChange("yes".equals(checkOnlyOnChangeElem.getTextTrim()));
094            }
095
096        } catch (ParserException e) {
097            throw new JmriConfigureXmlException(e);
098        }
099
100        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
101        return true;
102    }
103
104//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionAudioXml.class);
105}