001package jmri.jmrit.logixng.implementation.configurexml;
002
003import java.util.List;
004
005import jmri.InstanceManager;
006import jmri.jmrit.logixng.*;
007import jmri.jmrit.logixng.Module.Parameter;
008import jmri.jmrit.logixng.ModuleManager;
009import jmri.jmrit.logixng.implementation.DefaultModule;
010
011import org.jdom2.Element;
012
013/**
014 * Handle XML configuration for DefaultModule objects.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
017 * @author Daniel Bergqvist Copyright (C) 2020
018 */
019public class DefaultModuleXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
020
021    public DefaultModuleXml() {
022    }
023    
024    /**
025     * Default implementation for storing the contents of a DefaultModule
026     *
027     * @param o Object to store, of type DefaultModule
028     * @return Element containing the complete info
029     */
030    @Override
031    public Element store(Object o) {
032        DefaultModule p = (DefaultModule) o;
033
034        Element element = new Element("Module");
035        element.setAttribute("class", this.getClass().getName());
036        element.addContent(new Element("systemName").addContent(p.getSystemName()));
037        
038        storeCommon(p, element);
039        
040        element.addContent(new Element("rootSocketType").addContent(p.getRootSocketType().getName()));
041        
042        Element elementParameters = new Element("Parameters");
043        for (Parameter data : p.getParameters()) {
044            Element elementParameter = new Element("Parameter");
045            elementParameter.addContent(new Element("name").addContent(data.getName()));
046            elementParameter.addContent(new Element("isInput").addContent(data.isInput() ? "yes" : "no"));
047            elementParameter.addContent(new Element("isOutput").addContent(data.isOutput() ? "yes" : "no"));
048            elementParameters.addContent(elementParameter);
049        }
050        element.addContent(elementParameters);
051        
052        Element e2 = new Element("RootSocket");
053        e2.addContent(new Element("socketName").addContent(p.getChild(0).getName()));
054        MaleSocket socket = p.getRootSocket().getConnectedSocket();
055        String socketSystemName;
056        if (socket != null) {
057            socketSystemName = socket.getSystemName();
058        } else {
059            socketSystemName = p.getSocketSystemName();
060        }
061        if (socketSystemName != null) {
062            e2.addContent(new Element("systemName").addContent(socketSystemName));
063        }
064        element.addContent(e2);
065        
066        return element;
067    }
068    
069    @Override
070    public boolean load(Element shared, Element perNode) {
071        String sys = getSystemName(shared);
072        String uname = getUserName(shared);
073        
074        String rootSocketTypeName = shared.getChild("rootSocketType").getTextTrim();
075        
076        FemaleSocketManager.SocketType socketType =
077                InstanceManager.getDefault(FemaleSocketManager.class)
078                        .getSocketTypeByType(rootSocketTypeName);
079        
080        DefaultModule h = (DefaultModule) InstanceManager.getDefault(ModuleManager.class)
081                .createModule(sys, uname, socketType);
082        
083        loadCommon(h, shared);
084        
085        List<Element> parameterList = shared.getChild("Parameters").getChildren();  // NOI18N
086        log.debug("Found {} parameters", parameterList.size() );  // NOI18N
087
088        for (Element e : parameterList) {
089            Element elementName = e.getChild("name");
090            
091            boolean isInput = "yes".equals(e.getChild("isInput").getTextTrim());
092            boolean isOutput = "yes".equals(e.getChild("isOutput").getTextTrim());
093            
094            h.addParameter(elementName.getTextTrim(), isInput, isOutput);
095        }
096        
097        Element socketName = shared.getChild("RootSocket").getChild("socketName");
098        h.getChild(0).setName(socketName.getTextTrim());
099        Element socketSystemName = shared.getChild("RootSocket").getChild("systemName");
100        if (socketSystemName != null) {
101            h.setSocketSystemName(socketSystemName.getTextTrim());
102        }
103        
104        return true;
105    }
106    
107    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DefaultModuleXml.class);
108}