Search in sources :

Example 26 with Element

use of org.jdom2.Element 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 27 with Element

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

the class DiceBagModel method saveToDocument.

/**
	 * <p>Loads the current dicebag's information into the
	 * given JDOM document.</p>
	 *
	 * @param doc
	 */
private void saveToDocument(Document doc) {
    Element party = new Element("dice-bag");
    party.setAttribute("name", m_name);
    for (String dieString : m_dice) {
        Element die = new Element("dice-roll");
        die.addContent(dieString);
        party.addContent(die);
    }
    doc.setRootElement(party);
}
Also used : Element(org.jdom2.Element)

Example 28 with Element

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

the class Localized method update.

/**
	 * 
	 * @param e
	 * @param attribute if {@code null}, use the trimmed text.
	 */
private void update(Element e, String attribute) {
    List<?> children = e.getChildren(ELEMENT_LOC);
    for (Object object : children) {
        if (object instanceof Element) {
            Element child = (Element) object;
            String lang = child.getAttributeValue(ATTRIBUTE_LANGUAGE);
            String name;
            if (attribute == null)
                name = child.getTextTrim();
            else
                name = child.getAttributeValue(attribute);
            if (lang != null && !lang.isEmpty())
                addName(lang, name);
        }
    }
}
Also used : Element(org.jdom2.Element)

Example 29 with Element

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

the class BlockManagerXml method addBeanSetting.

void addBeanSetting(Element e, BeanSetting bs) {
    if (bs.getBean() == null) {
        log.error("Invalid BeanSetting - did not save");
        return;
    }
    // persist bean name, type and value
    Element bse = new Element("beansetting");
    // for now, assume turnout
    bse.setAttribute("setting", "" + bs.getSetting());
    Element be = new Element("turnout");
    be.setAttribute("systemName", bs.getBeanName());
    bse.addContent(be);
    e.addContent(bse);
}
Also used : Element(org.jdom2.Element)

Example 30 with Element

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

the class BlockManagerXml method loadPath.

/**
     * Load path into an existing Block from XML.
     *
     * @param block   Block to receive path
     * @param element Element containing path information
     * @return true if path added to block; false otherwise
     * @throws jmri.configurexml.JmriConfigureXmlException if element contains
     *                                                     malformed or
     *                                                     schematically invalid
     *                                                     XMl
     */
public boolean loadPath(Block block, Element element) throws JmriConfigureXmlException {
    // load individual path
    int toDir = 0;
    int fromDir = 0;
    try {
        toDir = element.getAttribute("todir").getIntValue();
        fromDir = element.getAttribute("fromdir").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("Could not parse path attribute");
    } catch (NullPointerException e) {
        handleException("Block Path entry in file missing required attribute", null, block.getSystemName(), block.getUserName(), null);
    }
    Block toBlock = null;
    if (element.getAttribute("block") != null) {
        String name = element.getAttribute("block").getValue();
        toBlock = InstanceManager.getDefault(jmri.BlockManager.class).getBlock(name);
    }
    Path path = new Path(toBlock, toDir, fromDir);
    List<Element> settings = element.getChildren("beansetting");
    for (int i = 0; i < settings.size(); i++) {
        Element setting = settings.get(i);
        loadBeanSetting(path, setting);
    }
    // check if path already in block
    if (!block.hasPath(path)) {
        block.addPath(path);
        return true;
    } else {
        log.debug("Skipping load of duplicate path {}", path);
        return false;
    }
}
Also used : Path(jmri.Path) Element(org.jdom2.Element) Block(jmri.Block)

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