Search in sources :

Example 1 with XmlFile

use of jmri.jmrit.XmlFile in project JMRI by JMRI.

the class VSDecoderPreferences method save.

public void save() {
    if (prefFile == null) {
        return;
    }
    XmlFile xf = new XmlFile() {
    };
    // odd syntax is due to XmlFile being abstract
    xf.makeBackupFile(prefFile);
    File file = new File(prefFile);
    try {
        //The file does not exist, create it before writing
        File parentDir = file.getParentFile();
        if (!parentDir.exists()) {
            if (// make directory, check result
            !parentDir.mkdir()) {
                log.error("failed to make parent directory");
            }
        }
        if (// create file, check result
        !file.createNewFile()) {
            log.error("createNewFile failed");
        }
    } catch (Exception exp) {
        log.error("Exception while writing the new VSDecoder preferences file, may not be complete: " + exp);
    }
    try {
        Element root = new Element("vsdecoder-preferences");
        //Document doc = XmlFile.newDocument(root, XmlFile.dtdLocation+"vsdecoder-preferences.dtd");
        Document doc = XmlFile.newDocument(root);
        // add XSLT processing instruction
        // <?xml-stylesheet type="text/xsl" href="XSLT/throttle.xsl"?>
        /*TODO      java.util.Map<String,String> m = new java.util.HashMap<String,String>();
             m.put("type", "text/xsl");
             m.put("href", jmri.jmrit.XmlFile.xsltLocation+"throttles-preferences.xsl");
             ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
             doc.addContent(0,p);*/
        root.setContent(store());
        xf.writeXML(file, doc);
    } catch (Exception ex) {
        // TODO fix null value for Attribute
        log.warn("Exception in storing vsdecoder preferences xml: " + ex);
    }
}
Also used : XmlFile(jmri.jmrit.XmlFile) Element(org.jdom2.Element) Document(org.jdom2.Document) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 2 with XmlFile

use of jmri.jmrit.XmlFile in project JMRI by JMRI.

the class EcosLocoToRoster method loadProgrammerFile.

// From PaneProgFrame
protected void loadProgrammerFile(RosterEntry r) {
    // Open and parse programmer file
    XmlFile pf = new XmlFile() {
    };
    // XmlFile is abstract
    try {
        programmerRoot = pf.rootFromName(filename);
        readConfig(programmerRoot, r);
    } catch (Exception e) {
        log.error("exception reading programmer file: " + filename);
        // provide traceback too
        e.printStackTrace();
    }
}
Also used : XmlFile(jmri.jmrit.XmlFile)

Example 3 with XmlFile

use of jmri.jmrit.XmlFile in project JMRI by JMRI.

the class PaneProgDp3Action method setUpRosterPanel.

