1 16 package org.apache.log4j.chainsaw; 17 18 import java.awt.event.ActionEvent ; 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.StringReader ; 22 import javax.swing.AbstractAction ; 23 import javax.swing.JFileChooser ; 24 import javax.swing.JFrame ; 25 import javax.swing.JOptionPane ; 26 import javax.xml.parsers.ParserConfigurationException ; 27 import javax.xml.parsers.SAXParserFactory ; 28 import org.apache.log4j.Logger; 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.SAXException ; 31 import org.xml.sax.XMLReader ; 32 33 39 class LoadXMLAction 40 extends AbstractAction 41 { 42 43 private static final Logger LOG = Logger.getLogger(LoadXMLAction.class); 44 45 46 private final JFrame mParent; 47 48 52 private final JFileChooser mChooser = new JFileChooser (); 53 { 54 mChooser.setMultiSelectionEnabled(false); 55 mChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 56 } 57 58 59 private final XMLReader mParser; 60 61 private final XMLFileHandler mHandler; 62 63 64 72 LoadXMLAction(JFrame aParent, MyTableModel aModel) 73 throws SAXException , ParserConfigurationException 74 { 75 mParent = aParent; 76 mHandler = new XMLFileHandler(aModel); 77 mParser = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 78 mParser.setContentHandler(mHandler); 79 } 80 81 85 public void actionPerformed(ActionEvent aIgnore) { 86 LOG.info("load file called"); 87 if (mChooser.showOpenDialog(mParent) == JFileChooser.APPROVE_OPTION) { 88 LOG.info("Need to load a file"); 89 final File chosen = mChooser.getSelectedFile(); 90 LOG.info("loading the contents of " + chosen.getAbsolutePath()); 91 try { 92 final int num = loadFile(chosen.getAbsolutePath()); 93 JOptionPane.showMessageDialog( 94 mParent, 95 "Loaded " + num + " events.", 96 "CHAINSAW", 97 JOptionPane.INFORMATION_MESSAGE); 98 } catch (Exception e) { 99 LOG.warn("caught an exception loading the file", e); 100 JOptionPane.showMessageDialog( 101 mParent, 102 "Error parsing file - " + e.getMessage(), 103 "CHAINSAW", 104 JOptionPane.ERROR_MESSAGE); 105 } 106 } 107 } 108 109 117 private int loadFile(String aFile) 118 throws SAXException , IOException 119 { 120 synchronized (mParser) { 121 final StringBuffer buf = new StringBuffer (); 123 buf.append("<?xml version=\"1.0\" standalone=\"yes\"?>\n"); 124 buf.append("<!DOCTYPE log4j:eventSet "); 125 buf.append("[<!ENTITY data SYSTEM \"file:///"); 126 buf.append(aFile); 127 buf.append("\">]>\n"); 128 buf.append("<log4j:eventSet xmlns:log4j=\"Claira\">\n"); 129 buf.append("&data;\n"); 130 buf.append("</log4j:eventSet>\n"); 131 132 final InputSource is = 133 new InputSource (new StringReader (buf.toString())); 134 mParser.parse(is); 135 return mHandler.getNumEvents(); 136 } 137 } 138 } 139 | Popular Tags |