Search in sources :

Example 1 with Comment

use of org.jdom2.Comment in project pcgen by PCGen.

the class TravelMethodFactory method create.

public static TravelMethod create(Document methodSet) {
    Localized name;
    Map<String, Map<String, Combo>> multByRoadByTerrains;
    Map<String, List<Localized>> terrains2;
    Map<String, Map<Localized, String>> terrainsById2;
    Map<String, List<Localized>> routes2;
    Map<String, Map<Localized, String>> routesById2;
    List<Method> methods;
    Element travel = methodSet.getRootElement();
    NumberFormat nf = getNumberFormat(travel);
    name = new Localized(travel);
    multByRoadByTerrains = new HashMap<>();
    terrains2 = new HashMap<>();
    terrainsById2 = new HashMap<>();
    routes2 = new HashMap<>();
    routesById2 = new HashMap<>();
    methods = new ArrayList<>();
    for (Object methodObj : travel.getChildren()) {
        Element child = (Element) methodObj;
        if (child.getName().equals(XML_ELEMENT_WAY)) {
            String wayId = child.getAttributeValue(XML_ATTRIBUTE_ID);
            List<Localized> terrains = new ArrayList<>();
            terrains2.put(wayId, terrains);
            List<Localized> routes = new ArrayList<>();
            routes2.put(wayId, routes);
            Map<Localized, String> terrainsById = new HashMap<>();
            terrainsById2.put(wayId, terrainsById);
            Map<Localized, String> routesById = new HashMap<>();
            routesById2.put(wayId, routesById);
            for (Object o : child.getChildren()) {
                if (o instanceof Element) {
                    Element grandchild = (Element) o;
                    if (grandchild.getName().equals(XML_ELEMENT_TERRAIN)) {
                        String id = grandchild.getAttributeValue(XML_ATTRIBUTE_ID);
                        Localized terrain = new Localized(grandchild);
                        terrains.add(terrain);
                        terrainsById.put(terrain, id);
                        if (!multByRoadByTerrains.containsKey(id)) {
                            multByRoadByTerrains.put(id, new TreeMap<>());
                        }
                    } else if (grandchild.getName().equals(XML_ELEMENT_ROUTE)) {
                        String id = grandchild.getAttributeValue(XML_ATTRIBUTE_ID);
                        Localized route = new Localized(grandchild);
                        routes.add(route);
                        routesById.put(route, id);
                        for (Object gcc : grandchild.getChildren(XML_ELEMENT_COMBO)) {
                            if (gcc instanceof Element) {
                                Element grandgrandchild = (Element) gcc;
                                String idTerrain = grandgrandchild.getAttributeValue(XML_ELEMENT_TERRAIN);
                                Number mult = parseNumber(nf, grandgrandchild, XML_ATTRIBUTE_MULT, 1);
                                Number addMph = parseNumber(nf, grandgrandchild, XML_ATTRIBUTE_ADDMPH, 0);
                                Number addKmh = parseNumber(nf, grandgrandchild, XML_ATTRIBUTE_ADDKMH, 0);
                                if (!multByRoadByTerrains.containsKey(idTerrain)) {
                                    multByRoadByTerrains.put(idTerrain, new TreeMap<>());
                                }
                                multByRoadByTerrains.get(idTerrain).put(id, new Combo(mult, addMph, addKmh));
                            }
                        }
                    }
                }
            }
        // Sort the terrains by locale name
        // TODO sort, but with one that do toString on the object. Collections.sort(terrains, Collator.getInstance());
        // not sorting routes intentionally (it goes from easier to navigate to hardest)
        } else if (child.getName().equals(XML_ELEMENT_METHOD)) {
            String way = child.getAttributeValue(XML_ELEMENT_WAY);
            Method method = new Method(new Localized(child), way);
            methods.add(method);
            for (Object o : child.getChildren()) {
                if (o instanceof Element) {
                    Element grandchild = (Element) o;
                    if (grandchild.getName().equals(XML_ELEMENT_PACE)) {
                        Localized pace = new Localized(grandchild);
                        boolean useDays = Boolean.parseBoolean(grandchild.getAttributeValue(XML_ATTRIBUTE_DAYS));
                        Localized comment = new Localized(grandchild, XML_ATTRIBUTE_COMMENT);
                        Number mult = parseNumber(nf, grandchild, XML_ATTRIBUTE_MULT, 1);
                        Pace newPace = new Pace(pace, comment, useDays, mult);
                        method.add(newPace);
                    }
                    if (grandchild.getName().equals(XML_ELEMENT_CHOOSE_FROM)) {
                        // XXX other default?
                        Number kmh = parseNumber(nf, grandchild, XML_ATTRIBUTE_KMH, 0.75);
                        // XXX other default?
                        Number mph = parseNumber(nf, grandchild, XML_ATTRIBUTE_MPH, 0.5);
                        // XXX other default?
                        Number hoursInDay = parseNumber(nf, grandchild, XML_ATTRIBUTE_HOURSINDAY, 24);
                        for (Object o2 : grandchild.getChildren(XML_ELEMENT_CHOICE)) {
                            if (o2 instanceof Element) {
                                Element grandgrandchild = (Element) o2;
                                Localized choiceName = new Localized(grandgrandchild);
                                Number mult = parseNumber(nf, grandgrandchild, XML_ATTRIBUTE_MULT, 1);
                                Choice c = new Choice(choiceName, hoursInDay, mult.doubleValue() * kmh.doubleValue(), mult.doubleValue() * mph.doubleValue());
                                method.add(c);
                            }
                        }
                    }
                }
            }
        }
    }
    return new TravelMethodImplementation(name, multByRoadByTerrains, terrains2, terrainsById2, routes2, routesById2, methods);
}
Also used : Choice(plugin.overland.model.TravelMethodImplementation.Choice) HashMap(java.util.HashMap) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) Combo(plugin.overland.model.TravelMethodImplementation.Combo) Pace(plugin.overland.model.TravelMethodImplementation.Pace) ArrayList(java.util.ArrayList) List(java.util.List) Method(plugin.overland.model.TravelMethodImplementation.Method) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Localized(plugin.overland.util.Localized) NumberFormat(java.text.NumberFormat)

Example 2 with Comment

use of org.jdom2.Comment in project coprhd-controller by CoprHD.

the class XmlDiff method compareXml.

/**
 * Compares two documents and outputs different part info.
 * <p>
 * 1. ignore sequence 2. ignore element text value 3. ignore xml comment element
 * </p>
 *
 * @param oldDocument
 *            The old xml document
 * @param newDocument
 *            The new xml document
 * @return
 *         The instance of diff
 */
public static Pair<String, String> compareXml(Document oldDocument, Document newDocument) {
    Element oldRootElement = oldDocument.getRootElement();
    Element newRootElement = newDocument.getRootElement();
    if (compareElement(oldRootElement, newRootElement)) {
        return null;
    }
    XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
    return new Pair<String, String>(xmlOutputter.outputString(oldRootElement), xmlOutputter.outputString(newRootElement));
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Element(org.jdom2.Element)

Example 3 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 4 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 5 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)

Aggregations

Element (org.jdom2.Element)39 Attribute (org.jdom2.Attribute)10 Document (org.jdom2.Document)9 Comment (org.jdom2.Comment)8 ArrayList (java.util.ArrayList)4 File (java.io.File)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 List (java.util.List)3 JButton (javax.swing.JButton)3 JDOMException (org.jdom2.JDOMException)3 ProcessingInstruction (org.jdom2.ProcessingInstruction)3 Modification (com.thoughtworks.go.domain.materials.Modification)2 Date (java.util.Date)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