Search in sources :

Example 66 with Attribute

use of ucar.nc2.Attribute in project JMRI by JMRI.

the class WebServerManager method preferencesFromMiniServerPreferences.

@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Catch is covering both JDOMException and IOException, FindBugs seems confused")
private void preferencesFromMiniServerPreferences(File MSFile, File WSFile) {
    XmlFile xmlFile = new XmlFile() {
    };
    try {
        Element MSRoot = xmlFile.rootFromFile(MSFile);
        Element WSRoot = new Element(WebServerPreferences.WEB_SERVER_PREFERENCES);
        // NOI18N
        Element MSPrefs = MSRoot.getChild("MiniServerPreferences");
        for (Object pref : MSPrefs.getChildren()) {
            WSRoot.addContent((Element) pref);
        }
        for (Attribute attr : MSPrefs.getAttributes()) {
            if (attr.getName().equals("getDisallowedFrames")) {
                // NOI18N
                Element DF = new Element(WebServerPreferences.DISALLOWED_FRAMES);
                // NOI18N
                String[] frames = attr.getValue().split("\\n");
                for (String frame : frames) {
                    DF.addContent(new Element(WebServerPreferences.FRAME).addContent(frame));
                }
                WSRoot.addContent(DF);
            } else if (attr.getName().equals("getPort")) {
                // NOI18N
                WSRoot.setAttribute(WebServerPreferences.PORT, attr.getValue());
            } else if (attr.getName().equals("getClickDelay")) {
                // NOI18N
                WSRoot.setAttribute(WebServerPreferences.CLICK_DELAY, attr.getValue());
            } else if (attr.getName().equals("getRefreshDelay")) {
                // NOI18N
                WSRoot.setAttribute(WebServerPreferences.REFRESH_DELAY, attr.getValue());
            } else {
                // double cast because clone() is Protected on Object
                WSRoot.setAttribute(attr.clone());
            }
        }
        Document WSDoc = XmlFile.newDocument(WSRoot);
        File parent = new File(WSFile.getParent());
        if (!parent.exists()) {
            // directory known to not exist from previous conditional
            boolean created = parent.mkdir();
            if (!created) {
                log.error("Failed to create directory {}", parent.toString());
                throw new java.io.IOException("Failed to create directory " + parent.toString());
            }
        }
        // known to not exist or this method would not have been called
        boolean created = WSFile.createNewFile();
        if (!created) {
            log.error("Failed to new create file {}", WSFile.toString());
            throw new java.io.IOException("Failed to create new file " + WSFile.toString());
        }
        xmlFile.writeXML(WSFile, WSDoc);
    } catch (IOException | JDOMException ex) {
        log.error("Error converting miniServer preferences to Web Server preferences.", ex);
    }
}
Also used : XmlFile(jmri.jmrit.XmlFile) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) File(java.io.File) XmlFile(jmri.jmrit.XmlFile) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 67 with Attribute

use of ucar.nc2.Attribute in project JMRI by JMRI.

the class JsonServerPreferences method load.

public void load(Element child) {
    Attribute a;
    a = child.getAttribute(HEARTBEAT_INTERVAL);
    if (a != null) {
        try {
            this.setHeartbeatInterval(a.getIntValue());
            this.asLoadedHeartbeatInterval = this.getHeartbeatInterval();
        } catch (DataConversionException e) {
            this.setHeartbeatInterval(15000);
            log.error("Unable to read heartbeat interval. Setting to default value.", e);
        }
    }
    a = child.getAttribute(PORT);
    if (a != null) {
        try {
            this.setPort(a.getIntValue());
            this.asLoadedPort = this.getPort();
        } catch (DataConversionException e) {
            this.setPort(2056);
            log.error("Unable to read port. Setting to default value.", e);
        }
    }
}
Also used : Attribute(org.jdom2.Attribute) DataConversionException(org.jdom2.DataConversionException)

Example 68 with Attribute

use of ucar.nc2.Attribute in project JMRI by JMRI.

the class DefaultCatalogTreeManagerXml method loadCatalogTrees.

/**
     * Utility method to load the individual CatalogTree objects.
     *
     * @param catalogTrees element containing trees
     */
