Search in sources :

Example 41 with Element

use of org.jdom2.Element in project JMRI by JMRI.

the class MergSD2SignalHeadXml method load.

@Override
public boolean load(Element shared, Element perNode) {
    int aspects = 2;
    List<Element> l = shared.getChildren("turnoutname");
    if (l.size() == 0) {
        l = shared.getChildren("turnout");
        aspects = l.size() + 1;
    }
    NamedBeanHandle<Turnout> input1 = null;
    NamedBeanHandle<Turnout> input2 = null;
    NamedBeanHandle<Turnout> input3 = null;
    String yesno = "";
    boolean feather = false;
    boolean home = true;
    // put it together
    String sys = getSystemName(shared);
    String uname = getUserName(shared);
    if (shared.getAttribute("feather") != null) {
        yesno = shared.getAttribute("feather").getValue();
    }
    if ((yesno != null) && (!yesno.equals(""))) {
        if (yesno.equals("yes")) {
            feather = true;
        } else if (yesno.equals("no")) {
            feather = false;
        }
    }
    if (shared.getAttribute("home") != null) {
        yesno = shared.getAttribute("home").getValue();
    }
    if ((yesno != null) && (!yesno.equals(""))) {
        if (yesno.equals("yes")) {
            home = true;
        } else if (yesno.equals("no")) {
            home = false;
        }
    }
    try {
        aspects = shared.getAttribute("aspects").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.warn("Could not parse level attribute!");
    } catch (NullPointerException e) {
    // considered normal if the attribute not present
    }
    SignalHead h;
    //@TODO could re-arange this so that it falls through
    switch(aspects) {
        case 2:
            input1 = loadTurnout(l.get(0));
            break;
        case 3:
            input1 = loadTurnout(l.get(0));
            input2 = loadTurnout(l.get(1));
            break;
        case 4:
            input1 = loadTurnout(l.get(0));
            input2 = loadTurnout(l.get(1));
            input3 = loadTurnout(l.get(2));
            break;
        default:
            log.error("incorrect number of aspects " + aspects + " when loading Signal " + sys);
    }
    if (uname == null) {
        h = new MergSD2SignalHead(sys, aspects, input1, input2, input3, feather, home);
    } else {
        h = new MergSD2SignalHead(sys, uname, aspects, input1, input2, input3, feather, home);
    }
    loadCommon(h, shared);
    InstanceManager.getDefault(jmri.SignalHeadManager.class).register(h);
    return true;
}
Also used : Element(org.jdom2.Element) MergSD2SignalHead(jmri.implementation.MergSD2SignalHead) SignalHead(jmri.SignalHead) MergSD2SignalHead(jmri.implementation.MergSD2SignalHead) Turnout(jmri.Turnout)

Example 42 with Element

use of org.jdom2.Element in project JMRI by JMRI.

the class LsDecSignalHeadXml method loadTurnout.

NamedBeanHandle<Turnout> loadTurnout(Object o) {
    Element e = (Element) o;
    String name = e.getAttribute("systemName").getValue();
    Turnout t = InstanceManager.turnoutManagerInstance().getTurnout(name);
    return jmri.InstanceManager.getDefault(jmri.NamedBeanHandleManager.class).getNamedBeanHandle(name, t);
}
Also used : Element(org.jdom2.Element) Turnout(jmri.Turnout)

Example 43 with Element

use of org.jdom2.Element in project JMRI by JMRI.

the class MatrixSignalMastXml method store.

/**
     * Default implementation for storing the contents of a
     * MatrixSignalMastManager
     *
     * @param o Object to store, of type MatrixSignalMast
     * @return e Element containing the complete info
     */
@Override
public Element store(Object o) {
    // from mast p to XML
    MatrixSignalMast p = (MatrixSignalMast) o;
    Element e = new Element("matrixsignalmast");
    e.setAttribute("class", this.getClass().getName());
    // include content
    e.addContent(new Element("systemName").addContent(p.getSystemName()));
    // username, comment & properties
    storeCommon(p, e);
    // mast properties:
    Element unlit = new Element("unlit");
    if (p.allowUnLit()) {
        unlit.setAttribute("allowed", "yes");
        unlit.addContent(new Element("bitString").addContent(p.getUnLitChars()));
    } else {
        unlit.setAttribute("allowed", "no");
    }
    e.addContent(unlit);
    List<String> outputs = p.getOutputs();
    // convert char[] to xml-storable simple String
    // max. 5 outputs (either: turnouts (bean names) [or ToDo: DCC addresses (numbers)]
    // spotted by FindBugs as to never be null (check on creation of MatrixMast)
    Element outps = new Element("outputs");
    int i = 1;
    for (String _output : outputs) {
        String key = ("output" + i);
        Element outp = new Element("output");
        outp.setAttribute("matrixCol", key);
        // get name (Turnout)
        outp.addContent(p.getOutputName(i));
        outps.addContent(outp);
        i++;
    }
    if (outputs.size() != 0) {
        e.addContent(outps);
    }
    // string of max. 6 chars "001010" describing matrix row per aspect
    SignalAppearanceMap appMap = p.getAppearanceMap();
    if (appMap != null) {
        Element bss = new Element("bitStrings");
        java.util.Enumeration<String> aspects = appMap.getAspects();
        while (aspects.hasMoreElements()) {
            String key = aspects.nextElement();
            Element bs = new Element("bitString");
            bs.setAttribute("aspect", key);
            bs.addContent(p.getBitstring(key));
            bss.addContent(bs);
        }
        e.addContent(bss);
    }
    List<String> disabledAspects = p.getDisabledAspects();
    if (disabledAspects != null) {
        Element el = new Element("disabledAspects");
        for (String aspect : disabledAspects) {
            Element ele = new Element("disabledAspect");
            ele.addContent(aspect);
            el.addContent(ele);
        }
        if (disabledAspects.size() != 0) {
            e.addContent(el);
        }
    }
    return e;
}
Also used : Element(org.jdom2.Element) SignalAppearanceMap(jmri.SignalAppearanceMap) MatrixSignalMast(jmri.implementation.MatrixSignalMast)

