Search in sources :

Example 71 with Element

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

the class ConsistFile method writeFile.

/**
     * Write all consists to a file.
     *
     * @param consistList list of consist addresses
     * @param fileName    path to file
     * @throws java.io.IOException if unable to write file
     */
public void writeFile(ArrayList<DccLocoAddress> consistList, String fileName) throws IOException {
    // create root element
    Element root = new Element("consist-roster-config");
    Document doc = newDocument(root, dtdLocation + "consist-roster-config.dtd");
    // add XSLT processing instruction
    Map<String, String> m = new HashMap<>();
    m.put("type", "text/xsl");
    m.put("href", xsltLocation + "consistRoster.xsl");
    ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
    doc.addContent(0, p);
    Element roster = new Element("roster");
    for (int i = 0; i < consistList.size(); i++) {
        Consist newConsist = consistMan.getConsist(consistList.get(i));
        roster.addContent(consistToXml(newConsist));
    }
    root.addContent(roster);
    try {
        if (!checkFile(fileName)) {
            //The file does not exist, create it before writing
            File file = new File(fileName);
            File parentDir = file.getParentFile();
            if (!parentDir.exists()) {
                if (!parentDir.mkdir()) {
                    throw (new IOException());
                }
            }
            if (!file.createNewFile()) {
                throw (new IOException());
            }
        }
        writeXML(findFile(fileName), doc);
    } catch (IOException ioe) {
        log.error("IO Exception " + ioe);
        throw (ioe);
    }
}
Also used : HashMap(java.util.HashMap) Consist(jmri.Consist) Element(org.jdom2.Element) IOException(java.io.IOException) Document(org.jdom2.Document) File(java.io.File) XmlFile(jmri.jmrit.XmlFile) ProcessingInstruction(org.jdom2.ProcessingInstruction)

Example 72 with Element

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

the class ConsistFile method readFile.

/**
     * Read all consists from a file.
     *
     * @param fileName path to file
     * @throws org.jdom2.JDOMException if unable to parse consists
     * @throws java.io.IOException     if unable to read file
     */
@SuppressWarnings("unchecked")
public void readFile(String fileName) throws JDOMException, IOException {
    if (checkFile(fileName)) {
        Element root = rootFromName(fileName);
        Element roster;
        if (root == null) {
            log.warn("consist file could not be read");
            return;
        }
        roster = root.getChild("roster");
        if (roster == null) {
            log.debug("consist file does not contain a roster entry");
            return;
        }
        Iterator<Element> consistIterator = root.getDescendants(new ElementFilter("consist"));
        try {
            Element consist;
            do {
                consist = consistIterator.next();
                consistFromXml(consist);
            } while (consistIterator.hasNext());
        } catch (NoSuchElementException nde) {
            log.debug("end of consist list");
        }
    } else {
        log.info("Consist file does not exist.  One will be created if necessary.");
    }
}
Also used : Element(org.jdom2.Element) ElementFilter(org.jdom2.filter.ElementFilter) NoSuchElementException(java.util.NoSuchElementException)

Example 73 with Element

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

the class OptionsFile method readDispatcherOptions.

/*
     *  Reads Dispatcher Options from a file in the user's preferences directory
     *  If the file containing Dispatcher Options does not exist this routine returns quietly.
     */
