LoadXMLAction xref

View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.log4j.chainsaw;
    18
    19  import java.awt.event.ActionEvent;
    20  import java.io.File;
    21  import java.io.IOException;
    22  import java.io.StringReader;
    23  import javax.swing.AbstractAction;
    24  import javax.swing.JFileChooser;
    25  import javax.swing.JFrame;
    26  import javax.swing.JOptionPane;
    27  import javax.xml.parsers.ParserConfigurationException;
    28  import javax.xml.parsers.SAXParserFactory;
    29  import org.apache.log4j.Logger;
    30  import org.xml.sax.InputSource;
    31  import org.xml.sax.SAXException;
    32  import org.xml.sax.XMLReader;
    33
    34  /**
    35   * Encapsulates the action to load an XML file.
    36   *
    37   * @author <a href="mailto:oliver@puppycrawl.com">Oliver Burn</a>
    38   * @version 1.0
    39   */
    40  class LoadXMLAction
    41      extends AbstractAction
    42  {
    43      /** use to log messages **/
    44      private static final Logger LOG = Logger.getLogger(LoadXMLAction.class);
    45
    46      /** the parent frame **/
    47      private final JFrame mParent;
    48
    49      /**
    50       * the file chooser - configured to allow only the selection of a
    51       * single file.
    52       */
    53      private final JFileChooser mChooser = new JFileChooser();
    54      {
    55          mChooser.setMultiSelectionEnabled(false);
    56          mChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    57      }
    58
    59      /** parser to read XML files **/
    60      private final XMLReader mParser;
    61      /** the content handler **/
    62      private final XMLFileHandler mHandler;
    63
    64
    65      /**
    66       * Creates a new <code>LoadXMLAction</code> instance.
    67       *
    68       * @param aParent the parent frame
    69       * @param aModel the model to add events to
    70       * @exception SAXException if an error occurs
    71       * @throws ParserConfigurationException if an error occurs
    72       */
    73      LoadXMLAction(JFrame aParent, MyTableModel aModel)
    74          throws SAXException, ParserConfigurationException
    75      {
    76          mParent = aParent;
    77          mHandler = new XMLFileHandler(aModel);
    78          mParser = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
    79          mParser.setContentHandler(mHandler);
    80      }
    81
    82      /**
    83       * Prompts the user for a file to load events from.
    84       * @param aIgnore an <code>ActionEvent</code> value
    85       */
    86      public void actionPerformed(ActionEvent aIgnore) {
    87          LOG.info("load file called");
    88          if (mChooser.showOpenDialog(mParent) == JFileChooser.APPROVE_OPTION) {
    89              LOG.info("Need to load a file");
    90              final File chosen = mChooser.getSelectedFile();
    91              LOG.info("loading the contents of " + chosen.getAbsolutePath());
    92              try {
    93                  final int num = loadFile(chosen.getAbsolutePath());
    94                  JOptionPane.showMessageDialog(
    95                      mParent,
    96                      "Loaded " + num + " events.",
    97                      "CHAINSAW",
    98                      JOptionPane.INFORMATION_MESSAGE);
    99              } catch (Exception e) {
    100                 LOG.warn("caught an exception loading the file", e);
    101                 JOptionPane.showMessageDialog(
    102                     mParent,
    103                     "Error parsing file - " + e.getMessage(),
    104                     "CHAINSAW",
    105                     JOptionPane.ERROR_MESSAGE);
    106             }
    107         }
    108     }
    109
    110     /**
    111      * Loads the contents of file into the model
    112      *
    113      * @param aFile the file to extract events from
    114      * @return the number of events loaded
    115      * @throws SAXException if an error occurs
    116      * @throws IOException if an error occurs
    117      */
    118     private int loadFile(String aFile)
    119         throws SAXException, IOException
    120     {
    121         synchronized (mParser) {
    122             // Create a dummy document to parse the file
    123             final StringBuffer buf = new StringBuffer();
    124             buf.append("<?xml version=\"1.0\" standalone=\"yes\"?>\n");
    125             buf.append("<!DOCTYPE log4j:eventSet ");
    126             buf.append("[<!ENTITY data SYSTEM \"file:///");
    127             buf.append(aFile);
    128             buf.append("\">]>\n");
    129             buf.append("<log4j:eventSet xmlns:log4j=\"Claira\">\n");
    130             buf.append("&data;\n");
    131             buf.append("</log4j:eventSet>\n");
    132
    133             final InputSource is =
    134                 new InputSource(new StringReader(buf.toString()));
    135             mParser.parse(is);
    136             return mHandler.getNumEvents();
    137         }
    138     }
    139 }