001package jmri.jmrit.logixng.expressions.configurexml;
002
003import java.util.List;
004
005import jmri.InstanceManager;
006import jmri.jmrit.logixng.DigitalExpressionManager;
007import jmri.jmrit.logixng.expressions.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("result").addContent(p.getResult() ? "yes" : "no"));
040        element.addContent(new Element("logToLog").addContent(p.getLogToLog() ? "yes" : "no"));
041        element.addContent(new Element("logToScriptOutput").addContent(p.getLogToScriptOutput() ? "yes" : "no"));
042        element.addContent(new Element("formatType").addContent(p.getFormatType().name()));
043        element.addContent(new Element("format").addContent(p.getFormat()));
044        
045        Element parameters = new Element("DataList");
046        for (LogData.Data data : p.getDataList()) {
047            Element elementParameter = new Element("Data");
048            elementParameter.addContent(new Element("type").addContent(data.getDataType().name()));
049            elementParameter.addContent(new Element("data").addContent(data.getData()));
050            parameters.addContent(elementParameter);
051        }
052        element.addContent(parameters);
053        
054        return element;
055    }
056    
057    @Override
058    public boolean load(Element shared, Element perNode) {
059        String sys = getSystemName(shared);
060        String uname = getUserName(shared);
061        LogData h = new LogData(sys, uname);
062
063        loadCommon(h, shared);
064        
065        Element elem = shared.getChild("result");  // NOI18N
066        h.setResult((elem != null) ? elem.getTextTrim().equals("yes") : false);  // NOI18N
067        
068        elem = shared.getChild("logToLog");  // NOI18N
069        h.setLogToLog((elem != null) ? elem.getTextTrim().equals("yes") : false);  // NOI18N
070        
071        elem = shared.getChild("logToScriptOutput");  // NOI18N
072        h.setLogToScriptOutput((elem != null) ? elem.getTextTrim().equals("yes") : false);  // NOI18N
073        
074        elem = shared.getChild("formatType");  // NOI18N
075        h.setFormatType((elem != null) ? LogData.FormatType.valueOf(elem.getTextTrim()) : LogData.FormatType.OnlyText);
076        
077        elem = shared.getChild("format");  // NOI18N
078        h.setFormat((elem != null) ? elem.getValue() : "");
079        
080        List<Element> dataList = shared.getChild("DataList").getChildren();  // NOI18N
081        log.debug("Found {} dataList", dataList.size());  // NOI18N
082        
083        for (Element e : dataList) {
084            LogData.DataType type = LogData.DataType.LocalVariable;
085            Element elementType = e.getChild("type");
086            if (elementType != null) {
087                type = LogData.DataType.valueOf(elementType.getTextTrim());
088            }
089            
090            Element elementName = e.getChild("data");
091            
092            if (elementName == null) throw new IllegalArgumentException("Element 'name' does not exists");
093            
094            try {
095                h.getDataList().add(new LogData.Data(type, elementName.getTextTrim()));
096            } catch (ParserException ex) {
097                log.warn("Could not add {} to data list : {}", elementName.getTextTrim(), ex.getMessage());
098            }
099        }
100        
101        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
102        return true;
103    }
104    
105    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogDataXml.class);
106}