001: /*
002: * Copyright (C) The Apache Software Foundation. All rights reserved.
003: *
004: * This software is published under the terms of the Apache Software
005: * License version 1.1, a copy of which has been included with this
006: * distribution in the APACHE.txt file. */
007: package org.jahia.sqlprofiler.gui;
008:
009: import java.awt.event.ActionEvent;
010: import java.io.File;
011: import java.io.IOException;
012: import java.io.StringReader;
013: import javax.swing.AbstractAction;
014: import javax.swing.JFileChooser;
015: import javax.swing.JFrame;
016: import javax.swing.JOptionPane;
017: import javax.xml.parsers.ParserConfigurationException;
018: import javax.xml.parsers.SAXParserFactory;
019: import org.apache.log4j.Category;
020: import org.xml.sax.InputSource;
021: import org.xml.sax.SAXException;
022: import org.xml.sax.XMLReader;
023:
024: /**
025: * Encapsulates the action to load an XML file.
026: *
027: * @author <a href="mailto:oliver@puppycrawl.com">Oliver Burn</a>
028: * @version 1.0
029: */
030: class LoadXMLAction extends AbstractAction {
031: /** use to log messages **/
032: private static final Category LOG = Category
033: .getInstance(LoadXMLAction.class);
034:
035: /** the parent frame **/
036: private final JFrame mParent;
037:
038: /**
039: * the file chooser - configured to allow only the selection of a
040: * single file.
041: */
042: private final JFileChooser mChooser = new JFileChooser();
043: {
044: mChooser.setMultiSelectionEnabled(false);
045: mChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
046: }
047:
048: /** parser to read XML files **/
049: private final XMLReader mParser;
050: /** the content handler **/
051: private final XMLFileHandler mHandler;
052:
053: /**
054: * Creates a new <code>LoadXMLAction</code> instance.
055: *
056: * @param aParent the parent frame
057: * @param aModel the model to add events to
058: * @exception SAXException if an error occurs
059: * @throws ParserConfigurationException if an error occurs
060: */
061: LoadXMLAction(JFrame aParent, LoggerTableModel aModel)
062: throws SAXException, ParserConfigurationException {
063: mParent = aParent;
064: mHandler = new XMLFileHandler(aModel);
065: mParser = SAXParserFactory.newInstance().newSAXParser()
066: .getXMLReader();
067: mParser.setContentHandler(mHandler);
068: }
069:
070: /**
071: * Prompts the user for a file to load events from.
072: * @param aIgnore an <code>ActionEvent</code> value
073: */
074: public void actionPerformed(ActionEvent aIgnore) {
075: LOG.info("load file called");
076: if (mChooser.showOpenDialog(mParent) == JFileChooser.APPROVE_OPTION) {
077: LOG.info("Need to load a file");
078: final File chosen = mChooser.getSelectedFile();
079: LOG.info("loading the contents of "
080: + chosen.getAbsolutePath());
081: try {
082: final int num = loadFile(chosen.getAbsolutePath());
083: JOptionPane.showMessageDialog(mParent, "Loaded " + num
084: + " events.", "CHAINSAW",
085: JOptionPane.INFORMATION_MESSAGE);
086: } catch (Exception e) {
087: LOG.warn("caught an exception loading the file", e);
088: JOptionPane.showMessageDialog(mParent,
089: "Error parsing file - " + e.getMessage(),
090: "CHAINSAW", JOptionPane.ERROR_MESSAGE);
091: }
092: }
093: }
094:
095: /**
096: * Loads the contents of file into the model
097: *
098: * @param aFile the file to extract events from
099: * @return the number of events loaded
100: * @throws SAXException if an error occurs
101: * @throws IOException if an error occurs
102: */
103: private int loadFile(String aFile) throws SAXException, IOException {
104: synchronized (mParser) {
105: // Create a dummy document to parse the file
106: final StringBuffer buf = new StringBuffer();
107: buf.append("<?xml version=\"1.0\" standalone=\"yes\"?>\n");
108: buf.append("<!DOCTYPE log4j:eventSet ");
109: buf.append("[<!ENTITY data SYSTEM \"file:///");
110: buf.append(aFile);
111: buf.append("\">]>\n");
112: buf.append("<log4j:eventSet xmlns:log4j=\"Claira\">\n");
113: buf.append("&data;\n");
114: buf.append("</log4j:eventSet>\n");
115:
116: final InputSource is = new InputSource(new StringReader(buf
117: .toString()));
118: mParser.parse(is);
119: return mHandler.getNumEvents();
120: }
121: }
122: }
|