001package jmri.jmrit.logixng.util.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.NamedBeanAddressing;
006import jmri.jmrit.logixng.util.LogixNG_SelectString;
007import jmri.jmrit.logixng.util.parser.ParserException;
008
009import org.jdom2.Element;
010
011/**
012 * Xml class for jmri.jmrit.logixng.util.LogixNG_SelectString.
013 *
014 * @author Daniel Bergqvist (C) 2022
015 */
016public class LogixNG_SelectStringXml {
017
018    /**
019     * Default implementation for storing the contents of a LogixNG_SelectEnum
020     *
021     * @param selectStr the LogixNG_SelectString object
022     * @param tagName the name of the element
023     * @return Element containing the complete info
024     */
025    public Element store(LogixNG_SelectString selectStr, String tagName) {
026
027        LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
028
029        Element enumElement = new Element(tagName);
030
031        enumElement.addContent(new Element("addressing").addContent(selectStr.getAddressing().name()));
032        if (selectStr.getValue() != null) {
033            enumElement.addContent(new Element("value").addContent(selectStr.getValue()));
034        }
035        if (selectStr.getReference() != null && !selectStr.getReference().isEmpty()) {
036            enumElement.addContent(new Element("reference").addContent(selectStr.getReference()));
037        }
038        var memory = selectStr.getMemory();
039        if (memory != null) {
040            enumElement.addContent(new Element("memory").addContent(memory.getName()));
041        }
042        // Not used anymore. It's kept to not change old tables and panels files
043        enumElement.addContent(new Element("listenToMemory").addContent("no"));
044        if (selectStr.getLocalVariable() != null && !selectStr.getLocalVariable().isEmpty()) {
045            enumElement.addContent(new Element("localVariable").addContent(selectStr.getLocalVariable()));
046        }
047        if (selectStr.getFormula() != null && !selectStr.getFormula().isEmpty()) {
048            enumElement.addContent(new Element("formula").addContent(selectStr.getFormula()));
049        }
050
051        if (selectStr.getAddressing() == NamedBeanAddressing.Table) {
052            enumElement.addContent(selectTableXml.store(selectStr.getSelectTable(), "table"));
053        }
054
055        return enumElement;
056    }
057
058    public void load(Element strElement, LogixNG_SelectString selectStr)
059            throws JmriConfigureXmlException {
060
061        if (strElement != null) {
062
063            LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
064
065            try {
066                Element elem = strElement.getChild("addressing");
067                if (elem != null) {
068                    selectStr.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
069                }
070
071                elem = strElement.getChild("value");
072                if (elem != null) {
073                    selectStr.setValue(elem.getTextTrim());
074                }
075
076                elem = strElement.getChild("reference");
077                if (elem != null) selectStr.setReference(elem.getTextTrim());
078
079                Element memoryName = strElement.getChild("memory");
080                if (memoryName != null) {
081                    Memory m = InstanceManager.getDefault(MemoryManager.class).getMemory(memoryName.getTextTrim());
082                    if (m != null) selectStr.setMemory(m);
083                    else selectStr.removeMemory();
084                }
085
086                elem = strElement.getChild("localVariable");
087                if (elem != null) selectStr.setLocalVariable(elem.getTextTrim());
088
089                elem = strElement.getChild("formula");
090                if (elem != null) selectStr.setFormula(elem.getTextTrim());
091
092                if (strElement.getChild("table") != null) {
093                    selectTableXml.load(strElement.getChild("table"), selectStr.getSelectTable());
094                }
095
096            } catch (ParserException e) {
097                throw new JmriConfigureXmlException(e);
098            }
099        }
100    }
101
102    /**
103     * This method is for backward compability up to and including 4.99.4.Remove this class after 5.0.
104     *
105     * @param shared the shared element
106     * @param selectStr the LogixNG_SelectEnum
107     * @param addressingElementName the name of the element of the addressing
108     * @param valueElementName the name of the element of the string
109     * @param referenceElementName the name of the element of the reference
110     * @param localVariableElementName the name of the element of the local variable
111     * @param formulaElementName the name of the element of the formula
112     * @throws JmriConfigureXmlException if an exception occurs
113     */
114    public void loadLegacy(
115            Element shared,
116            LogixNG_SelectString selectStr,
117            String addressingElementName,
118            String valueElementName,
119            String referenceElementName,
120            String localVariableElementName,
121            String formulaElementName)
122            throws JmriConfigureXmlException {
123
124        Element name = shared.getChild(valueElementName);
125        if (name != null) {
126            selectStr.setValue(name.getTextTrim());
127        }
128
129        try {
130            Element elem = shared.getChild(addressingElementName);
131            if (elem != null) {
132                selectStr.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
133            }
134
135            elem = shared.getChild(referenceElementName);
136            if (elem != null) selectStr.setReference(elem.getTextTrim());
137
138            elem = shared.getChild(localVariableElementName);
139            if (elem != null) selectStr.setLocalVariable(elem.getTextTrim());
140
141            elem = shared.getChild(formulaElementName);
142            if (elem != null) selectStr.setFormula(elem.getTextTrim());
143
144        } catch (ParserException e) {
145            throw new JmriConfigureXmlException(e);
146        }
147    }
148
149}