001package jmri.jmrit.logixng.actions.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.DigitalActionManager;
006import jmri.jmrit.logixng.NamedBeanAddressing;
007import jmri.jmrit.logixng.actions.ActionSound;
008import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectEnumXml;
009import jmri.jmrit.logixng.util.parser.ParserException;
010
011import org.jdom2.Element;
012
013/**
014 * Handle XML configuration for ActionSound objects.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
017 * @author Daniel Bergqvist Copyright (C) 2021
018 */
019public class ActionSoundXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
020
021    public ActionSoundXml() {
022    }
023
024    /**
025     * Default implementation for storing the contents of a SE8cSignalMast
026     *
027     * @param o Object to store, of type TripleLightSignalMast
028     * @return Element containing the complete info
029     */
030    @Override
031    public Element store(Object o) {
032        ActionSound p = (ActionSound) o;
033
034        Element element = new Element("ActionSound");
035        element.setAttribute("class", this.getClass().getName());
036        element.addContent(new Element("systemName").addContent(p.getSystemName()));
037
038        storeCommon(p, element);
039
040        var selectEnumXml = new LogixNG_SelectEnumXml<ActionSound.Operation>();
041        element.addContent(selectEnumXml.store(p.getSelectEnum(), "operation"));
042
043        element.addContent(new Element("soundAddressing").addContent(p.getSoundAddressing().name()));
044        element.addContent(new Element("sound").addContent(p.getSound()));
045        element.addContent(new Element("soundReference").addContent(p.getSoundReference()));
046        element.addContent(new Element("soundLocalVariable").addContent(p.getSoundLocalVariable()));
047        element.addContent(new Element("soundFormula").addContent(p.getSoundFormula()));
048
049        return element;
050    }
051
052    @Override
053    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
054        String sys = getSystemName(shared);
055        String uname = getUserName(shared);
056        ActionSound h = new ActionSound(sys, uname);
057
058        loadCommon(h, shared);
059
060        var selectEnumXml = new LogixNG_SelectEnumXml<ActionSound.Operation>();
061
062        selectEnumXml.load(shared.getChild("operation"), h.getSelectEnum());
063        selectEnumXml.loadLegacy(
064                shared, h.getSelectEnum(),
065                "operationAddressing",
066                "operationType",
067                "operationReference",
068                "operationLocalVariable",
069                "operationFormula");
070
071        try {
072            Element elem = shared.getChild("soundAddressing");
073            if (elem != null) {
074                h.setSoundAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
075            }
076
077            Element soundElement = shared.getChild("sound");
078            if (soundElement != null) {
079                try {
080                    h.setSound(soundElement.getText());
081                } catch (NumberFormatException e) {
082                    log.error("cannot parse sound: {}", soundElement.getTextTrim(), e);
083                }
084            }
085
086            elem = shared.getChild("soundReference");
087            if (elem != null) h.setSoundReference(elem.getTextTrim());
088
089            elem = shared.getChild("soundLocalVariable");
090            if (elem != null) h.setSoundLocalVariable(elem.getTextTrim());
091
092            elem = shared.getChild("soundFormula");
093            if (elem != null) h.setSoundFormula(elem.getTextTrim());
094
095        } catch (ParserException e) {
096            throw new JmriConfigureXmlException(e);
097        }
098
099        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
100        return true;
101    }
102
103    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionSoundXml.class);
104}