Search in sources :

Example 96 with Element

use of org.neo4j.ogm.domain.gh806.Element 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 97 with Element

use of org.neo4j.ogm.domain.gh806.Element in project JMRI by JMRI.

the class ExportRosterItemAction method doTransfer.

@Override
boolean doTransfer() {
    // read the file for the "from" entry and write it out
    // ensure preferences will be found for read
    FileUtil.createDirectory(LocoFile.getFileLocation());
    // locate the file
    //File f = new File(mFullFromFilename);
    // read it
    // used as a temporary
    LocoFile lf = new LocoFile();
    Element lroot;
    try {
        lroot = lf.rootFromName(mFullFromFilename).clone();
    } catch (Exception e) {
        log.error("Exception while loading loco XML file: " + mFullFromFilename + " exception: " + e);
        return false;
    }
    // create a new entry
    mToEntry = new RosterEntry(mFromEntry, mFromID);
    // transfer the contents to the new file
    LocoFile newLocoFile = new LocoFile();
    // File fout = new File(mFullToFilename);
    mToEntry.setFileName(mToFilename);
    mToEntry.setId(mFromEntry.getId());
    newLocoFile.writeFile(mToFile, lroot, mToEntry);
    return true;
}
Also used : Element(org.jdom2.Element)

Example 98 with Element

use of org.neo4j.ogm.domain.gh806.Element 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 99 with Element

use of org.neo4j.ogm.domain.gh806.Element in project JMRI by JMRI.

the class CopyRosterItemAction method doTransfer.

@Override
boolean doTransfer() {
    // read the from file, change the ID, and write it out
    log.debug("doTransfer starts");
    // ensure preferences will be found
    FileUtil.createDirectory(LocoFile.getFileLocation());
    // locate the file
    //File f = new File(mFullFromFilename);
    // read it
    // used as a temporary
    LocoFile lf = new LocoFile();
    Element lroot;
    try {
        lroot = lf.rootFromName(mFullFromFilename);
    } catch (Exception e) {
        log.error("Exception while loading loco XML file: " + mFullFromFilename + " exception: " + e);
        return false;
    }
    // create a new entry
    mToEntry = new RosterEntry(mFromEntry, mToID);
    // set the filename from the ID
    mToEntry.ensureFilenameExists();
    // detach the content element from it's existing file so 
    // it can be reused
    lroot.detach();
    // transfer the contents to a new file
    LocoFile newLocoFile = new LocoFile();
    File fout = new File(LocoFile.getFileLocation() + mToEntry.getFileName());
    newLocoFile.writeFile(fout, lroot, mToEntry);
    return true;
}
Also used : Element(org.jdom2.Element) File(java.io.File)

Example 100 with Element

use of org.neo4j.ogm.domain.gh806.Element in project JMRI by JMRI.

the class ImportRosterItemAction method loadEntryFromElement.

protected boolean loadEntryFromElement(Element lroot) {
    // create a new entry from XML info - find the element
    Element loco = lroot.getChild("locomotive");
    mToEntry = new RosterEntry(loco);
    // set the filename from the ID
    mToEntry.setId(mToID);
    // to force recreation
    mToEntry.setFileName("");
    mToEntry.ensureFilenameExists();
    // transfer the contents to a new file
    LocoFile newLocoFile = new LocoFile();
    File fout = new File(LocoFile.getFileLocation() + mToEntry.getFileName());
    newLocoFile.writeFile(fout, lroot, mToEntry);
    return true;
}
Also used : Element(org.jdom2.Element) File(java.io.File)

Aggregations

Element (org.jdom2.Element)3327 Document (org.jdom2.Document)502 Test (org.junit.Test)457 ArrayList (java.util.ArrayList)327 IOException (java.io.IOException)268 Attribute (org.jdom2.Attribute)207 JDOMException (org.jdom2.JDOMException)202 Element (org.osate.aadl2.Element)143 Namespace (org.jdom2.Namespace)136 Test (org.junit.jupiter.api.Test)131 List (java.util.List)130 SAXBuilder (org.jdom2.input.SAXBuilder)125 File (java.io.File)124 HashMap (java.util.HashMap)117 XMLOutputter (org.jdom2.output.XMLOutputter)103 XConfiguration (org.apache.oozie.util.XConfiguration)98 Configuration (org.apache.hadoop.conf.Configuration)96 NamedElement (org.osate.aadl2.NamedElement)77 StringReader (java.io.StringReader)67 Iterator (java.util.Iterator)63