001package jmri.jmrit.sample.configurexml;
002
003import java.text.SimpleDateFormat;
004import java.util.Locale;
005
006import jmri.jmrit.sample.SampleFunctionalClass;
007import org.jdom2.Element;
008
009/**
010 * Handle XML persistance of SampleFunctionalClass objects.
011 * <p>
012 * Note we have not (yet) updated xml/schema to provide an XML schema for this
013 * sample.  Configure PanelPro to first run the jython/TurnOffXmlValidation.py script to disable
014 * verification when loading files written by this.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2018
017 */
018public class SampleFunctionalClassXml extends jmri.configurexml.AbstractXmlAdapter {
019
020    public SampleFunctionalClassXml() {
021    }
022
023    /**
024     * Default implementation for storing the contents of a SampleFunctionalClassXml.
025     *
026     * @param o Object to store
027     * @return Element containing the complete info
028     */
029    @Override
030    public Element store(Object o) {
031        
032        SampleFunctionalClass s = (SampleFunctionalClass) o;
033        
034        Element elem = new Element("samplefunctionalclass");
035        elem.setAttribute("class", this.getClass().getName());
036
037        elem.addContent(
038            new Element("remember")
039                .addContent(s.toString())
040        );
041        return elem;
042    }
043
044    @Override
045    public boolean load(Element shared, Element perNode) {
046
047        boolean result = true;
048        
049        String content = shared.getChild("remember").getValue();
050        
051        // the following creates and registers
052        
053        new SampleFunctionalClass(content);
054        
055        return result;
056    }
057    
058    // Conversion format for dates created by Java Date.toString().
059    // The Locale needs to be always US, irrelevant from computer's and program's settings!
060    final SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
061
062    /**
063     * Update static data from XML file
064     *
065     * @param element Top level Element to unpack.
066     * @param o       ignored
067     */
068    @Override
069    public void load(Element element, Object o) {
070        log.error("load(Element, Object) called unexpectedly");
071    }
072
073    @Override
074    public int loadOrder() {
075        return jmri.Manager.TIMEBASE;
076    }
077
078    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SampleFunctionalClassXml.class);
079
080}