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