001package jmri.jmrit.logixng.actions.configurexml; 002 003import jmri.InstanceManager; 004import jmri.Memory; 005import jmri.configurexml.JmriConfigureXmlException; 006import jmri.jmrit.logixng.DigitalActionManager; 007import jmri.jmrit.logixng.NamedBeanAddressing; 008import jmri.jmrit.logixng.NamedTable; 009import jmri.jmrit.logixng.NamedTableManager; 010import jmri.jmrit.logixng.actions.ActionMemory; 011import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml; 012import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectTableXml; 013import jmri.jmrit.logixng.util.parser.ParserException; 014 015import org.jdom2.Element; 016 017/** 018 * Handle XML configuration for ActionMemory objects. 019 * 020 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010 021 * @author Daniel Bergqvist Copyright (C) 2019 022 */ 023public class ActionMemoryXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML { 024 025 public ActionMemoryXml() { 026 } 027 028 /** 029 * Default implementation for storing the contents of a ActionMemory 030 * 031 * @param o Object to store, of type ActionMemory 032 * @return Element containing the complete info 033 */ 034 @Override 035 public Element store(Object o) { 036 ActionMemory p = (ActionMemory) o; 037 038 LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml(); 039 040 Element element = new Element("ActionMemory"); 041 element.setAttribute("class", this.getClass().getName()); 042 element.addContent(new Element("systemName").addContent(p.getSystemName())); 043 044 storeCommon(p, element); 045 046 var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>(); 047 element.addContent(selectNamedBeanXml.store(p.getSelectNamedBean(), "namedBean")); 048 049 var selectOtherMemoryNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>(); 050 element.addContent(selectOtherMemoryNamedBeanXml.store(p.getSelectOtherMemoryNamedBean(), "otherMemoryNamedBean")); 051 052 element.addContent(new Element("memoryOperation").addContent(p.getMemoryOperation().name())); 053 054 element.addContent(new Element("otherConstant").addContent(p.getConstantValue())); 055 element.addContent(new Element("otherVariable").addContent(p.getOtherLocalVariable())); 056 element.addContent(new Element("otherFormula").addContent(p.getOtherFormula())); 057 058 element.addContent(selectTableXml.store(p.getSelectTable(), "table")); 059 060 return element; 061 } 062 063 @Override 064 public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException { 065 String sys = getSystemName(shared); 066 String uname = getUserName(shared); 067 ActionMemory h = new ActionMemory(sys, uname); 068 069 LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml(); 070 071 loadCommon(h, shared); 072 073 var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>(); 074 selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean()); 075 selectNamedBeanXml.loadLegacy(shared, h.getSelectNamedBean(), "memory"); 076 077 var selectOtherMemoryNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>(); 078 selectOtherMemoryNamedBeanXml.load(shared.getChild("otherMemoryNamedBean"), h.getSelectOtherMemoryNamedBean()); 079 selectOtherMemoryNamedBeanXml.loadLegacy(shared, h.getSelectOtherMemoryNamedBean(), "otherMemory", null, null, null, null); 080 081 Element queryType = shared.getChild("memoryOperation"); 082 if (queryType != null) { 083 try { 084 h.setMemoryOperation(ActionMemory.MemoryOperation.valueOf(queryType.getTextTrim())); 085 } catch (ParserException e) { 086 log.error("cannot set memory operation: {}", queryType.getTextTrim(), e); 087 } 088 } 089 090 try { 091 Element elem = shared.getChild("otherConstant"); 092 if (elem != null) h.setOtherConstantValue(elem.getTextTrim()); 093 094 elem = shared.getChild("otherTableCell"); 095 if (elem != null) { 096 boolean result = false; 097 String ref = elem.getTextTrim(); 098 if (!ref.isEmpty()) { 099 String[] refParts = ref.substring(1).split("[\\[\\]]"); // Remove first { and then split on [ and ] 100// System.out.format("refParts.length: %d, '%s', '%s'%n", refParts.length, refParts[0], refParts[1]); 101 if (refParts.length == 3) { 102 String table = refParts[0]; 103 String[] rowColumnParts = refParts[1].split(","); 104 if (rowColumnParts.length == 2) { 105 String row = rowColumnParts[0]; 106 String column = rowColumnParts[1]; 107// System.out.format("Table: '%s', row: '%s', column: '%s'%n", table, row, column); 108 109 h.getSelectTable().setTableNameAddressing(NamedBeanAddressing.Direct); 110 if (table != null) { 111 NamedTable t = InstanceManager.getDefault(NamedTableManager.class).getNamedTable(table); 112 if (t != null) h.getSelectTable().setTable(t); 113 else h.getSelectTable().removeTable(); 114 } 115 h.getSelectTable().setTableRowAddressing(NamedBeanAddressing.Direct); 116 h.getSelectTable().setTableRowName(row); 117 h.getSelectTable().setTableColumnAddressing(NamedBeanAddressing.Direct); 118 h.getSelectTable().setTableColumnName(column); 119 result = true; 120 } 121 } 122 if (!result) throw new JmriConfigureXmlException("otherTableCell has invalid value: "+ref); 123 } 124 } 125 126 elem = shared.getChild("otherVariable"); 127 if (elem != null) h.setOtherLocalVariable(elem.getTextTrim()); 128 129 elem = shared.getChild("otherFormula"); 130 if (elem != null) h.setOtherFormula(elem.getTextTrim()); 131 132 } catch (ParserException e) { 133 throw new JmriConfigureXmlException(e); 134 } 135 136 selectTableXml.load(shared.getChild("table"), h.getSelectTable()); 137 138 InstanceManager.getDefault(DigitalActionManager.class).registerAction(h); 139 return true; 140 } 141 142 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionMemoryXml.class); 143}