001package jmri.jmrit.logixng.util.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.NamedBeanAddressing;
006import jmri.jmrit.logixng.NamedTable;
007import jmri.jmrit.logixng.NamedTableManager;
008import jmri.jmrit.logixng.util.LogixNG_SelectTable;
009import jmri.jmrit.logixng.util.parser.ParserException;
010
011import org.jdom2.Element;
012
013/**
014 * Xml class for jmri.jmrit.logixng.util.LogixNG_SelectTable.
015 * @author Daniel Bergqvist (C) 2022
016 */
017public class LogixNG_SelectTableXml {
018
019    /**
020     * Default implementation for storing the contents of a LogixNG_SelectTable
021     *
022     * @param selectTable the LogixNG_SelectTable object
023     * @param tagName the name of the element
024     * @return Element containing the complete info
025     */
026    public Element store(LogixNG_SelectTable selectTable, String tagName) {
027        Element tableElement = new Element(tagName);
028
029        LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
030
031        Element tableNameElement = new Element("tableName");
032        tableNameElement.addContent(new Element("addressing").addContent(selectTable.getTableNameAddressing().name()));
033        NamedBeanHandle<NamedTable> table = selectTable.getTable();
034        if (table != null) {
035            tableNameElement.addContent(new Element("name").addContent(table.getName()));
036        }
037        if (selectTable.getTableNameReference() != null && !selectTable.getTableNameReference().isEmpty()) {
038            tableNameElement.addContent(new Element("reference").addContent(selectTable.getTableNameReference()));
039        }
040        var memory = selectTable.getTableNameMemory();
041        if (memory != null) {
042            tableNameElement.addContent(new Element("memory").addContent(memory.getName()));
043        }
044        if (selectTable.getTableNameLocalVariable() != null && !selectTable.getTableNameLocalVariable().isEmpty()) {
045            tableNameElement.addContent(new Element("localVariable").addContent(selectTable.getTableNameLocalVariable()));
046        }
047        if (selectTable.getTableNameFormula() != null && !selectTable.getTableNameFormula().isEmpty()) {
048            tableNameElement.addContent(new Element("formula").addContent(selectTable.getTableNameFormula()));
049        }
050        if (selectTable.getTableNameAddressing() == NamedBeanAddressing.Table) {
051            tableNameElement.addContent(selectTableXml.store(selectTable.getSelectTableName(), "table"));
052        }
053        tableElement.addContent(tableNameElement);
054
055        Element tableRowElement = new Element("row");
056        tableRowElement.addContent(new Element("addressing").addContent(selectTable.getTableRowAddressing().name()));
057        if (selectTable.getTableRowName() != null && !selectTable.getTableRowName().isEmpty()) {
058            tableRowElement.addContent(new Element("name").addContent(selectTable.getTableRowName()));
059        }
060        if (selectTable.getTableRowReference() != null && !selectTable.getTableRowReference().isEmpty()) {
061            tableRowElement.addContent(new Element("reference").addContent(selectTable.getTableRowReference()));
062        }
063        memory = selectTable.getTableRowMemory();
064        if (memory != null) {
065            tableRowElement.addContent(new Element("memory").addContent(memory.getName()));
066        }
067        if (selectTable.getTableRowLocalVariable() != null && !selectTable.getTableRowLocalVariable().isEmpty()) {
068            tableRowElement.addContent(new Element("localVariable").addContent(selectTable.getTableRowLocalVariable()));
069        }
070        if (selectTable.getTableRowFormula() != null && !selectTable.getTableRowFormula().isEmpty()) {
071            tableRowElement.addContent(new Element("formula").addContent(selectTable.getTableRowFormula()));
072        }
073        if (selectTable.getTableRowAddressing() == NamedBeanAddressing.Table) {
074            tableRowElement.addContent(selectTableXml.store(selectTable.getSelectTableRow(), "table"));
075        }
076        tableElement.addContent(tableRowElement);
077
078        Element tableColumnElement = new Element("column");
079        tableColumnElement.addContent(new Element("addressing").addContent(selectTable.getTableColumnAddressing().name()));
080        if (selectTable.getTableColumnName() != null && !selectTable.getTableColumnName().isEmpty()) {
081            tableColumnElement.addContent(new Element("name").addContent(selectTable.getTableColumnName()));
082        }
083        if (selectTable.getTableColumnReference() != null && !selectTable.getTableColumnReference().isEmpty()) {
084            tableColumnElement.addContent(new Element("reference").addContent(selectTable.getTableColumnReference()));
085        }
086        memory = selectTable.getTableColumnMemory();
087        if (memory != null) {
088            tableColumnElement.addContent(new Element("memory").addContent(memory.getName()));
089        }
090        if (selectTable.getTableColumnLocalVariable() != null && !selectTable.getTableColumnLocalVariable().isEmpty()) {
091            tableColumnElement.addContent(new Element("localVariable").addContent(selectTable.getTableColumnLocalVariable()));
092        }
093        if (selectTable.getTableColumnFormula() != null && !selectTable.getTableColumnFormula().isEmpty()) {
094            tableColumnElement.addContent(new Element("formula").addContent(selectTable.getTableColumnFormula()));
095        }
096        if (selectTable.getTableColumnAddressing() == NamedBeanAddressing.Table) {
097            tableColumnElement.addContent(selectTableXml.store(selectTable.getSelectTableColumn(), "table"));
098        }
099        tableElement.addContent(tableColumnElement);
100
101        return tableElement;
102    }
103
104    public void load(Element tableElement, LogixNG_SelectTable selectTable) throws JmriConfigureXmlException {
105
106        if (tableElement != null) {
107
108            LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
109
110            try {
111                Element tableName = tableElement.getChild("tableName");
112
113                // Table name
114
115                Element elem = tableName.getChild("addressing");
116                if (elem != null) {
117                    selectTable.setTableNameAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
118                }
119
120                elem = tableName.getChild("name");
121                if (elem != null) {
122                    NamedTable t = InstanceManager.getDefault(NamedTableManager.class).getNamedTable(elem.getTextTrim());
123                    if (t != null) selectTable.setTable(t);
124                    else selectTable.removeTable();
125                }
126
127                elem = tableName.getChild("reference");
128                if (elem != null) selectTable.setTableNameReference(elem.getTextTrim());
129
130                Element memoryName = tableName.getChild("memory");
131                if (memoryName != null) {
132                    Memory m = InstanceManager.getDefault(MemoryManager.class).getMemory(memoryName.getTextTrim());
133                    if (m != null) selectTable.setTableNameMemory(m);
134                    else selectTable.removeTableNameMemory();
135                }
136
137                elem = tableName.getChild("localVariable");
138                if (elem != null) selectTable.setTableNameLocalVariable(elem.getTextTrim());
139
140                elem = tableName.getChild("formula");
141                if (elem != null) selectTable.setTableNameFormula(elem.getTextTrim());
142
143                elem = tableName.getChild("table");
144                if (elem != null) {
145                    selectTableXml.load(elem, selectTable.getSelectTableName());
146                }
147
148
149                // Table row
150
151                Element tableRow = tableElement.getChild("row");
152                elem = tableRow.getChild("addressing");
153                if (elem != null) {
154                    selectTable.setTableRowAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
155                }
156
157                elem = tableRow.getChild("name");
158                if (elem != null) {
159                    selectTable.setTableRowName(elem.getTextTrim());
160                }
161
162                elem = tableRow.getChild("reference");
163                if (elem != null) selectTable.setTableRowReference(elem.getTextTrim());
164
165                memoryName = tableRow.getChild("memory");
166                if (memoryName == null
167                        && selectTable.getTableNameAddressing() != NamedBeanAddressing.Memory
168                        && selectTable.getTableRowAddressing() == NamedBeanAddressing.Memory
169                        && selectTable.getTableColumnAddressing() != NamedBeanAddressing.Memory) {
170                    // Handle bug pre JMRI 5.7.6
171                    memoryName = tableName.getChild("memory");
172                    selectTable.removeTableNameMemory();
173                }
174                if (memoryName != null) {
175                    Memory m = InstanceManager.getDefault(MemoryManager.class).getMemory(memoryName.getTextTrim());
176                    if (m != null) selectTable.setTableRowMemory(m);
177                    else selectTable.removeTableRowMemory();
178                }
179
180                elem = tableRow.getChild("localVariable");
181                if (elem != null) selectTable.setTableRowLocalVariable(elem.getTextTrim());
182
183                elem = tableRow.getChild("formula");
184                if (elem != null) selectTable.setTableRowFormula(elem.getTextTrim());
185
186                elem = tableRow.getChild("table");
187                if (elem != null) {
188                    selectTableXml.load(elem, selectTable.getSelectTableRow());
189                }
190
191
192                // Table column
193
194                Element tableColumn = tableElement.getChild("column");
195                elem = tableColumn.getChild("addressing");
196                if (elem != null) {
197                    selectTable.setTableColumnAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
198                }
199
200                elem = tableColumn.getChild("name");
201                if (elem != null) {
202                    selectTable.setTableColumnName(elem.getTextTrim());
203                }
204
205                elem = tableColumn.getChild("reference");
206                if (elem != null) selectTable.setTableColumnReference(elem.getTextTrim());
207
208                memoryName = tableColumn.getChild("memory");
209                if (memoryName == null
210                        && selectTable.getTableNameAddressing() != NamedBeanAddressing.Memory
211                        && selectTable.getTableRowAddressing() != NamedBeanAddressing.Memory
212                        && selectTable.getTableColumnAddressing() == NamedBeanAddressing.Memory) {
213                    // Handle bug pre JMRI 5.7.6
214                    memoryName = tableName.getChild("memory");
215                    selectTable.removeTableNameMemory();
216                }
217                if (memoryName != null) {
218                    Memory m = InstanceManager.getDefault(MemoryManager.class).getMemory(memoryName.getTextTrim());
219                    if (m != null) selectTable.setTableColumnMemory(m);
220                    else selectTable.removeTableColumnMemory();
221                }
222
223                elem = tableColumn.getChild("localVariable");
224                if (elem != null) selectTable.setTableColumnLocalVariable(elem.getTextTrim());
225
226                elem = tableColumn.getChild("formula");
227                if (elem != null) selectTable.setTableColumnFormula(elem.getTextTrim());
228
229                elem = tableColumn.getChild("table");
230                if (elem != null) {
231                    selectTableXml.load(elem, selectTable.getSelectTableColumn());
232                }
233
234            } catch (ParserException e) {
235                throw new JmriConfigureXmlException(e);
236            }
237        }
238    }
239
240}