Search in sources :

Example 86 with Element

use of org.eclipse.xtext.parsetree.impl.bug305397.Element in project JMRI by JMRI.

the class WarrantPreferences method openFile.

public void openFile(String name) {
    _fileName = name;
    WarrantPreferencesXml prefsXml = new WarrantPreferencesXml();
    File file = new File(_fileName);
    Element root;
    try {
        root = prefsXml.rootFromFile(file);
    } catch (java.io.FileNotFoundException ea) {
        log.debug("Could not find Warrant preferences file.  Normal if preferences have not been saved before.");
        root = null;
    } catch (IOException | JDOMException eb) {
        log.error("Exception while loading warrant preferences: " + eb);
        root = null;
    }
    if (root != null) {
        //            log.info("Found Warrant preferences file: {}", _fileName);
        loadLayoutParams(root.getChild(LAYOUT_PARAMS));
        if (!loadSpeedMap(root.getChild(SPEED_MAP_PARAMS))) {
            loadSpeedMapFromOldXml();
            log.error("Unable to read ramp parameters. Setting to default values.");
        }
    } else {
        loadSpeedMapFromOldXml();
    }
}
Also used : Element(org.jdom2.Element) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 87 with Element

use of org.eclipse.xtext.parsetree.impl.bug305397.Element in project JMRI by JMRI.

the class WarrantPreferences method loadSpeedMap.

// Avoid firePropertyChange until SignalSpeedMap is completely loaded
private boolean loadSpeedMap(Element child) {
    if (child == null) {
        return false;
    }
    Element rampParms = child.getChild(STEP_INCREMENTS);
    if (rampParms == null) {
        return false;
    }
    Attribute a;
    if ((a = rampParms.getAttribute(TIME_INCREMENT)) != null) {
        try {
            this._msIncrTime = a.getIntValue();
        } catch (DataConversionException ex) {
            this._msIncrTime = 500;
            log.error("Unable to read ramp time increment. Setting to default value (500ms).", ex);
        }
    }
    if ((a = rampParms.getAttribute(RAMP_INCREMENT)) != null) {
        try {
            this._throttleIncr = a.getFloatValue();
        } catch (DataConversionException ex) {
            this._throttleIncr = 0.03f;
            log.error("Unable to read ramp throttle increment. Setting to default value (0.03).", ex);
        }
    }
    if ((a = rampParms.getAttribute(THROTTLE_SCALE)) != null) {
        try {
            _throttleScale = a.getFloatValue();
        } catch (DataConversionException ex) {
            _throttleScale = .90f;
            log.error("Unable to read throttle scale. Setting to default value (0.90f).", ex);
        }
    }
    rampParms = child.getChild(SPEED_NAME_PREFS);
    if (rampParms == null) {
        return false;
    }
    if ((a = rampParms.getAttribute("percentNormal")) != null) {
        if (a.getValue().equals("yes")) {
            _interpretation = 1;
        } else {
            _interpretation = 2;
        }
    }
    if ((a = rampParms.getAttribute(INTERPRETATION)) != null) {
        try {
            _interpretation = a.getIntValue();
        } catch (DataConversionException ex) {
            _interpretation = 1;
            log.error("Unable to read interpetation of Speed Map. Setting to default value % normal.", ex);
        }
    }
    HashMap<String, Float> map = new LinkedHashMap<>();
    List<Element> list = rampParms.getChildren();
    for (int i = 0; i < list.size(); i++) {
        String name = list.get(i).getName();
        Float speed = 0f;
        try {
            speed = Float.valueOf(list.get(i).getText());
        } catch (NumberFormatException nfe) {
            log.error("Speed names has invalid content for {} = ", name, list.get(i).getText());
        }
        log.debug("Add {}, {} to AspectSpeed Table", name, speed);
        map.put(name, speed);
    }
    // no firePropertyChange
    this.setSpeedNames(map);
    rampParms = child.getChild(APPEARANCE_PREFS);
    if (rampParms == null) {
        return false;
    }
    LinkedHashMap<String, String> heads = new LinkedHashMap<>();
    list = rampParms.getChildren();
    for (int i = 0; i < list.size(); i++) {
        String name = Bundle.getMessage(list.get(i).getName());
        String speed = list.get(i).getText();
        heads.put(name, speed);
    }
    // no firePropertyChange
    this.setAppearances(heads);
    // Now set SignalSpeedMap members.
    SignalSpeedMap speedMap = jmri.InstanceManager.getDefault(SignalSpeedMap.class);
    speedMap.setRampParams(_msIncrTime, _msIncrTime);
    speedMap.setDefaultThrottleFactor(_throttleScale);
    speedMap.setLayoutScale(_scale);
    speedMap.setAspects(new HashMap<>(this._speedNames), _interpretation);
    speedMap.setAppearances(new HashMap<>(this._headAppearances));
    return true;
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) LinkedHashMap(java.util.LinkedHashMap) SignalSpeedMap(jmri.implementation.SignalSpeedMap) DataConversionException(org.jdom2.DataConversionException)

