001package jmri.implementation.configurexml;
002
003import java.util.List;
004import jmri.InstanceManager;
005import jmri.SignalMast;
006import jmri.implementation.VirtualSignalMast;
007import org.jdom2.Element;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Handle XML configuration for DefaultSignalMastManager objects.
013 *
014 * @author Bob Jacobsen Copyright: Copyright (c) 2009
015 * 
016 */
017public class VirtualSignalMastXml
018        extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
019
020    public VirtualSignalMastXml() {
021    }
022
023    /**
024     * Default implementation for storing the contents of a
025     * DefaultSignalMastManager
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        VirtualSignalMast p = (VirtualSignalMast) o;
033        Element e = new Element("virtualsignalmast");
034        e.setAttribute("class", this.getClass().getName());
035        e.addContent(new Element("systemName").addContent(p.getSystemName()));
036        storeCommon(p, e);
037        Element unlit = new Element("unlit");
038        if (p.allowUnLit()) {
039            unlit.setAttribute("allowed", "yes");
040        } else {
041            unlit.setAttribute("allowed", "no");
042        }
043        e.addContent(unlit);
044        List<String> disabledAspects = p.getDisabledAspects();
045        if (disabledAspects != null) {
046            Element el = new Element("disabledAspects");
047            for (String aspect : disabledAspects) {
048                Element ele = new Element("disabledAspect");
049                ele.addContent(aspect);
050                el.addContent(ele);
051            }
052            if (disabledAspects.size() != 0) {
053                e.addContent(el);
054            }
055        }
056        return e;
057    }
058
059    @Override
060    public boolean load(Element shared, Element perNode) {
061        VirtualSignalMast m;
062        String sys = getSystemName(shared);
063        SignalMast previous = InstanceManager.getDefault(jmri.SignalMastManager.class)
064                .getBySystemName(sys);
065        if (previous != null) {
066            if (previous instanceof VirtualSignalMast) {
067                m = (VirtualSignalMast) previous;
068            } else {
069                log.error("Cannot load signal mast because system name {} is already in use.", sys);
070                return false;
071            }
072        } else {
073            m = new VirtualSignalMast(sys);
074        }
075
076        if (getUserName(shared) != null) {
077            m.setUserName(getUserName(shared));
078        }
079
080        loadCommon(m, shared);
081        if (shared.getChild("unlit") != null) {
082            Element unlit = shared.getChild("unlit");
083            if (unlit.getAttribute("allowed") != null) {
084                if (unlit.getAttribute("allowed").getValue().equals("no")) {
085                    m.setAllowUnLit(false);
086                } else {
087                    m.setAllowUnLit(true);
088                }
089            }
090        }
091        Element e = shared.getChild("disabledAspects");
092        if (e != null) {
093            List<Element> list = e.getChildren("disabledAspect");
094            for (Element aspect : list) {
095                m.setAspectDisabled(aspect.getText());
096            }
097        }
098
099        InstanceManager.getDefault(jmri.SignalMastManager.class)
100                .register(m);
101
102        return true;
103    }
104
105    private final static Logger log = LoggerFactory.getLogger(VirtualSignalMastXml.class);
106}