001package jmri.jmrit.logixng.actions.configurexml;
002
003
004import jmri.*;
005import jmri.configurexml.JmriConfigureXmlException;
006import jmri.Memory;
007import jmri.Reporter;
008import jmri.jmrit.logixng.DigitalActionManager;
009import jmri.jmrit.logixng.NamedBeanAddressing;
010import jmri.jmrit.logixng.actions.ActionReporter;
011import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml;
012import jmri.jmrit.logixng.util.parser.ParserException;
013
014import org.jdom2.Element;
015
016/**
017 * Handle XML configuration for Reporter objects.
018 *
019 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
020 * @author Daniel Bergqvist Copyright 2021
021 * @author Dave Sand Copyright (C) 2021
022 */
023public class ActionReporterXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
024
025    public ActionReporterXml() {
026    }
027
028    /**
029     * Default implementation for storing the contents of a Reporter
030     *
031     * @param o Object to store, of type Reporter
032     * @return Element containing the complete info
033     */
034    @Override
035    public Element store(Object o) {
036        ActionReporter p = (ActionReporter) o;
037
038        Element element = new Element("ActionReporter");
039        element.setAttribute("class", this.getClass().getName());
040        element.addContent(new Element("systemName").addContent(p.getSystemName()));
041
042        storeCommon(p, element);
043
044        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Reporter>();
045        element.addContent(selectNamedBeanXml.store(p.getSelectNamedBean(), "namedBean"));
046
047        var selectMemoryNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>();
048        element.addContent(selectMemoryNamedBeanXml.store(p.getSelectMemoryNamedBean(), "memoryNamedBean"));
049
050        element.addContent(new Element("reporterValue").addContent(p.getReporterValue().name()));
051
052        element.addContent(new Element("dataAddressing").addContent(p.getDataAddressing().name()));
053        element.addContent(new Element("dataReference").addContent(p.getDataReference()));
054        element.addContent(new Element("dataLocalVariable").addContent(p.getDataLocalVariable()));
055        element.addContent(new Element("dataFormula").addContent(p.getDataFormula()));
056
057        return element;
058    }
059
060    @Override
061    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {     // Test class that inherits this class throws exception
062        String sys = getSystemName(shared);
063        String uname = getUserName(shared);
064        ActionReporter h = new ActionReporter(sys, uname);
065
066        loadCommon(h, shared);
067
068        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Reporter>();
069        selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean());
070        selectNamedBeanXml.loadLegacy(shared, h.getSelectNamedBean(), "reporter");
071
072        var selectMemoryNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>();
073        selectMemoryNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectMemoryNamedBean());
074        selectMemoryNamedBeanXml.loadLegacy(shared, h.getSelectMemoryNamedBean(), "memory");
075
076        try {
077            Element elem = shared.getChild("reporterValue");
078            if (elem != null) {
079                h.setReporterValue(ActionReporter.ReporterValue.valueOf(elem.getTextTrim()));
080            }
081
082            elem = shared.getChild("dataAddressing");
083            if (elem != null) {
084                h.setDataAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
085            }
086
087            elem = shared.getChild("dataReference");
088            if (elem != null) h.setDataReference(elem.getTextTrim());
089
090            elem = shared.getChild("dataLocalVariable");
091            if (elem != null) h.setDataLocalVariable(elem.getTextTrim());
092
093            elem = shared.getChild("dataFormula");
094            if (elem != null) h.setDataFormula(elem.getTextTrim());
095
096        } catch (ParserException e) {
097            throw new JmriConfigureXmlException(e);
098        }
099
100        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
101        return true;
102    }
103
104//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionReporterXml.class);
105}