001package jmri.util.docbook.configurexml;
002
003import java.util.ArrayList;
004import jmri.util.docbook.RevHistory;
005import jmri.util.docbook.Revision;
006import org.jdom2.Element;
007import org.jdom2.Namespace;
008
009/**
010 * Load/Store RevHistory objects.
011 * <p>
012 * This interacts somewhat differently with the ConfigureXML system. RevHistory
013 * objects are _not_ registed with the manager, but rather handled explicitly by
014 * them. The "load()" method is therefore a null-op here.
015 *
016 * @author Bob Jacobsen Copyright (c) 2010
017 */
018public class RevHistoryXml extends jmri.configurexml.AbstractXmlAdapter {
019
020    private static final String NAMESPACE = "http://docbook.org/ns/docbook"; // NOI18N
021    
022    /**
023     * Usual configurexml method, this one doesn't do anything because the
024     * content is explicitly loaded from the file
025     */
026    @Override
027    public boolean load(Element shared, Element perNode) {
028        return true;
029    }
030
031    static public RevHistory loadRevHistory(Element e) {
032        RevHistory r = new RevHistory();
033
034        java.util.List<Element> list = e.getChildren("revision", Namespace.getNamespace(NAMESPACE));
035        for (int i = 0; i < list.size(); i++) {
036            loadRevision(r, list.get(i));
037        }
038        return r;
039    }
040
041    static void loadRevision(RevHistory r, Element e) {
042        Element s;
043        Namespace n = Namespace.getNamespace(NAMESPACE);
044        int revnumber = 0;
045        s = e.getChild("revnumber", n);
046        if (s != null) {
047            String c = s.getText();
048            revnumber = Integer.parseInt(c);
049        }
050
051        String date = null;
052        s = e.getChild("date", n);
053        if (s != null) {
054            date = s.getText();
055        }
056
057        String authorinitials = null;
058        s = e.getChild("authorinitials", n);
059        if (s != null) {
060            authorinitials = s.getText();
061        }
062
063        String revremark = null;
064        s = e.getChild("revremark", n);
065        if (s != null) {
066            revremark = s.getText();
067        }
068
069        r.addRevision(revnumber, date, authorinitials, revremark);
070    }
071
072    /**
073     * This method is not implemented: See class comment above for more discussion.
074     */
075    @Override
076    public void load(Element e, Object o) {
077        throw new UnsupportedOperationException("Method not coded");
078    }
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    public Element store(Object o) {
085        return storeDirectly(o);
086    }
087
088    static public Element storeDirectly(Object o) {
089        final RevHistory r = (RevHistory) o;
090        if (r == null) {
091            return null;
092        }
093
094        Element e = historyElement(r);
095
096        // and return
097        return e;
098    }
099
100    static Element historyElement(RevHistory r) {
101        ArrayList<Revision> list = r.getList();
102
103        Element e = new Element("revhistory", NAMESPACE);
104
105        for (int i = 0; i < list.size(); i++) {
106            Element revision = revisionElement(list.get(i));
107            e.addContent(revision);
108        }
109
110        return e;
111    }
112
113    static Element revisionElement(Revision r) {
114        Element rev = new Element("revision", NAMESPACE);
115
116        Element revnumber = new Element("revnumber", NAMESPACE);
117        revnumber.addContent("" + r.revnumber);
118        rev.addContent(revnumber);
119
120        Element date = new Element("date", NAMESPACE);
121        date.addContent(r.date);
122        rev.addContent(date);
123
124        Element authorinitials = new Element("authorinitials", NAMESPACE);
125        authorinitials.addContent(r.authorinitials);
126        rev.addContent(authorinitials);
127
128        Element revremark = new Element("revremark", NAMESPACE);
129        revremark.addContent(r.revremark);
130        rev.addContent(revremark);
131
132        return rev;
133    }
134}