[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Sat Jul 28 03:45:31 UTC 2007


gordonp
Fri Jul 27 23:45:31 EDT 2007
Update of /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/util
In directory dev.open-bio.org:/tmp/cvs-serv26938/src/main/ca/ucalgary/seahawk/util

Modified Files:
	HTMLUtils.java 
Log Message:
Made binary data encapsulation more robust
moby-live/Java/src/main/ca/ucalgary/seahawk/util HTMLUtils.java,1.3,1.4
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/util/HTMLUtils.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/util/HTMLUtils.java	2007/06/08 14:04:27	1.3
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/util/HTMLUtils.java	2007/07/28 03:45:31	1.4
@@ -8,50 +8,65 @@
 
 import java.io.*;
 import java.net.*;
-
+import java.util.Vector;
 
 public class HTMLUtils{
 
     public final static String BINARY_DATA_LABEL = "[binary data not shown]";
-    public final static String ACCEPTABLE_IMAGE_DATATYPE = "WebFormatImage"; //from the ontology
+    public final static String ACCEPTABLE_IMAGE_DATATYPE = "WebFormatImage"; //from the ontology, should eventually be configurable
 
     public static String encapsulateBinaryData(URL sourceMobyData, String html) throws Exception{
 	MobyContentInstance dataPackage = MobyDataUtils.fromXMLDocument(sourceMobyData);
 	// Look for binary data and replace it with an image ref (if its an image
 	// displayable in our viewer), or simply "(binary data)".
 	for(MobyDataJob job: dataPackage.values()){
-	    for(MobyDataInstance data: job.values()){
-		if(data instanceof MobyDataBytes){
-		    MobyDataBytes binaryMobyObject = (MobyDataBytes) data;
-		    //System.err.println("Replacing binary data in display of "+binaryMobyObject.getName());
-
-		    String encodedData = binaryMobyObject.get(MobyDataBytes.ENCODED_MEMBER_NAME)
-			.getValue().toString();
-		    if(html.indexOf(encodedData) == -1){
-			System.err.println("Could not find the encoded data in the html given!!!"); 			
-		    }
-
-		    // if an image, save and show it
-		    if(binaryMobyObject.getDataType().inheritsFrom(ACCEPTABLE_IMAGE_DATATYPE)){
-			String imgSuffix = binaryMobyObject.getDataType().getName();
-			if(imgSuffix.length() < 3){
-			    imgSuffix = ".img";
+	    for(MobyDataInstance primary: job.values()){
+		Iterable<MobyDataObject> values = null;
+		if(primary instanceof MobyDataObjectSet ||
+		   primary instanceof MobyDataObjectVector){
+		    values = (Iterable<MobyDataObject>) primary;
+		}
+		else if(primary instanceof MobyDataObject){
+		    values = new Vector<MobyDataObject>();
+		    ((Vector) values).add((MobyDataObject) primary);
+		}
+		else{
+		    // ignore secondaries, they can't be binary
+		    continue;
+		}
+		for(MobyDataObject data: values){
+		    if(data instanceof MobyDataBytes){
+			MobyDataBytes binaryMobyObject = (MobyDataBytes) data;
+			//System.err.println("Replacing binary data in display of "+binaryMobyObject.getName());
+			
+			String encodedData = binaryMobyObject.get(MobyDataBytes.ENCODED_MEMBER_NAME)
+			    .getValue().toString();
+			if(html.indexOf(encodedData) == -1){
+			    System.err.println("Could not find the encoded data in the html given!!!"); 			
 			}
+			
+			// if an image, save and show it
+			if(binaryMobyObject.getDataType().inheritsFrom(ACCEPTABLE_IMAGE_DATATYPE)){
+			    String imgSuffix = binaryMobyObject.getDataType().getName();
+			    if(imgSuffix.length() < 3){
+				imgSuffix = ".img";
+			    }
+			    else{
+				imgSuffix = "."+imgSuffix.substring(0, 3).toLowerCase();
+			    }
+			    File imageFile = File.createTempFile("seahawkSavedImage", imgSuffix);
+			    imageFile.deleteOnExit();
+			    
+			    FileOutputStream fos = new FileOutputStream(imageFile);
+			    fos.write(binaryMobyObject.getBytes());
+			    fos.close();
+			    
+			    html = html.replace(encodedData, "<br><img src=\""+imageFile.toURI().toURL()+"\">");
+			}
+			// Otherwise put a placeholder
 			else{
-			    imgSuffix = "."+imgSuffix.substring(0, 3).toLowerCase();
+			    html = html.replace(encodedData, BINARY_DATA_LABEL);
 			}
-			File imageFile = File.createTempFile("seahawkSavedImage", imgSuffix);
-			imageFile.deleteOnExit();
-
-			FileOutputStream fos = new FileOutputStream(imageFile);
-			fos.write(binaryMobyObject.getBytes());
-			fos.close();
-
-			html = html.replace(encodedData, "<br><img src=\""+imageFile.toURI().toURL()+"\">");
-		    }
-		    // Otherwise put a placeholder
-		    else{
-			html = html.replace(encodedData, BINARY_DATA_LABEL);
 		    }
 		}
 	    }
@@ -154,9 +169,13 @@
 
 	MobyContentInstance content = null;
 	String xmltext = text;
+	if(xmltext.indexOf("??") == 0){
+	    xmltext = xmltext.substring(2);
+	}
+
 	// 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
+	if(preStart == 0 || preStart == 2 || //unix
 	   xmltext.indexOf("--StartFragment--") != -1){ //windows
 	    xmltext = xmltext.substring(preStart, xmltext.indexOf("</pre")); 
 	    xmltext = xmltext.substring(xmltext.indexOf(">")+1);			
@@ -196,10 +215,20 @@
 	    }
 	} catch(Exception e){
 	    e.printStackTrace();
-	    System.err.println("This issue may be related to your choice of registry ("+
+	    System.err.println("This issue may or may not be related to your choice of registry ("+
 			       (SeahawkOptions.getRegistry() == null ? "MOBY default" : SeahawkOptions.getRegistry().getSynonym())
 			       +")");
+	    System.err.println(xmltext);
 	}
 	return content;
     }
+
+    public static String getURLContents(URL u) throws Exception{
+	StringBuffer buffer = new StringBuffer();
+	LineNumberReader reader = new LineNumberReader(new InputStreamReader(u.openStream()));
+	for(String line = reader.readLine(); line != null; line = reader.readLine()){
+	    buffer.append(line);
+	}
+	return buffer.toString();
+    }
 }




More information about the MOBY-guts mailing list