001package jmri.jmrit.logixng.actions.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.DigitalActionManager;
006import jmri.jmrit.logixng.NamedBeanAddressing;
007import jmri.jmrit.logixng.actions.ActionScript;
008import jmri.jmrit.logixng.util.parser.ParserException;
009import jmri.script.configurexml.ScriptEngineSelectorXml;
010
011import org.jdom2.Element;
012
013/**
014 * Handle XML configuration for ActionScript objects.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
017 * @author Daniel Bergqvist Copyright (C) 2021
018 */
019public class ActionScriptXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
020
021    public ActionScriptXml() {
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        ActionScript p = (ActionScript) o;
033
034        var scriptEngineSelectorXml = new ScriptEngineSelectorXml();
035
036        Element element = new Element("ActionScript");
037        element.setAttribute("class", this.getClass().getName());
038        element.addContent(new Element("systemName").addContent(p.getSystemName()));
039
040        storeCommon(p, element);
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(scriptEngineSelectorXml.store(p.getScriptEngineSelector(), "scriptLanguage"));
055
056        return element;
057    }
058
059    @Override
060    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
061        String sys = getSystemName(shared);
062        String uname = getUserName(shared);
063        ActionScript h = new ActionScript(sys, uname);
064
065        loadCommon(h, shared);
066
067        var scriptEngineSelectorXml = new ScriptEngineSelectorXml();
068
069        try {
070            Element elem = shared.getChild("operationAddressing");
071            if (elem != null) {
072                h.setOperationAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
073            }
074
075            Element queryType = shared.getChild("operationType");
076            if (queryType != null) {
077                if ("JythonCommand".equals(queryType.getTextTrim())) {
078                    h.setOperationType(ActionScript.OperationType.SingleLineCommand);
079                } else {
080                    h.setOperationType(ActionScript.OperationType.valueOf(queryType.getTextTrim()));
081                }
082            }
083
084            elem = shared.getChild("operationReference");
085            if (elem != null) h.setOperationReference(elem.getTextTrim());
086
087            elem = shared.getChild("operationLocalVariable");
088            if (elem != null) h.setOperationLocalVariable(elem.getTextTrim());
089
090            elem = shared.getChild("operationFormula");
091            if (elem != null) h.setOperationFormula(elem.getTextTrim());
092
093
094            elem = shared.getChild("scriptAddressing");
095            if (elem != null) {
096                h.setScriptAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
097            }
098
099            Element scriptElement = shared.getChild("script");
100            if (scriptElement != null) {
101                try {
102                    h.setScript(scriptElement.getText());
103                } catch (NumberFormatException e) {
104                    log.error("cannot parse script: {}", scriptElement.getTextTrim(), e);
105                }
106            }
107
108            elem = shared.getChild("scriptReference");
109            if (elem != null) h.setScriptReference(elem.getTextTrim());
110
111            elem = shared.getChild("scriptLocalVariable");
112            if (elem != null) h.setScriptLocalVariable(elem.getTextTrim());
113
114            elem = shared.getChild("scriptFormula");
115            if (elem != null) h.setScriptFormula(elem.getTextTrim());
116
117        } catch (ParserException e) {
118            throw new JmriConfigureXmlException(e);
119        }
120
121        Element elem = shared.getChild("scriptLanguage");
122        if (elem != null) {
123            scriptEngineSelectorXml.load(elem, h.getScriptEngineSelector());
124        }
125
126        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
127        return true;
128    }
129
130    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionScriptXml.class);
131}