Search in sources :

Example 21 with JDOMException

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

the class NameCheckAction method actionPerformed.

@SuppressWarnings("unchecked")
@Override
public void actionPerformed(ActionEvent e) {
    if (fci == null) {
        fci = jmri.jmrit.XmlFile.userFileChooser("XML files", "xml");
    }
    // request the filename from an open dialog
    fci.rescanCurrentDirectory();
    int retVal = fci.showOpenDialog(_who);
    // handle selection or cancel
    if (retVal == JFileChooser.APPROVE_OPTION) {
        File file = fci.getSelectedFile();
        if (log.isDebugEnabled()) {
            log.debug("located file " + file + " for XML processing");
        }
        // handle the file (later should be outside this thread?)
        try {
            Element root = readFile(file);
            if (log.isDebugEnabled()) {
                log.debug("parsing complete");
            }
            // check to see if there's a decoder element
            if (root.getChild("decoder") == null) {
                log.warn("Does not appear to be a decoder file");
                return;
            }
            Iterator<Element> iter = root.getChild("decoder").getChild("variables").getDescendants(new ElementFilter("variable"));
            jmri.jmrit.symbolicprog.NameFile nfile = jmri.jmrit.symbolicprog.NameFile.instance();
            String warnings = "";
            while (iter.hasNext()) {
                Element varElement = iter.next();
                // for each variable, see if can find in names file
                Attribute labelAttr = varElement.getAttribute("label");
                String label = null;
                if (labelAttr != null) {
                    label = labelAttr.getValue();
                }
                Attribute itemAttr = varElement.getAttribute("item");
                String item = null;
                if (itemAttr != null) {
                    item = itemAttr.getValue();
                }
                if (log.isDebugEnabled()) {
                    log.debug("Variable called \"" + ((label != null) ? label : "<none>") + "\" \"" + ((item != null) ? item : "<none>"));
                }
                if (!(label == null ? false : nfile.checkName(label)) && !(item == null ? false : nfile.checkName(item))) {
                    log.warn("Variable not found: label=\"" + ((label != null) ? label : "<none>") + "\" item=\"" + ((item != null) ? label : "<none>") + "\"");
                    warnings += "Variable not found: label=\"" + ((label != null) ? label : "<none>") + "\" item=\"" + ((item != null) ? item : "<none>") + "\"\n";
                }
            }
            if (!warnings.equals("")) {
                JOptionPane.showMessageDialog(_who, warnings);
            } else {
                JOptionPane.showMessageDialog(_who, "No mismatched items found");
            }
        } catch (HeadlessException | IOException | JDOMException ex) {
            JOptionPane.showMessageDialog(_who, "Error parsing decoder file: " + ex);
        }
    } else {
        log.debug("XmlFileCheckAction cancelled in open dialog");
    }
}
Also used : HeadlessException(java.awt.HeadlessException) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) ElementFilter(org.jdom2.filter.ElementFilter) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 22 with JDOMException

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

the class BlockValueFile method readBlockValues.

/*
     *  Reads Block values from a file in the user's preferences directory
     *  If the file containing block values does not exist this routine returns quietly.
     *  If a Block named in the file does not exist currently, that entry is quietly ignored.
     */
@SuppressWarnings("unchecked")
public void readBlockValues() throws org.jdom2.JDOMException, java.io.IOException {
    log.debug("entered readBlockValues");
    List<String> blocks = blockManager.getSystemNameList();
    // check if file exists
    if (checkFile(defaultFileName)) {
        // file is present, 
        root = rootFromName(defaultFileName);
        if ((root != null) && (blocks.size() > 0)) {
            // there is a file and there are Blocks defined
            Element blockvalues = root.getChild("blockvalues");
            if (blockvalues != null) {
                // there are values defined, read and set block values if Block exists.
                List<Element> blockList = blockvalues.getChildren("block");
                for (int i = 0; i < blockList.size(); i++) {
                    if ((blockList.get(i)).getAttribute("systemname") == null) {
                        log.warn("unexpected null in systemName " + blockList.get(i) + " " + blockList.get(i).getAttributes());
                        break;
                    }
                    String sysName = blockList.get(i).getAttribute("systemname").getValue();
                    // get Block - ignore entry if block not found
                    Block b = blockManager.getBySystemName(sysName);
                    if (b != null) {
                        // Block was found, set its value
                        Object v = blockList.get(i).getAttribute("value").getValue();
                        if (blockList.get(i).getAttribute("valueClass") != null) {
                            if (blockList.get(i).getAttribute("valueClass").getValue().equals("jmri.jmrit.roster.RosterEntry")) {
                                jmri.jmrit.roster.RosterEntry re = jmri.jmrit.roster.Roster.getDefault().getEntryForId(((String) v));
                                if (re != null) {
                                    v = re;
                                }
                            }
                        }
                        b.setValue(v);
                        // set direction if there is one
                        int dd = jmri.Path.NONE;
                        Attribute a = blockList.get(i).getAttribute("dir");
                        if (a != null) {
                            try {
                                dd = a.getIntValue();
                            } catch (org.jdom2.DataConversionException e) {
                                log.error("failed to convert direction attribute");
                            }
                        }
                        b.setDirection(dd);
                    }
                }
            }
        }
    }
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) Block(jmri.Block)

