001package jmri.jmrix.configurexml;
002
003import java.util.List;
004import jmri.configurexml.AbstractXmlAdapter;
005import jmri.jmrix.ConnectionConfig;
006import jmri.jmrix.PortAdapter;
007import org.jdom2.Element;
008
009/**
010 * Abstract base (and partial implementation) for classes persisting the status
011 * of connections.
012 *
013 * @author Bob Jacobsen Copyright: Copyright (c) 2003
014 */
015abstract public class AbstractConnectionConfigXml extends AbstractXmlAdapter {
016
017    public AbstractConnectionConfigXml() {
018    }
019
020    abstract protected void getInstance();
021
022    abstract protected void register();
023
024    protected void register(ConnectionConfig c) {
025        c.register();
026    }
027
028    @Override
029    public Element store(Object o, boolean shared) {
030        return this.store(o);
031    }
032
033    protected void storeCommon(Element e, PortAdapter adapter) {
034        if (adapter.getSystemConnectionMemo() != null) {
035            e.setAttribute("userName", adapter.getSystemConnectionMemo().getUserName());
036            e.setAttribute("systemPrefix", adapter.getSystemConnectionMemo().getSystemPrefix());
037        }
038        if (adapter.getManufacturer() != null) {
039            e.setAttribute("manufacturer", adapter.getManufacturer());
040        }
041
042        if (adapter.getDisabled()) {
043            e.setAttribute("disabled", "yes");
044        } else {
045            e.setAttribute("disabled", "no");
046        }
047
048        e.setAttribute("reconnectMaxInterval",String.valueOf(adapter.getReconnectMaxInterval()));
049        e.setAttribute("reconnectMaxAttempts",String.valueOf(adapter.getReconnectMaxAttempts()));
050
051        saveOptions(e, adapter);
052    }
053
054    /**
055     * Customizable method if you need to add anything more
056     *
057     * @param e Element being created, update as needed
058     */
059    protected void extendElement(Element e) {
060    }
061
062    /**
063     * Load common attributes and elements.
064     *
065     * @param shared  the shared element
066     * @param perNode the per node element
067     * @param adapter the port adapter
068     */
069    protected void loadCommon(Element shared, Element perNode, PortAdapter adapter) {
070        if (perNode.getAttribute("option1") != null) {
071            String option1Setting = perNode.getAttribute("option1").getValue();
072            adapter.configureOption1(option1Setting);
073        }
074        if (perNode.getAttribute("option2") != null) {
075            String option2Setting = perNode.getAttribute("option2").getValue();
076            adapter.configureOption2(option2Setting);
077        }
078        if (perNode.getAttribute("option3") != null) {
079            String option3Setting = perNode.getAttribute("option3").getValue();
080            adapter.configureOption3(option3Setting);
081        }
082        if (perNode.getAttribute("option4") != null) {
083            String option4Setting = perNode.getAttribute("option4").getValue();
084            adapter.configureOption4(option4Setting);
085        }
086
087        loadOptions(perNode.getChild("options"), perNode.getChild("options"), adapter);
088
089        try {
090            adapter.setManufacturer(perNode.getAttribute("manufacturer").getValue());
091        } catch (NullPointerException ex) { //Considered normal if not present
092
093        }
094
095        if (adapter.getSystemConnectionMemo() != null) {
096            if (shared.getAttribute("userName") != null) {
097                adapter.getSystemConnectionMemo().setUserName(shared.getAttribute("userName").getValue());
098            }
099
100            if (shared.getAttribute("systemPrefix") != null) {
101                adapter.getSystemConnectionMemo().setSystemPrefix(shared.getAttribute("systemPrefix").getValue());
102            }
103        }
104
105        if (shared.getAttribute("disabled") != null) {
106            String yesno = shared.getAttribute("disabled").getValue();
107            if ((yesno != null) && (!yesno.isEmpty())) {
108                if (yesno.equals("no")) {
109                    adapter.setDisabled(false);
110                } else if (yesno.equals("yes")) {
111                    adapter.setDisabled(true);
112                }
113            }
114        }
115
116        if (shared.getAttribute("reconnectMaxInterval") != null) {
117            String reconnectI = shared.getAttribute("reconnectMaxInterval").getValue();
118            if ((reconnectI!=null) && (!reconnectI.isEmpty() )) {
119                adapter.setReconnectMaxInterval(Integer.parseInt(reconnectI));
120            }
121        }
122
123        if (shared.getAttribute("reconnectMaxAttempts") != null) {
124            String reconnectA = shared.getAttribute("reconnectMaxAttempts").getValue();
125            if ((reconnectA!=null) && (!reconnectA.isEmpty() )) {
126                adapter.setReconnectMaxAttempts(Integer.parseInt(reconnectA));
127            }
128        }
129
130    }
131
132
133    /**
134     * save options
135     *
136     * @param e       the element
137     * @param adapter the port adapter
138     */
139    protected void saveOptions(Element e, PortAdapter adapter) {
140        Element element = new Element("options");
141        String[] options = adapter.getOptions();
142
143        for (String i : options) {
144            Element elem = new Element("option");
145            elem.addContent(new Element("name").addContent(i));
146            elem.addContent(new Element("value").addContent(adapter.getOptionState(i)));
147            element.addContent(elem);
148        }
149        e.addContent(element);
150    }
151
152    /**
153     * load options
154     *
155     * @param shared  the shared element
156     * @param perNode the per node element
157     * @param adapter the port adapter
158     */
159    protected void loadOptions(Element shared, Element perNode, PortAdapter adapter) {
160        if (perNode == null) {
161            return;
162        }
163        List<Element> optionList = perNode.getChildren("option");
164        for (Element so : optionList) {
165            adapter.setOptionState(so.getChild("name").getText(), so.getChild("value").getText());
166        }
167    }
168
169    /**
170     * Method to unpack additional XML structures after connection creation, but
171     * before connection is usable.
172     *
173     * @param shared  connection information common to all nodes
174     * @param perNode connection information unique to this node
175     */
176    protected void unpackElement(Element shared, Element perNode) {
177    }
178
179    /**
180     * Service routine to look through "parameter" child elements to find a
181     * particular parameter value
182     *
183     * @param e    Element containing parameters
184     * @param name name of desired parameter
185     * @return String value
186     */
187    protected String findParmValue(Element e, String name) {
188        List<Element> l = e.getChildren("parameter");
189        for (Element n : l) {
190            if (n.getAttributeValue("name").equals(name)) {
191                return n.getTextTrim();
192            }
193        }
194        return null;
195    }
196
197    /**
198     * Store the outputInterval in a connection element for persistence.
199     *
200     * @param adapter the adapter for which properties are stored
201     * @param e the "connection" element being filled
202     */
203    protected void setOutputInterval(PortAdapter adapter, Element e) {
204        if (adapter.getSystemConnectionMemo().getOutputInterval() > 0) {
205            e.setAttribute("turnoutInterval", String.valueOf(adapter.getSystemConnectionMemo().getOutputInterval()));
206        } else {
207            e.setAttribute("turnoutInterval", "0");
208        }
209    }
210
211    // initialize logging
212    // private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AbstractConnectionConfigXml.class);
213
214}