001package jmri.implementation.configurexml; 002 003import java.util.List; 004import jmri.InstanceManager; 005import jmri.JmriException; 006import jmri.SignalAppearanceMap; 007import jmri.Turnout; 008import jmri.implementation.TurnoutSignalMast; 009import org.jdom2.Element; 010import org.slf4j.Logger; 011import org.slf4j.LoggerFactory; 012 013/** 014 * Handle XML configuration for TurnoutSignalMast objects. 015 * 016 * @author Bob Jacobsen Copyright: Copyright (c) 2009 017 */ 018public class TurnoutSignalMastXml 019 extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML { 020 021 public TurnoutSignalMastXml() { 022 } 023 024 /** 025 * Default implementation for storing the contents of a 026 * DefaultSignalMastManager 027 * 028 * @param o Object to store, of type TripleTurnoutSignalHead 029 * @return Element containing the complete info 030 */ 031 @Override 032 public Element store(Object o) { 033 TurnoutSignalMast p = (TurnoutSignalMast) o; 034 Element e = new Element("turnoutsignalmast"); 035 e.setAttribute("class", this.getClass().getName()); 036 e.addContent(new Element("systemName").addContent(p.getSystemName())); 037 038 storeCommon(p, e); 039 Element unlit = new Element("unlit"); 040 if (p.allowUnLit()) { 041 unlit.setAttribute("allowed", "yes"); 042 unlit.addContent(new Element("turnout").addContent(p.getUnLitTurnoutName())); 043 if (p.getUnLitTurnoutState() == Turnout.CLOSED) { 044 unlit.addContent(new Element("turnoutstate").addContent("closed")); 045 } else { 046 unlit.addContent(new Element("turnoutstate").addContent("thrown")); 047 } 048 } else { 049 unlit.setAttribute("allowed", "no"); 050 } 051 e.addContent(unlit); 052 SignalAppearanceMap appMap = p.getAppearanceMap(); 053 if (appMap != null) { 054 java.util.Enumeration<String> aspects = appMap.getAspects(); 055 while (aspects.hasMoreElements()) { 056 String key = aspects.nextElement(); 057 Element el = new Element("aspect"); 058 el.setAttribute("defines", key); 059 el.addContent(new Element("turnout").addContent(p.getTurnoutName(key))); 060 if (p.getTurnoutState(key) == Turnout.CLOSED) { 061 el.addContent(new Element("turnoutstate").addContent("closed")); 062 } else { 063 el.addContent(new Element("turnoutstate").addContent("thrown")); 064 } 065 e.addContent(el); 066 } 067 } 068 List<String> disabledAspects = p.getDisabledAspects(); 069 if (disabledAspects != null) { 070 Element el = new Element("disabledAspects"); 071 for (String aspect : disabledAspects) { 072 Element ele = new Element("disabledAspect"); 073 ele.addContent(aspect); 074 el.addContent(ele); 075 } 076 if (disabledAspects.size() != 0) { 077 e.addContent(el); 078 } 079 } 080 if (p.resetPreviousStates()) { 081 e.addContent(new Element("resetPreviousStates").addContent("yes")); 082 } 083 return e; 084 } 085 086 @Override 087 public boolean load(Element shared, Element perNode) { 088 TurnoutSignalMast m; 089 String sys = getSystemName(shared); 090 try { 091 m = (TurnoutSignalMast) InstanceManager.getDefault(jmri.SignalMastManager.class) 092 .provideCustomSignalMast(sys, TurnoutSignalMast.class); 093 } catch (JmriException e) { 094 log.error("Failed to load TurnoutSignalMast {}", sys, e); 095 return false; 096 } 097 098 if (getUserName(shared) != null) { 099 m.setUserName(getUserName(shared)); 100 } 101 102 loadCommon(m, shared); 103 104 if (shared.getChild("unlit") != null) { 105 Element unlit = shared.getChild("unlit"); 106 if (unlit.getAttribute("allowed") != null) { 107 if (unlit.getAttribute("allowed").getValue().equals("no")) { 108 m.setAllowUnLit(false); 109 } else { 110 m.setAllowUnLit(true); 111 String turnout = unlit.getChild("turnout").getText(); 112 String turnoutState = unlit.getChild("turnoutstate").getText(); 113 int turnState = Turnout.THROWN; 114 if (turnoutState.equals("closed")) { 115 turnState = Turnout.CLOSED; 116 } 117 m.setUnLitTurnout(turnout, turnState); 118 } 119 } 120 } 121 122 List<Element> list = shared.getChildren("aspect"); 123 for (int i = 0; i < list.size(); i++) { 124 Element e = list.get(i); 125 String aspect = e.getAttribute("defines").getValue(); 126 String turnout = e.getChild("turnout").getText(); 127 String turnoutState = e.getChild("turnoutstate").getText(); 128 int turnState = Turnout.THROWN; 129 if (turnoutState.equals("closed")) { 130 turnState = Turnout.CLOSED; 131 } 132 m.setTurnout(aspect, turnout, turnState); 133 } 134 Element e = shared.getChild("disabledAspects"); 135 if (e != null) { 136 list = e.getChildren("disabledAspect"); 137 for (Element aspect : list) { 138 m.setAspectDisabled(aspect.getText()); 139 } 140 } 141 if ((shared.getChild("resetPreviousStates") != null) 142 && shared.getChild("resetPreviousStates").getText().equals("yes")) { 143 m.resetPreviousStates(true); 144 } 145 146 return true; 147 } 148 149 private final static Logger log = LoggerFactory.getLogger(TurnoutSignalMastXml.class); 150}