public void readDispatcherOptions(DispatcherFrame f) throws org.jdom2.JDOMException, java.io.IOException {
    // check if file exists
    if (checkFile(defaultFileName)) {
        // file is present, 
        log.debug("Reading Dispatcher options from file {}", defaultFileName);
        root = rootFromName(defaultFileName);
        dispatcher = f;
        if (root != null) {
            // there is a file
            Element options = root.getChild("options");
            if (options != null) {
                // there are options defined, read and set Dispatcher options
                if (options.getAttribute("lename") != null) {
                    // there is a layout editor name selected
                    String leName = options.getAttribute("lename").getValue();
                    // get list of Layout Editor panels
                    ArrayList<LayoutEditor> layoutEditorList = jmri.jmrit.display.PanelMenu.instance().getLayoutEditorPanelList();
                    if (layoutEditorList.size() == 0) {
                        log.warn("Dispatcher options specify a Layout Editor panel that is not present.");
                    } else {
                        boolean found = false;
                        for (int i = 0; i < layoutEditorList.size(); i++) {
                            if (leName.equals(layoutEditorList.get(i).getTitle())) {
                                found = true;
                                dispatcher.setLayoutEditor(layoutEditorList.get(i));
                            }
                        }
                        if (!found) {
                            log.warn("Layout Editor panel - " + leName + " - not found.");
                        }
                    }
                }
                if (options.getAttribute("usesignaltype") != null) {
                    dispatcher.setSignalType(0x00);
                    if (options.getAttribute("usesignaltype").getValue().equals("signalmast")) {
                        dispatcher.setSignalType(0x01);
                    }
                }
                if (options.getAttribute("useconnectivity") != null) {
                    dispatcher.setUseConnectivity(true);
                    if (options.getAttribute("useconnectivity").getValue().equals("no")) {
                        dispatcher.setUseConnectivity(false);
                    }
                }
                if (options.getAttribute("trainsfromroster") != null) {
                    dispatcher.setTrainsFromRoster(true);
                    if (options.getAttribute("trainsfromroster").getValue().equals("no")) {
                        dispatcher.setTrainsFromRoster(false);
                    }
                }
                if (options.getAttribute("trainsfromtrains") != null) {
                    dispatcher.setTrainsFromTrains(true);
                    if (options.getAttribute("trainsfromtrains").getValue().equals("no")) {
                        dispatcher.setTrainsFromTrains(false);
                    }
                }
                if (options.getAttribute("trainsfromuser") != null) {
                    dispatcher.setTrainsFromUser(true);
                    if (options.getAttribute("trainsfromuser").getValue().equals("no")) {
                        dispatcher.setTrainsFromUser(false);
                    }
                }
                if (options.getAttribute("autoallocate") != null) {
                    dispatcher.setAutoAllocate(false);
                    if (options.getAttribute("autoallocate").getValue().equals("yes")) {
                        dispatcher.setAutoAllocate(true);
                    }
                }
                if (options.getAttribute("autoturnouts") != null) {
                    dispatcher.setAutoTurnouts(true);
                    if (options.getAttribute("autoturnouts").getValue().equals("no")) {
                        dispatcher.setAutoTurnouts(false);
                    }
                }
                if (options.getAttribute("trustknownturnouts") != null) {
                    dispatcher.setTrustKnownTurnouts(false);
                    if (options.getAttribute("trustknownturnouts").getValue().equals("yes")) {
                        dispatcher.setTrustKnownTurnouts(true);
                    }
                }
                if (options.getAttribute("minthrottleinterval") != null) {
                    String s = (options.getAttribute("minthrottleinterval")).getValue();
                    dispatcher.setMinThrottleInterval(Integer.parseInt(s));
                }
                if (options.getAttribute("fullramptime") != null) {
                    String s = (options.getAttribute("fullramptime")).getValue();
                    dispatcher.setFullRampTime(Integer.parseInt(s));
                }
                if (options.getAttribute("hasoccupancydetection") != null) {
                    dispatcher.setHasOccupancyDetection(true);
                    if (options.getAttribute("hasoccupancydetection").getValue().equals("no")) {
                        dispatcher.setHasOccupancyDetection(false);
                    }
                }
                if (options.getAttribute("shortactivetrainnames") != null) {
                    dispatcher.setShortActiveTrainNames(true);
                    if (options.getAttribute("shortactivetrainnames").getValue().equals("no")) {
                        dispatcher.setShortActiveTrainNames(false);
                    }
                }
                if (options.getAttribute("shortnameinblock") != null) {
                    dispatcher.setShortNameInBlock(true);
                    if (options.getAttribute("shortnameinblock").getValue().equals("no")) {
                        dispatcher.setShortNameInBlock(false);
                    }
                }
                if (options.getAttribute("extracolorforallocated") != null) {
                    dispatcher.setExtraColorForAllocated(true);
                    if (options.getAttribute("extracolorforallocated").getValue().equals("no")) {
                        dispatcher.setExtraColorForAllocated(false);
                    }
                }
                if (options.getAttribute("nameinallocatedblock") != null) {
                    dispatcher.setNameInAllocatedBlock(true);
                    if (options.getAttribute("nameinallocatedblock").getValue().equals("no")) {
                        dispatcher.setNameInAllocatedBlock(false);
                    }
                }
                if (options.getAttribute("supportvsdecoder") != null) {
                    dispatcher.setSupportVSDecoder(true);
                    if (options.getAttribute("supportvsdecoder").getValue().equals("no")) {
                        dispatcher.setSupportVSDecoder(false);
                    }
                }
                if (options.getAttribute("layoutscale") != null) {
                    String s = (options.getAttribute("layoutscale")).getValue();
                    for (int i = 1; i <= Scale.NUM_SCALES; i++) {
                        if (Scale.getShortScaleID(i).equals(s)) {
                            dispatcher.setScale(i);
                        }
                    }
                }
                if (options.getAttribute("usescalemeters") != null) {
                    dispatcher.setUseScaleMeters(true);
                    if (options.getAttribute("usescalemeters").getValue().equals("no")) {
                        dispatcher.setUseScaleMeters(false);
                    }
                }
                if (options.getAttribute("userosterentryinblock") != null) {
                    dispatcher.setRosterEntryInBlock(false);
                    if (options.getAttribute("userosterentryinblock").getValue().equals("yes")) {
                        dispatcher.setRosterEntryInBlock(true);
                    }
                }
            }
        }
    } else {
        log.debug("No Dispatcher options file found at {}, using defaults", defaultFileName);
    }
}
Also used : LayoutEditor(jmri.jmrit.display.layoutEditor.LayoutEditor) Element(org.jdom2.Element)