synchronized void setUpRosterPanel() {
    re = null;
    if (rosterPanel == null) {
        rosterPanel = new JPanel();
        rosterPanel.setLayout(new BorderLayout());
        JPanel p = new JPanel();
        // NOI18N
        p.add(new JLabel(Bundle.getMessage("RosterId")));
        p.add(rosterIdField);
        rosterPanel.add(p, BorderLayout.NORTH);
        // NOI18N
        rosterIdField.setText(SymbolicProgBundle.getMessage("LabelNewDecoder"));
        // NOI18N
        saveBasicRoster = new JButton(Bundle.getMessage("Save"));
        saveBasicRoster.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                try {
                    log.debug("saveBasicRoster button pressed, calls saveRosterEntry");
                    saveRosterEntry();
                } catch (JmriException ex) {
                    return;
                }
            }
        });
        TitledBorder border = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black));
        // NOI18N
        border.setTitle(Bundle.getMessage("CreateBasicRosterEntry"));
        rosterPanel.setBorder(border);
        rosterPanel.setVisible(false);
        f.getContentPane().add(rosterPanel, BorderLayout.EAST);
    } else {
        // NOI18N
        rosterIdField.setText(SymbolicProgBundle.getMessage("LabelNewDecoder"));
    }
    if (progPane != null) {
        progPane.dispose();
        rosterPanel.remove(progPane);
        progPane = null;
        rosterPanel.revalidate();
        f.getContentPane().repaint();
        f.repaint();
        f.pack();
    }
    if (jmri.InstanceManager.getNullableDefault(jmri.ProgrammerManager.class) != null && jmri.InstanceManager.getDefault(jmri.ProgrammerManager.class).isGlobalProgrammerAvailable()) {
        this.mProgrammer = jmri.InstanceManager.getDefault(jmri.ProgrammerManager.class).getGlobalProgrammer();
    }
    cvModel = new CvTableModel(statusLabel, mProgrammer);
    iCvModel = new IndexedCvTableModel(statusLabel, mProgrammer);
    variableModel = new VariableTableModel(statusLabel, new String[] { "Name", "Value" }, cvModel, iCvModel);
    if (decoderFile != null) {
        Element decoderRoot = null;
        try {
            decoderRoot = decoderFile.rootFromName(DecoderFile.fileLocation + decoderFile.getFilename());
        } catch (JDOMException | IOException e) {
            log.error("Exception while loading decoder XML file: " + decoderFile.getFilename(), e);
            return;
        }
        // NOI18N
        modelElem = decoderFile.getModelElement();
        // NOI18N
        decoderFile.loadVariableModel(decoderRoot.getChild("decoder"), variableModel);
        rosterPanel.setVisible(true);
    } else {
        rosterPanel.setVisible(false);
        return;
    }
    Element programmerRoot;
    XmlFile pf = new XmlFile() {
    };
    // XmlFile is abstract
    java.beans.PropertyChangeListener dccNews = new java.beans.PropertyChangeListener() {

        @Override
        public void propertyChange(java.beans.PropertyChangeEvent e) {
            updateDccAddress();
        }
    };
    // NOI18N
    primaryAddr = variableModel.findVar("Short Address");
    if (primaryAddr == null) {
        // NOI18N
        log.debug("DCC Address monitor didnt find a Short Address variable");
    } else {
        primaryAddr.addPropertyChangeListener(dccNews);
    }
    // NOI18N
    extendAddr = variableModel.findVar("Long Address");
    if (extendAddr == null) {
        // NOI18N
        log.debug("DCC Address monitor didnt find an Long Address variable");
    } else {
        extendAddr.addPropertyChangeListener(dccNews);
    }
    // NOI18N
    addMode = (EnumVariableValue) variableModel.findVar("Address Format");
    if (addMode == null) {
        // NOI18N
        log.debug("DCC Address monitor didnt find an Address Format variable");
    } else {
        addMode.addPropertyChangeListener(dccNews);
    }
    try {
        // NOI18N
        programmerRoot = pf.rootFromName("programmers" + File.separator + "Basic.xml");
        Element base;
        if ((base = programmerRoot.getChild("programmer")) == null) {
            // NOI18N
            // NOI18N
            log.error("xml file top element is not programmer");
            return;
        }
        // for all "pane" elements in the programmer
        // NOI18N
        List<Element> paneList = base.getChildren("pane");
        // NOI18N
        log.debug("will process {} pane definitions", paneList.size());
        String name = jmri.util.jdom.LocaleSelector.getAttribute(paneList.get(0), "name");
        progPane = new ThisProgPane(this, name, paneList.get(0), cvModel, iCvModel, variableModel, modelElem);
        // NOI18N
        progPane.setVariableValue("Short Address", cv1);
        // NOI18N
        progPane.setVariableValue("Long Address", longAddress);
        // NOI18N
        progPane.setCVValue("29", cv29);
        // NOI18N
        progPane.setCVValue("19", cv19);
        rosterPanel.add(progPane, BorderLayout.CENTER);
        rosterPanel.revalidate();
        rosterPanel.setVisible(true);
        f.getContentPane().repaint();
        f.repaint();
        f.pack();
        return;
    } catch (JDOMException | IOException e) {
        // NOI18N
        log.error("exception reading programmer file: ", e);
    }
}
Also used : JPanel(javax.swing.JPanel) JButton(javax.swing.JButton) jmri(jmri) TitledBorder(javax.swing.border.TitledBorder) BorderLayout(java.awt.BorderLayout) CvTableModel(jmri.jmrit.symbolicprog.CvTableModel) IndexedCvTableModel(jmri.jmrit.symbolicprog.IndexedCvTableModel) VariableTableModel(jmri.jmrit.symbolicprog.VariableTableModel) XmlFile(jmri.jmrit.XmlFile) JLabel(javax.swing.JLabel) ActionEvent(java.awt.event.ActionEvent) ActionListener(java.awt.event.ActionListener) IndexedCvTableModel(jmri.jmrit.symbolicprog.IndexedCvTableModel)

