001package jmri.jmrit.logixng.expressions.configurexml;
002
003import jmri.InstanceManager;
004import jmri.jmrit.logixng.DigitalExpressionManager;
005import jmri.jmrit.logixng.MaleSocket;
006import jmri.jmrit.logixng.expressions.TriggerOnce;
007
008import org.jdom2.Element;
009
010import jmri.jmrit.logixng.DigitalExpressionBean;
011
012/**
013 * Handle XML configuration for ActionLightXml objects.
014 *
015 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
016 * @author Daniel Bergqvist Copyright (C) 2019
017 */
018public class TriggerOnceXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
019
020    public TriggerOnceXml() {
021    }
022    
023    /**
024     * Default implementation for storing the contents of a ActionMany
025     *
026     * @param o Object to store, of type ActionMany
027     * @return Element containing the complete info
028     */
029    @Override
030    public Element store(Object o) {
031        TriggerOnce p = (TriggerOnce) o;
032
033        Element element = new Element("TriggerOnce");
034        element.setAttribute("class", this.getClass().getName());
035        element.addContent(new Element("systemName").addContent(p.getSystemName()));
036
037        storeCommon(p, element);
038
039        Element e2 = new Element("Socket");
040        e2.addContent(new Element("socketName").addContent(p.getChild(0).getName()));
041        MaleSocket socket = p.getChild(0).getConnectedSocket();
042        String socketSystemName;
043        if (socket != null) {
044            socketSystemName = socket.getSystemName();
045        } else {
046            socketSystemName = p.getChildSocketSystemName();
047        }
048        if (socketSystemName != null) {
049            e2.addContent(new Element("systemName").addContent(socketSystemName));
050        }
051        element.addContent(e2);
052        
053        return element;
054    }
055    
056    @Override
057    public boolean load(Element shared, Element perNode) {
058        String sys = getSystemName(shared);
059        String uname = getUserName(shared);
060        TriggerOnce h;
061        if (uname == null) {
062            h = new TriggerOnce(sys, null);
063        } else {
064            h = new TriggerOnce(sys, uname);
065        }
066
067        loadCommon(h, shared);
068
069        Element socketElement = shared.getChild("Socket");
070        String socketName = socketElement.getChild("socketName").getTextTrim();
071        Element systemNameElement = socketElement.getChild("systemName");
072        String systemName = null;
073        if (systemNameElement != null) {
074            systemName = systemNameElement.getTextTrim();
075        }
076        h.getChild(0).setName(socketName);
077        h.setChildSocketSystemName(systemName);
078        
079        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
080        return true;
081    }
082    
083//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TriggerOnceXml.class);
084}