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