Example 4 with XmlFile

use of jmri.jmrit.XmlFile in project JMRI by JMRI.

the class PaneProgFrame method loadProgrammerFile.

protected void loadProgrammerFile(RosterEntry r) {
    // Open and parse programmer file
    XmlFile pf = new XmlFile() {
    };
    // XmlFile is abstract
    try {
        programmerRoot = pf.rootFromName(filename);
        // get the showEmptyPanes attribute, if yes/no update our state
        if (programmerRoot.getChild("programmer").getAttribute("showEmptyPanes") != null) {
            if (log.isDebugEnabled()) {
                log.debug("Found in programmer " + programmerRoot.getChild("programmer").getAttribute("showEmptyPanes").getValue());
            }
            programmerShowEmptyPanes = programmerRoot.getChild("programmer").getAttribute("showEmptyPanes").getValue();
        } else {
            programmerShowEmptyPanes = "";
        }
        if (log.isDebugEnabled()) {
            log.debug("programmerShowEmptyPanes=" + programmerShowEmptyPanes);
        }
        // get extra any panes from the decoder file
        Attribute a;
        if ((a = programmerRoot.getChild("programmer").getAttribute("decoderFilePanes")) != null && a.getValue().equals("yes")) {
            if (decoderRoot != null) {
                decoderPaneList = decoderRoot.getChildren("pane");
            }
        }
        // load programmer config from programmer tree
        readConfig(programmerRoot, r);
    } catch (org.jdom2.JDOMException e) {
        log.error("exception parsing programmer file: " + filename, e);
        // provide traceback too
        e.printStackTrace();
    } catch (java.io.IOException e) {
        log.error("exception reading programmer file: " + filename, e);
        // provide traceback too
        e.printStackTrace();
    }
}
Also used : XmlFile(jmri.jmrit.XmlFile) Attribute(org.jdom2.Attribute)

Example 5 with XmlFile

use of jmri.jmrit.XmlFile in project JMRI by JMRI.

the class SchemaTestBase method validate.

@Test
public void validate() {
    Assume.assumeFalse("Ignoring schema validation.", Boolean.getBoolean("jmri.skipschematests"));
    XmlFile.setDefaultValidate(XmlFile.Validate.CheckDtdThenSchema);
    XmlFile xf = new XmlFileImpl();
    try {
        xf.rootFromFile(file);
        if (!this.pass) {
            Assert.fail("Validation of \"" + file.getPath() + "\" should have failed");
        }
    } catch (IOException | JDOMException ex) {
        // throw unexpected errors
        if (this.pass) {
            Assert.fail("Failed to validate \"" + file.getPath() + "\" due to: " + ex);
        }
    }
}
Also used : XmlFile(jmri.jmrit.XmlFile) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) Test(org.junit.Test)

Aggregations

XmlFile (jmri.jmrit.XmlFile)15 Element (org.jdom2.Element)10 File (java.io.File)8 Document (org.jdom2.Document)6 IOException (java.io.IOException)5 JDOMException (org.jdom2.JDOMException)3 Attribute (org.jdom2.Attribute)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 BorderLayout (java.awt.BorderLayout)1 Component (java.awt.Component)1 Point (java.awt.Point)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 URL (java.net.URL)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 JButton (javax.swing.JButton)1 JComponent (javax.swing.JComponent)1 JFileChooser (javax.swing.JFileChooser)1 JInternalFrame (javax.swing.JInternalFrame)1