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