001package jmri.jmrit.logixng.expressions.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.DigitalExpressionManager;
006import jmri.jmrit.logixng.expressions.ExpressionReporter;
007import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml;
008
009import org.jdom2.Element;
010
011/**
012 * Handle XML configuration for ActionReporterXml objects.
013 *
014 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
015 * @author Daniel Bergqvist Copyright (C) 2019
016 */
017public class ExpressionReporterXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
018
019    public ExpressionReporterXml() {
020    }
021
022    /**
023     * Default implementation for storing the contents of a SE8cSignalHead
024     *
025     * @param o Object to store, of type TripleTurnoutSignalHead
026     * @return Element containing the complete info
027     */
028    @Override
029    public Element store(Object o) {
030        ExpressionReporter p = (ExpressionReporter) o;
031
032        Element element = new Element("ExpressionReporter");
033        element.setAttribute("class", this.getClass().getName());
034        element.addContent(new Element("systemName").addContent(p.getSystemName()));
035
036        storeCommon(p, element);
037
038        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Reporter>();
039        element.addContent(selectNamedBeanXml.store(p.getSelectNamedBean(), "namedBean"));
040
041        var selectMemoryNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>();
042        element.addContent(selectMemoryNamedBeanXml.store(p.getSelectMemoryNamedBean(), "memoryNamedBean"));
043
044        element.addContent(new Element("reporterValue").addContent(p.getReporterValue().name()));
045        element.addContent(new Element("reporterOperation").addContent(p.getReporterOperation().name()));
046        element.addContent(new Element("compareTo").addContent(p.getCompareTo().name()));
047
048        element.addContent(new Element("caseInsensitive").addContent(p.getCaseInsensitive() ? "yes" : "no"));
049
050        element.addContent(new Element("constant").addContent(p.getConstantValue()));
051
052        String variableName = p.getLocalVariable();
053        if (variableName != null) {
054            element.addContent(new Element("variable").addContent(variableName));
055        }
056
057        element.addContent(new Element("regEx").addContent(p.getRegEx()));
058
059        return element;
060    }
061
062    @Override
063    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {     // Test class that inherits this class throws exception
064        String sys = getSystemName(shared);
065        String uname = getUserName(shared);
066        ExpressionReporter h = new ExpressionReporter(sys, uname);
067
068        loadCommon(h, shared);
069
070        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Reporter>();
071        selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean());
072        selectNamedBeanXml.loadLegacy(shared, h.getSelectNamedBean(), "reporter", null, null, null, null);
073
074        var selectMemoryNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>();
075        selectMemoryNamedBeanXml.load(shared.getChild("memoryNamedBean"), h.getSelectMemoryNamedBean());
076        selectMemoryNamedBeanXml.loadLegacy(shared, h.getSelectMemoryNamedBean(), "memory", null, null, null, null);
077
078        Element variableName = shared.getChild("variable");
079        if (variableName != null) {
080            h.setLocalVariable(variableName.getTextTrim());
081        }
082
083        Element constant = shared.getChild("constant");
084        if (constant != null) {
085            h.setConstantValue(constant.getText());
086        }
087
088        Element regEx = shared.getChild("regEx");
089        if (regEx != null) {
090            h.setRegEx(regEx.getText());
091        }
092
093        Element reporterValue = shared.getChild("reporterValue");
094        if (reporterValue != null) {
095            h.setReporterValue(ExpressionReporter.ReporterValue.valueOf(reporterValue.getTextTrim()));
096        }
097
098        Element reporterOperation = shared.getChild("reporterOperation");
099        if (reporterOperation != null) {
100            h.setReporterOperation(ExpressionReporter.ReporterOperation.valueOf(reporterOperation.getTextTrim()));
101        }
102
103        Element compareTo = shared.getChild("compareTo");
104        if (compareTo != null) {
105            h.setCompareTo(ExpressionReporter.CompareTo.valueOf(compareTo.getTextTrim()));
106        }
107
108        Element caseInsensitive = shared.getChild("caseInsensitive");
109        if (caseInsensitive != null) {
110            h.setCaseInsensitive("yes".equals(caseInsensitive.getTextTrim()));
111        } else {
112            h.setCaseInsensitive(false);
113        }
114
115        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
116        return true;
117    }
118
119//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionTurnoutXml.class);
120}