Example 88 with Element

use of org.eclipse.xtext.parsetree.impl.bug305397.Element in project JMRI by JMRI.

the class TrainSwitchListText method load.

public static void load(Element e) {
    Element emts = e.getChild(Xml.SWITCH_LIST_TEXT_STRINGS);
    if (emts == null) {
        return;
    }
    Attribute a;
    if (emts.getChild(Xml.SWICH_LIST_FOR) != null) {
        if ((a = emts.getChild(Xml.SWICH_LIST_FOR).getAttribute(Xml.TEXT)) != null) {
            setStringSwitchListFor(a.getValue());
        }
    }
    if (emts.getChild(Xml.SCHEDULED_WORK_TRAIN) != null) {
        if ((a = emts.getChild(Xml.SCHEDULED_WORK_TRAIN).getAttribute(Xml.TEXT)) != null) {
            setStringScheduledWork(a.getValue());
        }
    }
    if (emts.getChild(Xml.DEPARTS_AT) != null) {
        if ((a = emts.getChild(Xml.DEPARTS_AT).getAttribute(Xml.TEXT)) != null) {
            setStringDepartsAt(a.getValue());
        }
    }
    if (emts.getChild(Xml.DEPARTS_EXPECTED_ARRIVAL) != null) {
        if ((a = emts.getChild(Xml.DEPARTS_EXPECTED_ARRIVAL).getAttribute(Xml.TEXT)) != null) {
            setStringDepartsAtExpectedArrival(a.getValue());
        }
    }
    if (emts.getChild(Xml.DEPARTED_EXPECTED) != null) {
        if ((a = emts.getChild(Xml.DEPARTED_EXPECTED).getAttribute(Xml.TEXT)) != null) {
            setStringDepartedExpected(a.getValue());
        }
    }
    if (emts.getChild(Xml.VISIT_NUMBER) != null) {
        if ((a = emts.getChild(Xml.VISIT_NUMBER).getAttribute(Xml.TEXT)) != null) {
            setStringVisitNumber(a.getValue());
        }
    }
    if (emts.getChild(Xml.VISIT_NUMBER_DEPARTED) != null) {
        if ((a = emts.getChild(Xml.VISIT_NUMBER_DEPARTED).getAttribute(Xml.TEXT)) != null) {
            setStringVisitNumberDeparted(a.getValue());
        }
    }
    if (emts.getChild(Xml.VISIT_NUMBER_TERMINATES) != null) {
        if ((a = emts.getChild(Xml.VISIT_NUMBER_TERMINATES).getAttribute(Xml.TEXT)) != null) {
            setStringVisitNumberTerminates(a.getValue());
        }
    }
    if (emts.getChild(Xml.VISIT_NUMBER_TERMINATES_DEPARTED) != null) {
        if ((a = emts.getChild(Xml.VISIT_NUMBER_TERMINATES_DEPARTED).getAttribute(Xml.TEXT)) != null) {
            setStringVisitNumberTerminatesDeparted(a.getValue());
        }
    }
    if (emts.getChild(Xml.VISIT_NUMBER_DONE) != null) {
        if ((a = emts.getChild(Xml.VISIT_NUMBER_DONE).getAttribute(Xml.TEXT)) != null) {
            setStringVisitNumberDone(a.getValue());
        }
    }
    if (emts.getChild(Xml.TRAIN_DIRECTION_CHANGE) != null) {
        if ((a = emts.getChild(Xml.TRAIN_DIRECTION_CHANGE).getAttribute(Xml.TEXT)) != null) {
            setStringTrainDirectionChange(a.getValue());
        }
    }
    if (emts.getChild(Xml.NO_CAR_PICK_UPS) != null) {
        if ((a = emts.getChild(Xml.NO_CAR_PICK_UPS).getAttribute(Xml.TEXT)) != null) {
            setStringNoCarPickUps(a.getValue());
        }
    }
    if (emts.getChild(Xml.NO_CAR_SET_OUTS) != null) {
        if ((a = emts.getChild(Xml.NO_CAR_SET_OUTS).getAttribute(Xml.TEXT)) != null) {
            setStringNoCarDrops(a.getValue());
        }
    }
    if (emts.getChild(Xml.TRAIN_DONE) != null) {
        if ((a = emts.getChild(Xml.TRAIN_DONE).getAttribute(Xml.TEXT)) != null) {
            setStringTrainDone(a.getValue());
        }
    }
    if (emts.getChild(Xml.TRAIN_DEPARTS_CARS) != null) {
        if ((a = emts.getChild(Xml.TRAIN_DEPARTS_CARS).getAttribute(Xml.TEXT)) != null) {
            setStringTrainDepartsCars(a.getValue());
        }
    }
    if (emts.getChild(Xml.TRAIN_DEPARTS_LOADS) != null) {
        if ((a = emts.getChild(Xml.TRAIN_DEPARTS_LOADS).getAttribute(Xml.TEXT)) != null) {
            setStringTrainDepartsLoads(a.getValue());
        }
    }
    if (emts.getChild(Xml.SWITCH_LIST_TRACK) != null) {
        if ((a = emts.getChild(Xml.SWITCH_LIST_TRACK).getAttribute(Xml.TEXT)) != null) {
            setStringSwitchListByTrack(a.getValue());
        }
    }
    if (emts.getChild(Xml.HOLD_CAR) != null) {
        if ((a = emts.getChild(Xml.HOLD_CAR).getAttribute(Xml.TEXT)) != null) {
            setStringHoldCar(a.getValue());
        }
    }
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element)

