Search in sources :

Example 21 with Parent

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

the class DefaultConditionalManagerXml method loadConditionals.

/**
     * Utility method to load the individual Logix objects. If there's no
     * additional info needed for a specific logix type, invoke this with the
     * parent of the set of Logix elements.
     *
     * @param conditionals Element containing the Logix elements to load.
     */
public void loadConditionals(Element conditionals) {
    List<Element> conditionalList = conditionals.getChildren("conditional");
    if (log.isDebugEnabled()) {
        log.debug("Found " + conditionalList.size() + " conditionals");
    }
    ConditionalManager tm = InstanceManager.getDefault(jmri.ConditionalManager.class);
    for (int i = 0; i < conditionalList.size(); i++) {
        Element condElem = conditionalList.get(i);
        String sysName = getSystemName(condElem);
        if (sysName == null) {
            log.warn("unexpected null in systemName " + condElem);
            break;
        }
        // omitted username is treated as empty, not null
        String userName = getUserName(condElem);
        if (userName == null) {
            userName = "";
        }
        if (log.isDebugEnabled()) {
            log.debug("create conditional: ({})({})", sysName, userName);
        }
        // Try getting the conditional.  This should fail
        Conditional c = tm.getBySystemName(sysName);
        if (c == null) {
            // Check for parent Logix
            Logix x = tm.getParentLogix(sysName);
            if (x == null) {
                log.warn("Conditional '{}' has no parent Logix", sysName);
                continue;
            }
            // Found a potential parent Logix, check the Logix index
            boolean inIndex = false;
            for (int j = 0; j < x.getNumConditionals(); j++) {
                String cName = x.getConditionalByNumberOrder(j);
                if (sysName.equals(cName)) {
                    inIndex = true;
                    break;
                }
            }
            if (!inIndex) {
                log.warn("Conditional '{}' is not in the Logix index", sysName);
                continue;
            }
            // Create the condtional
            c = tm.createNewConditional(sysName, userName);
        }
        if (c == null) {
            // Should never get here
            log.error("Conditional '{}' cannot be created", sysName);
            continue;
        }
        // conditional already exists
        // load common parts
        loadCommon(c, condElem);
        String ant = "";
        int logicType = Conditional.ALL_AND;
        if (condElem.getAttribute("antecedent") != null) {
            ant = condElem.getAttribute("antecedent").getValue();
        }
        if (condElem.getAttribute("logicType") != null) {
            logicType = Integer.parseInt(condElem.getAttribute("logicType").getValue());
        }
        c.setLogicType(logicType, ant);
        // load state variables, if there are any
        List<Element> conditionalVarList = condElem.getChildren("conditionalStateVariable");
        if (conditionalVarList.size() == 0) {
            log.warn("No state variables found for conditional " + sysName);
        }
        ArrayList<ConditionalVariable> variableList = new ArrayList<>();
        for (int n = 0; n < conditionalVarList.size(); n++) {
            ConditionalVariable variable = new ConditionalVariable();
            if (conditionalVarList.get(n).getAttribute("operator") == null) {
                log.warn("unexpected null in operator " + conditionalVarList.get(n) + " " + conditionalVarList.get(n).getAttributes());
            } else {
                int oper = Integer.parseInt(conditionalVarList.get(n).getAttribute("operator").getValue());
                if (oper == Conditional.OPERATOR_AND_NOT) {
                    variable.setNegation(true);
                    oper = Conditional.OPERATOR_AND;
                } else if (oper == Conditional.OPERATOR_NOT) {
                    variable.setNegation(true);
                    oper = Conditional.OPERATOR_NONE;
                }
                variable.setOpern(oper);
            }
            if (conditionalVarList.get(n).getAttribute("negated") != null) {
                if ("yes".equals(conditionalVarList.get(n).getAttribute("negated").getValue())) {
                    variable.setNegation(true);
                } else {
                    variable.setNegation(false);
                }
            }
            variable.setType(Integer.parseInt(conditionalVarList.get(n).getAttribute("type").getValue()));
            variable.setName(conditionalVarList.get(n).getAttribute("systemName").getValue());
            if (conditionalVarList.get(n).getAttribute("dataString") != null) {
                variable.setDataString(conditionalVarList.get(n).getAttribute("dataString").getValue());
            }
            if (conditionalVarList.get(n).getAttribute("num1") != null) {
                variable.setNum1(Integer.parseInt(conditionalVarList.get(n).getAttribute("num1").getValue()));
            }
            if (conditionalVarList.get(n).getAttribute("num2") != null) {
                variable.setNum2(Integer.parseInt(conditionalVarList.get(n).getAttribute("num2").getValue()));
            }
            variable.setTriggerActions(true);
            if (conditionalVarList.get(n).getAttribute("triggersCalc") != null) {
                if ("no".equals(conditionalVarList.get(n).getAttribute("triggersCalc").getValue())) {
                    variable.setTriggerActions(false);
                }
            }
            variableList.add(variable);
        }
        c.setStateVariables(variableList);
        // load actions - there better be some
        List<Element> conditionalActionList = condElem.getChildren("conditionalAction");
        // Really OK, since a user may use such conditionals to define a reusable
        // expression of state variables.  These conditions are then used as a 
        // state variable in other conditionals.  (pwc)
        //if (conditionalActionList.size() == 0) {
        //    log.warn("No actions found for conditional "+sysName);
        //}
        ArrayList<ConditionalAction> actionList = ((DefaultConditional) c).getActionList();
        org.jdom2.Attribute attr = null;
        for (int n = 0; n < conditionalActionList.size(); n++) {
            ConditionalAction action = new DefaultConditionalAction();
            attr = conditionalActionList.get(n).getAttribute("option");
            if (attr != null) {
                action.setOption(Integer.parseInt(attr.getValue()));
            } else {
                log.warn("unexpected null in option " + conditionalActionList.get(n) + " " + conditionalActionList.get(n).getAttributes());
            }
            // actionDelay is removed.  delay data is stored as a String to allow
            // such data be referenced by internal memory.
            // For backward compatibility, set delay "int" as a string
            attr = conditionalActionList.get(n).getAttribute("delay");
            if (attr != null) {
                action.setActionString(attr.getValue());
            }
            attr = conditionalActionList.get(n).getAttribute("type");
            if (attr != null) {
                action.setType(Integer.parseInt(attr.getValue()));
            } else {
                log.warn("unexpected null in type " + conditionalActionList.get(n) + " " + conditionalActionList.get(n).getAttributes());
            }
            attr = conditionalActionList.get(n).getAttribute("systemName");
            if (attr != null) {
                action.setDeviceName(attr.getValue());
            } else {
                log.warn("unexpected null in systemName " + conditionalActionList.get(n) + " " + conditionalActionList.get(n).getAttributes());
            }
            attr = conditionalActionList.get(n).getAttribute("data");
            if (attr != null) {
                action.setActionData(Integer.parseInt(attr.getValue()));
            } else {
                log.warn("unexpected null in action data " + conditionalActionList.get(n) + " " + conditionalActionList.get(n).getAttributes());
            }
            attr = conditionalActionList.get(n).getAttribute("string");
            if (attr != null) {
                action.setActionString(attr.getValue());
            } else {
                log.warn("unexpected null in action string " + conditionalActionList.get(n) + " " + conditionalActionList.get(n).getAttributes());
            }
            if (!actionList.contains(action))
                actionList.add(action);
        }
        c.setAction(actionList);
        // 1/16/2011 - trigger for execution of the action list changed to execute each 
        // time state is computed.  Formerly execution of the action list was done only
        // when state changes.  All conditionals are upgraded to this new policy.
        // However, for conditionals with actions that toggle on change of state
        // the old policy should be used.
        boolean triggerOnChange = false;
        if (condElem.getAttribute("triggerOnChange") != null) {
            if ("yes".equals(condElem.getAttribute("triggerOnChange").getValue())) {
                triggerOnChange = true;
            }
        } else {
            /* Don't upgrade -Let old be as is
                 for (int k=0; k<actionList.size(); k++){
                 ConditionalAction action = actionList.get(k);
                 if (action.getOption()==Conditional.ACTION_OPTION_ON_CHANGE){
                 triggerOnChange = true;
                 break;
                 }
                 }
                 */
            triggerOnChange = true;
        }
        c.setTriggerOnChange(triggerOnChange);
    }
}
Also used : DefaultConditionalAction(jmri.implementation.DefaultConditionalAction) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) DefaultConditional(jmri.implementation.DefaultConditional) Conditional(jmri.Conditional) ConditionalVariable(jmri.ConditionalVariable) DefaultConditional(jmri.implementation.DefaultConditional) Logix(jmri.Logix) ConditionalAction(jmri.ConditionalAction) DefaultConditionalAction(jmri.implementation.DefaultConditionalAction) DefaultConditionalManager(jmri.managers.DefaultConditionalManager) ConditionalManager(jmri.ConditionalManager)

