001package jmri.jmrix.loconet.logixng.configurexml;
002
003import java.util.*;
004
005import jmri.*;
006import jmri.configurexml.JmriConfigureXmlException;
007import jmri.jmrit.logixng.DigitalExpressionManager;
008import jmri.jmrix.loconet.LocoNetSystemConnectionMemo;
009import jmri.jmrix.loconet.logixng.ExpressionSlotUsage;
010
011import org.jdom2.Element;
012
013/**
014 * Handle XML configuration for ExpressionSlotUsage objects.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
017 * @author Daniel Bergqvist Copyright (C) 2020
018 */
019public class ExpressionSlotUsageXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
020
021    public ExpressionSlotUsageXml() {
022    }
023    
024    /**
025     * Default implementation for storing the contents of a ExpressionSlotUsage
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        ExpressionSlotUsage p = (ExpressionSlotUsage) o;
033
034        Element element = new Element("ExpressionLoconetSlotUsage");
035        element.setAttribute("class", this.getClass().getName());
036        element.addContent(new Element("systemName").addContent(p.getSystemName()));
037        
038        storeCommon(p, element);
039
040        if (p.getMemo() != null) {
041            element.addContent(new Element("systemConnection")
042                    .addContent(p.getMemo().getSystemPrefix()));
043        }
044        
045        element.addContent(new Element("advanced").addContent(p.getAdvanced() ? "yes" : "no"));
046        element.addContent(new Element("has_hasNot").addContent(p.get_Has_HasNot().name()));
047        element.addContent(new Element("simpleState").addContent(p.getSimpleState().name()));
048        
049        Element advancedStatesElement = new Element("advancedStates");
050        for (ExpressionSlotUsage.AdvancedState state : p.getAdvancedStates()) {
051            advancedStatesElement.addContent(new Element("state").addContent(state.name()));
052        }
053        element.addContent(advancedStatesElement);
054        element.addContent(new Element("compare").addContent(p.getCompare().name()));
055        
056        element.addContent(new Element("number").addContent(Integer.toString(p.getNumber())));
057        
058        element.addContent(new Element("percentPieces").addContent(p.getPercentPieces().name()));
059        
060        element.addContent(new Element("totalSlots").addContent(Integer.toString(p.getTotalSlots())));
061
062        return element;
063    }
064    
065    @Override
066    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {     // Test class that inherits this class throws exception
067        String sys = getSystemName(shared);
068        String uname = getUserName(shared);
069        ExpressionSlotUsage h = new ExpressionSlotUsage(sys, uname, null);
070
071        loadCommon(h, shared);
072
073        Element systemConnection = shared.getChild("systemConnection");
074        if (systemConnection != null) {
075            String systemConnectionName = systemConnection.getTextTrim();
076            List<LocoNetSystemConnectionMemo> systemConnections =
077                    jmri.InstanceManager.getList(LocoNetSystemConnectionMemo.class);
078            
079            for (LocoNetSystemConnectionMemo memo : systemConnections) {
080                if (memo.getSystemPrefix().equals(systemConnectionName)) {
081                    h.setMemo(memo);
082                    break;
083                }
084            }
085        }
086
087        Element advanced = shared.getChild("advanced");
088        if (advanced != null) {
089            h.setAdvanced("yes".equals(advanced.getTextTrim()));
090        } else {
091            h.setAdvanced(false);
092        }
093
094        Element has_hasNot = shared.getChild("has_hasNot");
095        if (has_hasNot != null) {
096            h.set_Has_HasNot(ExpressionSlotUsage.Has_HasNot.valueOf(has_hasNot.getTextTrim()));
097        }
098
099        Element simpleState = shared.getChild("simpleState");
100        if (simpleState != null) {
101            h.setSimpleState(ExpressionSlotUsage.SimpleState.valueOf(simpleState.getTextTrim()));
102        }
103
104        Set<ExpressionSlotUsage.AdvancedState> stateSet = new HashSet<>();
105        Element advancedStates = shared.getChild("advancedStates");
106        for (Element state : advancedStates.getChildren()) {
107            stateSet.add(ExpressionSlotUsage.AdvancedState.valueOf(state.getTextTrim()));
108        }
109        h.setAdvancedStates(stateSet);
110
111        Element compare = shared.getChild("compare");
112        if (compare != null) {
113            h.setCompare(ExpressionSlotUsage.Compare.valueOf(compare.getTextTrim()));
114        }
115
116        Element number = shared.getChild("number");
117        if (number != null) {
118            h.setNumber(Integer.parseInt(number.getText()));
119        }
120
121        Element percentPieces = shared.getChild("percentPieces");
122        if (percentPieces != null) {
123            h.setPercentPieces(ExpressionSlotUsage.PercentPieces.valueOf(percentPieces.getTextTrim()));
124        }
125
126        Element totalNumber = shared.getChild("totalSlots");
127        if (totalNumber != null) {
128            h.setTotalSlots(Integer.parseInt(totalNumber.getText()));
129        }
130
131        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
132        return true;
133    }
134    
135//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionSlotUsageXml.class);
136}