Search in sources :

Example 6 with JDOMException

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

the class SchemaTestBase method validate.

@Test
public void validate() {
    Assume.assumeFalse("Ignoring schema validation.", Boolean.getBoolean("jmri.skipschematests"));
    XmlFile.setDefaultValidate(XmlFile.Validate.CheckDtdThenSchema);
    XmlFile xf = new XmlFileImpl();
    try {
        xf.rootFromFile(file);
        if (!this.pass) {
            Assert.fail("Validation of \"" + file.getPath() + "\" should have failed");
        }
    } catch (IOException | JDOMException ex) {
        // throw unexpected errors
        if (this.pass) {
            Assert.fail("Failed to validate \"" + file.getPath() + "\" due to: " + ex);
        }
    }
}
Also used : XmlFile(jmri.jmrit.XmlFile) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) Test(org.junit.Test)

Example 7 with JDOMException

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

the class DecoderIndexFile method readFile.

/**
     * Read the contents of a decoderIndex XML file into this object. Note that
     * this does not clear any existing entries; reset the instance to do that.
     *
     * @param name the name of the decoder index file
     * @throws org.jdom2.JDOMException if unable to parse to decoder index file
     * @throws java.io.IOException     if unable to read decoder index file
     */
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 if a decoder file
    if (root.getChild("decoderIndex") != null) {
        if (root.getChild("decoderIndex").getAttribute("version") != null) {
            fileVersion = Integer.parseInt(root.getChild("decoderIndex").getAttribute("version").getValue());
        }
        log.debug("found fileVersion of " + fileVersion);
        readMfgSection(root.getChild("decoderIndex"));
        readFamilySection(root.getChild("decoderIndex"));
    } else {
        log.error("Unrecognized decoderIndex file contents in file: " + name);
    }
}
Also used : Element(org.jdom2.Element)

Example 8 with JDOMException

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

the class DecoderIndexFile method writeFile.

public void writeFile(String name, DecoderIndexFile oldIndex, String[] files) throws java.io.IOException {
    if (log.isDebugEnabled()) {
        log.debug("writeFile " + name);
    }
    // This is taken in large part from "Java and XML" page 368
    File file = new File(FileUtil.getUserFilesPath() + name);
    // create root element and document
    Element root = new Element("decoderIndex-config");
    root.setAttribute("noNamespaceSchemaLocation", "http://jmri.org/xml/schema/decoder.xsd", org.jdom2.Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"));
    Document doc = newDocument(root);
    // add XSLT processing instruction
    // <?xml-stylesheet type="text/xsl" href="XSLT/DecoderID.xsl"?>
    java.util.Map<String, String> m = new java.util.HashMap<>();
    m.put("type", "text/xsl");
    m.put("href", xsltLocation + "DecoderID.xsl");
    ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
    doc.addContent(0, p);
    // add top-level elements
    Element index;
    root.addContent(index = new Element("decoderIndex"));
    index.setAttribute("version", Integer.toString(fileVersion));
    log.debug("version written to file as " + fileVersion);
    // add mfg list from existing DecoderIndexFile item
    Element mfgList = new Element("mfgList");
    // copy dates from original mfgList element
    if (oldIndex.nmraListDate != null) {
        mfgList.setAttribute("nmraListDate", oldIndex.nmraListDate);
    }
    if (oldIndex.updated != null) {
        mfgList.setAttribute("updated", oldIndex.updated);
    }
    if (oldIndex.lastAdd != null) {
        mfgList.setAttribute("lastadd", oldIndex.lastAdd);
    }
    // We treat "NMRA" special...
    Element mfg = new Element("manufacturer");
    mfg.setAttribute("mfg", "NMRA");
    mfg.setAttribute("mfgID", "999");
    mfgList.addContent(mfg);
    // start working on the rest of the entries
    List<String> keys = new ArrayList<>(oldIndex._mfgIdFromNameHash.keySet());
    Collections.sort(keys);
    for (Object item : keys) {
        String mfgName = (String) item;
        if (!mfgName.equals("NMRA")) {
            mfg = new Element("manufacturer");
            mfg.setAttribute("mfg", mfgName);
            mfg.setAttribute("mfgID", oldIndex._mfgIdFromNameHash.get(mfgName));
            mfgList.addContent(mfg);
        }
    }
    // add family list by scanning files
    Element familyList = new Element("familyList");
    for (String fileName : files) {
        DecoderFile d = new DecoderFile();
        try {
            Element droot = d.rootFromName(DecoderFile.fileLocation + fileName);
            Element family = droot.getChild("decoder").getChild("family").clone();
            family.setAttribute("file", fileName);
            familyList.addContent(family);
        } catch (org.jdom2.JDOMException exj) {
            log.error("could not parse " + fileName + ": " + exj.getMessage());
        } catch (java.io.FileNotFoundException exj) {
            log.error("could not read " + fileName + ": " + exj.getMessage());
        } catch (IOException exj) {
            log.error("other exception while dealing with " + fileName + ": " + exj.getMessage());
        }
    }
    index.addContent(mfgList);
    index.addContent(familyList);
    writeXML(file, doc);
    // force a read of the new file next time
    resetInstance();
}
Also used : HashMap(java.util.HashMap) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) File(java.io.File) XmlFile(jmri.jmrit.XmlFile) ProcessingInstruction(org.jdom2.ProcessingInstruction)

