Search in sources :

Example 6 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 7 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 8 with XmlFile

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

the class WebServerManager method preferencesFromMiniServerPreferences.

@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Catch is covering both JDOMException and IOException, FindBugs seems confused")
private void preferencesFromMiniServerPreferences(File MSFile, File WSFile) {
    XmlFile xmlFile = new XmlFile() {
    };
    try {
        Element MSRoot = xmlFile.rootFromFile(MSFile);
        Element WSRoot = new Element(WebServerPreferences.WEB_SERVER_PREFERENCES);
        // NOI18N
        Element MSPrefs = MSRoot.getChild("MiniServerPreferences");
        for (Object pref : MSPrefs.getChildren()) {
            WSRoot.addContent((Element) pref);
        }
        for (Attribute attr : MSPrefs.getAttributes()) {
            if (attr.getName().equals("getDisallowedFrames")) {
                // NOI18N
                Element DF = new Element(WebServerPreferences.DISALLOWED_FRAMES);
                // NOI18N
                String[] frames = attr.getValue().split("\\n");
                for (String frame : frames) {
                    DF.addContent(new Element(WebServerPreferences.FRAME).addContent(frame));
                }
                WSRoot.addContent(DF);
            } else if (attr.getName().equals("getPort")) {
                // NOI18N
                WSRoot.setAttribute(WebServerPreferences.PORT, attr.getValue());
            } else if (attr.getName().equals("getClickDelay")) {
                // NOI18N
                WSRoot.setAttribute(WebServerPreferences.CLICK_DELAY, attr.getValue());
            } else if (attr.getName().equals("getRefreshDelay")) {
                // NOI18N
                WSRoot.setAttribute(WebServerPreferences.REFRESH_DELAY, attr.getValue());
            } else {
                // double cast because clone() is Protected on Object
                WSRoot.setAttribute(attr.clone());
            }
        }
        Document WSDoc = XmlFile.newDocument(WSRoot);
        File parent = new File(WSFile.getParent());
        if (!parent.exists()) {
            // directory known to not exist from previous conditional
            boolean created = parent.mkdir();
            if (!created) {
                log.error("Failed to create directory {}", parent.toString());
                throw new java.io.IOException("Failed to create directory " + parent.toString());
            }
        }
        // known to not exist or this method would not have been called
        boolean created = WSFile.createNewFile();
        if (!created) {
            log.error("Failed to new create file {}", WSFile.toString());
            throw new java.io.IOException("Failed to create new file " + WSFile.toString());
        }
        xmlFile.writeXML(WSFile, WSDoc);
    } catch (IOException | JDOMException ex) {
        log.error("Error converting miniServer preferences to Web Server preferences.", ex);
    }
}
Also used : XmlFile(jmri.jmrit.XmlFile) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) File(java.io.File) XmlFile(jmri.jmrit.XmlFile) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 9 with XmlFile

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

the class ThrottlesPreferences 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 throttles preferences file, may not be complete: " + exp);
    }
    try {
        Element root = new Element("throttles-preferences");
        Document doc = XmlFile.newDocument(root, XmlFile.getDefaultDtdLocation() + "throttles-preferences.dtd");
        // 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) {
        log.warn("Exception in storing throttles preferences xml: " + ex);
    }
    this.dirty = false;
}
Also used : XmlFile(jmri.jmrit.XmlFile) Element(org.jdom2.Element) Document(org.jdom2.Document) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 10 with XmlFile

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

the class VSDFile method init.

protected boolean init() {
    VSDXmlFile xmlfile = new VSDXmlFile();
    initialized = false;
    try {
        // Debug: List all the top-level contents in the file.
        Enumeration<?> entries = this.entries();
        while (entries.hasMoreElements()) {
            ZipEntry z = (ZipEntry) entries.nextElement();
            log.debug("Entry: " + z.getName());
        }
        ZipEntry config = this.getEntry(VSDXmlFileName);
        if (config == null) {
            _statusMsg = "File does not contain " + VSDXmlFileName;
            log.error(_statusMsg);
            return (false);
        }
        File f2 = new File(this.getURL(VSDXmlFileName));
        root = xmlfile.rootFromFile(f2);
        ValidateStatus rv = this.validate(root);
        if (!rv.getValid()) {
            // Need to make this one fancier right here.
            _statusMsg = rv.getMessage();
            log.error("VALIDATE FAILED: File " + VSDXmlFileName);
        }
        initialized = rv.getValid();
        return (initialized);
    } catch (java.io.IOException ioe) {
        _statusMsg = "IO Error auto-loading VSD File: " + VSDXmlFileName + " " + ioe.toString();
        log.warn(_statusMsg);
        return (false);
    } catch (NullPointerException npe) {
        _statusMsg = "NP Error auto-loading VSD File: path = " + VSDXmlFileName + " " + npe.toString();
        log.warn(_statusMsg);
        return (false);
    } catch (org.jdom2.JDOMException ex) {
        _statusMsg = "JDOM Exception loading VSDecoder from path " + VSDXmlFileName + " " + ex.toString();
        log.error(_statusMsg);
        return (false);
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile) XmlFile(jmri.jmrit.XmlFile)

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