001package jmri.jmrit.logixng.expressions.configurexml;
002
003import jmri.*;
004import jmri.jmrit.logixng.DigitalExpressionManager;
005import jmri.jmrit.logixng.Is_IsNot_Enum;
006import jmri.jmrit.logixng.expressions.ExpressionClock;
007
008import org.jdom2.Element;
009
010/**
011 * Handle XML configuration for ActionLightXml objects.
012 *
013 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
014 * @author Daniel Bergqvist Copyright (C) 2019
015 */
016public class ExpressionClockXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
017
018    public ExpressionClockXml() {
019    }
020    
021    /**
022     * Default implementation for storing the contents of a SE8cSignalHead
023     *
024     * @param o Object to store, of type TripleLightSignalHead
025     * @return Element containing the complete info
026     */
027    @Override
028    public Element store(Object o) {
029        ExpressionClock p = (ExpressionClock) o;
030
031        Element element = new Element("ExpressionClock");
032        element.setAttribute("class", this.getClass().getName());
033        element.addContent(new Element("systemName").addContent(p.getSystemName()));
034
035        storeCommon(p, element);
036
037        element.addContent(new Element("is_isNot").addContent(p.get_Is_IsNot().name()));
038        element.addContent(new Element("type").addContent(p.getType().name()));
039        element.addContent(new Element("beginTime").addContent(Integer.toString(p.getBeginTime())));
040        element.addContent(new Element("endTime").addContent(Integer.toString(p.getEndTime())));
041
042        return element;
043    }
044    
045    @Override
046    public boolean load(Element shared, Element perNode) {
047        String sys = getSystemName(shared);
048        String uname = getUserName(shared);
049        ExpressionClock h = new ExpressionClock(sys, uname);
050
051        loadCommon(h, shared);
052
053        Element is_IsNot = shared.getChild("is_isNot");
054        if (is_IsNot != null) {
055            h.set_Is_IsNot(Is_IsNot_Enum.valueOf(is_IsNot.getTextTrim()));
056        }
057
058        Element type = shared.getChild("type");
059        if (type != null) {
060            h.setType(ExpressionClock.Type.valueOf(type.getTextTrim()));
061        }
062        
063        int beginTime = 0;
064        int endTime = 0;
065        Element beginTimeElement = shared.getChild("beginTime");
066        Element endTimeElement = shared.getChild("endTime");
067        
068        if (beginTimeElement != null) {
069            try {
070                beginTime = Integer.parseInt(beginTimeElement.getTextTrim());
071            } catch (NumberFormatException e) {
072                log.error("cannot parse beginTime: {}", beginTimeElement.getTextTrim(), e);
073            }
074        }
075        if (endTimeElement != null) {
076            try {
077                endTime =  Integer.parseInt(endTimeElement.getTextTrim());
078                h.setRange(beginTime, endTime);
079            } catch (NumberFormatException e) {
080                log.error("cannot parse endTime: {}", endTimeElement.getTextTrim(), e);
081            }
082        }
083        h.setRange(beginTime, endTime);
084
085        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
086        return true;
087    }
088    
089    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionClockXml.class);
090}