Example 89 with Element

use of org.eclipse.xtext.parsetree.impl.bug305397.Element in project JMRI by JMRI.

the class TrainSwitchListText method store.

// must synchronize changes with operation-config.dtd
public static Element store() {
    Element values;
    Element e = new Element(Xml.SWITCH_LIST_TEXT_STRINGS);
    // only save strings that have been modified
    if (!getStringSwitchListFor().equals(Bundle.getMessage("SwitchListFor"))) {
        e.addContent(values = new Element(Xml.SWICH_LIST_FOR));
        values.setAttribute(Xml.TEXT, getStringSwitchListFor());
    }
    if (!getStringScheduledWork().equals(Bundle.getMessage("ScheduledWork"))) {
        e.addContent(values = new Element(Xml.SCHEDULED_WORK_TRAIN));
        values.setAttribute(Xml.TEXT, getStringScheduledWork());
    }
    if (!getStringDepartsAt().equals(Bundle.getMessage("DepartsAt"))) {
        e.addContent(values = new Element(Xml.DEPARTS_AT));
        values.setAttribute(Xml.TEXT, getStringDepartsAt());
    }
    if (!getStringDepartsAtExpectedArrival().equals(Bundle.getMessage("DepartsAtExpectedArrival"))) {
        e.addContent(values = new Element(Xml.DEPARTS_EXPECTED_ARRIVAL));
        values.setAttribute(Xml.TEXT, getStringDepartsAtExpectedArrival());
    }
    if (!getStringDepartedExpected().equals(Bundle.getMessage("DepartedExpected"))) {
        e.addContent(values = new Element(Xml.DEPARTED_EXPECTED));
        values.setAttribute(Xml.TEXT, getStringDepartedExpected());
    }
    if (!getStringVisitNumber().equals(Bundle.getMessage("VisitNumber"))) {
        e.addContent(values = new Element(Xml.VISIT_NUMBER));
        values.setAttribute(Xml.TEXT, getStringVisitNumber());
    }
    if (!getStringVisitNumberDeparted().equals(Bundle.getMessage("VisitNumberDeparted"))) {
        e.addContent(values = new Element(Xml.VISIT_NUMBER_DEPARTED));
        values.setAttribute(Xml.TEXT, getStringVisitNumberDeparted());
    }
    if (!getStringVisitNumberTerminates().equals(Bundle.getMessage("VisitNumberTerminates"))) {
        e.addContent(values = new Element(Xml.VISIT_NUMBER_TERMINATES));
        values.setAttribute(Xml.TEXT, getStringVisitNumberTerminates());
    }
    if (!getStringVisitNumberTerminatesDeparted().equals(Bundle.getMessage("VisitNumberTerminatesDeparted"))) {
        e.addContent(values = new Element(Xml.VISIT_NUMBER_TERMINATES_DEPARTED));
        values.setAttribute(Xml.TEXT, getStringVisitNumberTerminatesDeparted());
    }
    if (!getStringVisitNumberDone().equals(Bundle.getMessage("VisitNumberDone"))) {
        e.addContent(values = new Element(Xml.VISIT_NUMBER_DONE));
        values.setAttribute(Xml.TEXT, getStringVisitNumberDone());
    }
    if (!getStringTrainDirectionChange().equals(Bundle.getMessage("TrainDirectionChange"))) {
        e.addContent(values = new Element(Xml.TRAIN_DIRECTION_CHANGE));
        values.setAttribute(Xml.TEXT, getStringTrainDirectionChange());
    }
    if (!getStringNoCarPickUps().equals(Bundle.getMessage("NoCarPickUps"))) {
        e.addContent(values = new Element(Xml.NO_CAR_PICK_UPS));
        values.setAttribute(Xml.TEXT, getStringNoCarPickUps());
    }
    if (!getStringNoCarDrops().equals(Bundle.getMessage("NoCarDrops"))) {
        e.addContent(values = new Element(Xml.NO_CAR_SET_OUTS));
        values.setAttribute(Xml.TEXT, getStringNoCarDrops());
    }
    if (!getStringTrainDone().equals(Bundle.getMessage("TrainDone"))) {
        e.addContent(values = new Element(Xml.TRAIN_DONE));
        values.setAttribute(Xml.TEXT, getStringTrainDone());
    }
    if (!getStringTrainDepartsCars().equals(Bundle.getMessage("TrainDepartsCars"))) {
        e.addContent(values = new Element(Xml.TRAIN_DEPARTS_CARS));
        values.setAttribute(Xml.TEXT, getStringTrainDepartsCars());
    }
    if (!getStringTrainDepartsLoads().equals(Bundle.getMessage("TrainDepartsLoads"))) {
        e.addContent(values = new Element(Xml.TRAIN_DEPARTS_LOADS));
        values.setAttribute(Xml.TEXT, getStringTrainDepartsLoads());
    }
    if (!getStringSwitchListByTrack().equals(Bundle.getMessage("SwitchListByTrack"))) {
        e.addContent(values = new Element(Xml.SWITCH_LIST_TRACK));
        values.setAttribute(Xml.TEXT, getStringSwitchListByTrack());
    }
    if (!getStringHoldCar().equals(Bundle.getMessage("HoldCar"))) {
        e.addContent(values = new Element(Xml.HOLD_CAR));
        values.setAttribute(Xml.TEXT, getStringHoldCar());
    }
    return e;
}
Also used : Element(org.jdom2.Element)

