001package jmri.managers.configurexml;
002
003import java.util.List;
004import jmri.InstanceManager;
005import jmri.configurexml.AbstractXmlAdapter;
006import jmri.managers.ManagerDefaultSelector;
007import jmri.profile.ProfileManager;
008import org.jdom2.Element;
009
010/**
011 * Handle XML persistence of ManagerDefaultSelector
012 * <p>
013 * This class is named as being the persistent form of the
014 * ManagerDefaultSelector class, but there's no object of that form created or
015 * used.
016 *
017 * @author Bob Jacobsen Copyright: Copyright (c) 2010
018 * @since 2.9.7
019 */
020public class ManagerDefaultSelectorXml extends AbstractXmlAdapter {
021
022    public ManagerDefaultSelectorXml() {
023    }
024
025    /**
026     * Default implementation for storing the static contents of a
027     * ManagerDefaultSelector
028     *
029     * @param o Object to store, of type ManagerDefaultSelector
030     * @return Element containing the complete info
031     */
032    @Override
033    public Element store(Object o) {
034        Element e = new Element("managerdefaults");
035        e.setAttribute("class", getClass().getName());
036        for (Class<?> c : InstanceManager.getDefault(ManagerDefaultSelector.class).defaults.keySet()) {
037            String n = InstanceManager.getDefault(ManagerDefaultSelector.class).defaults.get(c);
038            Element p = new Element("setting");
039            Element key = new Element("key");
040            key.addContent(c.getName());
041            Element value = new Element("value");
042            value.addContent(n);
043            p.addContent(key);
044            p.addContent(value);
045            e.addContent(p);
046        }
047        return e;
048    }
049
050    @Override
051    public boolean load(Element shared, Element perNode) {
052        List<Element> list = shared.getChildren("setting");
053
054        for (Element s : list) {
055            String name = s.getChild("value").getText();
056            String className = s.getChild("key").getText();
057            Class<?> c = null;
058            try {
059                c = Class.forName(className);
060            } catch (java.lang.ClassNotFoundException | java.lang.NoClassDefFoundError ex) {
061                continue;
062            }
063            InstanceManager.getDefault(ManagerDefaultSelector.class).setDefault(c, name);
064
065        }
066        // put into effect
067        InstanceManager.getDefault(ManagerDefaultSelector.class).configure(ProfileManager.getDefault().getActiveProfile());
068        InstanceManager.getOptionalDefault(jmri.ConfigureManager.class).ifPresent((cm) -> {
069            cm.registerPref(InstanceManager.getDefault(ManagerDefaultSelector.class));
070        });
071        return true;
072    }
073
074    //private final static Logger log = LoggerFactory.getLogger(ManagerDefaultSelectorXml.class);
075
076}