Search in sources :

Example 11 with Parent

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

the class AbstractMemoryManagerConfigXML method loadMemories.

/**
     * Utility method to load the individual Memory objects. If there's no
     * additional info needed for a specific Memory type, invoke this with the
     * parent of the set of Memory elements.
     *
     * @param memories Element containing the Memory elements to load.
     */
@SuppressWarnings("unchecked")
public void loadMemories(Element memories) {
    List<Element> memoryList = memories.getChildren("memory");
    if (log.isDebugEnabled()) {
        log.debug("Found " + memoryList.size() + " Memory objects");
    }
    MemoryManager tm = InstanceManager.memoryManagerInstance();
    for (int i = 0; i < memoryList.size(); i++) {
        String sysName = getSystemName(memoryList.get(i));
        if (sysName == null) {
            log.warn("unexpected null in systemName " + (memoryList.get(i)));
            break;
        }
        String userName = getUserName(memoryList.get(i));
        checkNameNormalization(sysName, userName, tm);
        if (log.isDebugEnabled()) {
            log.debug("create Memory: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
        }
        Memory m = tm.newMemory(sysName, userName);
        if (memoryList.get(i).getAttribute("value") != null) {
            loadValue(memoryList.get(i), m);
        }
        // load common parts
        loadCommon(m, memoryList.get(i));
    }
}
Also used : Memory(jmri.Memory) Element(org.jdom2.Element) MemoryManager(jmri.MemoryManager)

Example 12 with Parent

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

the class AbstractReporterManagerConfigXML method loadReporters.

/**
     * Utility method to load the individual Reporter objects. If there's no
     * additional info needed for a specific Reporter type, invoke this with the
     * parent of the set of Reporter elements.
     *
     * @param reporters Element containing the Reporter elements to load.
     * @return true if successful
     */
@SuppressWarnings("unchecked")
public boolean loadReporters(Element reporters) {
    boolean result = true;
    List<Element> reporterList = reporters.getChildren("reporter");
    if (log.isDebugEnabled()) {
        log.debug("Found " + reporterList.size() + " reporters");
    }
    ReporterManager tm = InstanceManager.getDefault(jmri.ReporterManager.class);
    for (int i = 0; i < reporterList.size(); i++) {
        String sysName = getSystemName(reporterList.get(i));
        if (sysName == null) {
            log.warn("unexpected null in systemName " + reporterList.get(i) + " " + reporterList.get(i).getAttributes());
            result = false;
            break;
        }
        String userName = getUserName(reporterList.get(i));
        if (log.isDebugEnabled()) {
            log.debug("create Reporter: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
        }
        Reporter r = tm.newReporter(sysName, userName);
        loadCommon(r, reporterList.get(i));
    }
    return result;
}
Also used : ReporterManager(jmri.ReporterManager) Element(org.jdom2.Element) Reporter(jmri.Reporter)

Example 13 with Parent

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

the class AbstractSensorManagerConfigXML method loadSensors.

/**
     * Utility method to load the individual Sensor objects. If there's no
     * additional info needed for a specific sensor type, invoke this with the
     * parent of the set of Sensor elements.
     *
     * @param sensors Element containing the Sensor elements to load.
     * @return true if succeeded
     */
@SuppressWarnings("unchecked")
public boolean loadSensors(Element sensors) throws jmri.configurexml.JmriConfigureXmlException {
    boolean result = true;
    List<Element> sensorList = sensors.getChildren("sensor");
    if (log.isDebugEnabled()) {
        log.debug("Found " + sensorList.size() + " sensors");
    }
    SensorManager tm = InstanceManager.sensorManagerInstance();
    long goingActive = 0L;
    long goingInActive = 0L;
    if (sensors.getChild("globalDebounceTimers") != null) {
        Element timer = sensors.getChild("globalDebounceTimers");
        try {
            if (timer.getChild("goingActive") != null) {
                String active = timer.getChild("goingActive").getText();
                goingActive = Long.valueOf(active);
                tm.setDefaultSensorDebounceGoingActive(goingActive);
            }
        } catch (NumberFormatException ex) {
            log.error(ex.toString());
        }
        try {
            if (timer.getChild("goingInActive") != null) {
                String inActive = timer.getChild("goingInActive").getText();
                goingInActive = Long.valueOf(inActive);
                tm.setDefaultSensorDebounceGoingInActive(goingInActive);
            }
        } catch (NumberFormatException ex) {
            log.error(ex.toString());
        }
    }
    for (int i = 0; i < sensorList.size(); i++) {
        String sysName = getSystemName(sensorList.get(i));
        if (sysName == null) {
            handleException("Unexpected missing system name while loading sensors", null, null, null, null);
            result = false;
            break;
        }
        boolean inverted = false;
        String userName = getUserName(sensorList.get(i));
        checkNameNormalization(sysName, userName, tm);
        if (sensorList.get(i).getAttribute("inverted") != null) {
            if (sensorList.get(i).getAttribute("inverted").getValue().equals("true")) {
                inverted = true;
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("create sensor: (" + sysName + ")");
        }
        Sensor s;
        try {
            s = tm.newSensor(sysName, userName);
        } catch (IllegalArgumentException e) {
            handleException("Could not create sensor", null, sysName, userName, null);
            result = false;
            continue;
        }
        // load common parts
        loadCommon(s, sensorList.get(i));
        if (sensorList.get(i).getChild("debounceTimers") != null) {
            Element timer = sensorList.get(i).getChild("debounceTimers");
            try {
                if (timer.getChild("goingActive") != null) {
                    String active = timer.getChild("goingActive").getText();
                    s.setSensorDebounceGoingActiveTimer(Long.valueOf(active));
                }
            } catch (NumberFormatException ex) {
                log.error(ex.toString());
            }
            try {
                if (timer.getChild("goingInActive") != null) {
                    String inActive = timer.getChild("goingInActive").getText();
                    s.setSensorDebounceGoingInActiveTimer(Long.valueOf(inActive));
                }
            } catch (NumberFormatException ex) {
                log.error(ex.toString());
            }
        }
        if (sensorList.get(i).getChild("useGlobalDebounceTimer") != null) {
            if (sensorList.get(i).getChild("useGlobalDebounceTimer").getText().equals("yes")) {
                s.useDefaultTimerSettings(true);
            }
        }
        s.setInverted(inverted);
        if (sensorList.get(i).getChild("pullResistance") != null) {
            String pull = sensorList.get(i).getChild("pullResistance").getText();
            log.debug("setting pull to {} for sensor {}", pull, s);
            s.setPullResistance(jmri.Sensor.PullResistance.getByShortName(pull));
        }
    }
    return result;
}
Also used : SensorManager(jmri.SensorManager) Element(org.jdom2.Element) Sensor(jmri.Sensor)

Example 14 with Parent

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

the class XmlFile method writeXML.

/**
     * Write a File as XML.
     *
     * @throws FileNotFoundException if file not found
     * @param file File to be created.
     * @param doc  Document to be written out. This should never be null.
     */
public void writeXML(File file, Document doc) throws IOException, FileNotFoundException {
    // ensure parent directory exists
    if (file.getParent() != null) {
        FileUtil.createDirectory(file.getParent());
    }
    // write the result to selected file
    try (FileOutputStream o = new FileOutputStream(file)) {
        XMLOutputter fmt = new XMLOutputter();
        fmt.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.TRIM_FULL_WHITE));
        fmt.output(doc, o);
        o.flush();
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) FileOutputStream(java.io.FileOutputStream)

Example 15 with Parent

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

the class DefaultCatalogTreeManagerXml method storeNode.

/**
     * Recursively store a CatalogTree.
     *
     * @param parent the element to store node in
     * @param node   the root node of the tree
     */
public void storeNode(Element parent, CatalogTreeNode node) {
    if (log.isDebugEnabled()) {
        log.debug("storeNode " + node.toString() + ", has " + node.getLeaves().size() + " leaves.");
    }
    Element element = new Element("node");
    element.setAttribute("nodeName", node.toString());
    List<CatalogTreeLeaf> leaves = node.getLeaves();
    for (int i = 0; i < leaves.size(); i++) {
        Element el = new Element("leaf");
        CatalogTreeLeaf leaf = leaves.get(i);
        el.setAttribute("name", leaf.getName());
        el.setAttribute("path", leaf.getPath());
        element.addContent(el);
    }
    parent.addContent(element);
    // is node.children actually of <Element> type?
    @SuppressWarnings("unchecked") Enumeration<CatalogTreeNode> e = node.children();
    while (e.hasMoreElements()) {
        CatalogTreeNode n = e.nextElement();
        storeNode(element, n);
    }
}
Also used : CatalogTreeLeaf(jmri.jmrit.catalog.CatalogTreeLeaf) Element(org.jdom2.Element) CatalogTreeNode(jmri.jmrit.catalog.CatalogTreeNode)

Aggregations

Element (org.jdom2.Element)24 File (java.io.File)6 XmlFile (jmri.jmrit.XmlFile)6 Document (org.jdom2.Document)6 Attribute (org.jdom2.Attribute)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 Logix (jmri.Logix)2 AppConfigBase (apps.AppConfigBase)1 ConfigBundle (apps.ConfigBundle)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 BorderLayout (java.awt.BorderLayout)1 CardLayout (java.awt.CardLayout)1 Color (java.awt.Color)1 Dimension (java.awt.Dimension)1 ActionEvent (java.awt.event.ActionEvent)1 FileOutputStream (java.io.FileOutputStream)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ServiceLoader (java.util.ServiceLoader)1