Example 23 with JDOMException

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

the class LocationManagerXml method readFile.

/**
     * Read the contents of a roster XML file into this object. Note that this
     * does not clear any existing entries.
     */
@Override
public void readFile(String name) throws org.jdom2.JDOMException, java.io.IOException {
    // suppress rootFromName(name) warning message by checking to see if file exists
    if (findFile(name) == null) {
        log.debug("{} file could not be found", name);
        return;
    }
    // find root
    Element root = rootFromName(name);
    if (root == null) {
        log.debug("{} file could not be read", name);
        return;
    }
    LocationManager.instance().load(root);
    ScheduleManager.instance().load(root);
    setDirty(false);
    log.debug("Locations have been loaded!");
}
Also used : Element(org.jdom2.Element)

Example 24 with JDOMException

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

the class ProfileManager method readProfiles.

private void readProfiles() throws JDOMException, IOException {
    try {
        boolean reWrite = false;
        if (!catalog.exists()) {
            this.writeProfiles();
        }
        if (!catalog.canRead()) {
            return;
        }
        this.readingProfiles = true;
        Document doc = (new SAXBuilder()).build(catalog);
        profiles.clear();
        for (Element e : doc.getRootElement().getChild(PROFILES).getChildren()) {
            File pp = FileUtil.getFile(e.getAttributeValue(Profile.PATH));
            try {
                Profile p = new Profile(pp);
                this.addProfile(p);
            } catch (FileNotFoundException ex) {
                log.info("Cataloged profile \"{}\" not in expected location\nSearching for it in {}", e.getAttributeValue(Profile.ID), pp.getParentFile());
                this.findProfiles(pp.getParentFile());
                reWrite = true;
            }
        }
        searchPaths.clear();
        for (Element e : doc.getRootElement().getChild(SEARCH_PATHS).getChildren()) {
            File path = FileUtil.getFile(e.getAttributeValue(Profile.PATH));
            if (!searchPaths.contains(path)) {
                this.addSearchPath(path);
            }
            if (Boolean.parseBoolean(e.getAttributeValue(DEFAULT))) {
                this.defaultSearchPath = path;
            }
        }
        if (searchPaths.isEmpty()) {
            this.addSearchPath(FileUtil.getFile(FileUtil.getPreferencesPath()));
        }
        this.readingProfiles = false;
        if (reWrite) {
            this.writeProfiles();
        }
        this.profiles.sort(null);
    } catch (JDOMException | IOException ex) {
        this.readingProfiles = false;
        throw ex;
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) File(java.io.File)

Example 25 with JDOMException

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

the class NodeIdentity method init.

private synchronized void init() {
    File identityFile = this.identityFile();
    if (identityFile.exists()) {
        try {
            Document doc = (new SAXBuilder()).build(identityFile);
            String id = doc.getRootElement().getChild(NODE_IDENTITY).getAttributeValue(NODE_IDENTITY);
            this.formerIdentities.clear();
            doc.getRootElement().getChild(FORMER_IDENTITIES).getChildren().stream().forEach((e) -> {
                this.formerIdentities.add(e.getAttributeValue(NODE_IDENTITY));
            });
            if (!this.validateIdentity(id)) {
                log.debug("Node identity {} is invalid. Generating new node identity.", id);
                this.formerIdentities.add(id);
                this.getIdentity(true);
            } else {
                this.getIdentity(true);
            }
        } catch (JDOMException | IOException ex) {
            log.error("Unable to read node identities: {}", ex.getLocalizedMessage());
            this.getIdentity(true);
        }
    } else {
        this.getIdentity(true);
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) File(java.io.File)

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