validate filenames (when dragging lists to files)

This commit is contained in:
Reinhard Pointner 2008-02-03 18:06:12 +00:00
parent 5a490faca2
commit 637ce351ff
8 changed files with 106 additions and 54 deletions

View File

@ -13,7 +13,7 @@
<property name="lib.nekohtml" value="nekohtml.jar" /> <property name="lib.nekohtml" value="nekohtml.jar" />
<property name="lib.xerces" value="xercesImpl.jar" /> <property name="lib.xerces" value="xercesImpl.jar" />
<property name="lib.simmetrics" value="simmetrics_jar_v1_6_2_d07_02_07.jar" /> <property name="lib.simmetrics" value="simmetrics.jar" />
<target name="jar" depends="build"> <target name="jar" depends="build">
@ -36,7 +36,7 @@
</zipfileset> </zipfileset>
<zipfileset src="${lib}/${lib.simmetrics}"> <zipfileset src="${lib}/${lib.simmetrics}">
<include if="fatjar" name="**/*.class" /> <include if="fatjar" name="**/*.class" />
<exclude name="**/*Test*" /> <exclude if="fatjar" name="**/*Test*" />
</zipfileset> </zipfileset>
</jar> </jar>
</target> </target>

View File

@ -20,4 +20,16 @@ public class FileBotUtil {
component.getActionMap().put(key, action); 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("[\\\\/:*?\"<>|]", "");
}
} }

View File

@ -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;
}

View File

@ -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<String> lines = new ArrayList<String>();
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<String> it = lines.iterator();
while (it.hasNext()) {
buffer.append(it.next());
if (it.hasNext())
buffer.append("\n");
}
clip.setContents(new StringSelection(buffer.toString()), null);
}
}

View File

@ -3,30 +3,30 @@ package net.sourceforge.filebot.ui.transfer;
import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.TransferHandler; import javax.swing.TransferHandler;
import javax.swing.tree.TreePath;
public class DefaultTransferHandler extends TransferHandler { public class DefaultTransferHandler extends TransferHandler {
private ImportHandler importHandler; private ImportHandler importHandler;
private ExportHandler exportHandler; private ExportHandler exportHandler;
private ClipboardHandler clipboardHandler;
private boolean dragging = false; private boolean dragging = false;
public DefaultTransferHandler(ImportHandler importHandler, ExportHandler exportHandler) { public DefaultTransferHandler(ImportHandler importHandler, ExportHandler exportHandler) {
this(importHandler, exportHandler, new DefaultClipboardHandler());
}
public DefaultTransferHandler(ImportHandler importHandler, ExportHandler exportHandler, ClipboardHandler clipboardHandler) {
this.importHandler = importHandler; this.importHandler = importHandler;
this.exportHandler = exportHandler; this.exportHandler = exportHandler;
this.clipboardHandler = clipboardHandler;
} }
@ -89,46 +89,8 @@ public class DefaultTransferHandler extends TransferHandler {
@Override @Override
public void exportToClipboard(JComponent comp, Clipboard clip, int action) throws IllegalStateException { public void exportToClipboard(JComponent comp, Clipboard clip, int action) throws IllegalStateException {
ArrayList<String> lines = new ArrayList<String>(); if (clipboardHandler != null)
clipboardHandler.exportToClipboard(comp, clip, action);
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<String> it = lines.iterator();
while (it.hasNext()) {
buffer.append(it.next());
if (it.hasNext())
buffer.append("\n");
}
clip.setContents(new StringSelection(buffer.toString()), null);
} }
} }

View File

@ -9,6 +9,7 @@ import javax.swing.AbstractAction;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import net.sourceforge.filebot.resources.ResourceManager; import net.sourceforge.filebot.resources.ResourceManager;
import net.sourceforge.filebot.ui.FileBotUtil;
public class SaveAction extends AbstractAction { public class SaveAction extends AbstractAction {
@ -44,7 +45,7 @@ public class SaveAction extends AbstractAction {
JFileChooser chooser = new JFileChooser(); JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false); chooser.setMultiSelectionEnabled(false);
chooser.setSelectedFile(new File(getDefaultFileName())); chooser.setSelectedFile(new File(FileBotUtil.validateFileName(getDefaultFileName())));
if (chooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) if (chooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION)
return; return;

View File

@ -11,6 +11,8 @@ import java.util.List;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.TransferHandler; import javax.swing.TransferHandler;
import net.sourceforge.filebot.ui.FileBotUtil;
public class SaveableExportHandler implements ExportHandler { public class SaveableExportHandler implements ExportHandler {
@ -51,7 +53,7 @@ public class SaveableExportHandler implements ExportHandler {
@Override @Override
public Transferable createTransferable(JComponent c) { public Transferable createTransferable(JComponent c) {
try { try {
File temporaryFile = new File(tmpdir, saveable.getDefaultFileName()); File temporaryFile = new File(tmpdir, FileBotUtil.validateFileName(saveable.getDefaultFileName()));
temporaryFile.createNewFile(); temporaryFile.createNewFile();
saveable.save(temporaryFile); saveable.save(temporaryFile);
@ -62,5 +64,4 @@ public class SaveableExportHandler implements ExportHandler {
return null; return null;
} }
} }