[MOBY-guts] biomoby commit
Paul Gordon
gordonp at dev.open-bio.org
Thu Apr 26 15:15:45 UTC 2007
gordonp
Thu Apr 26 11:15:45 EDT 2007
Update of /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui
In directory dev.open-bio.org:/tmp/cvs-serv530/src/main/ca/ucalgary/seahawk/gui
Modified Files:
FileAndTextTransferHandler.java
Log Message:
Refactored several aspects of the transfer handler for easier extension
moby-live/Java/src/main/ca/ucalgary/seahawk/gui FileAndTextTransferHandler.java,1.4,1.5
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/FileAndTextTransferHandler.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/FileAndTextTransferHandler.java 2007/03/23 20:23:52 1.4
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/FileAndTextTransferHandler.java 2007/04/26 15:15:45 1.5
@@ -1,5 +1,6 @@
package ca.ucalgary.seahawk.gui;
+import ca.ucalgary.seahawk.util.HTMLUtils;
import org.biomoby.shared.MobyPrefixResolver;
import org.biomoby.shared.data.*;
import org.biomoby.shared.parser.MobyTags;
@@ -21,21 +22,18 @@
* windowing system, so dropped files, URLs and string can be loaded into
* tabs. Dragging from Seahawk tabs is not yet supported.
*/
-class FileAndTextTransferHandler extends TransferHandler {
+public class FileAndTextTransferHandler extends TransferHandler {
private DataFlavor fileFlavor, stringFlavor;
private MobyContentGUI gui;
- private JTextArea source;
- private boolean shouldRemove;
- protected String newline = "\n";
- FileAndTextTransferHandler(MobyContentGUI mcg) {
+ public FileAndTextTransferHandler(MobyContentGUI mcg) {
gui = mcg;
fileFlavor = DataFlavor.javaFileListFlavor;
stringFlavor = DataFlavor.stringFlavor;
}
public boolean importData(JComponent c, Transferable t) {
-
+ System.err.println("importData called for file and text transfer handler");
if (!canImport(c, t.getTransferDataFlavors())) {
System.err.println("Cannot drop data into Seahawk: " + t);
return false;
@@ -46,63 +44,27 @@
if (hasFileFlavor(t.getTransferDataFlavors())) {
// Load any dragged file as a URL
for (File file: (java.util.List<File>) t.getTransferData(DataFlavor.javaFileListFlavor)) {
- String fileString = file.toURI().toURL().toString().toLowerCase();
-
// Windows Web shortcut?
- URL u = null;
- if(fileString.lastIndexOf(".url") == fileString.length()-4){
- LineNumberReader in = new LineNumberReader(
- new InputStreamReader(file.toURI().toURL().openStream()));
- for(String line = in.readLine(); line != null; line = in.readLine()){
- int urlIndex = line.indexOf("URL=");
- if(line.indexOf("URL=") == 0){
- try{
- u = new URL(line.substring(4));
- } catch(MalformedURLException murle){
- System.err.println("Could not format " +line);
- murle.printStackTrace();
- // Not a URL, but really it should have been.
- // oh well, move on to normal file loading...
- }
- break; // done with this file as a URL, no need to execute other loadPaneFromURL
- }
- }
- }
+ URL u = HTMLUtils.checkForURLShortcut(file);
// Was it a shortcut file after all?
if(u != null){
+ System.err.println("Dropped item appears to be a URL shortcut...");
gui.loadPaneFromURL(u, true);
}
// Any other type of file is loaded as-is
else{
+ System.err.println("Dropped item appears to be a file...");
gui.loadPaneFromURL(file.toURI().toURL(), true);
}
}
return true;
} else if (hasTextFlavor(t.getTransferDataFlavors())) {
// Make a string out of the text
- StringBuffer textBuffer = new StringBuffer();
- Reader reader = DataFlavor.selectBestTextFlavor(t.getTransferDataFlavors()).getReaderForText(t);
- //Reader reader = DataFlavor.plainTextFlavor.getReaderForText(t);
- char[] buffer = new char[1024];
- for(int charsRead = reader.read(buffer); charsRead != -1; charsRead = reader.read(buffer)){
- textBuffer.append(buffer, 0, charsRead);
- }
- // Some readers put funny nulls before each char (8->16 bit char encoding goof)
- String text = textBuffer.toString().replaceAll("\00", "");
+ boolean NOT_JUST_PLAIN_TEXT = false;
+ String text = convertToString(t, NOT_JUST_PLAIN_TEXT);
// Is it an address bar icon dragged from firefox? Strip the surrounding html
- String linktext = null;
- if(text.indexOf("<html><a href=\"") == 0 && text.indexOf("</a></html>") == text.length()-11){
- linktext = text.substring(15, text.indexOf("\"",15));
- }
- else if(text.indexOf("<a href=\"") == 0 && text.indexOf("</a>") == text.length()-4){ //unix
- linktext = text.substring(9, text.indexOf("\"",9));
- }
- else if(text.indexOf("<!--StartFragment--><a href=\"") != -1 && // windows
- text.indexOf("</a><!--EndFragment-->") != -1){
- int startHref = text.indexOf("<!--StartFragment--><a href=\"");
- linktext = text.substring(startHref+29, text.indexOf("\"",startHref+29));
- }
+ String linktext = HTMLUtils.checkForHyperlinkText(text);
// First see if it's a URL
URL u = null;
@@ -112,65 +74,18 @@
// Not a URL, oh well, move on to string handling...
}
if(u != null){
+ System.err.println("Dropped item appears to be a URL...");
gui.loadPaneFromURL(u, true);
return true;
}
// If the text looks like a MOBY XML payload, be nice and create
// the proper object to load
- if(//(text.indexOf("moby:") != -1 || text.indexOf("articleName=\"") != -1) &&
- text.indexOf("namespace=\"") != -1 &&
- text.indexOf("id=\"") != -1){
- String xmltext = text;
- // See if the xml needs unescaping (e.g. if preformatted HTML dropped from Firefox or Thunderbird)
- int preStart = xmltext.indexOf("<pre");
- if(preStart == 0 || //unix
- xmltext.indexOf("--StartFragment--") != -1){ //windows
- xmltext = xmltext.substring(preStart, xmltext.indexOf("</pre"));
- xmltext = xmltext.substring(xmltext.indexOf(">")+1);
- xmltext = xmltext.replaceAll(">", ">");
- xmltext = xmltext.replaceAll("<", "<");
- xmltext = xmltext.replaceAll("&", "&");
- }
- xmltext = xmltext.trim(); // remove leading and trailing spaces
- MobyContentInstance content = null;
- try{
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware (true);
- DocumentBuilder db = dbf.newDocumentBuilder();
- if(xmltext.indexOf("xmlns:moby") == -1){
- // Ensure that the moby: prefix is declared in the first tag
- xmltext = xmltext.replaceFirst("<([A-Za-z0-9_][^> \t\n]*)", "<$1 xmlns:moby=\""+
- MobyPrefixResolver.MOBY_XML_NAMESPACE+"\"");
- }
- if(xmltext.indexOf("<?xml") != 0){
- xmltext = "<?xml version=\"1.0\"?>\n" + xmltext;
- }
-
- org.w3c.dom.Element data = db.parse(new ByteArrayInputStream(xmltext.getBytes())).getDocumentElement();
- if(data.getLocalName().equals(MobyTags.MOBY)){
- content = MobyDataUtils.fromXMLDocument(data);
- }
- else if(data.getLocalName().equals(MobyTags.MOBYCONTENT)){
- content = new MobyContentInstance(data);
- }
- else if(data.getLocalName().equals(MobyTags.MOBYDATA)){
- content = new MobyContentInstance();
- content.parseDataGroup(data);
- }
- else{
- // Any other data can be processed by the base object class
- content = new MobyContentInstance(MobyDataObject.createInstanceFromDOM(data), "Drag 'n' Drop data");
- }
-
- if(content != null){
- gui.loadPaneFromObject(content, true);
- return true;
- }
- } catch(Exception e){
- // Not good MOBY XML, oh well, lets fall back on the HTML-based display below...
- e.printStackTrace();
- }
+ MobyContentInstance content = HTMLUtils.checkForMobyXML(text);
+ if(content != null){
+ System.err.println("Dropped item appears to be moby xml...");
+ gui.loadPaneFromObject(content, true);
+ return true;
}
// Otherwise save the data to a temp file and load it as a URL
@@ -206,21 +121,44 @@
gui.loadPaneFromURL(savedFile.toURI().toURL(), true);
return true;
}
+ else{
+ System.err.println("Cannot drop data into Seahawk, data was neither a file, nor text");
+ }
} catch (UnsupportedFlavorException ufe) {
- System.out.println("importData: unsupported data flavor");
- } catch (IOException ieo) {
- System.out.println("importData: I/O exception");
- }
+ System.out.println("importData: unsupported data flavor:"+ufe);
+ } catch (IOException ioe) {
+ ioe.printStackTrace();
+ System.out.println("importData: I/O exception: "+ioe);
+ } catch (Exception e){
+ e.printStackTrace();
+ System.out.println("importData: General exception: "+e);
+ }
return false;
}
- public boolean canImport(JComponent c, DataFlavor[] flavors) {
+ public String convertToString(Transferable t, boolean get_plain_text) throws Exception{
+ StringBuffer textBuffer = new StringBuffer();
+ Reader reader = get_plain_text ? DataFlavor.plainTextFlavor.getReaderForText(t) :
+ DataFlavor.selectBestTextFlavor(t.getTransferDataFlavors()).getReaderForText(t);
+ char[] buffer = new char[4096];
+ for(int charsRead = reader.read(buffer); charsRead != -1; charsRead = reader.read(buffer)){
+ textBuffer.append(buffer, 0, charsRead);
+ }
+ // Some readers put funny nulls before each char (8->16 bit char encoding goof)
+ return textBuffer.toString().replaceAll("\00", "");
+ }
+
+ /**
+ * Overrides same method in parent class, tells the dropper if we'll accept the data
+ * before the drop is actually attempted.
+ */
+ public boolean canImport(JComponent comp, DataFlavor[] flavors) {
if (hasFileFlavor(flavors)) { return true; }
if (hasTextFlavor(flavors)) { return true; }
return false;
}
- private boolean hasFileFlavor(DataFlavor[] flavors) {
+ protected boolean hasFileFlavor(DataFlavor[] flavors) {
for (DataFlavor flavor: flavors) {
if (flavor.isFlavorJavaFileListType()) {
return true;
@@ -229,7 +167,7 @@
return false;
}
- private boolean hasTextFlavor(DataFlavor[] flavors) {
+ protected boolean hasTextFlavor(DataFlavor[] flavors) {
for (DataFlavor flavor: flavors) {
if (flavor.isFlavorTextType()) {
return true;
More information about the MOBY-guts
mailing list