Search in sources :

Example 11 with JDOMException

use of org.jdom2.JDOMException 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 12 with JDOMException

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

Example 13 with JDOMException

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

the class NameFile method readFile.

/**
     * Read the contents of a NameFile XML file into this object. Note that this
     * does not clear any existing entries.
     */
void readFile(String name) throws org.jdom2.JDOMException, java.io.IOException {
    if (log.isDebugEnabled()) {
        log.debug("readFile " + name);
    }
    // read file, find root
    Element root = rootFromName(name);
    // decode type, invoke proper processing routine
    readNames(root);
}
Also used : Element(org.jdom2.Element)

Example 14 with JDOMException

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

the class LoadXmlThrottlesLayoutAction method loadThrottlesLayout.

/**
     * Parse the XML file and create ThrottleFrames. Returns true if throttle
     * loaded successfully.
     *
     * @param f The XML file containing throttles.
     */
@SuppressWarnings("unchecked")
public boolean loadThrottlesLayout(java.io.File f) throws java.io.IOException {
    try {
        ThrottlePrefs prefs = new ThrottlePrefs();
        Element root = prefs.rootFromFile(f);
        List<Element> throttles = root.getChildren("ThrottleFrame");
        if ((throttles != null) && (throttles.size() > 0)) {
            // OLD FORMAT    
            for (java.util.Iterator<Element> i = throttles.iterator(); i.hasNext(); ) {
                ThrottleFrame tf = ThrottleFrameManager.instance().createThrottleFrame();
                tf.setXml(i.next());
                tf.toFront();
            }
        } else {
            throttles = root.getChildren("ThrottleWindow");
            for (java.util.Iterator<Element> i = throttles.iterator(); i.hasNext(); ) {
                ThrottleWindow tw = ThrottleFrameManager.instance().createThrottleWindow();
                tw.setXml(i.next());
                tw.setVisible(true);
            }
            Element tlp = root.getChild("ThrottlesListPanel");
            if (tlp != null) {
                ThrottleFrameManager.instance().getThrottlesListPanel().setXml(tlp);
            }
        }
    } catch (org.jdom2.JDOMException ex) {
        log.warn("Loading Throttles exception", ex);
        return false;
    }
    return true;
}
Also used : Element(org.jdom2.Element)

Example 15 with JDOMException

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

the class LoadXmlVSDecoderAction method loadVSDecoderProfile.

/**
     * Parse the XML file and create ThrottleFrames. Returns true if throttle
     * loaded successfully.
     *
     * @param f The XML file containing throttles.
     */
public boolean loadVSDecoderProfile(java.io.File f) throws java.io.IOException {
    try {
        VSDecoderPrefs prefs = new VSDecoderPrefs();
        Element root = prefs.rootFromFile(f);
        // WARNING: This may be out of sync with the Store... the root element is <VSDecoderConfig>
        // not sure, must investigate.  See what XmlFile.rootFromFile(f) does...
        List<Element> profiles = root.getChildren("VSDecoder");
        if ((profiles != null) && (profiles.size() > 0)) {
            // Create a new VSDecoder object for each Profile in the XML file.
            for (java.util.Iterator<Element> i = profiles.iterator(); i.hasNext(); ) {
                Element e = i.next();
                log.debug(e.toString());
            //VSDecoder vsd = VSDecoderManager.instance().getVSDecoder(e.getAttribute("name").getValue(), f.getPath());
            }
        }
    } catch (org.jdom2.JDOMException ex) {
        log.warn("Loading VSDecoder Profile exception", ex);
        return false;
    }
    return true;
}
Also used : Element(org.jdom2.Element)

Aggregations

Element (org.jdom2.Element)154 Document (org.jdom2.Document)113 JDOMException (org.jdom2.JDOMException)89 IOException (java.io.IOException)75 SAXBuilder (org.jdom2.input.SAXBuilder)67 Test (org.junit.Test)36 File (java.io.File)32 ArrayList (java.util.ArrayList)22 InputStream (java.io.InputStream)17 Attribute (org.jdom2.Attribute)16 StringReader (java.io.StringReader)15 MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)14 HashMap (java.util.HashMap)13 XMLOutputter (org.jdom2.output.XMLOutputter)13 SAXException (org.xml.sax.SAXException)13 URL (java.net.URL)12 XmlFile (jmri.jmrit.XmlFile)12 List (java.util.List)11 MCRObject (org.mycore.datamodel.metadata.MCRObject)11 MCRException (org.mycore.common.MCRException)10