001package jmri.jmrit.logixng.expressions.configurexml;
002
003import java.util.ResourceBundle;
004import jmri.*;
005import jmri.configurexml.JmriConfigureXmlException;
006import jmri.Section;
007import jmri.jmrit.logixng.*;
008import jmri.jmrit.logixng.expressions.ExpressionSection;
009import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml;
010import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectEnumXml;
011import org.jdom2.Element;
012
013/**
014 * Handle XML configuration for ExpressionSectionXml objects.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
017 * @author Daniel Bergqvist Copyright 2021
018 * @author Dave Sand Copyright (C) 2023
019 */
020public class ExpressionSectionXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
021
022    static final ResourceBundle rb = ResourceBundle.getBundle("jmri.jmrit.logixng.actions.ActionBundle");
023
024    public ExpressionSectionXml() {
025    }
026
027    /**
028     * Default implementation for storing the contents of a ExpressionSection
029     *
030     * @param o Object to store, of type ExpressionSection
031     * @return Element containing the complete info
032     */
033    @Override
034    public Element store(Object o) {
035        ExpressionSection p = (ExpressionSection) o;
036
037        Element element = new Element("ExpressionSection");
038        element.setAttribute("class", this.getClass().getName());
039        element.addContent(new Element("systemName").addContent(p.getSystemName()));
040
041        storeCommon(p, element);
042
043        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Section>();
044        var selectEnumXml = new LogixNG_SelectEnumXml<ExpressionSection.SectionState>();
045
046        element.addContent(selectNamedBeanXml.store(p.getSelectNamedBean(), "namedBean"));
047
048        element.addContent(new Element("is_isNot").addContent(p.get_Is_IsNot().name()));
049
050        element.addContent(selectEnumXml.store(p.getSelectEnum(), "sectionStateData"));
051
052        return element;
053    }
054
055    @Override
056    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
057        String sys = getSystemName(shared);
058        String uname = getUserName(shared);
059        ExpressionSection h = new ExpressionSection(sys, uname);
060
061        loadCommon(h, shared);
062
063        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Section>();
064        var selectEnumXml = new LogixNG_SelectEnumXml<ExpressionSection.SectionState>();
065
066        selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean());
067        selectNamedBeanXml.loadLegacy(shared, h.getSelectNamedBean(), "section");
068
069        Element is_IsNot = shared.getChild("is_isNot");
070        if (is_IsNot != null) {
071            h.set_Is_IsNot(Is_IsNot_Enum.valueOf(is_IsNot.getTextTrim()));
072        }
073
074        selectEnumXml.load(shared.getChild("sectionStateData"), h.getSelectEnum());
075        selectEnumXml.loadLegacy(
076                shared,
077                h.getSelectEnum(),
078                "stateAddressing",
079                "sectionState",
080                "stateReference",
081                "stateLocalVariable",
082                "stateFormula");
083
084        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
085        return true;
086    }
087
088//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionSectionXml.class);
089}