Example 9 with JDOMException

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

the class ConsistFile method readFile.

/**
     * Read all consists from a file.
     *
     * @param fileName path to file
     * @throws org.jdom2.JDOMException if unable to parse consists
     * @throws java.io.IOException     if unable to read file
     */
@SuppressWarnings("unchecked")
public void readFile(String fileName) throws JDOMException, IOException {
    if (checkFile(fileName)) {
        Element root = rootFromName(fileName);
        Element roster;
        if (root == null) {
            log.warn("consist file could not be read");
            return;
        }
        roster = root.getChild("roster");
        if (roster == null) {
            log.debug("consist file does not contain a roster entry");
            return;
        }
        Iterator<Element> consistIterator = root.getDescendants(new ElementFilter("consist"));
        try {
            Element consist;
            do {
                consist = consistIterator.next();
                consistFromXml(consist);
            } while (consistIterator.hasNext());
        } catch (NoSuchElementException nde) {
            log.debug("end of consist list");
        }
    } else {
        log.info("Consist file does not exist.  One will be created if necessary.");
    }
}
Also used : Element(org.jdom2.Element) ElementFilter(org.jdom2.filter.ElementFilter) NoSuchElementException(java.util.NoSuchElementException)

Example 10 with JDOMException

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

the class OptionsFile method readDispatcherOptions.

/*
     *  Reads Dispatcher Options from a file in the user's preferences directory
     *  If the file containing Dispatcher Options does not exist this routine returns quietly.
     */
