001package jmri.configurexml;
002
003import jmri.DccLocoAddress;
004import org.jdom2.Attribute;
005import org.jdom2.Element;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009/**
010 * Handle XML configuration for DccLocoAddress objects.
011 *
012 * @author Bob Jacobsen Copyright: Copyright (c) 2005
013 */
014public class DccLocoAddressXml extends jmri.configurexml.AbstractXmlAdapter {
015
016    public DccLocoAddressXml() {
017    }
018
019    /**
020     * Default implementation for storing the contents of a DccLocoAddress
021     *
022     * @param o Object to store, of type DccLocoAddress
023     * @return Element containing the complete info
024     */
025    @Override
026    public Element store(Object o) {
027        DccLocoAddress p = (DccLocoAddress) o;
028
029        Element element = new Element("dcclocoaddress");
030
031        // include contents
032        if (p != null) {
033            element.setAttribute("number", "" + p.getNumber());
034            if (p.isLongAddress()) {
035                element.setAttribute("longaddress", "yes");
036            } else {
037                element.setAttribute("longaddress", "no");
038            }
039        } else {
040            element.setAttribute("number", "");
041            element.setAttribute("longaddress", "no");
042        }
043        return element;
044    }
045
046    @Override
047    public boolean load(Element shared, Element perNode) {
048        log.error("Invalid method called");
049        return false;
050    }
051
052    public DccLocoAddress getAddress(Element element) {
053        if (element.getAttribute("number").getValue().equals("")) {
054            return null;
055        }
056        int number = Integer.parseInt(element.getAttribute("number").getValue());
057        boolean longaddress = false;
058        Attribute a = element.getAttribute("longaddress");
059        if (a != null && a.getValue().equals("yes")) {
060            longaddress = true;
061        }
062        return new DccLocoAddress(number, longaddress);
063    }
064
065    @Override
066    public void load(Element element, Object o) {
067        log.error("Invalid method called");
068    }
069
070    private final static Logger log = LoggerFactory.getLogger(DccLocoAddressXml.class);
071}