Search in sources :

Example 51 with Element

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

the class XmlFile method addDefaultInfo.

/**
     * Add default information to the XML before writing it out.
     * <P>
     * Currently, this is identification information as an XML comment. This
     * includes: <UL>
     * <LI>The JMRI version used <LI>Date of writing <LI>A CVS id string, in
     * case the file gets checked in or out </UL>
     * <P>
     * It may be necessary to extend this to check whether the info is already
     * present, e.g. if re-writing a file.
     *
     * @param root The root element of the document that will be written.
     */
public static void addDefaultInfo(Element root) {
    String content = "Written by JMRI version " + jmri.Version.name() + " on " + (new Date()).toString() + " $Id$";
    Comment comment = new Comment(content);
    root.addContent(comment);
}
Also used : Comment(org.jdom2.Comment) Date(java.util.Date)

Example 52 with Element

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

the class XmlFile method getRoot.

/**
     * Get the root element from an XML document in a stream.
     *
     * @param stream input containing the XML document
     * @return the root element of the XML document
     * @throws org.jdom2.JDOMException if the XML document is invalid
     * @throws java.io.IOException     if the input cannot be read
     */
protected Element getRoot(InputStream stream) throws JDOMException, IOException {
    log.trace("getRoot from stream");
    SAXBuilder builder = getBuilder(getValidate());
    Document doc = builder.build(new BufferedInputStream(stream));
    // handle any process instructions
    doc = processInstructions(doc);
    // find root
    return doc.getRootElement();
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) BufferedInputStream(java.io.BufferedInputStream) Document(org.jdom2.Document)

Example 53 with Element

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

the class XmlFile method getRoot.

/**
     * Get the root element from an XML document in a Reader.
     *
     * Runs through a BufferedReader for increased performance.
     *
     *
     * @param verifySchema true if the XML document should be validated against
     *                     its schema
     * @param verifyDTD    true if the XML document should be validated against
     *                     its DTD
     * @param reader       input containing the XML document
     * @return the root element of the XML document
     * @throws org.jdom2.JDOMException if the XML document is invalid
     * @throws java.io.IOException     if the input cannot be read
     * @since 3.1.5
     * @deprecated 4.7.2 use setVerifySchema, setVerifyDTD methods
     */
@Deprecated
protected Element getRoot(boolean verifySchema, boolean verifyDTD, InputStreamReader reader) throws JDOMException, IOException {
    warnDeprecated();
    log.trace("getRoot from reader with encoding {}", reader.getEncoding());
    // argument controls validation
    SAXBuilder builder = getBuilder(getValidate());
    Document doc = builder.build(new BufferedReader(reader));
    // handle any process instructions
    doc = processInstructions(doc);
    // find root
    return doc.getRootElement();
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) BufferedReader(java.io.BufferedReader) Document(org.jdom2.Document)

Example 54 with Element

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

the class DefaultCatalogTreeManagerXml method loadNode.

/**
     * Recursively load a CatalogTree.
     *
     * @param element element containing the node to load
     * @param parent  the parent node of the node in element
     * @param model   the tree model containing the tree to add the node to
     */
public void loadNode(Element element, CatalogTreeNode parent, DefaultTreeModel model) {
    List<Element> nodeList = element.getChildren("node");
    if (log.isDebugEnabled()) {
        log.debug("Found " + nodeList.size() + " CatalogTreeNode objects");
    }
    for (int i = 0; i < nodeList.size(); i++) {
        Element elem = nodeList.get(i);
        Attribute attr = elem.getAttribute("nodeName");
        if (attr == null) {
            log.warn("unexpected null nodeName. elem= " + elem + ", attrs= " + elem.getAttributes());
            continue;
        }
        String nodeName = attr.getValue();
        CatalogTreeNode n = new CatalogTreeNode(nodeName);
        addLeaves(elem, n);
        model.insertNodeInto(n, parent, parent.getChildCount());
        loadNode(elem, n, model);
    }
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) CatalogTreeNode(jmri.jmrit.catalog.CatalogTreeNode)

Example 55 with Element

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

the class TrainInfoFile method writeTrainInfo.

/*
     *  Writes out Dispatcher options to a file in the user's preferences directory
     */
