KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > chainsaw > LoadXMLAction


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.log4j.chainsaw;
17
18 import java.awt.event.ActionEvent JavaDoc;
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import javax.swing.AbstractAction JavaDoc;
23 import javax.swing.JFileChooser JavaDoc;
24 import javax.swing.JFrame JavaDoc;
25 import javax.swing.JOptionPane JavaDoc;
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27 import javax.xml.parsers.SAXParserFactory JavaDoc;
28 import org.apache.log4j.Logger;
29 import org.xml.sax.InputSource JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31 import org.xml.sax.XMLReader JavaDoc;
32
33 /**
34  * Encapsulates the action to load an XML file.
35  *
36  * @author <a HREF="mailto:oliver@puppycrawl.com">Oliver Burn</a>
37  * @version 1.0
38  */

39 class LoadXMLAction
40     extends AbstractAction JavaDoc
41 {
42     /** use to log messages **/
43     private static final Logger LOG = Logger.getLogger(LoadXMLAction.class);
44
45     /** the parent frame **/
46     private final JFrame JavaDoc mParent;
47
48     /**
49      * the file chooser - configured to allow only the selection of a
50      * single file.
51      */

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

72     LoadXMLAction(JFrame JavaDoc aParent, MyTableModel aModel)
73         throws SAXException JavaDoc, ParserConfigurationException JavaDoc
74     {
75         mParent = aParent;
76         mHandler = new XMLFileHandler(aModel);
77         mParser = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
78         mParser.setContentHandler(mHandler);
79     }
80
81     /**
82      * Prompts the user for a file to load events from.
83      * @param aIgnore an <code>ActionEvent</code> value
84      */

85     public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc 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 JavaDoc 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     /**
110      * Loads the contents of file into the model
111      *
112      * @param aFile the file to extract events from
113      * @return the number of events loaded
114      * @throws SAXException if an error occurs
115      * @throws IOException if an error occurs
116      */

117     private int loadFile(String JavaDoc aFile)
118         throws SAXException JavaDoc, IOException JavaDoc
119     {
120         synchronized (mParser) {
121             // Create a dummy document to parse the file
122
final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc is =
133                 new InputSource JavaDoc(new StringReader JavaDoc(buf.toString()));
134             mParser.parse(is);
135             return mHandler.getNumEvents();
136         }
137     }
138 }
139
Popular Tags