001package jmri.jmrit.logixng.implementation.configurexml;
002
003import java.util.List;
004
005import jmri.jmrit.logixng.*;
006import jmri.jmrit.logixng.SymbolTable.InitialValueType;
007import jmri.jmrit.logixng.configurexml.MaleSocketXml;
008
009import org.jdom2.Element;
010
011import jmri.jmrit.logixng.implementation.AbstractMaleSocket;
012
013/**
014 * Handle XML configuration for ActionLightXml objects.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
017 * @author Daniel Bergqvist Copyright (C) 2019
018 */
019public class AbstractMaleSocketXml
020        extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML
021        implements MaleSocketXml {
022
023    public AbstractMaleSocketXml() {
024    }
025
026    /**
027     * Default implementation for storing the contents of a ActionMany
028     *
029     * @param o Object to store, of type ActionMany
030     * @return Element containing the complete info
031     */
032    @Override
033    public Element store(Object o) {
034        AbstractMaleSocket maleSocket = (AbstractMaleSocket) o;
035
036        Element element = new Element("AbstractMaleSocket");                     // NOI18N
037        element.setAttribute("enabled", maleSocket.isEnabled() ? "yes" : "no");  // NOI18N
038        element.setAttribute("locked", maleSocket.isLocked() ? "yes" : "no");    // NOI18N
039        element.setAttribute("system", maleSocket.isSystem() ? "yes" : "no");    // NOI18N
040        element.setAttribute("catchAbortExecution", maleSocket.getCatchAbortExecution()? "yes" : "no");  // NOI18N
041        element.setAttribute("class", this.getClass().getName());
042
043        // Only store error handling type of the inner most socket
044        if (!(maleSocket.getObject() instanceof MaleSocket)) {
045            element.addContent(new Element("errorHandling").addContent(maleSocket.getErrorHandlingType().name()));  // NOI18N
046        }
047
048        for (SymbolTable.VariableData data : maleSocket.getLocalVariables()) {
049            Element elementVariable = new Element("LocalVariable");                                     // NOI18N
050            elementVariable.addContent(new Element("name").addContent(data._name));                     // NOI18N
051            elementVariable.addContent(new Element("type").addContent(data._initialValueType.name()));   // NOI18N
052            elementVariable.addContent(new Element("data").addContent(data._initialValueData));         // NOI18N
053            element.addContent(elementVariable);
054        }
055
056        return element;
057    }
058
059    @Override
060    public boolean load(Element shared, Element perNode) {
061        return false;
062    }
063
064    @Override
065    public boolean load(Element maleSocketElement, MaleSocket maleSocket) {
066        if (!(maleSocket instanceof AbstractMaleSocket)) {
067            throw new IllegalArgumentException("maleSocket is not an AbstractMaleSocket: "+maleSocket.getClass().getName());
068        }
069
070        String enabled = "yes";     // NOI18N
071        if (maleSocketElement.getAttribute("enabled") != null) {  // NOI18N
072            enabled = maleSocketElement.getAttribute("enabled").getValue();  // NOI18N
073        }
074        ((AbstractMaleSocket)maleSocket).setEnabledFlag("yes".equals(enabled)); // NOI18N
075
076        String locked = "no";       // NOI18N
077        if (maleSocketElement.getAttribute("locked") != null) {  // NOI18N
078            locked = maleSocketElement.getAttribute("locked").getValue();   // NOI18N
079        }
080        ((AbstractMaleSocket)maleSocket).setLocked("yes".equals(locked));   // NOI18N
081
082        String system = "no";       // NOI18N
083        if (maleSocketElement.getAttribute("system") != null) {  // NOI18N
084            system = maleSocketElement.getAttribute("system").getValue();   // NOI18N
085        }
086        ((AbstractMaleSocket)maleSocket).setSystem("yes".equals(system));   // NOI18N
087
088        String catchAbortExecution = "no";      // NOI18N
089        if (maleSocketElement.getAttribute("catchAbortExecution") != null) {  // NOI18N
090            catchAbortExecution = maleSocketElement.getAttribute("catchAbortExecution").getValue();  // NOI18N
091        }
092        ((AbstractMaleSocket)maleSocket).setCatchAbortExecution("yes".equals(catchAbortExecution));  // NOI18N
093
094        Element errorHandlingElement = maleSocketElement.getChild("errorHandling");     // NOI18N
095        if (errorHandlingElement != null) {
096            maleSocket.setErrorHandlingType(MaleSocket.ErrorHandlingType
097                    .valueOf(errorHandlingElement.getTextTrim()));
098        }
099
100        List<Element> localVariableList = maleSocketElement.getChildren("LocalVariable");  // NOI18N
101        log.debug("Found {} male sockets", localVariableList.size() );  // NOI18N
102
103        for (Element e : localVariableList) {
104            Element elementName = e.getChild("name");   // NOI18N
105
106            InitialValueType type = null;
107            Element elementType = e.getChild("type");   // NOI18N
108            if (elementType != null) {
109                type = InitialValueType.valueOf(elementType.getTextTrim());
110            }
111
112            Element elementData = e.getChild("data");   // NOI18N
113
114            if (elementName == null) throw new IllegalArgumentException("Element 'name' does not exists");  // NOI18N
115            if (type == null) throw new IllegalArgumentException("Element 'type' does not exists");         // NOI18N
116            if (elementData == null) throw new IllegalArgumentException("Element 'data' does not exists");  // NOI18N
117
118            maleSocket.addLocalVariable(elementName.getTextTrim(), type, elementData.getTextTrim());
119        }
120
121        return true;
122    }
123
124    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AbstractMaleSocketXml.class);
125}