Search in sources :

Example 11 with Comment

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

the class BlockBossLogicXml method store.

/**
     * Default implementation for storing the contents of all the BLockBossLogic
     * elements.
     * <P>
     * Static members in the BlockBossLogic class record the complete set of
     * items. This function writes those out as a single XML element.
     *
     * @param o Object to start process, but not actually used
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    Enumeration<BlockBossLogic> e = BlockBossLogic.entries();
    if (!e.hasMoreElements()) {
        // nothing to write!
        return null;
    }
    Element blocks = new Element("signalelements");
    blocks.setAttribute("class", this.getClass().getName());
    while (e.hasMoreElements()) {
        BlockBossLogic p = e.nextElement();
        Element block = new Element("signalelement");
        block.setAttribute("signal", p.getDrivenSignal());
        block.setAttribute("mode", "" + p.getMode());
        if (p.getApproachSensor1() != null) {
            block.setAttribute("approachsensor1", p.getApproachSensor1());
        }
        if (p.getSensor1() != null) {
            block.addContent(storeSensor(p.getSensor1()));
        }
        if (p.getSensor2() != null) {
            block.addContent(storeSensor(p.getSensor2()));
        }
        if (p.getSensor3() != null) {
            block.addContent(storeSensor(p.getSensor3()));
        }
        if (p.getSensor4() != null) {
            block.addContent(storeSensor(p.getSensor4()));
        }
        if (p.getSensor5() != null) {
            block.addContent(storeSensor(p.getSensor5()));
        }
        if (p.getTurnout() != null) {
            block.setAttribute("watchedturnout", p.getTurnout());
        }
        if (p.getWatchedSignal1() != null) {
            block.setAttribute("watchedsignal1", p.getWatchedSignal1());
        }
        if (p.getWatchedSignal1Alt() != null) {
            block.setAttribute("watchedsignal1alt", p.getWatchedSignal1Alt());
        }
        if (p.getWatchedSignal2() != null) {
            block.setAttribute("watchedsignal2", p.getWatchedSignal2());
        }
        if (p.getWatchedSignal2Alt() != null) {
            block.setAttribute("watchedsignal2alt", p.getWatchedSignal2Alt());
        }
        if (p.getWatchedSensor1() != null) {
            block.setAttribute("watchedsensor1", p.getWatchedSensor1());
        }
        if (p.getWatchedSensor1Alt() != null) {
            block.setAttribute("watchedsensor1alt", p.getWatchedSensor1Alt());
        }
        if (p.getWatchedSensor2() != null) {
            block.setAttribute("watchedsensor2", p.getWatchedSensor2());
        }
        if (p.getWatchedSensor2Alt() != null) {
            block.setAttribute("watchedsensor2alt", p.getWatchedSensor2Alt());
        }
        block.setAttribute("limitspeed1", "" + p.getLimitSpeed1());
        block.setAttribute("limitspeed2", "" + p.getLimitSpeed2());
        block.setAttribute("useflashyellow", "" + p.getUseFlash());
        block.setAttribute("distantsignal", "" + p.getDistantSignal());
        // add comment, if present
        if (p.getComment() != null) {
            Element c = new Element("comment");
            c.addContent(p.getComment());
            block.addContent(c);
        }
        blocks.addContent(block);
    }
    return blocks;
}
Also used : BlockBossLogic(jmri.jmrit.blockboss.BlockBossLogic) Element(org.jdom2.Element)

Example 12 with Comment

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

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

the class Roster method writeFile.

/**
     * Write the entire roster to a file object. This does not do backup; that
     * has to be done separately. See writeRosterFile() for a public function
     * that finds the default location, does a backup and then calls this.
     *
     * @param file an op
     */
