001package jmri.jmrit.logixng.actions.configurexml;
002
003import java.util.List;
004
005import jmri.InstanceManager;
006import jmri.jmrit.logixng.DigitalActionManager;
007import jmri.jmrit.logixng.actions.LogData;
008import jmri.jmrit.logixng.util.parser.ParserException;
009
010import org.jdom2.Element;
011
012/**
013 * Handle XML configuration for ActionLightXml objects.
014 *
015 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
016 * @author Daniel Bergqvist Copyright (C) 2019
017 */
018public class LogDataXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
019
020    public LogDataXml() {
021    }
022
023    /**
024     * Default implementation for storing the contents of a SE8cSignalHead
025     *
026     * @param o Object to store, of type TripleLightSignalHead
027     * @return Element containing the complete info
028     */
029    @Override
030    public Element store(Object o) {
031        LogData p = (LogData) o;
032
033        Element element = new Element("LogData");
034        element.setAttribute("class", this.getClass().getName());
035        element.addContent(new Element("systemName").addContent(p.getSystemName()));
036
037        storeCommon(p, element);
038
039        element.addContent(new Element("logToLog").addContent(p.getLogToLog() ? "yes" : "no"));
040        element.addContent(new Element("logToScriptOutput").addContent(p.getLogToScriptOutput() ? "yes" : "no"));
041        element.addContent(new Element("formatType").addContent(p.getFormatType().name()));
042        element.addContent(new Element("format").addContent(p.getFormat()));
043
044        Element parameters = new Element("DataList");
045        for (LogData.Data data : p.getDataList()) {
046            Element elementParameter = new Element("Data");
047            elementParameter.addContent(new Element("type").addContent(data.getDataType().name()));
048            elementParameter.addContent(new Element("data").addContent(data.getData()));
049            parameters.addContent(elementParameter);
050        }
051        element.addContent(parameters);
052
053        return element;
054    }
055
056    @Override
057    public boolean load(Element shared, Element perNode) {
058        String sys = getSystemName(shared);
059        String uname = getUserName(shared);
060        LogData h = new LogData(sys, uname);
061
062        loadCommon(h, shared);
063
064        Element elem = shared.getChild("logToLog");  // NOI18N
065        h.setLogToLog((elem != null) ? elem.getTextTrim().equals("yes") : false);  // NOI18N
066
067        elem = shared.getChild("logToScriptOutput");  // NOI18N
068        h.setLogToScriptOutput((elem != null) ? elem.getTextTrim().equals("yes") : false);  // NOI18N
069
070        elem = shared.getChild("formatType");  // NOI18N
071        h.setFormatType((elem != null) ? LogData.FormatType.valueOf(elem.getTextTrim()) : LogData.FormatType.OnlyText);
072
073        elem = shared.getChild("format");  // NOI18N
074        h.setFormat((elem != null) ? elem.getValue() : "");
075
076        List<Element> dataList = shared.getChild("DataList").getChildren();  // NOI18N
077        log.debug("Found {} dataList", dataList.size() );  // NOI18N
078
079        for (Element e : dataList) {
080            LogData.DataType type = LogData.DataType.LocalVariable;
081            Element elementType = e.getChild("type");
082            if (elementType != null) {
083                type = LogData.DataType.valueOf(elementType.getTextTrim());
084            }
085
086            Element elementData = e.getChild("data");
087            if (elementData == null) throw new IllegalArgumentException("Element 'data' does not exists");
088
089            try {
090                h.getDataList().add(new LogData.Data(type, elementData.getTextTrim()));
091            } catch (ParserException ex) {
092                log.warn("Could not load {} to data list : {}", elementData.getTextTrim(), ex.getMessage());
093            }
094        }
095
096        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
097        return true;
098    }
099
100    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogDataXml.class);
101}