public void loadCatalogTrees(Element catalogTrees) {
    List<Element> catList = catalogTrees.getChildren("catalogTree");
    if (log.isDebugEnabled()) {
        log.debug("loadCatalogTrees: found " + catList.size() + " CatalogTree objects");
    }
    CatalogTreeManager mgr = InstanceManager.getDefault(jmri.CatalogTreeManager.class);
    for (int i = 0; i < catList.size(); i++) {
        Element elem = catList.get(i);
        Attribute attr = elem.getAttribute("systemName");
        if (attr == null) {
            log.warn("unexpected null systemName. elem= " + elem + ", attrs= " + elem.getAttributes());
            continue;
        }
        String sysName = attr.getValue();
        String userName;
        attr = elem.getAttribute("userName");
        if (attr == null) {
            log.warn("unexpected null userName. attrs= " + elem.getAttributes());
            continue;
        } else {
            userName = attr.getValue();
        }
        DefaultTreeModel ct = (DefaultTreeModel) mgr.getBySystemName(sysName);
        if (ct != null) {
            // tree already registered
            continue;
        }
        ct = (DefaultTreeModel) mgr.newCatalogTree(sysName, userName);
        if (log.isDebugEnabled()) {
            log.debug("CatalogTree: sysName= " + sysName + ", userName= " + userName);
        }
        CatalogTreeNode root = (CatalogTreeNode) ct.getRoot();
        elem = elem.getChild("node");
        loadNode(elem, root, ct);
    }
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) CatalogTreeManager(jmri.CatalogTreeManager) CatalogTreeNode(jmri.jmrit.catalog.CatalogTreeNode)

Example 69 with Attribute

use of ucar.nc2.Attribute in project JMRI by JMRI.

the class SignalHeadIconXml method load.

/**
     * Create a PositionableLabel, then add to a target JLayeredPane
     *
     * @param element Top level Element to unpack.
     * @param o       an Editor as an Object
     */
@Override
public void load(Element element, Object o) {
    // create the objects
    Editor ed = (Editor) o;
    SignalHeadIcon l = new SignalHeadIcon(ed);
    String name;
    Attribute attr = element.getAttribute("signalhead");
    if (attr == null) {
        log.error("incorrect information for signal head; must use signalhead name");
        ed.loadFailed();
        return;
    } else {
        name = attr.getValue();
    }
    SignalHead sh = jmri.InstanceManager.getDefault(jmri.SignalHeadManager.class).getSignalHead(name);
    if (sh != null) {
        l.setSignalHead(name);
    } else {
        log.error("SignalHead named '" + attr.getValue() + "' not found.");
        //    ed.loadFailed();
        return;
    }
    int rotation = 0;
    try {
        attr = element.getAttribute("rotate");
        rotation = attr.getIntValue();
    } catch (org.jdom2.DataConversionException e) {
    } catch (NullPointerException e) {
    // considered normal if the attributes are not present
    }
    List<Element> aspects = element.getChildren();
    if (aspects.size() > 0) {
        Element icons = element.getChild("icons");
        Element elem = element;
        if (icons != null) {
            List<Element> c = icons.getChildren();
            aspects = c;
            elem = icons;
        }
        for (int i = 0; i < aspects.size(); i++) {
            String aspect = aspects.get(i).getName();
            NamedIcon icon = loadIcon(l, aspect, elem, "SignalHead \"" + name + "\": icon \"" + aspect + "\" ", ed);
            if (icon != null) {
                l.setIcon(_nameMap.get(aspect), icon);
            } else {
                log.info("SignalHead \"" + name + "\": icon \"" + aspect + "\" removed");
            }
        }
        log.debug(aspects.size() + " icons loaded for " + l.getNameString());
    } else {
        // old style as attributes - somewhere around pre 2.5.4
        NamedIcon icon = loadSignalIcon("red", rotation, l, element, name, ed);
        if (icon != null) {
            l.setIcon(rbean.getString("SignalHeadStateRed"), icon);
        }
        icon = loadSignalIcon("yellow", rotation, l, element, name, ed);
        if (icon != null) {
            l.setIcon(rbean.getString("SignalHeadStateYellow"), icon);
        }
        icon = loadSignalIcon("green", rotation, l, element, name, ed);
        if (icon != null) {
            l.setIcon(rbean.getString("SignalHeadStateGreen"), icon);
        }
        icon = loadSignalIcon("lunar", rotation, l, element, name, ed);
        if (icon != null) {
            l.setIcon(rbean.getString("SignalHeadStateLunar"), icon);
        }
        icon = loadSignalIcon("held", rotation, l, element, name, ed);
        if (icon != null) {
            l.setIcon(rbean.getString("SignalHeadStateHeld"), icon);
        }
        icon = loadSignalIcon("dark", rotation, l, element, name, ed);
        if (icon != null) {
            l.setIcon(rbean.getString("SignalHeadStateDark"), icon);
        }
        icon = loadSignalIcon("flashred", rotation, l, element, name, ed);
        if (icon != null) {
            l.setIcon(rbean.getString("SignalHeadStateFlashingRed"), icon);
        }
        icon = loadSignalIcon("flashyellow", rotation, l, element, name, ed);
        if (icon != null) {
            l.setIcon(rbean.getString("SignalHeadStateFlashingYellow"), icon);
        }
        icon = loadSignalIcon("flashgreen", rotation, l, element, name, ed);
        if (icon != null) {
            l.setIcon(rbean.getString("SignalHeadStateFlashingGreen"), icon);
        }
        icon = loadSignalIcon("flashlunar", rotation, l, element, name, ed);
        if (icon != null) {
            l.setIcon(rbean.getString("SignalHeadStateFlashingLunar"), icon);
        }
    }
    Element elem = element.getChild("iconmaps");
    if (elem != null) {
        attr = elem.getAttribute("family");
        if (attr != null) {
            l.setFamily(attr.getValue());
        }
    }
    try {
        attr = element.getAttribute("clickmode");
        if (attr != null) {
            l.setClickMode(attr.getIntValue());
        }
    } catch (org.jdom2.DataConversionException e) {
        log.error("Failed on clickmode attribute: " + e);
    }
    try {
        attr = element.getAttribute("litmode");
        if (attr != null) {
            l.setLitMode(attr.getBooleanValue());
        }
    } catch (org.jdom2.DataConversionException e) {
        log.error("Failed on litmode attribute: " + e);
    }
    ed.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.SIGNALS, element);
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) SignalHead(jmri.SignalHead) SignalHeadIcon(jmri.jmrit.display.SignalHeadIcon) Editor(jmri.jmrit.display.Editor)

