001package jmri.jmrit.logixng.actions.configurexml; 002 003import java.util.*; 004 005import jmri.InstanceManager; 006import jmri.jmrit.logixng.DigitalActionManager; 007import jmri.jmrit.logixng.MaleSocket; 008import jmri.jmrit.logixng.actions.IfThenElse; 009 010import org.jdom2.Attribute; 011import org.jdom2.Element; 012 013 014/** 015 * Handle XML configuration for ActionLightXml objects. 016 * 017 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010 018 * @author Daniel Bergqvist Copyright (C) 2019 019 */ 020public class IfThenElseXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML { 021 022 /** 023 * Default implementation for storing the contents of a SE8cSignalHead 024 * 025 * @param o Object to store, of type TripleTurnoutSignalHead 026 * @return Element containing the complete info 027 */ 028 @Override 029 public Element store(Object o) { 030 IfThenElse p = (IfThenElse) o; 031 032 Element element = new Element("IfThenElse"); 033 element.setAttribute("class", this.getClass().getName()); 034 element.addContent(new Element("systemName").addContent(p.getSystemName())); 035 036 storeCommon(p, element); 037 038 element.setAttribute("executeType", p.getExecuteType().name()); 039 element.setAttribute("evaluateType", p.getEvaluateType().name()); 040 041 String socketSystemName; 042 Element e = new Element("Expressions"); 043 for (int i=0; i < p.getNumExpressions(); i++) { 044 Element e2 = new Element("Socket"); 045 e2.addContent(new Element("socketName").addContent(p.getExpressionSocket(i).getName())); 046 MaleSocket socket = p.getExpressionSocket(i).getConnectedSocket(); 047 if (socket != null) { 048 socketSystemName = socket.getSystemName(); 049 } else { 050 socketSystemName = p.getExpressionSocketSystemName(i); 051 } 052 if (socketSystemName != null) { 053 e2.addContent(new Element("systemName").addContent(socketSystemName)); 054 } 055 e.addContent(e2); 056 } 057 element.addContent(e); 058 059 e = new Element("Actions"); 060 for (int i=0; i < p.getNumActions(); i++) { 061 Element e2 = new Element("Socket"); 062 e2.addContent(new Element("socketName").addContent(p.getActionSocket(i).getName())); 063 MaleSocket socket = p.getActionSocket(i).getConnectedSocket(); 064 if (socket != null) { 065 socketSystemName = socket.getSystemName(); 066 } else { 067 socketSystemName = p.getActionSocketSystemName(i); 068 } 069 if (socketSystemName != null) { 070 e2.addContent(new Element("systemName").addContent(socketSystemName)); 071 } 072 e.addContent(e2); 073 } 074 element.addContent(e); 075 076 return element; 077 } 078 079 @Override 080 public boolean load(Element shared, Element perNode) { 081 082 IfThenElse.ExecuteType executeType = IfThenElse.ExecuteType.ExecuteOnChange; 083 IfThenElse.EvaluateType evaluateType = IfThenElse.EvaluateType.EvaluateAll; 084 085 Attribute typeAttr = shared.getAttribute("executeType"); 086 if (typeAttr != null) { 087 String typeStr = typeAttr.getValue(); 088 executeType = IfThenElse.ExecuteType.valueOf(typeStr); 089 } 090 091 typeAttr = shared.getAttribute("evaluateType"); 092 if (typeAttr != null) { 093 String typeStr = typeAttr.getValue(); 094 evaluateType = IfThenElse.EvaluateType.valueOf(typeStr); 095 } 096 097 // For backward compatibility pre JMRI 5.1.5 098 typeAttr = shared.getAttribute("type"); 099 if (typeAttr != null) { 100 String typeStr = typeAttr.getValue(); 101 executeType = IfThenElse.ExecuteType.valueOf(typeStr); 102 } 103 104 String sys = getSystemName(shared); 105 String uname = getUserName(shared); 106 107 List<Map.Entry<String, String>> expressionSystemNames = new ArrayList<>(); 108 109 Element expressionElement = shared.getChild("Expressions"); 110 if (expressionElement != null) { 111 for (Element socketElement : expressionElement.getChildren()) { 112 String socketName = socketElement.getChild("socketName").getTextTrim(); 113 Element systemNameElement = socketElement.getChild("systemName"); 114 String systemName = null; 115 if (systemNameElement != null) { 116 systemName = systemNameElement.getTextTrim(); 117 } 118 expressionSystemNames.add(new AbstractMap.SimpleEntry<>(socketName, systemName)); 119 } 120 } 121 122 List<Map.Entry<String, String>> actionSystemNames = new ArrayList<>(); 123 124 Element actionElement = shared.getChild("Actions"); 125 if (actionElement != null) { 126 for (Element socketElement : actionElement.getChildren()) { 127 String socketName = socketElement.getChild("socketName").getTextTrim(); 128 Element systemNameElement = socketElement.getChild("systemName"); 129 String systemName = null; 130 if (systemNameElement != null) { 131 systemName = systemNameElement.getTextTrim(); 132 } 133 actionSystemNames.add(new AbstractMap.SimpleEntry<>(socketName, systemName)); 134 } 135 } 136 137 // For backwards compability up until 5.1.3 138 if (shared.getChild("IfSocket") != null) { 139 String socketName = shared.getChild("IfSocket").getChild("socketName").getTextTrim(); 140 Element socketSystemName = shared.getChild("IfSocket").getChild("systemName"); 141 String systemName = null; 142 if (socketSystemName != null) { 143 systemName = socketSystemName.getTextTrim(); 144 } 145 expressionSystemNames.add(new AbstractMap.SimpleEntry<>(socketName, systemName)); 146 } 147 if (shared.getChild("ThenSocket") != null) { 148 String socketName = shared.getChild("ThenSocket").getChild("socketName").getTextTrim(); 149 Element socketSystemName = shared.getChild("ThenSocket").getChild("systemName"); 150 String systemName = null; 151 if (socketSystemName != null) { 152 systemName = socketSystemName.getTextTrim(); 153 } 154 actionSystemNames.add(new AbstractMap.SimpleEntry<>(socketName, systemName)); 155 } 156 if (shared.getChild("ElseSocket") != null) { 157 String socketName = shared.getChild("ElseSocket").getChild("socketName").getTextTrim(); 158 Element socketSystemName = shared.getChild("ElseSocket").getChild("systemName"); 159 String systemName = null; 160 if (socketSystemName != null) { 161 systemName = socketSystemName.getTextTrim(); 162 } 163 actionSystemNames.add(new AbstractMap.SimpleEntry<>(socketName, systemName)); 164 } 165 // For backwards compability up until 5.1.3 166 167 IfThenElse h = new IfThenElse(sys, uname, expressionSystemNames, actionSystemNames); 168 h.setExecuteType(executeType); 169 h.setEvaluateType(evaluateType); 170 171 loadCommon(h, shared); 172 173 InstanceManager.getDefault(DigitalActionManager.class).registerAction(h); 174 return true; 175 } 176 177// private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(IfThenElseXml.class); 178}