001package jmri.implementation.configurexml;
002
003import java.util.List;
004import jmri.InstanceManager;
005import jmri.SignalMast;
006import jmri.implementation.SignalHeadSignalMast;
007import org.jdom2.Element;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Handle XML configuration for a DefaultSignalMastManager objects.
013 *
014 * @author Bob Jacobsen Copyright: Copyright (c) 2009
015 * 
016 */
017public class SignalHeadSignalMastXml
018        extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
019
020    public SignalHeadSignalMastXml() {
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        SignalHeadSignalMast p = (SignalHeadSignalMast) o;
033        Element e = new Element("signalmast");
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        SignalMast m;
062        String sys = getSystemName(shared);
063        try {
064            m = InstanceManager.getDefault(jmri.SignalMastManager.class)
065                    .provideSignalMast(sys);
066        } catch (Exception e) {
067            log.error("An error occurred while trying to create the signal '{}' {}", sys, e.toString());
068            return false;
069        }
070        if (getUserName(shared) != null) {
071            m.setUserName(getUserName(shared));
072        }
073
074        loadCommon(m, shared);
075        if (shared.getChild("unlit") != null) {
076            Element unlit = shared.getChild("unlit");
077            if (unlit.getAttribute("allowed") != null) {
078                if (unlit.getAttribute("allowed").getValue().equals("no")) {
079                    m.setAllowUnLit(false);
080                } else {
081                    m.setAllowUnLit(true);
082                }
083            }
084        }
085        Element e = shared.getChild("disabledAspects");
086        if (e != null) {
087            List<Element> list = e.getChildren("disabledAspect");
088            for (Element aspect : list) {
089                ((SignalHeadSignalMast) m).setAspectDisabled(aspect.getText());
090            }
091        }
092        return true;
093    }
094
095    @Override
096    public void load(Element element, Object o) {
097        log.error("Invalid method called");
098    }
099
100    private final static Logger log = LoggerFactory.getLogger(SignalHeadSignalMastXml.class);
101}