001package jmri.jmrit; 002 003import java.awt.Component; 004import java.awt.event.ActionEvent; 005import java.io.File; 006import java.io.IOException; 007 008import javax.swing.JFileChooser; 009 010import jmri.util.swing.JmriAbstractAction; 011import jmri.util.swing.JmriJOptionPane; 012import jmri.util.swing.JmriPanel; 013import jmri.util.swing.WindowInterface; 014 015import org.jdom2.JDOMException; 016 017/** 018 * Make sure an XML file is readable, without doing a DTD or Schema validation. 019 * 020 * @author Bob Jacobsen Copyright (C) 2001, 2005, 2007 021 * @see jmri.jmrit.XmlFile 022 * @see jmri.jmrit.XmlFileValidateAction 023 */ 024public class XmlFileCheckAction extends JmriAbstractAction { 025 026 public XmlFileCheckAction(String s, Component who) { 027 super(s); 028 _who = who; 029 } 030 031 public XmlFileCheckAction(String s, WindowInterface wi) { 032 this(s, wi != null ? wi.getFrame() : null); 033 } 034 035 JFileChooser fci; 036 037 Component _who; 038 039 XmlFile xmlfile = new XmlFile() {}; // odd syntax is due to XmlFile being abstract 040 041 @Override 042 public void actionPerformed(ActionEvent e) { 043 if (fci == null) { 044 fci = jmri.jmrit.XmlFile.userFileChooser("XML files", "xml"); 045 } 046 // request the filename from an open dialog 047 fci.rescanCurrentDirectory(); 048 int retVal = fci.showOpenDialog(_who); 049 // handle selection or cancel 050 if (retVal == JFileChooser.APPROVE_OPTION) { 051 File file = fci.getSelectedFile(); 052 log.debug("located file {} for XML processing", file); 053 // handle the file (later should be outside this thread?) 054 try { 055 xmlfile.setValidate(XmlFile.Validate.None); 056 readFile(file); 057 JmriJOptionPane.showMessageDialog(_who, "OK"); 058 } catch (IOException | JDOMException ex) { 059 JmriJOptionPane.showMessageDialog(_who, "Error: " + ex); 060 } 061 log.debug("parsing complete"); 062 063 } else { 064 log.info("XmlFileCheckAction cancelled in open dialog"); 065 } 066 } 067 068 /** 069 * Read and verify a file is XML. 070 * 071 * @param file the file to read 072 * @throws org.jdom2.JDOMException if file is not XML 073 * @throws java.io.IOException if unable to read file 074 */ 075 void readFile(File file) throws JDOMException, IOException { 076 xmlfile.rootFromFile(file); 077 078 } 079 080 // never invoked, because we overrode actionPerformed above 081 @Override 082 public JmriPanel makePanel() { 083 throw new IllegalArgumentException("Should not be invoked"); 084 } 085 086 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(XmlFileCheckAction.class); 087}