Example 22 with Parent

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

the class DefaultRouteManagerXml method loadRoutes.

/**
     * Utility method to load the individual Route objects. If there's no
     * additional info needed for a specific route type, invoke this with the
     * parent of the set of Route elements.
     *
     * @param routes Element containing the Route elements to load.
     */
public void loadRoutes(Element routes) {
    List<Element> routeList = routes.getChildren("route");
    if (log.isDebugEnabled()) {
        log.debug("Found " + routeList.size() + " routes");
    }
    RouteManager tm = InstanceManager.getDefault(jmri.RouteManager.class);
    for (int i = 0; i < routeList.size(); i++) {
        String sysName = getSystemName(routeList.get(i));
        if (sysName == null) {
            log.warn("unexpected null in systemName " + routeList.get(i));
            break;
        }
        String userName = getUserName(routeList.get(i));
        String cTurnout = null;
        String cTurnoutState = null;
        String addedDelayTxt = null;
        String routeLockedTxt = null;
        String cLockTurnout = null;
        String cLockTurnoutState = null;
        int addedDelay = 0;
        if (routeList.get(i).getAttribute("controlTurnout") != null) {
            cTurnout = routeList.get(i).getAttribute("controlTurnout").getValue();
        }
        if (routeList.get(i).getAttribute("controlTurnoutState") != null) {
            cTurnoutState = routeList.get(i).getAttribute("controlTurnoutState").getValue();
        }
        if (routeList.get(i).getAttribute("controlLockTurnout") != null) {
            cLockTurnout = routeList.get(i).getAttribute("controlLockTurnout").getValue();
        }
        if (routeList.get(i).getAttribute("controlLockTurnoutState") != null) {
            cLockTurnoutState = routeList.get(i).getAttribute("controlLockTurnoutState").getValue();
        }
        if (routeList.get(i).getAttribute("addedDelay") != null) {
            addedDelayTxt = routeList.get(i).getAttribute("addedDelay").getValue();
            if (addedDelayTxt != null) {
                addedDelay = Integer.parseInt(addedDelayTxt);
            }
        }
        if (routeList.get(i).getAttribute("routeLocked") != null) {
            routeLockedTxt = routeList.get(i).getAttribute("routeLocked").getValue();
        }
        if (log.isDebugEnabled()) {
            log.debug("create route: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
        }
        Route r;
        try {
            r = tm.provideRoute(sysName, userName);
        } catch (IllegalArgumentException ex) {
            log.error("failed to create Route: " + sysName);
            return;
        }
        // load common parts
        loadCommon(r, routeList.get(i));
        // add control turnout if there is one
        if (cTurnout != null) {
            r.setControlTurnout(cTurnout);
            if (cTurnoutState != null) {
                if (cTurnoutState.equals("THROWN")) {
                    r.setControlTurnoutState(Route.ONTHROWN);
                } else if (cTurnoutState.equals("CHANGE")) {
                    r.setControlTurnoutState(Route.ONCHANGE);
                } else if (cTurnoutState.equals("VETOCLOSED")) {
                    r.setControlTurnoutState(Route.VETOCLOSED);
                } else if (cTurnoutState.equals("VETOTHROWN")) {
                    r.setControlTurnoutState(Route.VETOTHROWN);
                } else {
                    r.setControlTurnoutState(Route.ONCLOSED);
                }
            } else {
                log.error("cTurnoutState was null!");
            }
        }
        // set added delay
        r.setRouteCommandDelay(addedDelay);
        // determine if route locked
        if (routeLockedTxt != null && routeLockedTxt.equals("True")) {
            r.setLocked(true);
        }
        //add lock control turout if there is one
        if (cLockTurnout != null) {
            r.setLockControlTurnout(cLockTurnout);
            if (cLockTurnoutState != null) {
                if (cLockTurnoutState.equals("THROWN")) {
                    r.setLockControlTurnoutState(Route.ONTHROWN);
                } else if (cLockTurnoutState.equals("CHANGE")) {
                    r.setLockControlTurnoutState(Route.ONCHANGE);
                } else {
                    r.setLockControlTurnoutState(Route.ONCLOSED);
                }
            } else {
                log.error("cLockTurnoutState was null!");
            }
        }
        // load output turnouts if there are any - old format first (1.7.6 and before)
        List<Element> routeTurnoutList = routeList.get(i).getChildren("routeTurnout");
        if (routeTurnoutList.size() > 0) {
            // This route has turnouts
            for (int k = 0; k < routeTurnoutList.size(); k++) {
                if (((routeTurnoutList.get(k))).getAttribute("systemName") == null) {
                    log.warn("unexpected null in systemName " + ((routeTurnoutList.get(k))) + " " + ((routeTurnoutList.get(k))).getAttributes());
                    break;
                }
                String tSysName = ((routeTurnoutList.get(k))).getAttribute("systemName").getValue();
                String rState = ((routeTurnoutList.get(k))).getAttribute("state").getValue();
                int tSetState = Turnout.CLOSED;
                if (rState.equals("THROWN")) {
                    tSetState = Turnout.THROWN;
                } else if (rState.equals("TOGGLE")) {
                    tSetState = Route.TOGGLE;
                }
                // Add turnout to route
                r.addOutputTurnout(tSysName, tSetState);
            }
        }
        // load output turnouts if there are any - new format
        routeTurnoutList = routeList.get(i).getChildren("routeOutputTurnout");
        if (routeTurnoutList.size() > 0) {
            // This route has turnouts
            for (int k = 0; k < routeTurnoutList.size(); k++) {
                if (routeTurnoutList.get(k).getAttribute("systemName") == null) {
                    log.warn("unexpected null in systemName " + routeTurnoutList.get(k) + " " + routeTurnoutList.get(k).getAttributes());
                    break;
                }
                String tSysName = routeTurnoutList.get(k).getAttribute("systemName").getValue();
                String rState = routeTurnoutList.get(k).getAttribute("state").getValue();
                int tSetState = Turnout.CLOSED;
                if (rState.equals("THROWN")) {
                    tSetState = Turnout.THROWN;
                } else if (rState.equals("TOGGLE")) {
                    tSetState = Route.TOGGLE;
                }
                // we will not re add the turnout.
                if (!r.isOutputTurnoutIncluded(tSysName)) {
                    // Add turnout to route
                    r.addOutputTurnout(tSysName, tSetState);
                    // determine if turnout should be locked
                    Turnout t = r.getOutputTurnout(k);
                    if (r.getLocked()) {
                        t.setLocked(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, true);
                    }
                }
            }
        }
        // load output sensors if there are any - new format
        routeTurnoutList = routeList.get(i).getChildren("routeOutputSensor");
        if (routeTurnoutList.size() > 0) {
            // This route has turnouts
            for (int k = 0; k < routeTurnoutList.size(); k++) {
                if (routeTurnoutList.get(k).getAttribute("systemName") == null) {
                    log.warn("unexpected null in systemName " + routeTurnoutList.get(k) + " " + routeTurnoutList.get(k).getAttributes());
                    break;
                }
                String tSysName = routeTurnoutList.get(k).getAttribute("systemName").getValue();
                String rState = routeTurnoutList.get(k).getAttribute("state").getValue();
                int tSetState = Sensor.INACTIVE;
                if (rState.equals("ACTIVE")) {
                    tSetState = Sensor.ACTIVE;
                } else if (rState.equals("TOGGLE")) {
                    tSetState = Route.TOGGLE;
                }
                // we will not re add the turnout.                        
                if (r.isOutputSensorIncluded(tSysName)) {
                    break;
                }
                // Add turnout to route
                r.addOutputSensor(tSysName, tSetState);
            }
        }
        // load sound, script files if present
        Element fileElement = routeList.get(i).getChild("routeSoundFile");
        if (fileElement != null) {
            // convert to absolute path name
            r.setOutputSoundName(jmri.util.FileUtil.getExternalFilename(fileElement.getAttribute("name").getValue()));
        }
        fileElement = routeList.get(i).getChild("routeScriptFile");
        if (fileElement != null) {
            r.setOutputScriptName(jmri.util.FileUtil.getExternalFilename(fileElement.getAttribute("name").getValue()));
        }
        // load turnouts aligned sensor if there is one
        fileElement = routeList.get(i).getChild("turnoutsAlignedSensor");
        if (fileElement != null) {
            r.setTurnoutsAlignedSensor(fileElement.getAttribute("name").getValue());
        }
        // load route control sensors, if there are any
        List<Element> routeSensorList = routeList.get(i).getChildren("routeSensor");
        if (routeSensorList.size() > 0) {
            // This route has sensors
            for (int k = 0; k < routeSensorList.size(); k++) {
                if (routeSensorList.get(k).getAttribute("systemName") == null) {
                    log.warn("unexpected null in systemName " + routeSensorList.get(k) + " " + routeSensorList.get(k).getAttributes());
                    break;
                }
                // default mode
                int mode = Route.ONACTIVE;
                if (routeSensorList.get(k).getAttribute("mode") != null) {
                    String sm = routeSensorList.get(k).getAttribute("mode").getValue();
                    if (sm.equals("onActive")) {
                        mode = Route.ONACTIVE;
                    } else if (sm.equals("onInactive")) {
                        mode = Route.ONINACTIVE;
                    } else if (sm.equals("onChange")) {
                        mode = Route.ONCHANGE;
                    } else if (sm.equals("vetoActive")) {
                        mode = Route.VETOACTIVE;
                    } else if (sm.equals("vetoInactive")) {
                        mode = Route.VETOINACTIVE;
                    } else {
                        log.warn("unexpected sensor mode in route " + sysName + " was " + sm);
                    }
                }
                // Add Sensor to route
                r.addSensorToRoute(routeSensorList.get(k).getAttribute("systemName").getValue(), mode);
            }
        }
        // and start it working
        r.activateRoute();
    }
}
Also used : Element(org.jdom2.Element) Turnout(jmri.Turnout) Route(jmri.Route) DefaultRouteManager(jmri.managers.DefaultRouteManager) RouteManager(jmri.RouteManager)

Example 23 with Parent

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

the class AbstractLightManagerConfigXML method loadLights.

/**
     * Utility method to load the individual Light objects. If there's no
     * additional info needed for a specific light type, invoke this with the
     * parent of the set of Light elements.
     *
     * @param lights Element containing the Light elements to load.
     */
@SuppressWarnings("unchecked")
public boolean loadLights(Element lights) {
    boolean result = true;
    List<Element> lightList = lights.getChildren("light");
    if (log.isDebugEnabled()) {
        log.debug("Found " + lightList.size() + " lights");
    }
    LightManager tm = InstanceManager.lightManagerInstance();
    for (int i = 0; i < lightList.size(); i++) {
        String sysName = getSystemName(lightList.get(i));
        if (sysName == null) {
            log.warn("unexpected null in systemName " + lightList.get(i) + " " + lightList.get(i).getAttributes());
            result = false;
            break;
        }
        String userName = getUserName(lightList.get(i));
        checkNameNormalization(sysName, userName, tm);
        if (log.isDebugEnabled()) {
            log.debug("create light: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
        }
        Light lgt = null;
        try {
            lgt = tm.newLight(sysName, userName);
        } catch (IllegalArgumentException e) {
            log.error("failed to create Light: " + sysName);
            return false;
        }
        // load common parts
        loadCommon(lgt, lightList.get(i));
        // variable intensity, transition attributes
        double value;
        value = Double.parseDouble(lightList.get(i).getAttribute("minIntensity").getValue());
        lgt.setMinIntensity(value);
        value = Double.parseDouble(lightList.get(i).getAttribute("maxIntensity").getValue());
        lgt.setMaxIntensity(value);
        value = Double.parseDouble(lightList.get(i).getAttribute("transitionTime").getValue());
        lgt.setTransitionTime(value);
        // provide for legacy light control - panel files written by 2.9.5 or before
        if (lightList.get(i).getAttribute("controlType") != null) {
            // this is a legacy Light - create a LightControl from the input
            String temString = lightList.get(i).getAttribute("controlType").getValue();
            int type = Light.NO_CONTROL;
            try {
                type = Integer.parseInt(temString);
            } catch (NumberFormatException e) {
                log.error("error when converting control type in legacy Light load support");
                type = Light.NO_CONTROL;
            }
            if (type != Light.NO_CONTROL) {
                // this legacy light has a control - capture it
                LightControl lc = new LightControl(lgt);
                lc.setControlType(type);
                if (type == Light.SENSOR_CONTROL) {
                    lc.setControlSensorName(lightList.get(i).getAttribute("controlSensor").getValue());
                    try {
                        lc.setControlSensorSense(Integer.parseInt(lightList.get(i).getAttribute("sensorSense").getValue()));
                    } catch (NumberFormatException e) {
                        log.error("error when converting control sensor sense in legacy Light load");
                    }
                } else if (type == Light.FAST_CLOCK_CONTROL) {
                    int onHour = 0;
                    int onMin = 0;
                    int offHour = 0;
                    int offMin = 0;
                    try {
                        onHour = Integer.parseInt(lightList.get(i).getAttribute("fastClockOnHour").getValue());
                        onMin = Integer.parseInt(lightList.get(i).getAttribute("fastClockOnMin").getValue());
                        offHour = Integer.parseInt(lightList.get(i).getAttribute("fastClockOffHour").getValue());
                        offMin = Integer.parseInt(lightList.get(i).getAttribute("fastClockOffMin").getValue());
                    } catch (NumberFormatException e) {
                        log.error("error when converting fast clock items in legacy Light load");
                    }
                    lc.setFastClockControlSchedule(onHour, onMin, offHour, offMin);
                } else if (type == Light.TURNOUT_STATUS_CONTROL) {
                    lc.setControlTurnout(lightList.get(i).getAttribute("controlTurnout").getValue());
                    try {
                        lc.setControlTurnoutState(Integer.parseInt(lightList.get(i).getAttribute("turnoutState").getValue()));
                    } catch (NumberFormatException e) {
                        log.error("error when converting turnout state in legacy Light load");
                    }
                } else if (type == Light.TIMED_ON_CONTROL) {
                    lc.setControlTimedOnSensorName(lightList.get(i).getAttribute("timedControlSensor").getValue());
                    try {
                        lc.setTimedOnDuration(Integer.parseInt(lightList.get(i).getAttribute("duration").getValue()));
                    } catch (NumberFormatException e) {
                        log.error("error when converting timed sensor items in legacy Light load");
                    }
                }
                lgt.addLightControl(lc);
            }
        }
        // load lightcontrol children, if any
        List<Element> lightControlList = lightList.get(i).getChildren("lightcontrol");
        for (int n = 0; n < lightControlList.size(); n++) {
            boolean noErrors = true;
            Element elem = lightControlList.get(n);
            LightControl lc = new LightControl(lgt);
            String tem = elem.getAttribute("controlType").getValue();
            int type = Light.NO_CONTROL;
            try {
                type = Integer.parseInt(tem);
                lc.setControlType(type);
            } catch (NumberFormatException e) {
                log.error("error when converting control type while loading light " + sysName);
                noErrors = false;
            }
            if (type == Light.SENSOR_CONTROL) {
                lc.setControlSensorName(elem.getAttribute("controlSensor").getValue());
                try {
                    lc.setControlSensorSense(Integer.parseInt(elem.getAttribute("sensorSense").getValue()));
                } catch (NumberFormatException e) {
                    log.error("error when converting control sensor sense while loading light " + sysName);
                    noErrors = false;
                }
            } else if (type == Light.FAST_CLOCK_CONTROL) {
                int onHour = 0;
                int onMin = 0;
                int offHour = 0;
                int offMin = 0;
                try {
                    onHour = Integer.parseInt(elem.getAttribute("fastClockOnHour").getValue());
                    onMin = Integer.parseInt(elem.getAttribute("fastClockOnMin").getValue());
                    offHour = Integer.parseInt(elem.getAttribute("fastClockOffHour").getValue());
                    offMin = Integer.parseInt(elem.getAttribute("fastClockOffMin").getValue());
                    lc.setFastClockControlSchedule(onHour, onMin, offHour, offMin);
                } catch (NumberFormatException e) {
                    log.error("error when converting fast clock items while loading light " + sysName);
                    noErrors = false;
                }
            } else if (type == Light.TURNOUT_STATUS_CONTROL) {
                lc.setControlTurnout(elem.getAttribute("controlTurnout").getValue());
                try {
                    lc.setControlTurnoutState(Integer.parseInt(elem.getAttribute("turnoutState").getValue()));
                } catch (NumberFormatException e) {
                    log.error("error when converting turnout state while loading light " + sysName);
                    noErrors = false;
                }
            } else if (type == Light.TIMED_ON_CONTROL) {
                lc.setControlTimedOnSensorName(elem.getAttribute("timedControlSensor").getValue());
                try {
                    lc.setTimedOnDuration(Integer.parseInt(elem.getAttribute("duration").getValue()));
                } catch (NumberFormatException e) {
                    log.error("error when converting timed sensor items while loading light " + sysName);
                    noErrors = false;
                }
            } else if (type == Light.TWO_SENSOR_CONTROL) {
                lc.setControlSensorName(elem.getAttribute("controlSensor").getValue());
                lc.setControlSensor2Name(elem.getAttribute("controlSensor2").getValue());
                try {
                    lc.setControlSensorSense(Integer.parseInt(elem.getAttribute("sensorSense").getValue()));
                } catch (NumberFormatException e) {
                    log.error("error when converting control sensor2 sense while loading light " + sysName);
                    noErrors = false;
                }
            }
            if (noErrors) {
                lgt.addLightControl(lc);
            }
        }
        // done, start it working
        lgt.activateLight();
    }
    return result;
}
Also used : Light(jmri.Light) Element(org.jdom2.Element) LightManager(jmri.LightManager) LightControl(jmri.implementation.LightControl)

Example 24 with Parent

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

the class AbstractSignalHeadManagerXml method loadSignalHeads.

/**
     * Utility method to load the individual SignalHead objects. If there's no
     * additional info needed for a specific signal head type, invoke this with
     * the parent of the set of SignalHead elements.
     *
     * @param shared  Element containing the SignalHead elements to load.
     * @param perNode Element containing any per-node information associated
     *                with the shared Element.
     */
public void loadSignalHeads(Element shared, Element perNode) {
    InstanceManager.getDefault(jmri.SignalHeadManager.class);
    // load the contents
    List<Element> items = shared.getChildren();
    if (log.isDebugEnabled()) {
        log.debug("Found " + items.size() + " signal heads");
    }
    for (int i = 0; i < items.size(); i++) {
        // get the class, hence the adapter object to do loading
        Element item = items.get(i);
        String adapterName = item.getAttribute("class").getValue();
        log.debug("load via " + adapterName);
        try {
            XmlAdapter adapter = (XmlAdapter) Class.forName(adapterName).newInstance();
            // and do it
            adapter.load(item, null);
        } catch (Exception e) {
            log.error("Exception while loading {}: {}", item.getName(), e, e);
        }
    }
}
Also used : Element(org.jdom2.Element) XmlAdapter(jmri.configurexml.XmlAdapter)

Example 25 with Parent

use of org.jdom2.Parent 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)

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