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
    18  package org.apache.log4j.chainsaw;
    19
    20  import org.apache.log4j.chainsaw.icons.ChainsawIcons;
    21  import org.apache.log4j.helpers.LogLog;
    22  import org.apache.log4j.xml.XMLLayout;
    23
    24  import javax.swing.*;
    25  import java.awt.*;
    26  import java.awt.event.ActionEvent;
    27  import java.awt.event.KeyEvent;
    28  import java.io.*;
    29  import java.util.List;
    30  import java.util.Locale;
    31  import java.util.zip.ZipEntry;
    32  import java.util.zip.ZipOutputStream;
    33
    34
    35  /**
    36   * Allows the user to specify a particular file to which the current tab's
    37   * displayed events will be saved.
    38   *
    39   * @author Scott Deboy &lt;sdeboy@apache.org&gt;
    40   * @author Paul Smith &lt;psmith@apache.org&gt;
    41   * @author Stephen Pain
    42   */
    43  class FileSaveAction extends AbstractAction {
    44      private LogUI parent;
    45      private JFileChooser chooser = null;
    46
    47      /**
    48       * This action must have a reference to a LogUI
    49       * in order to retrieve events to save
    50       */
    51      public FileSaveAction(LogUI parent) {
    52          super("Save displayed events as...");
    53
    54          putValue(
    55              Action.ACCELERATOR_KEY,
    56              KeyStroke.getKeyStroke(KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    57          putValue(Action.MNEMONIC_KEY, KeyEvent.VK_S);
    58          putValue(
    59              Action.SHORT_DESCRIPTION, "Saves displayed events for the current tab");
    60          putValue(Action.SMALL_ICON, new ImageIcon(ChainsawIcons.FILE_SAVE_AS));
    61          this.parent = parent;
    62      }
    63
    64      /*
    65       * When the user chooses the Save action,
    66       * a File chooser is presented to allow them to
    67       * find an XML file to save events to.
    68       *
    69       * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
    70       */
    71      public void actionPerformed(ActionEvent e) {
    72
    73          if (chooser == null) {
    74              chooser = new JFileChooser();
    75          }
    76
    77          chooser.setAcceptAllFileFilterUsed(true);
    78          chooser.setDialogTitle("Save displayed events (XML or .zipped XML)...");
    79          chooser.showSaveDialog(parent);
    80
    81          File selectedFile = chooser.getSelectedFile();
    82
    83          if (selectedFile != null) {
    84              List v = parent.getCurrentLogPanel().getFilteredEvents();
    85
    86              if (((v != null) && (v.size() == 0)) || (v == null)) {
    87                  //no events to save
    88                  return;
    89              }
    90
    91              XMLLayout layout = new XMLLayout();
    92              layout.setProperties(true);
    93              boolean saveAsZip = selectedFile.getName().toLowerCase(Locale.ENGLISH).endsWith(".zip");
    94              Writer writer = null;
    95              try {
    96                  if (saveAsZip) {
    97                      ZipOutputStream zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(selectedFile)));
    98                      ZipEntry entry = new ZipEntry(selectedFile.getName().substring(0, selectedFile.getName().length() - ".zip".length()) + ".xml");
    99                      zipOutput.putNextEntry(entry);
    100                     writer = new OutputStreamWriter(zipOutput);
    101                 } else {
    102                     writer = new BufferedWriter(new FileWriter(selectedFile));
    103                 }
    104                 for (Object aV : v) {
    105                     LoggingEventWrapper loggingEventWrapper = (LoggingEventWrapper) aV;
    106                     layout.setLocationInfo(loggingEventWrapper.getLoggingEvent().getThrowableInformation() != null);
    107                     writer.write(layout.format(loggingEventWrapper.getLoggingEvent()));
    108                 }
    109             } catch (IOException ioe) {
    110                 LogLog.warn("Unable to save file", ioe);
    111             } finally {
    112                 if (writer != null) {
    113                     try {
    114                         writer.flush();
    115                         writer.close();
    116                     } catch (IOException e1) {
    117                         //ignore
    118                     }
    119                 }
    120             }
    121         }
    122     }
    123 }