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.ExpressionSignalHead;
008import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml;
009import jmri.jmrit.logixng.util.parser.ParserException;
010
011import org.jdom2.Element;
012
013/**
014 * Handle XML configuration for ExpressionSignalHeadXml objects.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
017 * @author Daniel Bergqvist Copyright (C) 2019
018 */
019public class ExpressionSignalHeadXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
020
021    public ExpressionSignalHeadXml() {
022    }
023
024    /**
025     * Default implementation for storing the contents of a SE8cSignalHead
026     *
027     * @param o Object to store, of type TripleLightSignalHead
028     * @return Element containing the complete info
029     */
030    @Override
031    public Element store(Object o) {
032        ExpressionSignalHead p = (ExpressionSignalHead) o;
033
034        Element element = new Element("ExpressionSignalHead");
035        element.setAttribute("class", this.getClass().getName());
036        element.addContent(new Element("systemName").addContent(p.getSystemName()));
037
038        storeCommon(p, element);
039
040        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<SignalHead>();
041        element.addContent(selectNamedBeanXml.store(p.getSelectNamedBean(), "namedBean"));
042
043        element.addContent(new Element("queryAddressing").addContent(p.getQueryAddressing().name()));
044        element.addContent(new Element("queryType").addContent(p.getQueryType().name()));
045        element.addContent(new Element("queryReference").addContent(p.getQueryReference()));
046        element.addContent(new Element("queryLocalVariable").addContent(p.getQueryLocalVariable()));
047        element.addContent(new Element("queryFormula").addContent(p.getQueryFormula()));
048
049        element.addContent(new Element("appearanceAddressing").addContent(p.getAppearanceAddressing().name()));
050        element.addContent(new Element("appearance").addContent(Integer.toString(p.getAppearance())));
051        element.addContent(new Element("appearanceReference").addContent(p.getAppearanceReference()));
052        element.addContent(new Element("appearanceLocalVariable").addContent(p.getAppearanceLocalVariable()));
053        element.addContent(new Element("appearanceFormula").addContent(p.getAppearanceFormula()));
054
055        var selectExampleNamedBeanXml = new LogixNG_SelectNamedBeanXml<SignalHead>();
056        element.addContent(selectExampleNamedBeanXml.store(p.getSelectExampleNamedBean(), "exampleNamedBean"));
057
058        return element;
059    }
060
061    @Override
062    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
063        String sys = getSystemName(shared);
064        String uname = getUserName(shared);
065        ExpressionSignalHead h = new ExpressionSignalHead(sys, uname);
066
067        loadCommon(h, shared);
068
069        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<SignalHead>();
070        selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean());
071        selectNamedBeanXml.loadLegacy(shared, h.getSelectNamedBean(), "signalHead");
072
073        try {
074            Element elem = shared.getChild("queryAddressing");
075            if (elem != null) {
076                h.setQueryAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
077            }
078
079            Element queryType = shared.getChild("queryType");
080            if (queryType != null) {
081                h.setQueryType(ExpressionSignalHead.QueryType.valueOf(queryType.getTextTrim()));
082            }
083
084            elem = shared.getChild("queryReference");
085            if (elem != null) h.setQueryReference(elem.getTextTrim());
086
087            elem = shared.getChild("queryLocalVariable");
088            if (elem != null) h.setQueryLocalVariable(elem.getTextTrim());
089
090            elem = shared.getChild("queryFormula");
091            if (elem != null) h.setQueryFormula(elem.getTextTrim());
092
093
094            elem = shared.getChild("appearanceAddressing");
095            if (elem != null) {
096                h.setAppearanceAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
097            }
098
099            Element appearanceElement = shared.getChild("appearance");
100            if (appearanceElement != null) {
101                try {
102                    int appearance = Integer.parseInt(appearanceElement.getTextTrim());
103                    h.setAppearance(appearance);
104                } catch (NumberFormatException e) {
105                    log.error("cannot parse apperance: {}", appearanceElement.getTextTrim(), e);
106                }
107            }
108
109            elem = shared.getChild("appearanceReference");
110            if (elem != null) h.setAppearanceReference(elem.getTextTrim());
111
112            elem = shared.getChild("appearanceLocalVariable");
113            if (elem != null) h.setAppearanceLocalVariable(elem.getTextTrim());
114
115            elem = shared.getChild("appearanceFormula");
116            if (elem != null) h.setAppearanceFormula(elem.getTextTrim());
117
118        } catch (ParserException e) {
119            throw new JmriConfigureXmlException(e);
120        }
121
122        var selectExampleNamedBeanXml = new LogixNG_SelectNamedBeanXml<SignalHead>();
123        selectExampleNamedBeanXml.load(shared.getChild("exampleNamedBean"), h.getSelectExampleNamedBean());
124        selectExampleNamedBeanXml.loadLegacy(shared, h.getSelectExampleNamedBean(), "exampleSignalHead", null, null, null, null);
125
126        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
127        return true;
128    }
129
130    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionSignalHeadXml.class);
131}