[MOBY-guts] biomoby commit

Paul Gordon gordonp at pub.open-bio.org
Fri Jul 22 05:11:54 UTC 2005


gordonp
Fri Jul 22 01:11:54 EDT 2005
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data
In directory pub.open-bio.org:/tmp/cvs-serv30795

Modified Files:
	MobyDataComposite.java MobyDataDateTime.java 
	MobyDataFloat.java MobyDataInt.java MobyDataObject.java 
	MobyDataObjectSet.java MobyDataSecondaryInstance.java 
	MobyDataString.java 
Added Files:
	MobyContentInstance.java MobyDataUtils.java 
Log Message:
First major release of fully functional data instance code

moby-live/Java/src/main/org/biomoby/shared/data MobyContentInstance.java,NONE,1.1 MobyDataUtils.java,NONE,1.1 MobyDataComposite.java,1.2,1.3 MobyDataDateTime.java,1.2,1.3 MobyDataFloat.java,1.2,1.3 MobyDataInt.java,1.2,1.3 MobyDataObject.java,1.3,1.4 MobyDataObjectSet.java,1.2,1.3 MobyDataSecondaryInstance.java,1.2,1.3 MobyDataString.java,1.2,1.3
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataComposite.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataComposite.java	2005/05/19 15:57:26	1.2
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataComposite.java	2005/07/22 05:11:54	1.3
@@ -11,6 +11,7 @@
 import java.util.Vector;
 
 import org.biomoby.shared.MobyDataType;
+import org.biomoby.shared.MobyException;
 import org.biomoby.shared.MobyNamespace;
 
 /**
@@ -28,9 +29,9 @@
     /**
      * Construct the object using a DOM fragment.
      *
-     * @throws IllegalArgumentException if the element is not a MobyObject tag
+     * @throws MobyException if the element is not a MobyObject tag
      */