Example 74 with Element

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

the class PositionableLabelXml method loadCommonAttributes.

public void loadCommonAttributes(Positionable l, int defaultLevel, Element element) {
    Attribute a = element.getAttribute("forcecontroloff");
    if ((a != null) && a.getValue().equals("true")) {
        l.setControlling(false);
    } else {
        l.setControlling(true);
    }
    // find coordinates
    int x = 0;
    int y = 0;
    try {
        x = element.getAttribute("x").getIntValue();
        y = element.getAttribute("y").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.error("failed to convert positional attribute");
    }
    l.setLocation(x, y);
    // find display level
    int level = defaultLevel;
    try {
        level = element.getAttribute("level").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
        log.warn("Could not parse level attribute!");
    } catch (NullPointerException e) {
    // considered normal if the attribute not present
    }
    l.setDisplayLevel(level);
    a = element.getAttribute("hidden");
    if ((a != null) && a.getValue().equals("yes")) {
        l.setHidden(true);
        l.setVisible(false);
    }
    a = element.getAttribute("positionable");
    if ((a != null) && a.getValue().equals("true")) {
        l.setPositionable(true);
    } else {
        l.setPositionable(false);
    }
    a = element.getAttribute("showtooltip");
    if ((a != null) && a.getValue().equals("true")) {
        l.setShowTooltip(true);
    } else {
        l.setShowTooltip(false);
    }
    a = element.getAttribute("editable");
    if ((a != null) && a.getValue().equals("true")) {
        l.setEditable(true);
    } else {
        l.setEditable(false);
    }
    a = element.getAttribute("degrees");
    if (a != null && l instanceof PositionableLabel) {
        try {
            int deg = a.getIntValue();
            ((PositionableLabel) l).setDegrees(deg);
        } catch (org.jdom2.DataConversionException dce) {
        }
    }
    Element elem = element.getChild("tooltip");
    if (elem == null) {
        // pre JMRI 3.5.2
        elem = element.getChild("toolTip");
    }
    if (elem != null) {
        ToolTip tip = l.getTooltip();
        if (tip != null) {
            tip.setText(elem.getText());
        }
    }
}
Also used : ToolTip(jmri.jmrit.display.ToolTip) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) PositionableLabel(jmri.jmrit.display.PositionableLabel) DataConversionException(org.jdom2.DataConversionException)

