001package jmri.jmrit.logixng.expressions.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.DigitalExpressionManager;
006import jmri.jmrit.logixng.NamedBeanAddressing;
007import jmri.jmrit.logixng.expressions.ExpressionScript;
008import jmri.jmrit.logixng.util.parser.ParserException;
009import jmri.script.configurexml.ScriptEngineSelectorXml;
010
011import org.jdom2.Element;
012
013/**
014 * Handle XML configuration for ExpressionSimpleScript objects.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
017 * @author Daniel Bergqvist Copyright (C) 2021
018 */
019public class ExpressionScriptXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
020
021    public ExpressionScriptXml() {
022    }
023
024    /**
025     * Default implementation for storing the contents of a SE8cSignalMast
026     *
027     * @param o Object to store, of type TripleLightSignalMast
028     * @return Element containing the complete info
029     */
030    @Override
031    public Element store(Object o) {
032        ExpressionScript p = (ExpressionScript) o;
033
034        Element element = new Element("ExpressionScript");
035        element.setAttribute("class", this.getClass().getName());
036        element.addContent(new Element("systemName").addContent(p.getSystemName()));
037
038        storeCommon(p, element);
039
040        var scriptEngineSelectorXml = new ScriptEngineSelectorXml();
041
042        element.addContent(new Element("operationAddressing").addContent(p.getOperationAddressing().name()));
043        element.addContent(new Element("operationType").addContent(p.getOperationType().name()));
044        element.addContent(new Element("operationReference").addContent(p.getOperationReference()));
045        element.addContent(new Element("operationLocalVariable").addContent(p.getOperationLocalVariable()));
046        element.addContent(new Element("operationFormula").addContent(p.getOperationFormula()));
047
048        element.addContent(new Element("scriptAddressing").addContent(p.getScriptAddressing().name()));
049        element.addContent(new Element("script").addContent(p.getScript()));
050        element.addContent(new Element("scriptReference").addContent(p.getScriptReference()));
051        element.addContent(new Element("scriptLocalVariable").addContent(p.getScriptLocalVariable()));
052        element.addContent(new Element("scriptFormula").addContent(p.getScriptFormula()));
053
054        element.addContent(new Element("registerListenerScript").addContent(p.getRegisterListenerScript()));
055        element.addContent(new Element("unregisterListenerScript").addContent(p.getUnregisterListenerScript()));
056
057        element.addContent(scriptEngineSelectorXml.store(p.getScriptEngineSelector(), "scriptLanguage"));
058
059        return element;
060    }
061
062    @Override
063    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
064        String sys = getSystemName(shared);
065        String uname = getUserName(shared);
066        ExpressionScript h = new ExpressionScript(sys, uname);
067
068        loadCommon(h, shared);
069
070        var scriptEngineSelectorXml = new ScriptEngineSelectorXml();
071
072        try {
073            Element elem = shared.getChild("operationAddressing");
074            if (elem != null) {
075                h.setOperationAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
076            }
077
078            Element queryType = shared.getChild("operationType");
079            if (queryType != null) {
080                if ("JythonCommand".equals(queryType.getTextTrim())) {
081                    h.setOperationType(ExpressionScript.OperationType.SingleLineCommand);
082                } else {
083                    h.setOperationType(ExpressionScript.OperationType.valueOf(queryType.getTextTrim()));
084                }
085            }
086
087            elem = shared.getChild("operationReference");
088            if (elem != null) h.setOperationReference(elem.getTextTrim());
089
090            elem = shared.getChild("operationLocalVariable");
091            if (elem != null) h.setOperationLocalVariable(elem.getTextTrim());
092
093            elem = shared.getChild("operationFormula");
094            if (elem != null) h.setOperationFormula(elem.getTextTrim());
095
096
097            elem = shared.getChild("scriptAddressing");
098            if (elem != null) {
099                h.setScriptAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
100            }
101
102            Element scriptElement = shared.getChild("script");
103            if (scriptElement != null) {
104                h.setScript(scriptElement.getText());
105            }
106
107            elem = shared.getChild("scriptReference");
108            if (elem != null) h.setScriptReference(elem.getTextTrim());
109
110            elem = shared.getChild("scriptLocalVariable");
111            if (elem != null) h.setScriptLocalVariable(elem.getTextTrim());
112
113            elem = shared.getChild("scriptFormula");
114            if (elem != null) h.setScriptFormula(elem.getTextTrim());
115
116            Element registerListenerScriptElement = shared.getChild("registerListenerScript");
117            if (registerListenerScriptElement != null) {
118                h.setRegisterListenerScript(registerListenerScriptElement.getText());
119            }
120
121            Element unregisterListenerScriptElement = shared.getChild("unregisterListenerScript");
122            if (unregisterListenerScriptElement != null) {
123                h.setUnregisterListenerScript(unregisterListenerScriptElement.getText());
124            }
125
126        } catch (ParserException e) {
127            throw new JmriConfigureXmlException(e);
128        }
129
130        Element elem = shared.getChild("scriptLanguage");
131        if (elem != null) {
132            scriptEngineSelectorXml.load(elem, h.getScriptEngineSelector());
133        }
134
135        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
136        return true;
137    }
138
139//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionScriptXml.class);
140}