public void readDispatcherOptions(DispatcherFrame f) throws org.jdom2.JDOMException, java.io.IOException {
    // check if file exists
    if (checkFile(defaultFileName)) {
        // file is present, 
        log.debug("Reading Dispatcher options from file {}", defaultFileName);
        root = rootFromName(defaultFileName);
        dispatcher = f;
        if (root != null) {
            // there is a file
            Element options = root.getChild("options");
            if (options != null) {
                // there are options defined, read and set Dispatcher options
                if (options.getAttribute("lename") != null) {
                    // there is a layout editor name selected
                    String leName = options.getAttribute("lename").getValue();
                    // get list of Layout Editor panels
                    ArrayList<LayoutEditor> layoutEditorList = jmri.jmrit.display.PanelMenu.instance().getLayoutEditorPanelList();
                    if (layoutEditorList.size() == 0) {
                        log.warn("Dispatcher options specify a Layout Editor panel that is not present.");
                    } else {
                        boolean found = false;
                        for (int i = 0; i < layoutEditorList.size(); i++) {
                            if (leName.equals(layoutEditorList.get(i).getTitle())) {
                                found = true;
                                dispatcher.setLayoutEditor(layoutEditorList.get(i));
                            }
                        }
                        if (!found) {
                            log.warn("Layout Editor panel - " + leName + " - not found.");
                        }
                    }
                }
                if (options.getAttribute("usesignaltype") != null) {
                    dispatcher.setSignalType(0x00);
                    if (options.getAttribute("usesignaltype").getValue().equals("signalmast")) {
                        dispatcher.setSignalType(0x01);
                    }
                }
                if (options.getAttribute("useconnectivity") != null) {
                    dispatcher.setUseConnectivity(true);
                    if (options.getAttribute("useconnectivity").getValue().equals("no")) {
                        dispatcher.setUseConnectivity(false);
                    }
                }
                if (options.getAttribute("trainsfromroster") != null) {
                    dispatcher.setTrainsFromRoster(true);
                    if (options.getAttribute("trainsfromroster").getValue().equals("no")) {
                        dispatcher.setTrainsFromRoster(false);
                    }
                }
                if (options.getAttribute("trainsfromtrains") != null) {
                    dispatcher.setTrainsFromTrains(true);
                    if (options.getAttribute("trainsfromtrains").getValue().equals("no")) {
                        dispatcher.setTrainsFromTrains(false);
                    }
                }
                if (options.getAttribute("trainsfromuser") != null) {
                    dispatcher.setTrainsFromUser(true);
                    if (options.getAttribute("trainsfromuser").getValue().equals("no")) {
                        dispatcher.setTrainsFromUser(false);
                    }
                }
                if (options.getAttribute("autoallocate") != null) {
                    dispatcher.setAutoAllocate(false);
                    if (options.getAttribute("autoallocate").getValue().equals("yes")) {
                        dispatcher.setAutoAllocate(true);
                    }
                }
                if (options.getAttribute("autoturnouts") != null) {
                    dispatcher.setAutoTurnouts(true);
                    if (options.getAttribute("autoturnouts").getValue().equals("no")) {
                        dispatcher.setAutoTurnouts(false);
                    }
                }
                if (options.getAttribute("trustknownturnouts") != null) {
                    dispatcher.setTrustKnownTurnouts(false);
                    if (options.getAttribute("trustknownturnouts").getValue().equals("yes")) {
                        dispatcher.setTrustKnownTurnouts(true);
                    }
                }
                if (options.getAttribute("minthrottleinterval") != null) {
                    String s = (options.getAttribute("minthrottleinterval")).getValue();
                    dispatcher.setMinThrottleInterval(Integer.parseInt(s));
                }
                if (options.getAttribute("fullramptime") != null) {
                    String s = (options.getAttribute("fullramptime")).getValue();
                    dispatcher.setFullRampTime(Integer.parseInt(s));
                }
                if (options.getAttribute("hasoccupancydetection") != null) {
                    dispatcher.setHasOccupancyDetection(true);
                    if (options.getAttribute("hasoccupancydetection").getValue().equals("no")) {
                        dispatcher.setHasOccupancyDetection(false);
                    }
                }
                if (options.getAttribute("shortactivetrainnames") != null) {
                    dispatcher.setShortActiveTrainNames(true);
                    if (options.getAttribute("shortactivetrainnames").getValue().equals("no")) {
                        dispatcher.setShortActiveTrainNames(false);
                    }
                }
                if (options.getAttribute("shortnameinblock") != null) {
                    dispatcher.setShortNameInBlock(true);
                    if (options.getAttribute("shortnameinblock").getValue().equals("no")) {
                        dispatcher.setShortNameInBlock(false);
                    }
                }
                if (options.getAttribute("extracolorforallocated") != null) {
                    dispatcher.setExtraColorForAllocated(true);
                    if (options.getAttribute("extracolorforallocated").getValue().equals("no")) {
                        dispatcher.setExtraColorForAllocated(false);
                    }
                }
                if (options.getAttribute("nameinallocatedblock") != null) {
                    dispatcher.setNameInAllocatedBlock(true);
                    if (options.getAttribute("nameinallocatedblock").getValue().equals("no")) {
                        dispatcher.setNameInAllocatedBlock(false);
                    }
                }
                if (options.getAttribute("supportvsdecoder") != null) {
                    dispatcher.setSupportVSDecoder(true);
                    if (options.getAttribute("supportvsdecoder").getValue().equals("no")) {
                        dispatcher.setSupportVSDecoder(false);
                    }
                }
                if (options.getAttribute("layoutscale") != null) {
                    String s = (options.getAttribute("layoutscale")).getValue();
                    for (int i = 1; i <= Scale.NUM_SCALES; i++) {
                        if (Scale.getShortScaleID(i).equals(s)) {
                            dispatcher.setScale(i);
                        }
                    }
                }
                if (options.getAttribute("usescalemeters") != null) {
                    dispatcher.setUseScaleMeters(true);
                    if (options.getAttribute("usescalemeters").getValue().equals("no")) {
                        dispatcher.setUseScaleMeters(false);
                    }
                }
                if (options.getAttribute("userosterentryinblock") != null) {
                    dispatcher.setRosterEntryInBlock(false);
                    if (options.getAttribute("userosterentryinblock").getValue().equals("yes")) {
                        dispatcher.setRosterEntryInBlock(true);
                    }
                }
            }
        }
    } else {
        log.debug("No Dispatcher options file found at {}, using defaults", defaultFileName);
    }
}
Also used : LayoutEditor(jmri.jmrit.display.layoutEditor.LayoutEditor) 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