001package jmri.implementation.configurexml; 002 003import java.util.List; 004import jmri.InstanceManager; 005import jmri.NamedBeanHandle; 006import jmri.SignalHead; 007import jmri.Turnout; 008import jmri.implementation.DoubleTurnoutSignalHead; 009import org.jdom2.Element; 010import org.slf4j.Logger; 011import org.slf4j.LoggerFactory; 012 013/** 014 * Handle XML configuration for DoubleTurnoutSignalHead objects. 015 * 016 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008 017 */ 018public class DoubleTurnoutSignalHeadXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML { 019 020 public DoubleTurnoutSignalHeadXml() { 021 } 022 023 /** 024 * Default implementation for storing the contents of a 025 * DoubleTurnoutSignalHead. 026 * 027 * @param o Object to store, of type TripleTurnoutSignalHead 028 * @return Element containing the complete info 029 */ 030 @Override 031 public Element store(Object o) { 032 DoubleTurnoutSignalHead p = (DoubleTurnoutSignalHead) o; 033 034 Element element = new Element("signalhead"); 035 element.setAttribute("class", this.getClass().getName()); 036 037 element.addContent(new Element("systemName").addContent(p.getSystemName())); 038 039 storeCommon(p, element); 040 041 element.addContent(addTurnoutElement(p.getGreen(), "green")); 042 element.addContent(addTurnoutElement(p.getRed(), "red")); 043 044 return element; 045 } 046 047 Element addTurnoutElement(NamedBeanHandle<Turnout> to, String which) { 048 Element el = new Element("turnoutname"); 049 el.setAttribute("defines", which); 050 el.addContent(to.getName()); 051 return el; 052 } 053 054 Element addTurnoutElement(Turnout to) { 055 String user = to.getUserName(); 056 String sys = to.getSystemName(); 057 058 Element el = new Element("turnout"); 059 el.setAttribute("systemName", sys); 060 if (user != null) { 061 el.setAttribute("userName", user); 062 } 063 064 return el; 065 } 066 067 @Override 068 public boolean load(Element shared, Element perNode) { 069 List<Element> l = shared.getChildren("turnoutname"); 070 if (l.size() == 0) { 071 l = shared.getChildren("turnout"); 072 } 073 NamedBeanHandle<Turnout> green = loadTurnout(l.get(0)); 074 NamedBeanHandle<Turnout> red = loadTurnout(l.get(1)); 075 // put it together 076 String sys = getSystemName(shared); 077 String uname = getUserName(shared); 078 SignalHead h; 079 if (uname == null) { 080 h = new DoubleTurnoutSignalHead(sys, green, red); 081 } else { 082 h = new DoubleTurnoutSignalHead(sys, uname, green, red); 083 } 084 085 loadCommon(h, shared); 086 087 SignalHead existingBean = 088 InstanceManager.getDefault(jmri.SignalHeadManager.class) 089 .getBySystemName(sys); 090 091 if ((existingBean != null) && (existingBean != h)) { 092 log.error("systemName is already registered: {}", sys); 093 } else { 094 InstanceManager.getDefault(jmri.SignalHeadManager.class).register(h); 095 } 096 097 return true; 098 } 099 100 /** 101 * Process stored signal head output (turnout). 102 * <p> 103 * Needs to handle two types of element: turnoutname is new form; turnout is 104 * old form. 105 * 106 * @param o xml object defining a turnout on an SE8C signal head 107 * @return named bean for the turnout 108 */ 109 NamedBeanHandle<Turnout> loadTurnout(Object o) { 110 Element e = (Element) o; 111 112 if (e.getName().equals("turnout")) { 113 String name = e.getAttribute("systemName").getValue(); 114 Turnout t; 115 if (e.getAttribute("userName") != null 116 && !e.getAttribute("userName").getValue().equals("")) { 117 name = e.getAttribute("userName").getValue(); 118 t = InstanceManager.turnoutManagerInstance().getTurnout(name); 119 } else { 120 t = InstanceManager.turnoutManagerInstance().getBySystemName(name); 121 } 122 if (t != null) { 123 return jmri.InstanceManager.getDefault(jmri.NamedBeanHandleManager.class).getNamedBeanHandle(name, t); 124 } else { 125 log.warn("Failed to find turnout {}. Check connection and configuration", name); 126 return null; 127 } 128 } else { 129 String name = e.getText(); 130 try { 131 Turnout t = InstanceManager.turnoutManagerInstance().provideTurnout(name); 132 return jmri.InstanceManager.getDefault(jmri.NamedBeanHandleManager.class).getNamedBeanHandle(name, t); 133 } catch (IllegalArgumentException ex) { 134 log.warn("Failed to provide Turnout \"{}\" in sendStatus", name); 135 return null; 136 } 137 } 138 } 139 140 private final static Logger log = LoggerFactory.getLogger(DoubleTurnoutSignalHeadXml.class); 141 142}