From 637ce351ffe3604fcb44889ddfd40f36a9fb0f43 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 3 Feb 2008 18:06:12 +0000 Subject: [PATCH] validate filenames (when dragging lists to files) --- build.xml | 8 +-- ...ar_v1_6_2_d07_02_07.jar => simmetrics.jar} | Bin .../sourceforge/filebot/ui/FileBotUtil.java | 12 ++++ .../filebot/ui/transfer/ClipboardHandler.java | 13 ++++ .../ui/transfer/DefaultClipboardHandler.java | 63 ++++++++++++++++++ .../ui/transfer/DefaultTransferHandler.java | 56 +++------------- .../filebot/ui/transfer/SaveAction.java | 3 +- .../ui/transfer/SaveableExportHandler.java | 5 +- 8 files changed, 106 insertions(+), 54 deletions(-) rename lib/{simmetrics_jar_v1_6_2_d07_02_07.jar => simmetrics.jar} (100%) create mode 100644 source/net/sourceforge/filebot/ui/transfer/ClipboardHandler.java create mode 100644 source/net/sourceforge/filebot/ui/transfer/DefaultClipboardHandler.java diff --git a/build.xml b/build.xml index 64b04209..7b56e8f4 100644 --- a/build.xml +++ b/build.xml @@ -13,7 +13,7 @@ - + @@ -33,11 +33,11 @@ - + - - + + diff --git a/lib/simmetrics_jar_v1_6_2_d07_02_07.jar b/lib/simmetrics.jar similarity index 100% rename from lib/simmetrics_jar_v1_6_2_d07_02_07.jar rename to lib/simmetrics.jar diff --git a/source/net/sourceforge/filebot/ui/FileBotUtil.java b/source/net/sourceforge/filebot/ui/FileBotUtil.java index c9c9d881..cf661e07 100644 --- a/source/net/sourceforge/filebot/ui/FileBotUtil.java +++ b/source/net/sourceforge/filebot/ui/FileBotUtil.java @@ -20,4 +20,16 @@ public class FileBotUtil { component.getActionMap().put(key, action); } + + /** + * Strip string of invalid characters + * + * @param filename original filename + * @return filename stripped of invalid characters + */ + public static String validateFileName(String filename) { + // strip \, /, :, *, ?, ", <, > and | + return filename.replaceAll("[\\\\/:*?\"<>|]", ""); + } + } diff --git a/source/net/sourceforge/filebot/ui/transfer/ClipboardHandler.java b/source/net/sourceforge/filebot/ui/transfer/ClipboardHandler.java new file mode 100644 index 00000000..fab9ca53 --- /dev/null +++ b/source/net/sourceforge/filebot/ui/transfer/ClipboardHandler.java @@ -0,0 +1,13 @@ + +package net.sourceforge.filebot.ui.transfer; + + +import java.awt.datatransfer.Clipboard; + +import javax.swing.JComponent; + + +public interface ClipboardHandler { + + public void exportToClipboard(JComponent comp, Clipboard clip, int action) throws IllegalStateException; +} diff --git a/source/net/sourceforge/filebot/ui/transfer/DefaultClipboardHandler.java b/source/net/sourceforge/filebot/ui/transfer/DefaultClipboardHandler.java new file mode 100644 index 00000000..909e0a4c --- /dev/null +++ b/source/net/sourceforge/filebot/ui/transfer/DefaultClipboardHandler.java @@ -0,0 +1,63 @@ + +package net.sourceforge.filebot.ui.transfer; + + +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.StringSelection; +import java.util.ArrayList; +import java.util.Iterator; + +import javax.swing.JComponent; +import javax.swing.JList; +import javax.swing.JTable; +import javax.swing.JTree; +import javax.swing.tree.TreePath; + + +public class DefaultClipboardHandler implements ClipboardHandler { + + @Override + public void exportToClipboard(JComponent comp, Clipboard clip, int action) throws IllegalStateException { + ArrayList lines = new ArrayList(); + + if (comp instanceof JList) { + JList list = (JList) comp; + for (Object value : list.getSelectedValues()) { + lines.add(value.toString()); + } + } else if (comp instanceof JTree) { + JTree tree = (JTree) comp; + for (TreePath path : tree.getSelectionPaths()) { + lines.add(path.getLastPathComponent().toString()); + } + } else if (comp instanceof JTable) { + JTable table = (JTable) comp; + + for (int row : table.getSelectedRows()) { + StringBuffer b = new StringBuffer(); + int maxCol = table.getColumnCount() - 1; + for (int col = 0; col <= maxCol; col++) { + b.append(table.getModel().getValueAt(row, col)); + + if (col != maxCol) + b.append("\t"); + } + + lines.add(b.toString()); + } + } + + StringBuffer buffer = new StringBuffer(); + Iterator it = lines.iterator(); + + while (it.hasNext()) { + buffer.append(it.next()); + + if (it.hasNext()) + buffer.append("\n"); + } + + clip.setContents(new StringSelection(buffer.toString()), null); + } + +} diff --git a/source/net/sourceforge/filebot/ui/transfer/DefaultTransferHandler.java b/source/net/sourceforge/filebot/ui/transfer/DefaultTransferHandler.java index d22b57e9..947fc75c 100644 --- a/source/net/sourceforge/filebot/ui/transfer/DefaultTransferHandler.java +++ b/source/net/sourceforge/filebot/ui/transfer/DefaultTransferHandler.java @@ -3,30 +3,30 @@ package net.sourceforge.filebot.ui.transfer; import java.awt.datatransfer.Clipboard; -import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; -import java.util.ArrayList; -import java.util.Iterator; import javax.swing.JComponent; -import javax.swing.JList; -import javax.swing.JTable; -import javax.swing.JTree; import javax.swing.TransferHandler; -import javax.swing.tree.TreePath; public class DefaultTransferHandler extends TransferHandler { private ImportHandler importHandler; private ExportHandler exportHandler; + private ClipboardHandler clipboardHandler; private boolean dragging = false; public DefaultTransferHandler(ImportHandler importHandler, ExportHandler exportHandler) { + this(importHandler, exportHandler, new DefaultClipboardHandler()); + } + + + public DefaultTransferHandler(ImportHandler importHandler, ExportHandler exportHandler, ClipboardHandler clipboardHandler) { this.importHandler = importHandler; this.exportHandler = exportHandler; + this.clipboardHandler = clipboardHandler; } @@ -89,46 +89,8 @@ public class DefaultTransferHandler extends TransferHandler { @Override public void exportToClipboard(JComponent comp, Clipboard clip, int action) throws IllegalStateException { - ArrayList lines = new ArrayList(); - - if (comp instanceof JList) { - JList list = (JList) comp; - for (Object value : list.getSelectedValues()) { - lines.add(value.toString()); - } - } else if (comp instanceof JTree) { - JTree tree = (JTree) comp; - for (TreePath path : tree.getSelectionPaths()) { - lines.add(path.getLastPathComponent().toString()); - } - } else if (comp instanceof JTable) { - JTable table = (JTable) comp; - - for (int row : table.getSelectedRows()) { - StringBuffer b = new StringBuffer(); - int maxCol = table.getColumnCount() - 1; - for (int col = 0; col <= maxCol; col++) { - b.append(table.getModel().getValueAt(row, col)); - - if (col != maxCol) - b.append("\t"); - } - - lines.add(b.toString()); - } - } - - StringBuffer buffer = new StringBuffer(); - Iterator it = lines.iterator(); - - while (it.hasNext()) { - buffer.append(it.next()); - - if (it.hasNext()) - buffer.append("\n"); - } - - clip.setContents(new StringSelection(buffer.toString()), null); + if (clipboardHandler != null) + clipboardHandler.exportToClipboard(comp, clip, action); } } diff --git a/source/net/sourceforge/filebot/ui/transfer/SaveAction.java b/source/net/sourceforge/filebot/ui/transfer/SaveAction.java index 10c7dd81..ecc4a3c0 100644 --- a/source/net/sourceforge/filebot/ui/transfer/SaveAction.java +++ b/source/net/sourceforge/filebot/ui/transfer/SaveAction.java @@ -9,6 +9,7 @@ import javax.swing.AbstractAction; import javax.swing.JFileChooser; import net.sourceforge.filebot.resources.ResourceManager; +import net.sourceforge.filebot.ui.FileBotUtil; public class SaveAction extends AbstractAction { @@ -44,7 +45,7 @@ public class SaveAction extends AbstractAction { JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(false); - chooser.setSelectedFile(new File(getDefaultFileName())); + chooser.setSelectedFile(new File(FileBotUtil.validateFileName(getDefaultFileName()))); if (chooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) return; diff --git a/source/net/sourceforge/filebot/ui/transfer/SaveableExportHandler.java b/source/net/sourceforge/filebot/ui/transfer/SaveableExportHandler.java index be144f55..2695c51f 100644 --- a/source/net/sourceforge/filebot/ui/transfer/SaveableExportHandler.java +++ b/source/net/sourceforge/filebot/ui/transfer/SaveableExportHandler.java @@ -11,6 +11,8 @@ import java.util.List; import javax.swing.JComponent; import javax.swing.TransferHandler; +import net.sourceforge.filebot.ui.FileBotUtil; + public class SaveableExportHandler implements ExportHandler { @@ -51,7 +53,7 @@ public class SaveableExportHandler implements ExportHandler { @Override public Transferable createTransferable(JComponent c) { try { - File temporaryFile = new File(tmpdir, saveable.getDefaultFileName()); + File temporaryFile = new File(tmpdir, FileBotUtil.validateFileName(saveable.getDefaultFileName())); temporaryFile.createNewFile(); saveable.save(temporaryFile); @@ -62,5 +64,4 @@ public class SaveableExportHandler implements ExportHandler { return null; } - }