Search in sources :

Example 96 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element in project JMRI by JMRI.

the class NameFile method readFile.

/**
     * Read the contents of a NameFile XML file into this object. Note that this
     * does not clear any existing entries.
     */
void readFile(String name) throws org.jdom2.JDOMException, java.io.IOException {
    if (log.isDebugEnabled()) {
        log.debug("readFile " + name);
    }
    // read file, find root
    Element root = rootFromName(name);
    // decode type, invoke proper processing routine
    readNames(root);
}
Also used : Element(org.jdom2.Element)

Example 97 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element in project JMRI by JMRI.

the class SymbolicProgFrame method writeFile.

// dead class doesn't need this fixed right now
@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION")
void writeFile() {
    log.warn("SymbolicProgFrame writeFile invoked - is this still right, or should the LocoFile method be used?");
    log.warn("Note use of VersionID attribute...");
    try {
        // get the file
        int retVal = fco.showSaveDialog(this);
        // handle selection or cancel
        if (retVal != JFileChooser.APPROVE_OPTION) {
            // leave early
            return;
        }
        File file = fco.getSelectedFile();
        // This is taken in large part from "Java and XML" page 368
        // create root element
        Element root = new Element("locomotive-config");
        Document doc = jmri.jmrit.XmlFile.newDocument(root, jmri.jmrit.XmlFile.getDefaultDtdLocation() + "locomotive-config.dtd");
        // add XSLT processing instruction
        // <?xml-stylesheet type="text/xsl" href="XSLT/locomotive.xsl"?>
        java.util.Map<String, String> m = new java.util.HashMap<String, String>();
        m.put("type", "text/xsl");
        m.put("href", jmri.jmrit.XmlFile.xsltLocation + "locomotive.xsl");
        ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
        doc.addContent(0, p);
        // add top-level elements
        Element values;
        root.addContent(// locomotive values are first item
        new Element("locomotive").setAttribute("roadNumber", locoRoadNumber.getText()).setAttribute("roadName", locoRoadName.getText()).setAttribute("mfg", locoMfg.getText()).setAttribute("model", locoModel.getText()).addContent(new Element("decoder").setAttribute("model", decoderModel.getText()).setAttribute("mfg", decoderMfg.getText()).setAttribute("versionID", "").setAttribute("mfgID", "")).addContent(values = new Element("values")));
        // Append a decoderDef element to values
        Element decoderDef;
        values.addContent(decoderDef = new Element("decoderDef"));
        // add the variable values to the decoderDef Element
        for (int i = 0; i < variableModel.getRowCount(); i++) {
            decoderDef.addContent(new Element("varValue").setAttribute("item", variableModel.getLabel(i)).setAttribute("value", variableModel.getValString(i)));
        }
        // add the CV values to the values Element
        for (int i = 0; i < cvModel.getRowCount(); i++) {
            values.addContent(new Element("CVvalue").setAttribute("name", cvModel.getName(i)).setAttribute("value", cvModel.getValString(i)));
        }
        // write the result to selected file
        java.io.FileOutputStream o = new java.io.FileOutputStream(file);
        try {
            XMLOutputter fmt = new XMLOutputter();
            fmt.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.PRESERVE));
            fmt.output(doc, o);
        } finally {
            o.close();
        }
        // mark file as OK
        variableModel.setFileDirty(false);
    } catch (Exception e) {
        log.error(e.getLocalizedMessage(), e);
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Element(org.jdom2.Element) Document(org.jdom2.Document) DecoderFile(jmri.jmrit.decoderdefn.DecoderFile) File(java.io.File) ProcessingInstruction(org.jdom2.ProcessingInstruction) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 98 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element in project JMRI by JMRI.

the class SymbolicProgFrame method processLocoFile.

void processLocoFile(Element loco) {
    // load the name et al
    locoRoadName.setText(loco.getAttributeValue("roadName"));
    locoRoadNumber.setText(loco.getAttributeValue("roadNumber"));
    locoMfg.setText(loco.getAttributeValue("mfg"));
    locoModel.setText(loco.getAttributeValue("model"));
    // load the variable definitions for the decoder
    Element decoder = loco.getChild("decoder");
    if (decoder != null) {
        // get the file name
        String mfg = decoder.getAttribute("mfg").getValue();
        String model = decoder.getAttribute("model").getValue();
        String filename = "xml" + File.separator + mfg + "_" + model + ".xml";
        if (log.isDebugEnabled()) {
            log.debug("will read decoder info from " + filename);
        }
        readAndParseConfigFile(new File(filename));
        if (log.isDebugEnabled()) {
            log.debug("finished processing decoder file for loco file");
        }
    } else {
        log.error("No decoder element found in config file");
    }
    // get the CVs and load
    Element values = loco.getChild("values");
    if (values != null) {
        // get the CV values and load
        List<Element> varList = values.getChildren("CVvalue");
        if (log.isDebugEnabled()) {
            log.debug("Found " + varList.size() + " CVvalues");
        }
        for (int i = 0; i < varList.size(); i++) {
            // locate the row
            if (((varList.get(i))).getAttribute("name") == null) {
                if (log.isDebugEnabled()) {
                    log.debug("unexpected null in name " + ((varList.get(i))) + " " + ((varList.get(i))).getAttributes());
                }
                break;
            }
            if (((varList.get(i))).getAttribute("value") == null) {
                if (log.isDebugEnabled()) {
                    log.debug("unexpected null in value " + ((varList.get(i))) + " " + ((varList.get(i))).getAttributes());
                }
                break;
            }
            String name = ((varList.get(i))).getAttribute("name").getValue();
            String value = ((varList.get(i))).getAttribute("value").getValue();
            if (log.isDebugEnabled()) {
                log.debug("CV: " + i + "th entry, CV number " + name + " has value: " + value);
            }
            CvValue cvObject = cvModel.allCvMap().get(name);
            cvObject.setValue(Integer.valueOf(value).intValue());
            cvObject.setState(CvValue.FROMFILE);
        }
        variableModel.configDone();
    } else {
        log.error("no values element found in config file; CVs not configured");
        return;
    }
    // get the variable values and load
    Element decoderDef = values.getChild("decoderDef");
    if (decoderDef != null) {
        List<Element> varList = decoderDef.getChildren("varValue");
        if (log.isDebugEnabled()) {
            log.debug("Found " + varList.size() + " varValues");
        }
        for (int i = 0; i < varList.size(); i++) {
            // locate the row
            Attribute itemAttr = null;
            if ((itemAttr = varList.get(i).getAttribute("item")) == null) {
                if (log.isDebugEnabled()) {
                    log.debug("unexpected null in item " + varList.get(i));
                }
                break;
            }
            if ((itemAttr = varList.get(i).getAttribute("name")) == null) {
                if (log.isDebugEnabled()) {
                    log.debug("unexpected null in name " + varList.get(i));
                }
                break;
            }
            String item = itemAttr.getValue();
            if (((varList.get(i))).getAttribute("value") == null) {
                if (log.isDebugEnabled()) {
                    log.debug("unexpected null in value " + ((varList.get(i))));
                }
                break;
            }
            String value = ((varList.get(i))).getAttribute("value").getValue();
            if (log.isDebugEnabled()) {
                log.debug("Variable " + i + " is " + item + " value: " + value);
            }
            int row;
            for (row = 0; row < variableModel.getRowCount(); row++) {
                if (variableModel.getLabel(row).equals(item)) {
                    break;
                }
            }
            if (log.isDebugEnabled()) {
                log.debug("Variable " + item + " is row " + row);
            }
            if (!value.equals("")) {
                // don't set if no value was stored
                variableModel.setIntValue(row, Integer.valueOf(value).intValue());
            }
            variableModel.setState(row, VariableValue.FROMFILE);
        }
        variableModel.configDone();
    } else {
        log.error("no decoderDef element found in config file");
    }
    // the act of loading values marks as dirty, but we're actually in synch
    variableModel.setFileDirty(false);
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) CvValue(jmri.jmrit.symbolicprog.CvValue) DecoderFile(jmri.jmrit.decoderdefn.DecoderFile) File(java.io.File)

Example 99 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element 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 100 with Element

use of org.eclipse.xtext.parsetree.reconstr.bug302128.Element in project JMRI by JMRI.

the class ProgrammerConfigPaneXml method store.

/**
     * Default implementation for storing the static contents
     *
     * @param o Object to store, of type PositionableLabel
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    ProgrammerConfigPane p = (ProgrammerConfigPane) o;
    Element programmer = new Element("programmer");
    if (p.getSelectedItem() != null) {
        programmer.setAttribute("defaultFile", p.getSelectedItem());
    }
    programmer.setAttribute("verifyBeforeWrite", "no");
    if (!p.getShowEmptyTabs()) {
        programmer.setAttribute("showEmptyPanes", "no");
    }
    if (p.getShowCvNums()) {
        programmer.setAttribute("showCvNumbers", "yes");
    }
    if (p.getCanCacheDefault()) {
        programmer.setAttribute("canCacheDefault", "yes");
    } else {
        programmer.setAttribute("canCacheDefault", "no");
    }
    if (p.getDoConfirmRead()) {
        programmer.setAttribute("doConfirmRead", "yes");
    } else {
        programmer.setAttribute("doConfirmRead", "no");
    }
    programmer.setAttribute("class", this.getClass().getName());
    return programmer;
}
Also used : Element(org.jdom2.Element) ProgrammerConfigPane(jmri.jmrit.symbolicprog.ProgrammerConfigPane)

Aggregations

Element (org.jdom2.Element)829 Attribute (org.jdom2.Attribute)76 Document (org.jdom2.Document)75 Test (org.junit.Test)70 File (java.io.File)53 ArrayList (java.util.ArrayList)45 JDOMException (org.jdom2.JDOMException)37 IOException (java.io.IOException)34 HashMap (java.util.HashMap)28 NamedIcon (jmri.jmrit.catalog.NamedIcon)27 List (java.util.List)26 XmlFile (jmri.jmrit.XmlFile)24 SAXBuilder (org.jdom2.input.SAXBuilder)21 Turnout (jmri.Turnout)20 DataConversionException (org.jdom2.DataConversionException)20 DocType (org.jdom2.DocType)19 Editor (jmri.jmrit.display.Editor)18 XMLOutputter (org.jdom2.output.XMLOutputter)18 Namespace (org.jdom2.Namespace)17 Point (java.awt.Point)15