Search in sources :

Example 41 with Attribute

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

the class PositionFile method getReceiverPosition.

/**
     * Get the nth receiver position in the file.
     *
     * @return null if not present
     */
public Point3d getReceiverPosition(int n) {
    List<Element> kids = root.getChildren("receiver");
    for (int i = 0; i < kids.size(); i++) {
        Element e = kids.get(i);
        Attribute a = e.getAttribute("number");
        if (a == null) {
            continue;
        }
        int num = -1;
        try {
            num = a.getIntValue();
        } catch (org.jdom2.DataConversionException ex) {
            log.error("in getReceiverPosition", ex);
        }
        if (num == n) {
            return positionFromElement(e.getChild("position"));
        }
    }
    return null;
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element)

Example 42 with Attribute

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

the class PositionFile method getReceiverActive.

/**
     * Get the nth receiver active state in the file.
     *
     * @return true if not present
     */
public boolean getReceiverActive(int n) {
    List<Element> kids = root.getChildren("receiver");
    for (int i = 0; i < kids.size(); i++) {
        Element e = kids.get(i);
        Attribute a = e.getAttribute("number");
        if (a == null) {
            continue;
        }
        int num = -1;
        try {
            num = a.getIntValue();
        } catch (org.jdom2.DataConversionException ex) {
            log.error("in getReceiverActive", ex);
        }
        if (num != n) {
            continue;
        }
        a = e.getAttribute("active");
        if (a == null) {
            // default value
            return true;
        }
        if (a.getValue().equals("false")) {
            return false;
        }
        return true;
    }
    return true;
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element)

Example 43 with Attribute

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

the class NameCheckAction method actionPerformed.

@SuppressWarnings("unchecked")
@Override
public void actionPerformed(ActionEvent e) {
    if (fci == null) {
        fci = jmri.jmrit.XmlFile.userFileChooser("XML files", "xml");
    }
    // request the filename from an open dialog
    fci.rescanCurrentDirectory();
    int retVal = fci.showOpenDialog(_who);
    // handle selection or cancel
    if (retVal == JFileChooser.APPROVE_OPTION) {
        File file = fci.getSelectedFile();
        if (log.isDebugEnabled()) {
            log.debug("located file " + file + " for XML processing");
        }
        // handle the file (later should be outside this thread?)
        try {
            Element root = readFile(file);
            if (log.isDebugEnabled()) {
                log.debug("parsing complete");
            }
            // check to see if there's a decoder element
            if (root.getChild("decoder") == null) {
                log.warn("Does not appear to be a decoder file");
                return;
            }
            Iterator<Element> iter = root.getChild("decoder").getChild("variables").getDescendants(new ElementFilter("variable"));
            jmri.jmrit.symbolicprog.NameFile nfile = jmri.jmrit.symbolicprog.NameFile.instance();
            String warnings = "";
            while (iter.hasNext()) {
                Element varElement = iter.next();
                // for each variable, see if can find in names file
                Attribute labelAttr = varElement.getAttribute("label");
                String label = null;
                if (labelAttr != null) {
                    label = labelAttr.getValue();
                }
                Attribute itemAttr = varElement.getAttribute("item");
                String item = null;
                if (itemAttr != null) {
                    item = itemAttr.getValue();
                }
                if (log.isDebugEnabled()) {
                    log.debug("Variable called \"" + ((label != null) ? label : "<none>") + "\" \"" + ((item != null) ? item : "<none>"));
                }
                if (!(label == null ? false : nfile.checkName(label)) && !(item == null ? false : nfile.checkName(item))) {
                    log.warn("Variable not found: label=\"" + ((label != null) ? label : "<none>") + "\" item=\"" + ((item != null) ? label : "<none>") + "\"");
                    warnings += "Variable not found: label=\"" + ((label != null) ? label : "<none>") + "\" item=\"" + ((item != null) ? item : "<none>") + "\"\n";
                }
            }
            if (!warnings.equals("")) {
                JOptionPane.showMessageDialog(_who, warnings);
            } else {
                JOptionPane.showMessageDialog(_who, "No mismatched items found");
            }
        } catch (HeadlessException | IOException | JDOMException ex) {
            JOptionPane.showMessageDialog(_who, "Error parsing decoder file: " + ex);
        }
    } else {
        log.debug("XmlFileCheckAction cancelled in open dialog");
    }
}
Also used : HeadlessException(java.awt.HeadlessException) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) ElementFilter(org.jdom2.filter.ElementFilter) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 44 with Attribute

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

the class BlockContentsIconXml method load.

/**
     * Load, starting with the BlockContentsIcon element, then all the
     * value-icon pairs
     *
     * @param element Top level Element to unpack.
     * @param o       an Editor as an Object
     */
