001package jmri.jmrix.openlcb.configurexml;
002
003import java.util.List;
004import jmri.InstanceManager;
005import jmri.JmriException;
006import jmri.SignalAppearanceMap;
007import jmri.jmrix.openlcb.OlcbSignalMast;
008import org.jdom2.Element;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012import javax.annotation.Nonnull;
013
014/**
015 * Handle XML configuration for OlcbSignalMast objects.
016 *
017 * @author Bob Jacobsen Copyright: Copyright (c) 2017, 2018
018 *
019 */
020public class OlcbSignalMastXml
021        extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
022
023    public OlcbSignalMastXml() {
024    }
025
026    /**
027     * Default implementation for storing the contents of a
028     * DefaultSignalMastManager
029     *
030     * @param o Object to store, of type OlcbSignalMast
031     * @return Element containing the complete info
032     */
033    @Override
034    public Element store(Object o) {
035        OlcbSignalMast p = (OlcbSignalMast) o;
036        Element e = new Element("olcbsignalmast");
037        e.setAttribute("class", this.getClass().getName());
038        e.addContent(new Element("systemName").addContent(p.getSystemName()));
039
040        storeCommon(p, e);
041
042        Element lit = new Element("lit");
043        lit.addContent(new Element("lit").addContent(p.getLitEventId()));
044        lit.addContent(new Element("notlit").addContent(p.getNotLitEventId()));
045        e.addContent(lit);
046
047        Element held = new Element("held");
048        held.addContent(new Element("held").addContent(p.getHeldEventId()));
049        held.addContent(new Element("notheld").addContent(p.getNotHeldEventId()));
050        e.addContent(held);
051
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("event").addContent(p.getOutputForAppearance(key)));
060                e.addContent(el);
061            }
062        }
063        List<String> disabledAspects = p.getDisabledAspects();
064        if (disabledAspects != null) {
065            Element el = new Element("disabledAspects");
066            for (String aspect : disabledAspects) {
067                Element ele = new Element("disabledAspect");
068                ele.addContent(aspect);
069                el.addContent(ele);
070            }
071            if (disabledAspects.size() != 0) {
072                e.addContent(el);
073            }
074        }
075        return e;
076    }
077
078    @Override
079    public boolean load(@Nonnull Element shared, Element perNode) {
080        OlcbSignalMast m;
081        String sys = getSystemName(shared);
082        try {
083            m = (OlcbSignalMast) InstanceManager.getDefault(jmri.SignalMastManager.class)
084                    .provideCustomSignalMast(sys, OlcbSignalMast.class);
085        } catch (JmriException e) {
086            log.error("Failed to load OlcbSignalMast {}", sys, e);
087            return false;
088        }
089
090        if (getUserName(shared) != null) {
091            m.setUserName(getUserName(shared));
092        }
093
094        return loadCommonOlcbMast(m, shared);
095    }
096
097    protected boolean loadCommonOlcbMast(OlcbSignalMast m, Element element) {
098        loadCommon(m, element);
099
100        if (element.getChild("lit") != null) {
101            Element lit = element.getChild("lit");
102            m.setLitEventId(lit.getChild("lit").getValue());
103            m.setNotLitEventId(lit.getChild("notlit").getValue());
104        }
105
106        if (element.getChild("held") != null) {
107            Element held = element.getChild("held");
108            m.setHeldEventId(held.getChild("held").getValue());
109            m.setNotHeldEventId(held.getChild("notheld").getValue());
110        }
111
112        List<Element> list = element.getChildren("aspect");
113        for (Element e : list) {
114            String aspect = e.getAttribute("defines").getValue();
115            String event = e.getChild("event").getValue();
116            m.setOutputForAppearance(aspect, event);
117        }
118        Element e = element.getChild("disabledAspects");
119        if (e != null) {
120            list = e.getChildren("disabledAspect");
121            for (Element aspect : list) {
122                m.setAspectDisabled(aspect.getText());
123            }
124        }
125
126        return true;
127
128    }
129
130    @Override
131    public void load(Element element, Object o) {
132        log.error("Invalid method called");
133    }
134
135    private final static Logger log = LoggerFactory.getLogger(OlcbSignalMastXml.class);
136}