001package jmri.implementation.configurexml;
002
003import java.util.List;
004import jmri.InstanceManager;
005import jmri.NamedBeanHandle;
006import jmri.SignalHead;
007import jmri.Turnout;
008import jmri.implementation.TripleTurnoutSignalHead;
009import org.jdom2.Element;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Handle XML configuration for TripleTurnoutSignalHead objects.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2003, 2008
017 */
018public class TripleTurnoutSignalHeadXml extends DoubleTurnoutSignalHeadXml {
019
020    public TripleTurnoutSignalHeadXml() {
021    }
022
023    /**
024     * Default implementation for storing the contents of a
025     * TripleTurnoutSignalHead.
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        TripleTurnoutSignalHead p = (TripleTurnoutSignalHead) o;
033
034        Element element = new Element("signalhead");
035        element.setAttribute("class", this.getClass().getName());
036
037        // include contents
038        element.addContent(new Element("systemName").addContent(p.getSystemName()));
039
040        storeCommon(p, element);
041
042        element.addContent(addTurnoutElement(p.getGreen(), "green"));
043        element.addContent(addTurnoutElement(p.getYellow(), "yellow"));
044        element.addContent(addTurnoutElement(p.getRed(), "red"));
045
046        return element;
047    }
048
049    @Override
050    public boolean load(Element shared, Element perNode) {
051        List<Element> l = shared.getChildren("turnoutname");
052        if (l.size() == 0) {
053            l = shared.getChildren("turnout");
054        }
055        NamedBeanHandle<Turnout> green = loadTurnout(l.get(0));
056        NamedBeanHandle<Turnout> yellow = loadTurnout(l.get(1));
057        NamedBeanHandle<Turnout> red = loadTurnout(l.get(2));
058
059        // put it together
060        String sys = getSystemName(shared);
061        String uname = getUserName(shared);
062        SignalHead h;
063        if (uname == null) {
064            h = new TripleTurnoutSignalHead(sys, green, yellow, red);
065        } else {
066            h = new TripleTurnoutSignalHead(sys, uname, green, yellow, red);
067        }
068
069        loadCommon(h, shared);
070
071        SignalHead existingBean =
072                InstanceManager.getDefault(jmri.SignalHeadManager.class)
073                        .getBySystemName(sys);
074
075        if ((existingBean != null) && (existingBean != h)) {
076            log.error("systemName is already registered: {}", sys);
077        } else {
078            InstanceManager.getDefault(jmri.SignalHeadManager.class).register(h);
079        }
080
081        return true;
082    }
083
084    @Override
085    public void load(Element element, Object o) {
086        log.error("Invalid method called");
087    }
088
089    private final static Logger log = LoggerFactory.getLogger(TripleTurnoutSignalHeadXml.class);
090
091}