Example 90 with Element

use of org.eclipse.xtext.parsetree.impl.bug305397.Element in project JMRI by JMRI.

the class Roster method readFile.

/**
     * Read the contents of a roster XML file into this object.
     * <P>
     * Note that this does not clear any existing entries.
     *
     * @param name filename of roster file
     */
void readFile(String name) throws org.jdom2.JDOMException, java.io.IOException {
    // roster exists?  
    if (!(new File(name)).exists()) {
        log.debug("no roster file found; this is normal if you haven't put decoders in your roster yet");
        return;
    }
    // find root
    Element root = rootFromName(name);
    if (root == null) {
        log.error("Roster file exists, but could not be read; roster not available");
        return;
    }
    // decode type, invoke proper processing routine if a decoder file
    if (root.getChild("roster") != null) {
        // NOI18N
        // NOI18N
        List<Element> l = root.getChild("roster").getChildren("locomotive");
        if (log.isDebugEnabled()) {
            log.debug("readFile sees " + l.size() + " children");
        }
        l.stream().forEach((e) -> {
            addEntry(new RosterEntry(e));
        });
        //any <?p?> processor directives and change them to back \n characters
        synchronized (_list) {
            _list.stream().map((entry) -> {
                //Extract the Comment field and create a new string for output
                String tempComment = entry.getComment();
                String xmlComment = "";
                //characters in tempComment.
                for (int k = 0; k < tempComment.length(); k++) {
                    if (tempComment.startsWith("<?p?>", k)) {
                        // NOI18N
                        // NOI18N
                        xmlComment = xmlComment + "\n";
                        k = k + 4;
                    } else {
                        xmlComment = xmlComment + tempComment.substring(k, k + 1);
                    }
                }
                entry.setComment(xmlComment);
                return entry;
            }).forEachOrdered((r) -> {
                //Now do the same thing for the decoderComment field
                String tempDecoderComment = r.getDecoderComment();
                String xmlDecoderComment = "";
                for (int k = 0; k < tempDecoderComment.length(); k++) {
                    if (tempDecoderComment.startsWith("<?p?>", k)) {
                        // NOI18N
                        // NOI18N
                        xmlDecoderComment = xmlDecoderComment + "\n";
                        k = k + 4;
                    } else {
                        xmlDecoderComment = xmlDecoderComment + tempDecoderComment.substring(k, k + 1);
                    }
                }
                r.setDecoderComment(xmlDecoderComment);
            });
        }
    } else {
        log.error("Unrecognized roster file contents in file: " + name);
    }
    if (root.getChild("rosterGroup") != null) {
        // NOI18N
        // NOI18N
        List<Element> groups = root.getChild("rosterGroup").getChildren("group");
        groups.stream().forEach((group) -> {
            addRosterGroup(group.getText());
        });
    }
}
Also used : FileUtilSupport(jmri.util.FileUtilSupport) RosterGroup(jmri.jmrit.roster.rostergroup.RosterGroup) ProcessingInstruction(org.jdom2.ProcessingInstruction) LoggerFactory(org.slf4j.LoggerFactory) RosterGroupSelector(jmri.jmrit.roster.rostergroup.RosterGroupSelector) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) SymbolicProgBundle(jmri.jmrit.symbolicprog.SymbolicProgBundle) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) Locale(java.util.Locale) HeadlessException(java.awt.HeadlessException) Nonnull(javax.annotation.Nonnull) PropertyChangeEvent(java.beans.PropertyChangeEvent) InstanceManager(jmri.InstanceManager) UserPreferencesManager(jmri.UserPreferencesManager) Logger(org.slf4j.Logger) PropertyChangeProvider(jmri.beans.PropertyChangeProvider) Set(java.util.Set) IOException(java.io.IOException) JOptionPane(javax.swing.JOptionPane) File(java.io.File) List(java.util.List) PropertyChangeListener(java.beans.PropertyChangeListener) FileUtil(jmri.util.FileUtil) PropertyChangeSupport(java.beans.PropertyChangeSupport) Collections(java.util.Collections) XmlFile(jmri.jmrit.XmlFile) Element(org.jdom2.Element) Element(org.jdom2.Element) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Aggregations

Element (org.jdom2.Element)829 Attribute (org.jdom2.Attribute)76 Document (org.jdom2.Document)75 Test (org.junit.Test)70 File (java.io.File)53 ArrayList (java.util.ArrayList)45 JDOMException (org.jdom2.JDOMException)37 IOException (java.io.IOException)34 HashMap (java.util.HashMap)28 NamedIcon (jmri.jmrit.catalog.NamedIcon)27 List (java.util.List)26 XmlFile (jmri.jmrit.XmlFile)24 SAXBuilder (org.jdom2.input.SAXBuilder)21 Turnout (jmri.Turnout)20 DataConversionException (org.jdom2.DataConversionException)20 DocType (org.jdom2.DocType)19 Editor (jmri.jmrit.display.Editor)18 XMLOutputter (org.jdom2.output.XMLOutputter)18 Namespace (org.jdom2.Namespace)17 Point (java.awt.Point)15