001package jmri.jmrit.logixng.util.configurexml;
002
003import java.util.List;
004
005import jmri.configurexml.JmriConfigureXmlException;
006import jmri.jmrit.logixng.NamedBeanAddressing;
007import jmri.jmrit.logixng.util.LogixNG_SelectStringList;
008import jmri.jmrit.logixng.util.parser.ParserException;
009
010import org.jdom2.Element;
011
012/**
013 * Xml class for jmri.jmrit.logixng.util.LogixNG_SelectStringList.
014 *
015 * @author Daniel Bergqvist (C) 2025
016 */
017public class LogixNG_SelectStringListXml {
018
019    /**
020     * Default implementation for storing the contents of a LogixNG_SelectEnum
021     *
022     * @param selectStr the LogixNG_SelectStringList object
023     * @param tagName the name of the element
024     * @return Element containing the complete info
025     */
026    public Element store(LogixNG_SelectStringList selectStr, String tagName) {
027
028        Element element = new Element(tagName);
029
030        element.addContent(new Element("addressing").addContent(selectStr.getAddressing().name()));
031        Element listElement = new Element("values");
032        List<String> list = selectStr.getList();
033        if (list != null) {
034            for (String s : list) {
035                listElement.addContent(new Element("value").addContent(s));
036            }
037        }
038        element.addContent(listElement);
039        if (selectStr.getLocalVariable() != null && !selectStr.getLocalVariable().isEmpty()) {
040            element.addContent(new Element("localVariable").addContent(selectStr.getLocalVariable()));
041        }
042        if (selectStr.getFormula() != null && !selectStr.getFormula().isEmpty()) {
043            element.addContent(new Element("formula").addContent(selectStr.getFormula()));
044        }
045
046        return element;
047    }
048
049    public void load(Element strElement, LogixNG_SelectStringList selectStr)
050            throws JmriConfigureXmlException {
051
052        if (strElement != null) {
053
054            try {
055                Element elem = strElement.getChild("addressing");
056                if (elem != null) {
057                    selectStr.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
058                }
059
060                List<String> list = selectStr.getList();
061                for (Element e : strElement.getChild("values").getChildren("value")) {
062                    list.add(e.getTextTrim());
063                }
064
065                elem = strElement.getChild("localVariable");
066                if (elem != null) selectStr.setLocalVariable(elem.getTextTrim());
067
068                elem = strElement.getChild("formula");
069                if (elem != null) selectStr.setFormula(elem.getTextTrim());
070
071            } catch (ParserException e) {
072                throw new JmriConfigureXmlException(e);
073            }
074        }
075    }
076
077}