public void writeTrainInfo(TrainInfo tf, String name) throws java.io.IOException {
    log.debug("entered writeTrainInfo");
    root = new Element("traininfofile");
    doc = newDocument(root, dtdLocation + "dispatcher-traininfo.dtd");
    // add XSLT processing instruction
    // <?xml-stylesheet type="text/xsl" href="XSLT/block-values.xsl"?>
    java.util.Map<String, String> m = new java.util.HashMap<>();
    m.put("type", "text/xsl");
    m.put("href", xsltLocation + "dispatcher-traininfo.xsl");
    org.jdom2.ProcessingInstruction p = new org.jdom2.ProcessingInstruction("xml-stylesheet", m);
    doc.addContent(0, p);
    // save Dispatcher TrainInfo in xml format
    Element traininfo = new Element("traininfo");
    traininfo.setAttribute("transitname", tf.getTransitName());
    traininfo.setAttribute("trainname", tf.getTrainName());
    traininfo.setAttribute("dccaddress", tf.getDCCAddress());
    traininfo.setAttribute("trainintransit", "" + (tf.getTrainInTransit() ? "yes" : "no"));
    traininfo.setAttribute("startblockname", tf.getStartBlockName());
    traininfo.setAttribute("endblockname", tf.getDestinationBlockName());
    traininfo.setAttribute("trainfromroster", "" + (tf.getTrainFromRoster() ? "yes" : "no"));
    traininfo.setAttribute("trainfromtrains", "" + (tf.getTrainFromTrains() ? "yes" : "no"));
    traininfo.setAttribute("trainfromuser", "" + (tf.getTrainFromUser() ? "yes" : "no"));
    traininfo.setAttribute("priority", Integer.toString(tf.getPriority()));
    traininfo.setAttribute("resetwhendone", "" + (tf.getResetWhenDone() ? "yes" : "no"));
    switch(tf.getDelayedRestart()) {
        case ActiveTrain.SENSORDELAY:
            traininfo.setAttribute("delayedrestart", "sensor");
            traininfo.setAttribute("delayedrestartsensor", tf.getRestartSensorName());
            break;
        case ActiveTrain.TIMEDDELAY:
            traininfo.setAttribute("delayedrestart", "timed");
            traininfo.setAttribute("delayedrestarttime", Integer.toString(tf.getRestartDelayMin()));
            break;
        default:
            traininfo.setAttribute("delayedrestart", "no");
            break;
    }
    traininfo.setAttribute("reverseatend", "" + (tf.getReverseAtEnd() ? "yes" : "no"));
    if (tf.getDelayedStart() == ActiveTrain.TIMEDDELAY) {
        traininfo.setAttribute("delayedstart", "timed");
    } else if (tf.getDelayedStart() == ActiveTrain.SENSORDELAY) {
        traininfo.setAttribute("delayedstart", "sensor");
        if (tf.getDelaySensorName() != null) {
            traininfo.setAttribute("delayedSensor", tf.getDelaySensorName());
        }
    }
    traininfo.setAttribute("terminatewhendone", (tf.getTerminateWhenDone() ? "yes" : "no"));
    traininfo.setAttribute("departuretimehr", Integer.toString(tf.getDepartureTimeHr()));
    traininfo.setAttribute("departuretimemin", Integer.toString(tf.getDepartureTimeMin()));
    traininfo.setAttribute("traintype", tf.getTrainType());
    traininfo.setAttribute("autorun", "" + (tf.getAutoRun() ? "yes" : "no"));
    traininfo.setAttribute("loadatstartup", "" + (tf.getLoadAtStartup() ? "yes" : "no"));
    traininfo.setAttribute("allocatealltheway", "" + (tf.getAllocateAllTheWay() ? "yes" : "no"));
    // here save items related to automatically running active trains
    traininfo.setAttribute("speedfactor", Float.toString(tf.getSpeedFactor()));
    traininfo.setAttribute("maxspeed", Float.toString(tf.getMaxSpeed()));
    traininfo.setAttribute("ramprate", tf.getRampRate());
    traininfo.setAttribute("resistancewheels", "" + (tf.getResistanceWheels() ? "yes" : "no"));
    traininfo.setAttribute("runinreverse", "" + (tf.getRunInReverse() ? "yes" : "no"));
    traininfo.setAttribute("sounddecoder", "" + (tf.getSoundDecoder() ? "yes" : "no"));
    traininfo.setAttribute("maxtrainlength", Float.toString(tf.getMaxTrainLength()));
    root.addContent(traininfo);
    // write out the file
    try {
        if (!checkFile(fileLocation + name)) {
            // file does not exist, create it
            File file = new File(fileLocation + name);
            if (// create file and check result
            !file.createNewFile()) {
                log.error("createNewFile failed");
            }
        }
        // write content to file
        writeXML(findFile(fileLocation + name), doc);
    } catch (java.io.IOException ioe) {
        log.error("IO Exception " + ioe);
        throw (ioe);
    }
}
Also used : Element(org.jdom2.Element) File(java.io.File)

Aggregations

Element (org.jdom2.Element)673 Attribute (org.jdom2.Attribute)103 Document (org.jdom2.Document)64 File (java.io.File)49 ArrayList (java.util.ArrayList)36 NamedIcon (jmri.jmrit.catalog.NamedIcon)28 IOException (java.io.IOException)27 DataConversionException (org.jdom2.DataConversionException)26 JDOMException (org.jdom2.JDOMException)26 XmlFile (jmri.jmrit.XmlFile)24 Test (org.junit.Test)24 Editor (jmri.jmrit.display.Editor)22 DocType (org.jdom2.DocType)21 Turnout (jmri.Turnout)20 ProcessingInstruction (org.jdom2.ProcessingInstruction)16 Point (java.awt.Point)15 HashMap (java.util.HashMap)15 SignalHead (jmri.SignalHead)15 LayoutEditor (jmri.jmrit.display.layoutEditor.LayoutEditor)15 Dimension (java.awt.Dimension)14