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 enumElement.addContent(new Element("listenToMemory").addContent(selectStr.getListenToMemory() ? "yes" : "no")); 043 if (selectStr.getLocalVariable() != null && !selectStr.getLocalVariable().isEmpty()) { 044 enumElement.addContent(new Element("localVariable").addContent(selectStr.getLocalVariable())); 045 } 046 if (selectStr.getFormula() != null && !selectStr.getFormula().isEmpty()) { 047 enumElement.addContent(new Element("formula").addContent(selectStr.getFormula())); 048 } 049 050 if (selectStr.getAddressing() == NamedBeanAddressing.Table) { 051 enumElement.addContent(selectTableXml.store(selectStr.getSelectTable(), "table")); 052 } 053 054 return enumElement; 055 } 056 057 public void load(Element strElement, LogixNG_SelectString selectStr) 058 throws JmriConfigureXmlException { 059 060 if (strElement != null) { 061 062 LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml(); 063 064 try { 065 Element elem = strElement.getChild("addressing"); 066 if (elem != null) { 067 selectStr.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim())); 068 } 069 070 elem = strElement.getChild("value"); 071 if (elem != null) { 072 selectStr.setValue(elem.getTextTrim()); 073 } 074 075 elem = strElement.getChild("reference"); 076 if (elem != null) selectStr.setReference(elem.getTextTrim()); 077 078 Element memoryName = strElement.getChild("memory"); 079 if (memoryName != null) { 080 Memory m = InstanceManager.getDefault(MemoryManager.class).getMemory(memoryName.getTextTrim()); 081 if (m != null) selectStr.setMemory(m); 082 else selectStr.removeMemory(); 083 } 084 085 Element listenToMemoryElem = strElement.getChild("listenToMemory"); 086 if (listenToMemoryElem != null) { 087 selectStr.setListenToMemory("yes".equals(listenToMemoryElem.getTextTrim())); 088 } 089 090 elem = strElement.getChild("localVariable"); 091 if (elem != null) selectStr.setLocalVariable(elem.getTextTrim()); 092 093 elem = strElement.getChild("formula"); 094 if (elem != null) selectStr.setFormula(elem.getTextTrim()); 095 096 if (strElement.getChild("table") != null) { 097 selectTableXml.load(strElement.getChild("table"), selectStr.getSelectTable()); 098 } 099 100 } catch (ParserException e) { 101 throw new JmriConfigureXmlException(e); 102 } 103 } 104 } 105 106 /** 107 * This method is for backward compability up to and including 4.99.4.Remove this class after 5.0. 108 * 109 * @param shared the shared element 110 * @param selectStr the LogixNG_SelectEnum 111 * @param addressingElementName the name of the element of the addressing 112 * @param valueElementName the name of the element of the string 113 * @param referenceElementName the name of the element of the reference 114 * @param localVariableElementName the name of the element of the local variable 115 * @param formulaElementName the name of the element of the formula 116 * @throws JmriConfigureXmlException if an exception occurs 117 */ 118 public void loadLegacy( 119 Element shared, 120 LogixNG_SelectString selectStr, 121 String addressingElementName, 122 String valueElementName, 123 String referenceElementName, 124 String localVariableElementName, 125 String formulaElementName) 126 throws JmriConfigureXmlException { 127 128 Element name = shared.getChild(valueElementName); 129 if (name != null) { 130 selectStr.setValue(name.getTextTrim()); 131 } 132 133 try { 134 Element elem = shared.getChild(addressingElementName); 135 if (elem != null) { 136 selectStr.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim())); 137 } 138 139 elem = shared.getChild(referenceElementName); 140 if (elem != null) selectStr.setReference(elem.getTextTrim()); 141 142 elem = shared.getChild(localVariableElementName); 143 if (elem != null) selectStr.setLocalVariable(elem.getTextTrim()); 144 145 elem = shared.getChild(formulaElementName); 146 if (elem != null) selectStr.setFormula(elem.getTextTrim()); 147 148 } catch (ParserException e) { 149 throw new JmriConfigureXmlException(e); 150 } 151 } 152 153}