@Override
public void load(Element element, Object o) {
    Editor ed = null;
    BlockContentsIcon l;
    if (o instanceof LayoutEditor) {
        ed = (LayoutEditor) o;
        l = new jmri.jmrit.display.layoutEditor.BlockContentsIcon("   ", (LayoutEditor) ed);
    } else if (o instanceof jmri.jmrit.display.Editor) {
        ed = (Editor) o;
        l = new BlockContentsIcon("", ed);
    } else {
        log.error("Unrecognizable class - " + o.getClass().getName());
        return;
    }
    String name;
    Attribute attr = element.getAttribute("blockcontents");
    if (attr == null) {
        log.error("incorrect information for a block contents; must use block name");
        ed.loadFailed();
        return;
    } else {
        name = attr.getValue();
    }
    loadTextInfo(l, element);
    Block m = jmri.InstanceManager.getDefault(jmri.BlockManager.class).getBlock(name);
    if (m != null) {
        l.setBlock(name);
    } else {
        log.error("Block named '" + attr.getValue() + "' not found.");
        ed.loadFailed();
    }
    Attribute a = element.getAttribute("selectable");
    if (a != null && a.getValue().equals("yes")) {
        l.setSelectable(true);
    } else {
        l.setSelectable(false);
    }
    // get the icon pairs
    List<Element> items = element.getChildren("blockstate");
    for (int i = 0; i < items.size(); i++) {
        // get the class, hence the adapter object to do loading
        Element item = items.get(i);
        String iconName = item.getAttribute("icon").getValue();
        NamedIcon icon = NamedIcon.getIconByName(iconName);
        if (icon == null) {
            icon = ed.loadFailed("Memory " + name, iconName);
            if (icon == null) {
                log.info("Memory \"" + name + "\" icon removed for url= " + iconName);
            }
        }
        if (icon != null) {
            String keyValue = item.getAttribute("value").getValue();
            l.addKeyAndIcon(icon, keyValue);
        }
    }
    ed.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.MEMORIES, element);
    int x = 0;
    int y = 0;
    try {
        x = element.getAttribute("x").getIntValue();
        y = element.getAttribute("y").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert positional attribute");
    }
    l.setOriginalLocation(x, y);
    l.displayState();
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) LayoutEditor(jmri.jmrit.display.layoutEditor.LayoutEditor) Attribute(org.jdom2.Attribute) BlockContentsIcon(jmri.jmrit.display.BlockContentsIcon) Element(org.jdom2.Element) Editor(jmri.jmrit.display.Editor) Block(jmri.Block) LayoutEditor(jmri.jmrit.display.layoutEditor.LayoutEditor) Editor(jmri.jmrit.display.Editor)

Example 45 with Attribute

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

the class IndicatorTrackIconXml method load.

/**
     * Create a IndicatorTrackIcon, then add to a target JLayeredPane
     *
     * @param element Top level Element to unpack.
     * @param o       Editor as an Object
     */
@Override
public void load(Element element, Object o) {
    // create the objects
    Editor p = (Editor) o;
    IndicatorTrackIcon l = new IndicatorTrackIcon(p);
    Element elem = element.getChild("iconmap");
    if (elem != null) {
        List<Element> status = elem.getChildren();
        if (status.size() > 0) {
            for (int i = 0; i < status.size(); i++) {
                String msg = "IndicatorTrack \"" + l.getNameString() + "\" icon \"" + status.get(i).getName() + "\" ";
                NamedIcon icon = loadIcon(l, status.get(i).getName(), elem, msg, p);
                if (icon != null) {
                    l.setIcon(status.get(i).getName(), icon);
                } else {
                    log.info(msg + " removed for url= " + status.get(i).getName());
                    return;
                }
            }
        }
        Attribute attr = elem.getAttribute("family");
        if (attr != null) {
            l.setFamily(attr.getValue());
        }
    }
    Element name = element.getChild("occupancyblock");
    if (name != null) {
        l.setOccBlock(name.getText());
    } else {
        // only write sensor if no OBlock, don't write double sensing
        name = element.getChild("occupancysensor");
        if (name != null) {
            l.setOccSensor(name.getText());
        }
    }
    l.setShowTrain(false);
    name = element.getChild("showTrainName");
    if (name != null) {
        if ("yes".equals(name.getText())) {
            l.setShowTrain(true);
        }
    }
    elem = element.getChild("paths");
    if (elem != null) {
        ArrayList<String> paths = new ArrayList<String>();
        List<Element> pth = elem.getChildren();
        for (int i = 0; i < pth.size(); i++) {
            paths.add(pth.get(i).getText());
        }
        l.setPaths(paths);
    }
    l.displayState(l.getStatus());
    l.updateSize();
    p.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.TURNOUTS, element);
}
Also used : IndicatorTrackIcon(jmri.jmrit.display.IndicatorTrackIcon) NamedIcon(jmri.jmrit.catalog.NamedIcon) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) Editor(jmri.jmrit.display.Editor)

Aggregations

Attribute (org.jdom2.Attribute)104 Element (org.jdom2.Element)96 DataConversionException (org.jdom2.DataConversionException)17 Editor (jmri.jmrit.display.Editor)15 ArrayList (java.util.ArrayList)13 NamedIcon (jmri.jmrit.catalog.NamedIcon)13 IOException (java.io.IOException)12 LayoutEditor (jmri.jmrit.display.layoutEditor.LayoutEditor)10 File (java.io.File)8 Color (java.awt.Color)7 List (java.util.List)7 HashMap (java.util.HashMap)6 Asn1Integer (com.android.hotspot2.asn1.Asn1Integer)5 Asn1Object (com.android.hotspot2.asn1.Asn1Object)5 Asn1Oid (com.android.hotspot2.asn1.Asn1Oid)5 OidMappings (com.android.hotspot2.asn1.OidMappings)5 ASN1Encodable (com.android.org.bouncycastle.asn1.ASN1Encodable)5 ASN1EncodableVector (com.android.org.bouncycastle.asn1.ASN1EncodableVector)5 ASN1Set (com.android.org.bouncycastle.asn1.ASN1Set)5 DERBitString (com.android.org.bouncycastle.asn1.DERBitString)5