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