FileDocCategorySizeDatePackage
LoadXMLAction.javaAPI DocApache log4j 1.2.154949Sat Aug 25 00:09:40 BST 2007org.apache.log4j.chainsaw

LoadXMLAction

public class LoadXMLAction extends AbstractAction
Encapsulates the action to load an XML file.
author
Oliver Burn
version
1.0

Fields Summary
private static final Logger
LOG
use to log messages
private final JFrame
mParent
the parent frame
private final JFileChooser
mChooser
the file chooser - configured to allow only the selection of a single file.
private final XMLReader
mParser
parser to read XML files
private final XMLFileHandler
mHandler
the content handler
Constructors Summary
LoadXMLAction(JFrame aParent, MyTableModel aModel)
Creates a new LoadXMLAction instance.

param
aParent the parent frame
param
aModel the model to add events to
exception
SAXException if an error occurs
throws
ParserConfigurationException if an error occurs

        mParent = aParent;
        mHandler = new XMLFileHandler(aModel);
        mParser = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
        mParser.setContentHandler(mHandler);
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent aIgnore)
Prompts the user for a file to load events from.

param
aIgnore an ActionEvent value

        LOG.info("load file called");
        if (mChooser.showOpenDialog(mParent) == JFileChooser.APPROVE_OPTION) {
            LOG.info("Need to load a file");
            final File chosen = mChooser.getSelectedFile();
            LOG.info("loading the contents of " + chosen.getAbsolutePath());
            try {
                final int num = loadFile(chosen.getAbsolutePath());
                JOptionPane.showMessageDialog(
                    mParent,
                    "Loaded " + num + " events.",
                    "CHAINSAW",
                    JOptionPane.INFORMATION_MESSAGE);
            } catch (Exception e) {
                LOG.warn("caught an exception loading the file", e);
                JOptionPane.showMessageDialog(
                    mParent,
                    "Error parsing file - " + e.getMessage(),
                    "CHAINSAW",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
    
private intloadFile(java.lang.String aFile)
Loads the contents of file into the model

param
aFile the file to extract events from
return
the number of events loaded
throws
SAXException if an error occurs
throws
IOException if an error occurs

        synchronized (mParser) {
            // Create a dummy document to parse the file
            final StringBuffer buf = new StringBuffer();
            buf.append("<?xml version=\"1.0\" standalone=\"yes\"?>\n");
            buf.append("<!DOCTYPE log4j:eventSet ");
            buf.append("[<!ENTITY data SYSTEM \"file:///");
            buf.append(aFile);
            buf.append("\">]>\n");
            buf.append("<log4j:eventSet xmlns:log4j=\"Claira\">\n");
            buf.append("&data;\n");
            buf.append("</log4j:eventSet>\n");

            final InputSource is =
                new InputSource(new StringReader(buf.toString()));
            mParser.parse(is);
            return mHandler.getNumEvents();
        }