001package jmri.jmrix.bidib.configurexml;
002
003import java.util.List;
004import jmri.InstanceManager;
005import jmri.JmriException;
006import jmri.SignalAppearanceMap;
007import jmri.jmrix.bidib.BiDiBSignalMast;
008import org.jdom2.Element;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Handle XML configuration for BiDiBSignalMast objects.
014 *
015 * @author Bob Jacobsen Copyright: Copyright (c) 2017, 2018
016 * @author Eckart Meyer Copyright: Copyright (c) 2020
017 * 
018 */
019public class BiDiBSignalMastXml
020        extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
021
022    public BiDiBSignalMastXml() {
023    }
024
025    /**
026     * Implementation for storing the contents of a
027     * BiDiBSignalMastManager
028     *
029     * @param o Object to store, of type BiDiBSignalMast
030     * @return Element containing the complete info
031     */
032    @Override
033    public Element store(Object o) {
034        BiDiBSignalMast p = (BiDiBSignalMast) o;
035        Element e = new Element("dccsignalmast"); //XML schema is the same than "dccsignalmast"
036        e.setAttribute("class", this.getClass().getName());
037        e.addContent(new Element("systemName").addContent(p.getSystemName()));
038
039        storeCommon(p, e);
040        Element unlit = new Element("unlit");
041        if (p.allowUnLit()) {
042            unlit.setAttribute("allowed", "yes");
043            unlit.addContent(new Element("aspect").addContent(Integer.toString(p.getUnlitId())));
044        } else {
045            unlit.setAttribute("allowed", "no");
046        }
047        e.addContent(unlit);
048
049        SignalAppearanceMap appMap = p.getAppearanceMap();
050        if (appMap != null) {
051            java.util.Enumeration<String> aspects = appMap.getAspects();
052            while (aspects.hasMoreElements()) {
053                String key = aspects.nextElement();
054                Element el = new Element("aspect");
055                el.setAttribute("defines", key);
056                el.addContent(new Element("number").addContent(Integer.toString(p.getOutputForAppearance(key))));
057                e.addContent(el);
058            }
059        }
060        List<String> disabledAspects = p.getDisabledAspects();
061        if (disabledAspects != null) {
062            Element el = new Element("disabledAspects");
063            for (String aspect : disabledAspects) {
064                Element ele = new Element("disabledAspect");
065                ele.addContent(aspect);
066                el.addContent(ele);
067            }
068            if (disabledAspects.size() != 0) {
069                e.addContent(el);
070            }
071        }
072        return e;
073    }
074
075    @Override
076    public boolean load(Element shared, Element perNode) {
077        BiDiBSignalMast m;
078        String sys = getSystemName(shared);
079        try {
080            m = (BiDiBSignalMast) InstanceManager.getDefault(jmri.SignalMastManager.class)
081                    .provideCustomSignalMast(sys, BiDiBSignalMast.class);
082        } catch (JmriException e) {
083            log.error("Failed to load BiDiBSignalMast {}:", sys, e);
084            return false;
085        }
086
087        if (getUserName(shared) != null) {
088            m.setUserName(getUserName(shared));
089        }
090
091        boolean result = loadCommonBiDiBMast(m, shared);
092        if (result) {
093            // query state from layout - must be done after loadCommonBiDiBMast since this will load the aspect names
094            m.queryAccessory();
095        }
096        return result;
097    }
098
099    protected boolean loadCommonBiDiBMast(BiDiBSignalMast m, Element element) {
100        loadCommon(m, element);
101        if (element.getChild("unlit") != null) {
102            Element unlit = element.getChild("unlit");
103            if (unlit.getAttribute("allowed") != null) {
104                if (unlit.getAttribute("allowed").getValue().equals("no")) {
105                    m.setAllowUnLit(false);
106                } else {
107                    m.setAllowUnLit(true);
108                    m.setUnlitId(Integer.parseInt(unlit.getChild("aspect").getValue()));
109                }
110            }
111        }
112        List<Element> list = element.getChildren("aspect");
113        for (int i = 0; i < list.size(); i++) {
114            Element e = list.get(i);
115            String aspect = e.getAttribute("defines").getValue();
116            int number = -1;
117            try {
118                String value = e.getChild("number").getValue();
119                number = Integer.parseInt(value);
120
121            } catch (Exception ex) {
122                log.error("failed to convert DCC number");
123            }
124            m.setOutputForAppearance(aspect, number);
125        }
126        Element e = element.getChild("disabledAspects");
127        if (e != null) {
128            list = e.getChildren("disabledAspect");
129            for (Element aspect : list) {
130                m.setAspectDisabled(aspect.getText());
131            }
132        }
133
134        return true;
135    }
136
137    @Override
138    public void load(Element element, Object o) {
139        log.error("Invalid method called");
140    }
141
142    private final static Logger log = LoggerFactory.getLogger(BiDiBSignalMastXml.class);
143}