001package jmri.jmrix.internal.configurexml;
002
003import jmri.jmrix.internal.InternalSensorManager;
004import org.jdom2.Element;
005
006/**
007 * Provides load and store functionality for configuring InternalSensorManagers.
008 * <p>
009 * Uses the store method from the abstract base class, but provides a load
010 * method here.
011 *
012 * @author Bob Jacobsen Copyright: Copyright (c) 2006
013 */
014public class InternalSensorManagerXml extends jmri.managers.configurexml.AbstractSensorManagerConfigXML {
015
016    public InternalSensorManagerXml() {
017        super();
018    }
019
020    @Override
021    public void setStoreElementClass(Element sensors) {
022        sensors.setAttribute("class", this.getClass().getName());
023    }
024
025    @Override
026    public Element store(Object o) {
027        Element sensors = new Element("sensors");
028
029        String defaultState;
030        switch (InternalSensorManager.getDefaultStateForNewSensors()) {
031            case jmri.Sensor.ACTIVE:
032                defaultState = "active";
033                break;
034            case jmri.Sensor.INACTIVE:
035                defaultState = "inactive";
036                break;
037            case jmri.Sensor.INCONSISTENT:
038                defaultState = "inconsistent";
039                break;
040            default:
041                defaultState = "unknown";
042        }
043
044        sensors.addContent(new Element("defaultInitialState").addContent(defaultState));
045
046        return store(o, sensors);
047
048    }
049
050    @Override
051    public boolean load(Element sharedSensors, Element perNodeSensors) throws jmri.configurexml.JmriConfigureXmlException {
052        // load individual sharedSensors
053        if (sharedSensors.getChild("defaultInitialState") != null) {
054            int defaultState = jmri.Sensor.UNKNOWN;
055            String state = sharedSensors.getChild("defaultInitialState").getText();
056            if (state.equals("active")) {
057                defaultState = jmri.Sensor.ACTIVE;
058            } else if (state.equals("inactive")) {
059                defaultState = jmri.Sensor.INACTIVE;
060            } else if (state.equals("inconsistent")) {
061                defaultState = jmri.Sensor.INCONSISTENT;
062            }
063            InternalSensorManager.setDefaultStateForNewSensors(defaultState);
064        }
065        boolean load = loadSensors(sharedSensors);
066
067        return load;
068    }
069
070//    private final static Logger log = LoggerFactory.getLogger(InternalSensorManagerXml.class);
071}