void writeFile(File file) throws java.io.IOException {
    // create root element
    // NOI18N
    Element root = new Element("roster-config");
    // NOI18N
    root.setAttribute(// NOI18N
    "noNamespaceSchemaLocation", // NOI18N
    "http://jmri.org/xml/schema/roster" + schemaVersion + ".xsd", // NOI18N
    org.jdom2.Namespace.getNamespace(// NOI18N
    "xsi", // NOI18N
    "http://www.w3.org/2001/XMLSchema-instance"));
    Document doc = newDocument(root);
    // add XSLT processing instruction
    // <?xml-stylesheet type="text/xsl" href="XSLT/roster.xsl"?>
    java.util.Map<String, String> m = new java.util.HashMap<>();
    // NOI18N
    m.put("type", "text/xsl");
    // NOI18N
    m.put("href", xsltLocation + "roster2array.xsl");
    // NOI18N
    ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
    doc.addContent(0, p);
    String newLocoString = SymbolicProgBundle.getMessage("LabelNewDecoder");
    //file version for writing
    synchronized (_list) {
        _list.forEach((entry) -> {
            //back when the file is read.
            if (!entry.getId().equals(newLocoString)) {
                String tempComment = entry.getComment();
                String xmlComment = "";
                //when \n is found.  In that case, insert <?p?>
                for (int k = 0; k < tempComment.length(); k++) {
                    if (tempComment.startsWith("\n", k)) {
                        // NOI18N
                        // NOI18N
                        xmlComment = xmlComment + "<?p?>";
                    } else {
                        xmlComment = xmlComment + tempComment.substring(k, k + 1);
                    }
                }
                entry.setComment(xmlComment);
                //Now do the same thing for the decoderComment field
                String tempDecoderComment = entry.getDecoderComment();
                String xmlDecoderComment = "";
                for (int k = 0; k < tempDecoderComment.length(); k++) {
                    if (tempDecoderComment.startsWith("\n", k)) {
                        // NOI18N
                        // NOI18N
                        xmlDecoderComment = xmlDecoderComment + "<?p?>";
                    } else {
                        xmlDecoderComment = xmlDecoderComment + tempDecoderComment.substring(k, k + 1);
                    }
                }
                entry.setDecoderComment(xmlDecoderComment);
            } else {
                log.debug("skip unsaved roster entry with default name " + entry.getId());
            }
        });
    //All Comments and Decoder Comment line feeds have been changed to processor directives
    }
    // add top-level elements
    // NOI18N
    Element values = new Element("roster");
    root.addContent(values);
    // add entries
    synchronized (_list) {
        _list.stream().forEach((entry) -> {
            if (!entry.getId().equals(newLocoString)) {
                values.addContent(entry.store());
            } else {
                log.debug("skip unsaved roster entry with default name " + entry.getId());
            }
        });
    }
    if (!this.rosterGroups.isEmpty()) {
        // NOI18N
        Element rosterGroup = new Element("rosterGroup");
        rosterGroups.keySet().stream().forEach((name) -> {
            // NOI18N
            Element group = new Element("group");
            if (!name.equals(Roster.ALLENTRIES)) {
                group.addContent(name);
                rosterGroup.addContent(group);
            }
        });
        root.addContent(rosterGroup);
    }
    writeXML(file, doc);
    //other parts of the program (e.g. in copying a roster)
    synchronized (_list) {
        _list.stream().forEach((entry) -> {
            if (!entry.getId().equals(newLocoString)) {
                String xmlComment = entry.getComment();
                String tempComment = "";
                for (int k = 0; k < xmlComment.length(); k++) {
                    if (xmlComment.startsWith("<?p?>", k)) {
                        // NOI18N
                        // NOI18N
                        tempComment = tempComment + "\n";
                        k = k + 4;
                    } else {
                        tempComment = tempComment + xmlComment.substring(k, k + 1);
                    }
                }
                entry.setComment(tempComment);
                String xmlDecoderComment = entry.getDecoderComment();
                // NOI18N
                String tempDecoderComment = "";
                for (int k = 0; k < xmlDecoderComment.length(); k++) {
                    if (xmlDecoderComment.startsWith("<?p?>", k)) {
                        // NOI18N
                        // NOI18N
                        tempDecoderComment = tempDecoderComment + "\n";
                        k = k + 4;
                    } else {
                        tempDecoderComment = tempDecoderComment + xmlDecoderComment.substring(k, k + 1);
                    }
                }
                entry.setDecoderComment(tempDecoderComment);
            } else {
                log.debug("skip unsaved roster entry with default name " + entry.getId());
            }
        });
    }
    // done - roster now stored, so can't be dirty
    setDirty(false);
    firePropertyChange(SAVED, false, true);
}
Also used : HashMap(java.util.HashMap) Element(org.jdom2.Element) Document(org.jdom2.Document) ProcessingInstruction(org.jdom2.ProcessingInstruction)

Example 14 with Comment

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

the class VariableTableModel method processIEnumVal.

protected VariableValue processIEnumVal(Element child, String name, String comment, String cvName, boolean readOnly, boolean infoOnly, boolean writeOnly, boolean opsOnly, String cv, String mask, String item, String productID, String modelID, String familyID) throws NumberFormatException {
    VariableValue iv;
    List<Element> l = child.getChildren("ienumChoice");
    IndexedEnumVariableValue v1 = new IndexedEnumVariableValue(name, comment, cvName, readOnly, infoOnly, writeOnly, opsOnly, cv, mask, _indxCvModel.allIndxCvMap(), _status, item);
    iv = v1;
    for (int x = 0; x < l.size(); x++) {
        Element ex = l.get(x);
        if (DecoderFile.isIncluded(ex, productID, modelID, familyID, "", "") == false) {
            // add inherited include, inherited exclude
            l.remove(x);
            x--;
        }
    }
    v1.nItems(l.size());
    for (int k = 0; k < l.size(); k++) {
        Element enumChElement = l.get(k);
        // is a value specified?
        Attribute valAttr = enumChElement.getAttribute("value");
        if (valAttr == null) {
            v1.addItem(LocaleSelector.getAttribute(enumChElement, "choice"));
        } else {
            v1.addItem(LocaleSelector.getAttribute(enumChElement, "choice"), Integer.parseInt(valAttr.getValue()));
        }
    }
    v1.lastItem();
    return iv;
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element)

