KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > sqlprofiler > gui > LoadXMLAction


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software
5  * License version 1.1, a copy of which has been included with this
6  * distribution in the APACHE.txt file. */

7 package org.jahia.sqlprofiler.gui;
8
9 import java.awt.event.ActionEvent JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.StringReader JavaDoc;
13 import javax.swing.AbstractAction JavaDoc;
14 import javax.swing.JFileChooser JavaDoc;
15 import javax.swing.JFrame JavaDoc;
16 import javax.swing.JOptionPane JavaDoc;
17 import javax.xml.parsers.ParserConfigurationException JavaDoc;
18 import javax.xml.parsers.SAXParserFactory JavaDoc;
19 import org.apache.log4j.Category;
20 import org.xml.sax.InputSource JavaDoc;
21 import org.xml.sax.SAXException JavaDoc;
22 import org.xml.sax.XMLReader JavaDoc;
23
24 /**
25  * Encapsulates the action to load an XML file.
26  *
27  * @author <a HREF="mailto:oliver@puppycrawl.com">Oliver Burn</a>
28  * @version 1.0
29  */

30 class LoadXMLAction
31     extends AbstractAction JavaDoc
32 {
33     /** use to log messages **/
34     private static final Category LOG =
35         Category.getInstance(LoadXMLAction.class);
36
37     /** the parent frame **/
38     private final JFrame JavaDoc mParent;
39
40     /**
41      * the file chooser - configured to allow only the selection of a
42      * single file.
43      */

44     private final JFileChooser JavaDoc mChooser = new JFileChooser JavaDoc();
45     {
46         mChooser.setMultiSelectionEnabled(false);
47         mChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
48     }
49
50     /** parser to read XML files **/
51     private final XMLReader JavaDoc mParser;
52     /** the content handler **/
53     private final XMLFileHandler mHandler;
54
55
56     /**
57      * Creates a new <code>LoadXMLAction</code> instance.
58      *
59      * @param aParent the parent frame
60      * @param aModel the model to add events to
61      * @exception SAXException if an error occurs
62      * @throws ParserConfigurationException if an error occurs
63      */

64     LoadXMLAction(JFrame JavaDoc aParent, LoggerTableModel aModel)
65         throws SAXException JavaDoc, ParserConfigurationException JavaDoc
66     {
67         mParent = aParent;
68         mHandler = new XMLFileHandler(aModel);
69         mParser = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
70         mParser.setContentHandler(mHandler);
71     }
72
73     /**
74      * Prompts the user for a file to load events from.
75      * @param aIgnore an <code>ActionEvent</code> value
76      */

77     public void actionPerformed(ActionEvent JavaDoc aIgnore) {
78         LOG.info("load file called");
79         if (mChooser.showOpenDialog(mParent) == JFileChooser.APPROVE_OPTION) {
80             LOG.info("Need to load a file");
81             final File JavaDoc chosen = mChooser.getSelectedFile();
82             LOG.info("loading the contents of " + chosen.getAbsolutePath());
83             try {
84                 final int num = loadFile(chosen.getAbsolutePath());
85                 JOptionPane.showMessageDialog(
86                     mParent,
87                     "Loaded " + num + " events.",
88                     "CHAINSAW",
89                     JOptionPane.INFORMATION_MESSAGE);
90             } catch (Exception JavaDoc e) {
91                 LOG.warn("caught an exception loading the file", e);
92                 JOptionPane.showMessageDialog(
93                     mParent,
94                     "Error parsing file - " + e.getMessage(),
95                     "CHAINSAW",
96                     JOptionPane.ERROR_MESSAGE);
97             }
98         }
99     }
100
101     /**
102      * Loads the contents of file into the model
103      *
104      * @param aFile the file to extract events from
105      * @return the number of events loaded
106      * @throws SAXException if an error occurs
107      * @throws IOException if an error occurs
108      */

109     private int loadFile(String JavaDoc aFile)
110         throws SAXException JavaDoc, IOException JavaDoc
111     {
112         synchronized (mParser) {
113             // Create a dummy document to parse the file
114
final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
115             buf.append("<?xml version=\"1.0\" standalone=\"yes\"?>\n");
116             buf.append("<!DOCTYPE log4j:eventSet ");
117             buf.append("[<!ENTITY data SYSTEM \"file:///");
118             buf.append(aFile);
119             buf.append("\">]>\n");
120             buf.append("<log4j:eventSet xmlns:log4j=\"Claira\">\n");
121             buf.append("&data;\n");
122             buf.append("</log4j:eventSet>\n");
123
124             final InputSource JavaDoc is =
125                 new InputSource JavaDoc(new StringReader JavaDoc(buf.toString()));
126             mParser.parse(is);
127             return mHandler.getNumEvents();
128         }
129     }
130 }
131
Popular Tags