[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Mon Mar 12 17:01:29 UTC 2007


gordonp
Mon Mar 12 13:01:29 EDT 2007
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data
In directory dev.open-bio.org:/tmp/cvs-serv4478/src/main/org/biomoby/shared/data

Modified Files:
	MobyDataBytes.java 
Log Message:
Chaged to a complex rather than string, as this is more correct
moby-live/Java/src/main/org/biomoby/shared/data MobyDataBytes.java,1.4,1.5
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataBytes.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataBytes.java	2006/07/07 04:12:40	1.4
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataBytes.java	2007/03/12 17:01:29	1.5
@@ -2,6 +2,7 @@
 package org.biomoby.shared.data;
 
 import org.biomoby.shared.MobyDataType;
+import org.biomoby.shared.MobyException;
 import java.io.*;
 
 /**	
@@ -9,17 +10,20 @@
  * binary data that will be Base64 encoded to fit in the MOBY XML envelope.
  * It will also decode Base64 and UUEncoded strings.
  *
- * Because getObject() returns a reference to the underlying byte array
- * for the MOBY Object, the object content can be modified by changing
- * members of this array.
+ * All members of the object can be accessed as they normally would be in 
+ * a MobyDataComposite (including the Base64 representation of the bytes in the "contents" member), 
+ * but the getBytes() and getObject() method, for
+ * convenience, return the bytes encoded by the "contents" member of the class.
+ *
+ * Note: changing the "contents" member's underlying StringBuffer changes the byte output!
  *
  * This class could be used to handle a JPEG's raw data for example.
  */
 
-public class MobyDataBytes extends MobyDataObject{
+public class MobyDataBytes extends MobyDataComposite{
 
-    private byte[] bytes;
-    public static String BASE64_DATATYPE = "text-base64"; 
+    public static final String BASE64_DATATYPE = "text-base64"; 
+    public static final String ENCODED_MEMBER_NAME = "contents"; 
 
     /** How much is read from a input stream (e.g. file) at once */
     public static final int BYTE_READ_SIZE = 4096;  
@@ -27,21 +31,57 @@
     /**
      * Construct the object using a DOM fragment.
      *
-     * @throws IllegalArgumentException if the element is not a text-base64 tag
+     * @throws IllegalArgumentException if the element is not a text-base64 tag, or doesn't inherit from it
      */
-    public MobyDataBytes(org.w3c.dom.Element element) throws IllegalArgumentException{
-	this(getName(element), getTextContents(element));
+    public MobyDataBytes(org.w3c.dom.Element element) throws MobyException{
+	super(MobyDataType.getDataType(BASE64_DATATYPE), getName(element));
 	setId(getId(element));
 	addNamespace(getNamespace(element));
+	
+	MobyDataType inputDataType = MobyDataType.getDataType(element.getLocalName());
+	if(!inputDataType.inheritsFrom(MobyDataType.getDataType(BASE64_DATATYPE))){
+	    throw new MobyException("The given tag ("+ element.getLocalName() +
+				    ") does not inherit from " + BASE64_DATATYPE +
+				    " in the MOBY Object Class Ontology, cannot " +
+				    "consider its contents as binary data");
+	}
+	setDataType(inputDataType);
+
+	// Now, we know we have one field that represents the bytes, 
+	// and there may be others which we will treat as regular composite members
+	populateMembersFromDOM(element);
+
+	MobyDataObject contents = get(ENCODED_MEMBER_NAME);
+	if(!(contents instanceof MobyDataString)){
+	    throw new MobyException("The encoded binary member (" + ENCODED_MEMBER_NAME + 
+				    ") of the given element (" +
+				    element.getLocalName()+
+				    ") was not a String primitive as expected");
+	}
     }
 
     /**
      * C-tor to use when you have binary data to encode
+     * 
+     * @throws MobyException if the data provided is null, or could not be encoded(?!)
      */
-    public MobyDataBytes(String name, byte[] data){
-	super(name);
-	setDataType(MobyDataType.getDataType(BASE64_DATATYPE));
-	bytes = data;
+    public MobyDataBytes(String name, byte[] data) throws MobyException{
+	super(MobyDataType.getDataType(BASE64_DATATYPE), name);
+	storeBytes(data);
+    }
+
+    /**
+     * C-tor to use when you have binary data to encode
+     */
+    public MobyDataBytes(String name, byte[] data, MobyDataType inputDataType) throws MobyException{
+	this(name, data);
+	if(!inputDataType.inheritsFrom(MobyDataType.getDataType(BASE64_DATATYPE))){
+	    throw new MobyException("The given data type ("+ inputDataType.getName() +
+				    ") does not inherit from " + BASE64_DATATYPE +
+				    " in the MOBY Object Class Ontology, cannot " +
+				    "consider the data given as binary data");
+	}
+	setDataType(inputDataType);
     }
 
     /**
@@ -50,11 +90,26 @@
      * Currently Base64 encoding is acceptable, UU Decoding has yet to be implemented..
      */
     public MobyDataBytes(String name, CharSequence data){
-	super(name);
+	super(MobyDataType.getDataType(BASE64_DATATYPE), name);
 	setDataType(MobyDataType.getDataType(BASE64_DATATYPE));
-	if(data != null){
-	    bytes = org.apache.axis.encoding.Base64.decode(data.toString());
+	// TODO: add check that the data is valid Base64 format!
+	put(ENCODED_MEMBER_NAME, new MobyDataString(ENCODED_MEMBER_NAME, data));
+    }
+
+    /**
+     * C-tor to use when you have received text-encoded binary.
+     *
+     * Currently Base64 encoding is acceptable, UU Decoding has yet to be implemented..
+     */
+    public MobyDataBytes(String name, CharSequence data, MobyDataType inputDataType) throws MobyException{
+	this(name, data);
+	if(!inputDataType.inheritsFrom(MobyDataType.getDataType(BASE64_DATATYPE))){
+	    throw new MobyException("The given data type ("+ inputDataType.getName() +
+				    ") does not inherit from " + BASE64_DATATYPE +
+				    " in the MOBY Object Class Ontology, cannot " +
+				    "consider the data given as binary data");
 	}
+	setDataType(inputDataType);
     }
 
     /**
@@ -62,14 +117,40 @@
      *
      * @param resourceURL the URL of the resource to encode, such as "file:..." or "http:..."
      */
-    public MobyDataBytes(String name, java.net.URL resourceURL) throws IOException{
-	super(name);
-	setDataType(MobyDataType.getDataType(BASE64_DATATYPE));
+    public MobyDataBytes(String name, java.net.URL resourceURL) throws MobyException, IOException{
+	super(MobyDataType.getDataType(BASE64_DATATYPE), name);
 	if(resourceURL == null){
 	    return;
 	}
 	InputStream inputStream = resourceURL.openStream();
-	bytes = readStream(inputStream);
+	storeBytes(readStream(inputStream));
+    }
+
+    protected void storeBytes(byte[] bytes) throws MobyException{
+	if(bytes == null){
+	    throw new MobyException("The given byte data to Base64 encode was null");  
+	}
+	String contents = org.apache.axis.encoding.Base64.encode(bytes);
+	if(contents == null){
+	     throw new MobyException("The byte contents could not be encoded in Base64 format");
+	}
+	put(ENCODED_MEMBER_NAME, new MobyDataString(ENCODED_MEMBER_NAME, contents));
+    }
+
+    /**
+     * C-tor to use when you want to encode a resource, such as an image file.
+     *
+     * @param resourceURL the URL of the resource to encode, such as "file:..." or "http:..."
+     */
+    public MobyDataBytes(String name, java.net.URL resourceURL, MobyDataType inputDataType) throws IOException, MobyException{
+	this(name, resourceURL);
+	if(!inputDataType.inheritsFrom(MobyDataType.getDataType(BASE64_DATATYPE))){
+	    throw new MobyException("The given data type ("+ inputDataType.getName() +
+				    ") does not inherit from " + BASE64_DATATYPE +
+				    " in the MOBY Object Class Ontology, cannot " +
+				    "consider the data at the given URL as binary data");
+	}
+	setDataType(inputDataType);
     }
 
     protected byte[] readStream(InputStream in) throws IOException {
@@ -92,39 +173,20 @@
      * file savers, etc.
      */
     public java.io.InputStream getInputStream(){
-	return new java.io.ByteArrayInputStream(bytes);
+	return new java.io.ByteArrayInputStream(getBytes());
     }
     
     /**
      * @return byte[] representing the (mutable) underlying data in this object
      */
     public Object getObject(){
-	return bytes;
+	return getBytes();
     }
 
     /**
      * The same as getObject, but doesn't require a cast of the result
      */
     public byte[] getBytes(){
-	return bytes;
-    }
-    
-    /**
-     * This method base64 encodes the binary data held in this MOBY Object
-     * if in service mode.
-     */
-    public String toXML(){
-	if(xmlMode == MobyDataInstance.SERVICE_XML_MODE){	    
-	    StringBuffer out = new StringBuffer();
-	    out.append("<text-base64>");
-	    if(bytes != null){
-		out.append(org.apache.axis.encoding.Base64.encode(bytes));
-	    }
-	    out.append("</text-base64>");
-	    return out.toString();
-	}
-	else{
-	    return super.toXML();
-	}
-    }
+	return org.apache.axis.encoding.Base64.decode(((MobyDataString) get(ENCODED_MEMBER_NAME)).getValue());
+    }    
 }




More information about the MOBY-guts mailing list