Example 15 with Comment

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

the class VariableTableModel method setIndxRow.

/**
     * Load one row in the IndexedVariableTableModel, by reading in the Element
     * containing its definition.
     * <p>
     * Invoked from DecoderFile
     *
     * @param row       number of row to fill
     * @param e         Element of type "variable"
     * @param productID product ID of decoder, passed in so that subparts of the
     *                  variable can use it for selection
     */
public int setIndxRow(int row, Element e, String productID, String modelID, String familyID) {
    // get the values for the VariableValue ctor
    // Note the name variable is actually the label attribute
    String name = LocaleSelector.getAttribute(e, "label");
    if (log.isDebugEnabled()) {
        log.debug("Starting to setIndexedRow \"" + name + "\" row " + row);
    }
    String cvName = e.getAttributeValue("CVname");
    String item = (e.getAttribute("item") != null ? e.getAttribute("item").getValue() : null);
    String comment = LocaleSelector.getAttribute(e, "comment");
    int piVal = Integer.valueOf(e.getAttribute("PI").getValue()).intValue();
    int siVal = (e.getAttribute("SI") != null ? Integer.valueOf(e.getAttribute("SI").getValue()).intValue() : -1);
    String cv = e.getAttribute("CV").getValue();
    String mask = null;
    if (e.getAttribute("mask") != null) {
        mask = e.getAttribute("mask").getValue();
    } else {
        mask = "VVVVVVVV";
    }
    boolean readOnly = e.getAttribute("readOnly") != null ? e.getAttribute("readOnly").getValue().equals("yes") : false;
    boolean infoOnly = e.getAttribute("infoOnly") != null ? e.getAttribute("infoOnly").getValue().equals("yes") : false;
    boolean writeOnly = e.getAttribute("writeOnly") != null ? e.getAttribute("writeOnly").getValue().equals("yes") : false;
    boolean opsOnly = e.getAttribute("opsOnly") != null ? e.getAttribute("opsOnly").getValue().equals("yes") : false;
    JButton br = new JButton("Read");
    _readButtons.addElement(br);
    JButton bw = new JButton("Write");
    _writeButtons.addElement(bw);
    setButtonsReadWrite(readOnly, infoOnly, writeOnly, bw, br, row);
    if (_indxCvModel == null) {
        log.error("IndexedCvModel reference is null; can not add variables");
        return -1;
    }
    // add the information to the indexed CV model
    int _newRow = _indxCvModel.addIndxCV(cvName, _piCv, piVal, _siCv, siVal, cv, readOnly, infoOnly, writeOnly);
    if (_newRow != row) {
        row = _newRow;
        if (log.isDebugEnabled()) {
            log.debug("new row is " + _newRow + ", row was " + row);
        }
    }
    // Find and process the specific content types
    VariableValue iv;
    iv = createIndexedVariableFromElement(e, name, comment, cvName, readOnly, infoOnly, writeOnly, opsOnly, cv, mask, item, productID, modelID, familyID);
    if (iv == null) {
        // trouble reporting
        reportBogus();
        return -1;
    }
    processModifierElements(e, iv);
    setToolTip(e, iv);
    // record new variable, update state, hook up listeners
    rowVector.addElement(iv);
    iv.setState(VariableValue.FROMFILE);
    iv.addPropertyChangeListener(this);
    // set to default value if specified (CV load may later override this)
    Attribute a;
    if ((a = e.getAttribute("default")) != null) {
        String val = a.getValue();
        if (log.isDebugEnabled()) {
            log.debug("Found default value: " + val + " for " + name);
        }
        iv.setIntValue(Integer.valueOf(val).intValue());
        if (_indxCvModel.getCvByRow(row).getInfoOnly()) {
            _indxCvModel.getCvByRow(row).setState(VariableValue.READ);
        } else {
            // correct for transition to "edited"
            _indxCvModel.getCvByRow(row).setState(VariableValue.FROMFILE);
        }
    } else {
        _indxCvModel.getCvByRow(row).setState(VariableValue.UNKNOWN);
    }
    return row;
}
Also used : Attribute(org.jdom2.Attribute) JButton(javax.swing.JButton)

Aggregations

Element (org.jdom2.Element)33 Attribute (org.jdom2.Attribute)10 Document (org.jdom2.Document)6 ArrayList (java.util.ArrayList)4 File (java.io.File)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 List (java.util.List)3 ProcessingInstruction (org.jdom2.ProcessingInstruction)3 RecyclerView (android.support.v7.widget.RecyclerView)2 View (android.view.View)2 BindView (butterknife.BindView)2 Modification (com.thoughtworks.go.domain.materials.Modification)2 JButton (javax.swing.JButton)2 Block (jmri.Block)2 NamedBeanHandle (jmri.NamedBeanHandle)2 SignalGroup (jmri.SignalGroup)2 SignalGroupManager (jmri.SignalGroupManager)2 SignalMast (jmri.SignalMast)2 SignalMastLogic (jmri.SignalMastLogic)2