Example 75 with Element

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

the class MultiSensorIconXml method load.

/**
     * Create a PositionableLabel, then add to a target JLayeredPane
     *
     * @param element Top level Element to unpack.
     * @param o       an Editor an Object
     */
@Override
public void load(Element element, Object o) {
    Editor pe = (Editor) o;
    MultiSensorIcon l = new MultiSensorIcon(pe);
    // create the objects
    int rotation = 0;
    try {
        rotation = element.getAttribute("rotate").getIntValue();
    } catch (org.jdom2.DataConversionException e) {
    } catch (NullPointerException e) {
    // considered normal if the attributes are not present
    }
    NamedIcon icon = loadSensorIcon("inactive", rotation, l, element, pe);
    if (icon != null) {
        l.setInactiveIcon(icon);
    } else {
        return;
    }
    icon = loadSensorIcon("unknown", rotation, l, element, pe);
    if (icon != null) {
        l.setUnknownIcon(icon);
    } else {
        return;
    }
    icon = loadSensorIcon("inconsistent", rotation, l, element, pe);
    if (icon != null) {
        l.setInconsistentIcon(icon);
    } else {
        return;
    }
    Attribute a = element.getAttribute("updown");
    if ((a != null) && a.getValue().equals("true")) {
        l.setUpDown(true);
    } else {
        l.setUpDown(false);
    }
    // get the icon pairs & load
    List<Element> items = element.getChildren();
    for (int i = 0; i < items.size(); i++) {
        // get the class, hence the adapter object to do loading
        Element item = items.get(i);
        if (item.getAttribute("sensor") != null) {
            String sensor = item.getAttribute("sensor").getValue();
            if (item.getAttribute("url") != null) {
                String name = item.getAttribute("url").getValue();
                icon = NamedIcon.getIconByName(name);
                if (icon == null) {
                    icon = pe.loadFailed("MultiSensor \"" + l.getNameString() + "\" ", name);
                    if (icon == null) {
                        log.error("MultiSensor \"" + l.getNameString() + "\" removed for url= " + name);
                        return;
                    }
                }
                try {
                    int deg = 0;
                    a = item.getAttribute("degrees");
                    if (a != null) {
                        deg = a.getIntValue();
                        double scale = 1.0;
                        a = item.getAttribute("scale");
                        if (a != null) {
                            scale = item.getAttribute("scale").getDoubleValue();
                        }
                        icon.setLoad(deg, scale, l);
                    }
                    if (deg == 0) {
                        a = item.getAttribute("rotate");
                        if (a != null) {
                            rotation = a.getIntValue();
                            icon.setRotation(rotation, l);
                        }
                    }
                } catch (org.jdom2.DataConversionException dce) {
                }
            } else {
                String name = item.getAttribute("icon").getValue();
                icon = NamedIcon.getIconByName(name);
                if (icon == null) {
                    icon = pe.loadFailed("MultiSensor \"" + l.getNameString(), name);
                    if (icon == null) {
                        log.info("MultiSensor \"" + l.getNameString() + " removed for url= " + name);
                        return;
                    }
                }
                if (rotation != 0) {
                    icon.setRotation(rotation, l);
                }
            }
            l.addEntry(sensor, icon);
        }
    }
    pe.putItem(l);
    // load individual item's option settings after editor has set its global settings
    loadCommonAttributes(l, Editor.SENSORS, element);
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) MultiSensorIcon(jmri.jmrit.display.MultiSensorIcon) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) Editor(jmri.jmrit.display.Editor)

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