001package jmri.jmrix.configurexml;
002
003import jmri.jmrix.AbstractStreamPortController;
004import org.jdom2.Element;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * Abstract base (and partial implementation) for classes persisting the status
010 * of (non-serial) Stream Port adapters.
011 *
012 * @author Bob Jacobsen Copyright: Copyright (c) 2003
013 * @author George Warner Copyright: Copyright (c) 2017
014 */
015abstract public class AbstractStreamConnectionConfigXml extends AbstractConnectionConfigXml {
016
017    /**
018     * constructor
019     */
020    public AbstractStreamConnectionConfigXml() {
021    }
022
023    protected AbstractStreamPortController adapter;
024
025    /**
026     * set the stream port adapter
027     *
028     * @param streamPortAdapter the stream port adapter to set
029     */
030    protected void setAdapter(AbstractStreamPortController streamPortAdapter) {
031        log.debug("setAdapter({})", streamPortAdapter);
032        adapter = streamPortAdapter;
033    }
034
035    /**
036     * get the stream port adapter
037     *
038     * @return the stream port adapter
039     */
040    protected AbstractStreamPortController getAdapter() {
041        log.debug("getAdapter()");
042        return adapter;
043    }
044
045    /**
046     * get the stream adapter
047     *
048     * @return the stream port adapter
049     */
050    public AbstractStreamPortController getStreamAdapter() {
051        log.debug("getStreamAdapter()");
052        return adapter;
053    }
054
055    /**
056     * get instance
057     *
058     * @param object to get the instance of
059     */
060    protected abstract void getInstance(Object object);
061
062    /**
063     * {@inheritDoc}
064     */
065    @Override
066    public Element store(Object object) {
067        log.debug("store({})", object);
068        getInstance(object);
069        Element e = new Element("connection");
070
071        if (adapter == null) {
072            log.warn("No adapter found while saving stream port configuration {}", object.toString());
073            return null;
074        }
075
076        // many of the following are required by the DTD; failing to include
077        // them makes the XML file unreadable, but at least the next
078        // invocation of the program can then continue.
079        storeCommon(e, adapter);
080
081        e.setAttribute("class", this.getClass().getName());
082
083        extendElement(e);
084
085        return e;
086    }
087
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    protected void extendElement(Element e) {
093        log.debug("extendElement({})", e);
094    }
095
096    /**
097     * load from xml elements
098     *
099     * @param shared  element
100     * @param perNode element
101     * @return boolean true if successful
102     */
103    @Override
104    public boolean load(Element shared, Element perNode) {
105        log.debug("load({}, {})", shared, perNode);
106        boolean result = true;  // assume success (optimist!)
107
108        getInstance();
109
110        loadCommon(shared, perNode, adapter);
111
112        // register, so can be picked up next time
113        register();
114
115        // once all the configure processing has happened, do any
116        // extra config
117        unpackElement(shared, perNode);
118        return result;
119    }
120
121    /**
122     * Update static data from XML file
123     *
124     * @param element Top level Element to unpack.
125     */
126    @Override
127    public void load(Element element, Object o) {
128        log.debug("load({}, {})", element, o);
129
130        getInstance(o);
131
132        loadCommon(element, element, adapter);
133
134        // register, so can be picked up next time
135        register();
136
137        // once all the configure processing has happened, do any
138        // extra config
139        unpackElement(element, element);
140    }
141
142    @Override
143    protected void dispose() {
144        adapter.dispose();
145    }
146
147    // initialize logging
148    private static final Logger log
149            = LoggerFactory.getLogger(AbstractStreamConnectionConfigXml.class);
150}