Example 70 with Attribute

use of ucar.nc2.Attribute in project JMRI by JMRI.

the class SignalMastIconXml method load.

/**
     * Create a SignalMastIcon, then add
     *
     * @param element Top level Element to unpack.
     * @param o       an Editor as an Object
     */
@Override
public void load(Element element, Object o) {
    // create the objects
    Editor ed = (Editor) o;
    SignalMastIcon l = new SignalMastIcon(ed);
    String name;
    Attribute attr;
    /*
         * We need to set the rotation and scaling first, prior to setting the
         * signalmast, otherwise we end up in a situation where by the icons do
         * not get rotated or scaled correctly.
         **/
    try {
        int rotation = 0;
        double scale = 1.0;
        // former attribute name.
        attr = element.getAttribute("rotation");
        if (attr != null) {
            rotation = attr.getIntValue();
        }
        attr = element.getAttribute("degrees");
        if (attr != null) {
            rotation = attr.getIntValue();
        }
        l.rotate(rotation);
        attr = element.getAttribute("scale");
        String text = "Error attr null";
        if (attr != null) {
            scale = attr.getDoubleValue();
            text = attr.getValue();
        }
        l.setScale(scale);
        if (log.isDebugEnabled()) {
            log.debug("Load SignalMast rotation= " + rotation + " scale= " + scale + " attr text= " + text);
        }
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert rotation or scale attribute");
    }
    attr = element.getAttribute("signalmast");
    if (attr == null) {
        log.error("incorrect information for signal mast; must use signalmast name");
        ed.loadFailed();
        return;
    } else {
        name = attr.getValue();
        if (log.isDebugEnabled()) {
            log.debug("Load SignalMast " + name);
        }
    }
    SignalMast sh = jmri.InstanceManager.getDefault(jmri.SignalMastManager.class).getSignalMast(name);
    if (sh != null) {
        l.setSignalMast(name);
    } else {
        log.error("SignalMast named '" + attr.getValue() + "' not found.");
        ed.loadFailed();
    //    return;
    }
    attr = element.getAttribute("imageset");
    if (attr != null) {
        l.useIconSet(attr.getValue());
    }
    attr = element.getAttribute("imageset");
    if (attr != null) {
        l.useIconSet(attr.getValue());
    }
    try {
        attr = element.getAttribute("clickmode");
        if (attr != null) {
            l.setClickMode(attr.getIntValue());
        }
    } catch (org.jdom2.DataConversionException e) {
        log.error("Failed on clickmode attribute: " + e);
    }
    try {
        attr = element.getAttribute("litmode");
        if (attr != null) {
            l.setLitMode(attr.getBooleanValue());
        }
    } catch (org.jdom2.DataConversionException e) {
        log.error("Failed on litmode attribute: " + e);
    }
    ed.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.SIGNALS, element);
}
Also used : Attribute(org.jdom2.Attribute) SignalMastIcon(jmri.jmrit.display.SignalMastIcon) SignalMast(jmri.SignalMast) Editor(jmri.jmrit.display.Editor)

Aggregations

Attribute (org.jdom2.Attribute)106 Element (org.jdom2.Element)75 Editor (jmri.jmrit.display.Editor)15 DataConversionException (org.jdom2.DataConversionException)14 NamedIcon (jmri.jmrit.catalog.NamedIcon)13 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)12 LayoutEditor (jmri.jmrit.display.layoutEditor.LayoutEditor)9 List (java.util.List)7 Color (java.awt.Color)6 File (java.io.File)6 HashMap (java.util.HashMap)6 ConfigureManager (jmri.ConfigureManager)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