001package jmri.implementation.configurexml;
002
003import java.util.List;
004import jmri.InstanceManager;
005import jmri.implementation.DccSignalHead;
006import org.jdom2.Element;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * Handle XML configuration for DccSignalHead objects.
012 *
013 * This file is part of JMRI.
014 *
015 * JMRI is free software; you can redistribute it and/or modify it under the
016 * terms of version 2 of the GNU General Public License as published by the Free
017 * Software Foundation. See the "COPYING" file for a copy of this license.
018 *
019 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
020 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
021 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
022 *
023 * @author Bob Jacobsen Copyright: Copyright (c) 2003, 2008, 2009
024 * @author Petr Koud'a Copyright: Copyright (c) 2007
025 */
026public class DccSignalHeadXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
027
028    public DccSignalHeadXml() {
029    }
030
031    /**
032     * Default implementation for storing the contents of a LsDecSignalHead
033     *
034     * @param o Object to store, of type LsDecSignalHead
035     * @return Element containing the complete info
036     */
037    @Override
038    public Element store(Object o) {
039        DccSignalHead p = (DccSignalHead) o;
040
041        Element element = new Element("signalhead");
042        element.setAttribute("class", this.getClass().getName());
043
044        element.addContent(new Element("systemName").addContent(p.getSystemName()));
045
046        storeCommon(p, element);
047
048        if (p.useAddressOffSet()) {
049            element.addContent(new Element("useAddressOffSet").addContent("yes"));
050        } else {
051            element.addContent(new Element("useAddressOffSet").addContent("no"));
052        }
053        element.addContent(new Element("packetsendcount").addContent(Integer.toString(p.getDccSignalHeadPacketSendCount())));
054
055        for (int i = 0; i < p.getValidStates().length; i++) {
056            String aspect = p.getValidStateKeys()[i];
057            //String address = p.getOutputForAppearance(i);
058            Element el = new Element("aspect");
059            el.setAttribute("defines", aspect); // non-localized appearance property key
060            el.addContent(new Element("number").addContent(Integer.toString(p.getOutputForAppearance(p.getValidStates()[i]))));
061            element.addContent(el);
062        }
063
064        return element;
065    }
066
067    @Override
068    public boolean load(Element shared, Element perNode) {
069        // put it together
070        String sys = getSystemName(shared);
071        String uname = getUserName(shared);
072        DccSignalHead h;
073        if (uname == null) {
074            h = new DccSignalHead(sys);
075        } else {
076            h = new DccSignalHead(sys, uname);
077        }
078
079        loadCommon(h, shared);
080
081        if (shared.getChild("useAddressOffSet") != null) {
082            if (shared.getChild("useAddressOffSet").getText().equals("yes")) {
083                h.useAddressOffSet(true);
084            }
085        }
086
087        if (shared.getChild("packetsendcount") != null) {
088            h.setDccSignalHeadPacketSendCount(Integer.parseInt(shared.getChild("packetsendcount").getValue()));
089        }
090
091        List<Element> list = shared.getChildren("aspect");
092        for (Element e : list) {
093            String aspect = e.getAttribute("defines").getValue(); // migrated to store non-localized key 4.15.6
094            // previous versions store localized appearance name as defines value
095            int number = -1;
096            try {
097                String value = e.getChild("number").getValue();
098                number = Integer.parseInt(value);
099
100            } catch (Exception ex) {
101                log.error("failed to convert DCC number");
102            }
103            int indexOfAspect = -1;
104
105            for (int i = 0; i < h.getValidStates().length; i++) {
106                if (h.getValidStateKeys()[i].equals(aspect) || h.getValidStateNames()[i].equals(aspect)) {
107                    // matching to stateKey is preferred to allow changing locale without errors
108                    indexOfAspect = i;
109                    break;
110                }
111            }
112            if (indexOfAspect != -1) {
113                h.setOutputForAppearance(h.getValidStates()[indexOfAspect], number);
114            }
115        }
116
117        InstanceManager.getDefault(jmri.SignalHeadManager.class).register(h);
118        return true;
119    }
120
121    @Override
122    public void load(Element element, Object o) {
123        log.error("Invalid method called");
124    }
125
126    private final static Logger log = LoggerFactory.getLogger(DccSignalHeadXml.class);
127
128}