001package jmri.implementation.configurexml;
002
003import jmri.InstanceManager;
004import jmri.SignalHead;
005import jmri.implementation.VirtualSignalHead;
006import org.jdom2.Element;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * Handle XML configuration for VirtualSignalHead objects.
012 *
013 * @author Bob Jacobsen Copyright: Copyright (c) 2005, 2008
014 */
015public class VirtualSignalHeadXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
016
017    public VirtualSignalHeadXml() {
018    }
019
020    /**
021     * Default implementation for storing the contents of a VirtualSignalHead.
022     *
023     * @param o Object to store, of type TripleTurnoutSignalHead
024     * @return Element containing the complete info
025     */
026    @Override
027    public Element store(Object o) {
028        VirtualSignalHead p = (VirtualSignalHead) o;
029
030        Element element = new Element("signalhead");
031        element.setAttribute("class", this.getClass().getName());
032
033        // include contents
034        element.addContent(new Element("systemName").addContent(p.getSystemName()));
035
036        storeCommon(p, element);
037
038        return element;
039    }
040
041    @Override
042    public boolean load(Element shared, Element perNode) {
043        // put it together
044        String sys = getSystemName(shared);
045        String uname = getUserName(shared);
046        SignalHead h;
047        if (uname == null) {
048            h = new VirtualSignalHead(sys);
049        } else {
050            h = new VirtualSignalHead(sys, uname);
051        }
052
053        loadCommon(h, shared);
054
055        SignalHead existingBean =
056                InstanceManager.getDefault(jmri.SignalHeadManager.class)
057                        .getBySystemName(sys);
058
059        if ((existingBean != null) && (existingBean != h)) {
060            log.error("systemName is already registered: {}", sys);
061        } else {
062            InstanceManager.getDefault(jmri.SignalHeadManager.class).register(h);
063        }
064
065        return true;
066    }
067
068    private final static Logger log = LoggerFactory.getLogger(VirtualSignalHeadXml.class);
069
070}