Example 44 with Element

use of org.jdom2.Element in project JMRI by JMRI.

the class DccSignalHeadXml method store.

/**
     * Default implementation for storing the contents of a LsDecSignalHead
     *
     * @param o Object to store, of type LsDecSignalHead
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    DccSignalHead p = (DccSignalHead) o;
    Element element = new Element("signalhead");
    element.setAttribute("class", this.getClass().getName());
    element.addContent(new Element("systemName").addContent(p.getSystemName()));
    storeCommon(p, element);
    if (p.useAddressOffSet()) {
        element.addContent(new Element("useAddressOffSet").addContent("yes"));
    } else {
        element.addContent(new Element("useAddressOffSet").addContent("no"));
    }
    for (int i = 0; i < p.getValidStates().length; i++) {
        String aspect = p.getValidStateNames()[i];
        //String address = p.getOutputForAppearance(i);
        Element el = new Element("aspect");
        el.setAttribute("defines", aspect);
        el.addContent(new Element("number").addContent(Integer.toString(p.getOutputForAppearance(p.getValidStates()[i]))));
        element.addContent(el);
    }
    return element;
}
Also used : Element(org.jdom2.Element) DccSignalHead(jmri.implementation.DccSignalHead)

Example 45 with Element

use of org.jdom2.Element in project JMRI by JMRI.

the class MergSD2SignalHeadXml method store.

/**
     * Default implementation for storing the contents of a MergSD2SignalHead
     *
     * @param o Object to store, of type MergSD2SignalHead
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    MergSD2SignalHead p = (MergSD2SignalHead) o;
    Element element = new Element("signalhead");
    element.setAttribute("class", this.getClass().getName());
    element.addContent(new Element("systemName").addContent(p.getSystemName()));
    element.setAttribute("aspects", p.getAspects() + "");
    if (p.getFeather()) {
        element.setAttribute("feather", "yes");
    }
    storeCommon(p, element);
    int aspects = p.getAspects();
    //@TODO could re-arange this so that it falls through
    switch(aspects) {
        case 2:
            element.addContent(addTurnoutElement(p.getInput1(), "input1"));
            if (!p.getHome()) {
                element.setAttribute("home", "no");
            }
            break;
        case 3:
            element.addContent(addTurnoutElement(p.getInput1(), "input1"));
            element.addContent(addTurnoutElement(p.getInput2(), "input2"));
            break;
        case 4:
            element.addContent(addTurnoutElement(p.getInput1(), "input1"));
            element.addContent(addTurnoutElement(p.getInput2(), "input2"));
            element.addContent(addTurnoutElement(p.getInput3(), "input3"));
            break;
        default:
            log.error("incorrect number of aspects " + aspects + " for Signal " + p.getDisplayName());
    }
    return element;
}
Also used : MergSD2SignalHead(jmri.implementation.MergSD2SignalHead) Element(org.jdom2.Element)

Aggregations

Element (org.jdom2.Element)673 Attribute (org.jdom2.Attribute)103 Document (org.jdom2.Document)64 File (java.io.File)49 ArrayList (java.util.ArrayList)36 NamedIcon (jmri.jmrit.catalog.NamedIcon)28 IOException (java.io.IOException)27 DataConversionException (org.jdom2.DataConversionException)26 JDOMException (org.jdom2.JDOMException)26 XmlFile (jmri.jmrit.XmlFile)24 Test (org.junit.Test)24 Editor (jmri.jmrit.display.Editor)22 DocType (org.jdom2.DocType)21 Turnout (jmri.Turnout)20 ProcessingInstruction (org.jdom2.ProcessingInstruction)16 Point (java.awt.Point)15 HashMap (java.util.HashMap)15 SignalHead (jmri.SignalHead)15 LayoutEditor (jmri.jmrit.display.layoutEditor.LayoutEditor)15 Dimension (java.awt.Dimension)14