001package jmri.jmrit.logixng.expressions.configurexml;
002
003import java.util.AbstractMap;
004import java.util.ArrayList;
005import java.util.List;
006import java.util.Map;
007
008import jmri.InstanceManager;
009import jmri.configurexml.JmriConfigureXmlException;
010import jmri.jmrit.logixng.DigitalExpressionManager;
011import jmri.jmrit.logixng.MaleSocket;
012import jmri.jmrit.logixng.expressions.Antecedent;
013
014import org.jdom2.Element;
015
016/**
017 * Handle XML configuration for ActionLightXml objects.
018 *
019 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
020 * @author Daniel Bergqvist Copyright (C) 2019
021 */
022public class AntecedentXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
023
024    public AntecedentXml() {
025    }
026    
027    /**
028     * Default implementation for storing the contents of a ActionMany
029     *
030     * @param o Object to store, of type ActionMany
031     * @return Element containing the complete info
032     */
033    @Override
034    public Element store(Object o) {
035        Antecedent p = (Antecedent) o;
036
037        Element element = new Element("Antecedent");
038        element.setAttribute("class", this.getClass().getName());
039        element.addContent(new Element("systemName").addContent(p.getSystemName()));
040        
041        storeCommon(p, element);
042        
043        element.addContent(new Element("antecedent").addContent(p.getAntecedent()));
044        
045        Element e = new Element("Expressions");
046        for (int i=0; i < p.getChildCount(); i++) {
047            Element e2 = new Element("Socket");
048            e2.addContent(new Element("socketName").addContent(p.getChild(i).getName()));
049            MaleSocket socket = p.getChild(i).getConnectedSocket();
050            String socketSystemName;
051            if (socket != null) {
052                socketSystemName = socket.getSystemName();
053            } else {
054                socketSystemName = p.getExpressionSystemName(i);
055            }
056            if (socketSystemName != null) {
057                e2.addContent(new Element("systemName").addContent(socketSystemName));
058            }
059            e.addContent(e2);
060        }
061        element.addContent(e);
062
063        return element;
064    }
065    
066    @Override
067    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
068        List<Map.Entry<String, String>> expressionSystemNames = new ArrayList<>();
069        
070        Element actionElement = shared.getChild("Expressions");
071        for (Element socketElement : actionElement.getChildren()) {
072            String socketName = socketElement.getChild("socketName").getTextTrim();
073            Element systemNameElement = socketElement.getChild("systemName");
074            String systemName = null;
075            if (systemNameElement != null) {
076                systemName = systemNameElement.getTextTrim();
077            }
078            expressionSystemNames.add(new AbstractMap.SimpleEntry<>(socketName, systemName));
079        }
080        
081        // put it together
082        String sys = getSystemName(shared);
083        String uname = getUserName(shared);
084        
085        String antecedent = shared.getChild("antecedent").getText();
086        
087        Antecedent h;
088        if (uname == null) {
089            h = new Antecedent(sys, null, expressionSystemNames);
090        } else {
091            h = new Antecedent(sys, uname, expressionSystemNames);
092        }
093        try {
094            h.setAntecedent(antecedent);
095        } catch (Exception e) {
096            throw new JmriConfigureXmlException("Antecedent.setAntecedent() has thrown exeception", e);
097        }
098
099        loadCommon(h, shared);
100
101        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
102        return true;
103    }
104    
105//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AntecedentXml.class);
106}