[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/util
In directory dev.open-bio.org:/tmp/cvs-serv530/src/main/ca/ucalgary/seahawk/util

Modified Files:
	HTMLUtils.java 
Log Message:
Refactored several aspects of the transfer handler for easier extension
moby-live/Java/src/main/ca/ucalgary/seahawk/util HTMLUtils.java,1.1,1.2
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/util/HTMLUtils.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/util/HTMLUtils.java	2007/04/18 16:02:09	1.1
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/util/HTMLUtils.java	2007/04/26 15:15:45	1.2
@@ -1,9 +1,14 @@
 package ca.ucalgary.seahawk.util;
 
+import org.biomoby.shared.*;
 import org.biomoby.shared.data.*;
+import org.biomoby.shared.parser.MobyTags;
+
+import javax.xml.parsers.*;
 
 import java.io.*;
-import java.net.URL;
+import java.net.*;
+
 
 public class HTMLUtils{
 
@@ -55,4 +60,142 @@
 	return html;
     }
 
+    /**
+     * Extracts a URL string from a given piece of text iof that's all it contains.
+     * Especially suited for dropped URL's etc. from browsers, as it cleans up the HTML junk
+     * browsers wrap links in.
+     */
+    public static String checkForHyperlinkText(String text){
+	if(text.indexOf("<html><a href=\"") == 0 && text.indexOf("</a></html>") == text.length()-11){
+	    return text.substring(15, text.indexOf("\"",15));
+	}
+	else if(text.indexOf("<a href=\"") == 0 && text.indexOf("</a>") == text.length()-4){ //unix
+	    return 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=\"");
+	    return text.substring(startHref+29, text.indexOf("\"",startHref+29));
+	}
+	return null;
+    }
+
+    /**
+     * Turns a file into a URL if the file was URL shortcut (Windows & Gnome).
+     */
+    public static URL checkForURLShortcut(File file) throws Exception{
+	URL u = null;
+	String fileString = file.toURI().toURL().toString().toLowerCase(); 
+	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
+		}
+	    }
+	}
+	return u;
+    }
+
+    /**
+     * By turning the tool tip text into HTML, we can make it multiline!
+     */
+    public static String htmlifyToolTipText(String text, int maxLineLength){
+	if(text == null){
+	}
+
+	if(text.length() < maxLineLength || maxLineLength < 2){
+	    return "<html>"+text+"</html>";
+	}
+
+	StringBuffer result = new StringBuffer("<html>");
+	int lineCharCount = 0;
+	String[] tokens = text.split("[ \t\n]");
+	for(String word: tokens){
+	    while(word.length()+1 > maxLineLength){  //single word is bigger than preset width, hyphenate
+		int cutPoint = maxLineLength - lineCharCount - 1;
+		result.append(word.substring(0, cutPoint)+"-<br>");
+		lineCharCount = 0;
+		word = word.substring(cutPoint);
+	    }
+	    if(lineCharCount != 0 && lineCharCount + word.length() > maxLineLength){
+		result.append("<br>" + word + " ");
+		lineCharCount = word.length()+1;
+		continue;
+	    }
+	    result.append(word+" ");
+	    lineCharCount += word.length()+1;
+	}
+	result.append("</html>");
+
+	return result.toString();
+    }
+
+    /**
+     * Given an HTML fragment (e.g. cut'n'pasted from a browser), see if we unescape the
+     * HTML we get a MOBY XML block (e.g. from the biomoby.org web site examples).
+     */
+    public static MobyContentInstance checkForMobyXML(String text){
+	if(//(text.indexOf("moby:") != -1 || text.indexOf("articleName=\"") != -1) &&
+	   text.indexOf("namespace=\"") == -1 ||
+	   text.indexOf("id=\"") == -1){
+	    return null;
+	}
+
+	MobyContentInstance content = null;
+	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("&gt;", ">");
+	    xmltext = xmltext.replaceAll("&lt;", "<");
+	    xmltext = xmltext.replaceAll("&amp;", "&");
+	}
+	xmltext = xmltext.trim();  // remove leading and trailing spaces
+	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");
+	    }
+	} catch(Exception e){
+	    e.printStackTrace();
+	}
+	return content;
+    }
 }




More information about the MOBY-guts mailing list