-    public MobyDataComposite(org.w3c.dom.Element element) throws IllegalArgumentException{
+    public MobyDataComposite(org.w3c.dom.Element element) throws MobyException{
 	this(new MobyDataType(element.getLocalName()), getName(element), getNamespace(element), getId(element));
 
 	// Decompose the children
@@ -70,6 +71,23 @@
 	members = new HashMap();
     }
 
+    public MobyDataComposite(MobyDataType type, String name){
+	this(type, name, "", "");
+    }
+
+    public MobyDataComposite(String typeName, String name){
+	this(new MobyDataType(typeName), name);
+    }
+
+    public MobyDataComposite(MobyDataType type){
+	this(type, "");
+    }
+
+    public MobyDataComposite(String typeName){
+	this(new MobyDataType(typeName));
+    }
+    
+
     /**
      * Report whether all required fields for the object's datatype have been instantiated 
      * (i.e. the object is ready to use as input to a service).
@@ -108,6 +126,10 @@
 	    for(int i = 0; i < fieldNames.length; i++){
 		//MobyDataObject mdsi = members.get(fieldNames[i]); //Java1.5
 		MobyDataObject mdsi = (MobyDataObject) members.get(fieldNames[i]);
+		// ensure the articleName is set correctly for the requirements of this object
+		if(!fieldNames[i].toString().equals(mdsi.getName())){
+		    mdsi.setName(fieldNames[i].toString());
+		}
 		int oldXmlMode = mdsi.getXmlMode();
 		if(oldXmlMode != MobyDataInstance.SERVICE_XML_MODE){
 		    mdsi.setXmlMode(MobyDataInstance.SERVICE_XML_MODE);

===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataDateTime.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataDateTime.java	2005/05/19 15:57:26	1.2
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataDateTime.java	2005/07/22 05:11:54	1.3
@@ -40,7 +40,7 @@
     }
     
     /**
-     * Constructor to build a MOBY DateTime object using an ISO 8601 formatted input string.
+     * Constructor to build a MOBY DateTime object using the W3C profile of an ISO 8601 formatted input string.
      * @param stringISO8601 if null, the current local date and time is used
      */
     public MobyDataDateTime(String articleName, String stringISO8601) throws IllegalArgumentException{
@@ -49,6 +49,10 @@
 	value = parseISO8601(stringISO8601);
     }
 
+    public MobyDataDateTime(String stringISO8601){
+	this("", stringISO8601);
+    }
+
     public MobyDataDateTime(String articleName, GregorianCalendar cal){
 	super(articleName, "");
 	setDataType(new MobyDataType("DateTime"));
@@ -56,6 +60,10 @@
 	((StringBuffer) super.getObject()).append(toString());  // set string representation in case super.toXML() is called explicitly
     }
 
+    public MobyDataDateTime(GregorianCalendar cal){
+	this("", cal);
+    }
+
     /**
      * For a description of ISO 8601 as used on the Web, see http://www.w3.org/TR/NOTE-datetime
      * This method is based on org.w3.util.DateParser v. 1.4 by Beno&icirc;t Mah&eacute; (bmahe at w3.org)

===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataFloat.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataFloat.java	2005/05/19 15:57:26	1.2
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataFloat.java	2005/07/22 05:11:54	1.3
@@ -49,6 +49,10 @@
 	}
     }
 
+    public MobyDataFloat(Number n){
+	this("", n);
+    }
+
     /**
      * Constructor to use if the incoming value is a primitive.
      * If you want to pass in a float or int, cast it to a double.
@@ -59,6 +63,10 @@
 	value = new BigDecimal(d);
     }
 
+    public MobyDataFloat(double d){
+	this("", d);
+    }
+
     /**
      * Constructor to use if the incoming value is a string representing a number 
      * (including mantissa/exponent format).
@@ -71,6 +79,10 @@
 	value = new BigDecimal(stringNumber);
     }
 
+    public MobyDataFloat(String stringNumber){
+	this("", stringNumber);
+    }
+
     public Object clone(){
 	MobyDataFloat copy = new MobyDataFloat(getName(), value);
 	copy.setDataType(getDataType());

===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataInt.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataInt.java	2005/05/19 15:57:26	1.2
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataInt.java	2005/07/22 05:11:54	1.3
@@ -50,6 +50,10 @@
 	}
     }
 
+    public MobyDataInt(Number n){
+	this("", n);
+    }
+
     /**
      * Constructor to use if the incoming value is a primitive.
      * If you want to pass in a float or double, cast it to an int.
@@ -60,6 +64,10 @@
 	value = new BigInteger(""+i);
     }
 
+    public MobyDataInt(int i){
+	this("", i);
+    }
+
     /**
      * Constructor to use if the incoming value is a string representing an integer number.
      *

===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataObject.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataObject.java	2005/07/19 12:39:59	1.3
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataObject.java	2005/07/22 05:11:54	1.4
@@ -45,9 +45,9 @@
     /**
      * Construct the object using a DOM fragment.
      *
-     * @throws IllegalArgumentException if the element is not an Object tag
+     * @throws MobyException if the element is not an Object tag, or is missing a required attribute
      */
-    public MobyDataObject(Element element) throws IllegalArgumentException{
+    public MobyDataObject(Element element) throws MobyException{
 	this(getNamespace(element).getName(), getId(element));
 	setName(getName(element));
     }
@@ -58,6 +58,7 @@
      */
     public MobyDataObject(String name){
         super(name);
+	setDataType(staticObjectDataType);
     }
 
     /**
@@ -84,13 +85,14 @@
     /**
      * This method creates a MobyDataObject of the appropriate subclass for
      * a given input XML tree (e.g. base objects like MobyDataObject and MobyDataInt, 
-     * or complex objects like MobyDataDateTime or MobyDataComposite).  This method
+     * or complex objects like MobyDataDateTime or MobyDataComposite, or even a 
+     * MobyDataSecondaryInstance).  This method
      * will also parse a MOBY XML Object wrapped in a Simple tag when the Simple
      * is passed in as the main element.
      *
      * @param objectTag the W3C DOM Element node corresponding to the object's enclosing tag, or a Simple tag
      */
-    public static MobyDataInstance createInstanceFromDOM(Element objectTag) throws IllegalArgumentException{
+    public static MobyDataInstance createInstanceFromDOM(Element objectTag) throws MobyException{
 	MobyDataObject object = null;
 
 	if(objectTag == null){
@@ -99,23 +101,31 @@
 
 	String objectClass = objectTag.getLocalName();
 	if("Simple".equals(objectClass)){
-	    NodeList children = objectTag.getElementsByTagName("*");
+	    NodeList children = objectTag.getChildNodes();
+	    int elementChild = -1;
+	    for(int i = 0; i < children.getLength(); i++){
+		if(children.item(i) instanceof Element){
+		    // Second instance of element child
+		    if(elementChild != -1){
+			throw new IllegalArgumentException("Simple element has more than one " +
+							   "child element, cannot create a MOBY data " +
+							   "instance due to the ambiguity of the input XML");
+		    }
+		    elementChild = i;
+		}
+	    }
 	    // A Simple tag is allowed to have exactly one child element
-	    if(children.getLength() == 0){
+	    if(elementChild == -1){
 		throw new IllegalArgumentException("Simple element has no MOBY " +
 						   "child element, there is no " +
 						   "instance to create");
 	    }
-	    else if(children.getLength() != 1){
-		throw new IllegalArgumentException("Simple element has more than one " +
-						   "child element, cannot create a MOBY data " +
-						   "instance due to the ambiguity of the input XML");		
-	    }
-	    return createInstanceFromDOM((Element) children.item(0));
+
+	    return createInstanceFromDOM((Element) children.item(elementChild));
 	}
 
 	// There are six types of objects we can populate with data directly
-	// plus Collections.  Otherwise its a composite.
+	// plus Collections and Secondary Parameters.  Otherwise it is a composite.
 	else if("Object".equals(objectClass)){
 	    return new MobyDataObject(objectTag);	    
 	}
@@ -137,14 +147,22 @@
 	else if("Collection".equals(objectClass)){
 	    return new MobyDataObjectSet(objectTag);
 	}
+	else if(MobyDataSecondaryInstance.ELEMENT_NAME.equals(objectClass)){
+	    return new MobyDataSecondaryInstance(objectTag);
+	}
 	// Must otherwise be a composite
 	else{
 	    return new MobyDataComposite(objectTag);
 	}
     }
 
-    protected static String getTextContents(Element element){
-	NodeList children = element.getChildNodes();
+    /**
+     * Similar to DOM level 3 getTextContent, but only elements are excepted as input, and text
+     * inside children elements is not picked up.
+     * Actually calls MobyObjectDecompositionImpl.getTextContent()
+     */
+    public static String getTextContents(Element element){
+	/*	NodeList children = element.getChildNodes();
 	int numChildren = children.getLength();
 	StringBuffer textContents = new StringBuffer();
 	for(int i = 0; i < numChildren; i++){
@@ -153,8 +171,8 @@
 	       childNode instanceof org.w3c.dom.CDATASection){
 		textContents.append(childNode.getNodeValue());
 	    }
-	}
-	return textContents.toString();
+	    }*/
+	return MobyObjectDecompositionImpl.getTextContent(element, false);
     }
 
     protected static MobyNamespace getNamespace(Element e){
@@ -165,7 +183,7 @@
     /**
      * Determine the name of the element if the parent Simple exists
      */
-    protected static String getName(Element e){
+    public static String getName(Element e){
 	Node p = e.getParentNode();
 	if(p == null || !(p instanceof Element)){
 	    return "";
@@ -174,7 +192,7 @@
 	return name == null ? "" : name;
     }
 
-    protected static String getId(Element e){
+    public static String getId(Element e){
 	String id = e.getAttribute("id");
 	return id == null ? "" : id;
     }

===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataObjectSet.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataObjectSet.java	2005/05/19 15:57:26	1.2
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataObjectSet.java	2005/07/22 05:11:54	1.3
@@ -6,6 +6,7 @@
 import java.util.Iterator;
 import java.util.Vector;
 
+import org.biomoby.shared.MobyException;
 import org.biomoby.shared.MobyPrimaryDataSet;
 import org.biomoby.shared.MobyPrefixResolver;
 
@@ -26,11 +27,11 @@
     Vector bag;
     private int xmlMode = MobyDataInstance.SERVICE_XML_MODE;
 
-    public MobyDataObjectSet(org.w3c.dom.Element e) throws IllegalArgumentException{
+    public MobyDataObjectSet(org.w3c.dom.Element e) throws MobyException{
 	this(MobyDataObject.getName(e), getChildren(e));
     }
     
-    public static Collection getChildren(org.w3c.dom.Element e){
+    public static Collection getChildren(org.w3c.dom.Element e) throws MobyException{
 	Vector members = new Vector();
 
 	org.w3c.dom.NodeList children = e.getElementsByTagName("*");  //wildcard
@@ -102,7 +103,8 @@
      * @return the MobyDataObjects that comprise the collection
      */
     public MobyDataObject[] getElementInstances(){
-	return (MobyDataObject[]) bag.toArray();
+	MobyDataObject instances[] = new MobyDataObject[bag.size()];
+	return (MobyDataObject[]) bag.toArray(instances);
     }
     
     private void checkInputClass(String action, Object mdsi) throws ClassCastException, NullPointerException{

===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataSecondaryInstance.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataSecondaryInstance.java	2005/07/19 12:59:21	1.2
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataSecondaryInstance.java	2005/07/22 05:11:54	1.3
@@ -1,14 +1,18 @@
 
 package org.biomoby.shared.data;
-import org.biomoby.shared.MobySecondaryData;
+
+import org.biomoby.shared.*;
+import org.w3c.dom.*;
 
 /**
  * A class that holds and displays secondary input data to a service.
  */
 
-public class MobyDataSecondaryInstance extends MobySecondaryData{
+public class MobyDataSecondaryInstance extends MobySecondaryData implements MobyDataInstance{
     protected String dataValue = null;
-    protected MobySecondaryData secDataType = null;
+    protected int xmlMode = MobyDataInstance.CENTRAL_XML_MODE;
+    public static final String ELEMENT_NAME = "Parameter";
+    public static final String VALUE_NAME = "Value";
 
     public MobyDataSecondaryInstance(MobySecondaryData type, String value){
 	this(type);
@@ -26,7 +30,40 @@
 	setDefaultValue(type.getDefaultValue());
 	setMinimumValue(type.getMinimumValue());
 	setMaximumValue(type.getMaximumValue());
-	secDataType = type;
+    }
+
+    /**
+     * Create an instance from XML input.  Unfortunately, under the current scheme
+     * we have no way of knowing what the type of the parameter is (int, string, etc.).
+     * By default we will treat it as a string, as this is the most lenient.
+     */
+    public MobyDataSecondaryInstance(Element objectTag) throws MobyException{
+	// What is the parameter name?
+	super(MobyDataObject.getName(objectTag));
+	setDataType("String");
+
+	if(objectTag == null){
+	    return;
+	}
+
+	String objectClass = objectTag.getLocalName();
+	if(!ELEMENT_NAME.equals(objectClass)){
+	    throw new MobyException("While creating a secondary parameter instance from XML: " +
+				    "the passed in element (" +objectClass + ") was not a " + ELEMENT_NAME);
+	}
+	NodeList values = objectTag.getElementsByTagNameNS(MobyPrefixResolver.MOBY_XML_NAMESPACE, VALUE_NAME);
+	if(values.getLength() == 0){
+	    throw new MobyException("While creating a secondary parameter instance from XML: " +
+				    "No " + VALUE_NAME + " element in the namespace " + 
+				    MobyPrefixResolver.MOBY_XML_NAMESPACE + " was found");
+	}
+	if(values.getLength() > 1){
+	    throw new MobyException("While creating a secondary parameter instance from XML: " +
+				    "More than one " + VALUE_NAME + " element ("+
+				    values.getLength() +") in the namespace " + 
+				    MobyPrefixResolver.MOBY_XML_NAMESPACE + " was found");
+	}
+	setValue(MobyDataObject.getTextContents((Element) values.item(0)));
     }
 
     public void setValue(String value) throws IllegalArgumentException{
@@ -37,9 +74,51 @@
 	return dataValue;
     }
 
+    public void setXmlMode(int mode) throws IllegalArgumentException{
+        if(mode != MobyDataInstance.CENTRAL_XML_MODE && mode != MobyDataInstance.SERVICE_XML_MODE){
+	    throw new IllegalArgumentException("Value passed to setXmlMode was neither " +
+					       "MobyDataInstance.CENTRAL_XML_MODE nor MobyDataInstance.SERVICE_XML_MODE");
+	}
+	xmlMode = mode;
+    }
+
+    /**
+     * Report whether toXML will produce Central template or service call instance XML.
+     *
+     * @return one of CENTRAL_XML_MODE or SERVICE_XML_MODE
+     */
+    public int getXmlMode(){
+	return xmlMode;
+    }
+
+    /**
+     * This method sanitizes parameter data value strings of XML escape characters such as the ampersand (&amp;) and the 
+     * less-than sign (&lt;).  WARNING: this method will not escape ampersand in the string "&amp;amp;", 
+     * or '&amp;#x26;' style character references.  We will assume that is this case you've probably 
+     * already written the string as XML. Also note that null strings are treated as empty strings.
+     *
+     * WARNING: As of yet, we do not deal with the false escaping of strings containg already-escaped 
+     * CDATA sections!
+     */
     public String toXML(){
-	return "  <moby:Parameter xmlns:moby=\""+org.biomoby.shared.MobyPrefixResolver.MOBY_XML_NAMESPACE+
-	    "\" moby:articleName='" + secDataType.getName() +
-	    "'><moby:Value>" + dataValue + "</moby:Value></moby:Parameter>\n";
+	// Sanitize the data before adding to the XML
+	String tmpValue = dataValue;
+	if(tmpValue != null){
+	    tmpValue = tmpValue.replaceAll("&(?!(amp|#x\\d+);)", "&amp;");
+	    tmpValue = tmpValue.replaceAll("<", "&lt;");
+	}
+	// A null value and an empty string are considered equivalent here
+	else{
+	    tmpValue = "";
+	}
+
+	if(xmlMode == MobyDataInstance.SERVICE_XML_MODE){
+	    return "  <moby:" + ELEMENT_NAME + " xmlns:moby=\""+MobyPrefixResolver.MOBY_XML_NAMESPACE+
+	    "\" moby:articleName='" + getName() +
+	    "'><moby:Value>" + tmpValue + "</moby:Value></moby:" + ELEMENT_NAME + ">\n";
+	}
+	else{
+	    return super.toXML();
+	}
     }
 }

===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataString.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataString.java	2005/05/19 15:57:26	1.2
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataString.java	2005/07/22 05:11:54	1.3
@@ -36,6 +36,10 @@
 	value = new StringBuffer(stringValue.toString());
     }
 
+    public MobyDataString(CharSequence stringValue){
+	this("", stringValue);
+    }
+
     public String toString(){
 	return value.toString();
     }
@@ -63,7 +67,7 @@
      * This method sanitizes strings of XML escape characters such as the ampersand (&amp;) and the 
      * less-than sign (&lt;).  WARNING: this method will not escape ampersand in the string "&amp;amp;", 
      * or '&amp;#x26;' style character references.  We will assume that is this case you've probably 
-     * already written the string as XML. 
+     * already written the string as XML. Also note that null strings are treated as empty strings.
      *
      * WARNING: As of yet, we do not deal with the false escaping of strings containg already-escaped 
      * CDATA sections!
@@ -76,6 +80,9 @@
 		tmpValue = tmpValue.replaceAll("&(?!(amp|#x\\d+);)", "&amp;");
 		tmpValue = tmpValue.replaceAll("<", "&lt;");
 	    }
+	    else{
+		tmpValue = "";
+	    }
 	    
 	    return "<String "+ getAttrXML() + ">" + tmpValue + "</String>";
 		 }




More information about the MOBY-guts mailing list