[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Tue Jun 9 19:22:21 UTC 2009


gordonp
Tue Jun  9 15:22:21 EDT 2009
Update of /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services
In directory dev.open-bio.org:/tmp/cvs-serv20813/src/main/ca/ucalgary/seahawk/services

Modified Files:
	TextClient.java 
Log Message:
Made registry savvy, added Dublin Core support for rule ID
moby-live/Java/src/main/ca/ucalgary/seahawk/services TextClient.java,1.6,1.7
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services/TextClient.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services/TextClient.java	2008/10/30 02:33:24	1.6
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services/TextClient.java	2009/06/09 19:22:21	1.7
@@ -2,6 +2,7 @@
 
 import ca.ucalgary.seahawk.util.SeahawkOptions;
 
+import org.biomoby.registry.meta.*;
 import org.biomoby.shared.*;
 import org.biomoby.shared.data.MobyDataInstance;
 
@@ -29,12 +30,23 @@
     private Vector<Transformer> moby2textConverters;  // XSLT engine
     private TransformerFactory transFactory;
     private DocumentBuilder docBuilder;
+    //private Map<String,Transformer> id; //todo: unique xslt doc id (e.g. LSID or URL) -> transformer
 
     private Map<String, Vector<String>> typeTemplates;  //mobytype -> Vector(xsltrule1, xsltrule2,...)
     private Map<String, Vector<MobyNamespace>> typeNsRestrictions;  //mobytype -> Vector(mobynamespace1, ...)
     private Map<String, String> templateMode;
+    private String id;
+    private Registry registry;
 
     public TextClient() throws Exception{
+	this(SeahawkOptions.getRegistry() == null ? 
+	     RegistryCache.getDefaultRegistry() :
+	     SeahawkOptions.getRegistry());
+    }
+
+    public TextClient(Registry reg) throws Exception{
+	registry = reg;
+
         // Now setup the XSLT to transform MOBY XML into text for use in
 	// non-XML aware applications.
         transFactory = TransformerFactory.newInstance();
@@ -61,13 +73,14 @@
 	// We actually need to read in the XSLT file and keep track of the template
 	// names, modes, etc..  Currently, this will not follow links that
 	// import other stylesheets into the one being examined.
-	Element xsltDOMRoot = null;
+        Element xsltDOMRoot = null;
 	Document domDoc = docBuilder.parse(xsltURL.openStream());
 	xsltDOMRoot = domDoc.getDocumentElement();
         if(xsltDOMRoot == null){
             throw new Exception("Error: Could not get XSLT document as DOM from source URL " 
 				+ xsltURL + " (empty or malformed document?)");
         }
+	getId(xsltDOMRoot);  // retrieve the dublin core metadata
 
 	String mode = null; // returned in the end
 
@@ -105,8 +118,7 @@
 	    }
 
 	    // See if the type exists in the ontology
-	    // TODO: add support for registry spec via LSID in mode (how to deal with colons?)
-	    MobyDataType type = MobyDataType.getDataType(templateNameParts[0], SeahawkOptions.getRegistry());
+	    MobyDataType type = MobyDataType.getDataType(templateNameParts[0], getRegistry());
 	    MobyNamespace ns = null;  //no ns restriction by default
 	    if(type == null){
 		// See if there is a namespace restriction e.g. Object-EC
@@ -114,11 +126,11 @@
 		    // Assumes no dashes in the namespace label itself
 		    String namespace = templateNameParts[0].substring(templateNameParts[0].lastIndexOf("-")+1);
 		    //System.err.println("Found a dash in "+templateNameParts[0] + ", assuming ns is " + namespace);
-		    ns = MobyNamespace.getNamespace(namespace, SeahawkOptions.getRegistry());
+		    ns = MobyNamespace.getNamespace(namespace, getRegistry());
 		    // If it can be parsed as a real namespace, treat the stuff before the "-NS" as the data type
 		    if(ns != null){
 			templateNameParts[0] = templateNameParts[0].substring(0, templateNameParts[0].lastIndexOf("-"));
-			type = MobyDataType.getDataType(templateNameParts[0], SeahawkOptions.getRegistry());
+			type = MobyDataType.getDataType(templateNameParts[0], getRegistry());
 		    }
 		    //System.err.println("Resolved ns is " + type);
 		}
@@ -142,7 +154,25 @@
 	return mode; // return the last mode value in the file (useful when mapping XSLT -> data format)
     }
 
-    /**
+    // Looks for Dublin Core metadata
+    private void getId(Element xsltDOMRoot){
+	NodeList idElements = xsltDOMRoot.getElementsByTagNameNS(MobyPrefixResolver.DUBLIN_CORE_NAMESPACE,
+								 "identifier");
+	if(idElements.getLength() == 0){
+	     idElements = xsltDOMRoot.getElementsByTagNameNS(MobyPrefixResolver.DUBLIN_CORE_NAMESPACE,
+							     "source");
+	}
+	if(idElements.getLength() == 0){
+	    return;
+	}
+	if(idElements.getLength() != 1){
+	    System.err.println("More than one Dublin Core identifier was found, cannot disambiguate.");
+	    return;
+	}
+	id = idElements.item(0).getTextContent();
+    }
+
+   /**
      * Report whether a rule exists in the provided XSLT that converts the given MOBY
      * data to the given text type.
      */
@@ -301,6 +331,8 @@
 	    for(Transformer transformer: moby2textConverters){
 		//System.err.println("Setting XSLT var "+XSLT_MODE_VAR+": " +targetTextType);
 		transformer.setParameter(XSLT_MODE_VAR, targetTextType);
+		//transformer.setParameter(XSLT_ELNAME_VAR, targetName);
+		//transformer.setParameter(XSLT_ELNS_VAR, targetNS);
 
 		// Do the actual transformation
 		StringWriter stringWriter = new StringWriter(1000);
@@ -321,4 +353,63 @@
 
 	return null;
     }
+
+    /**
+     * Convenience method to backtrack from a mapping rule to the Moby datatype it consumes.
+     *
+     * @param ruleSource where the rule should be loaded from
+     * @param ruleURI a unique ID for the rule, in the source XML using Dublin Core
+     *
+     * @return a template object with the (minimal) datatype and namespaces consumed by the rule
+     */
+    public static MobyPrimaryDataSimple getObjectConsumed(URL ruleSource, String ruleURI, Registry reg) throws Exception{
+	MobyPrimaryDataSimple template = null;
+	TextClient client = null;
+	try{
+	    client = new TextClient(reg);
+	    client.addMappingsFromURL(ruleSource);
+	} catch(Exception e){
+	    throw new Exception("Internal error: Could not create TextClient and load the rule (from " + 
+				ruleSource+"): "+e.getMessage(), 
+				e);
+	}	 
+	
+	if(client.id == null){
+	    throw new Exception("Internal error: loaded the transformation rule (URI " + ruleURI + 
+				") from " + ruleSource + " but could not retrieve it from TextClient " +
+				"using the URI.  Make sure the transformation rule contains a Dublin Core " +
+				"identifier element specifying this URI.");
+	}
+	else if(!client.id.equals(ruleURI)){
+	    throw new Exception("Internal error: loaded the transformation rule (URI " + ruleURI + 
+				") from " + ruleSource + " but could not retrieve it from TextClient " +
+				"using the URI.  The given Dublin Core info for the rule actually loaded " +
+				"was " + client.id);
+	}
+	else{
+	    template = new MobyPrimaryDataSimple("templateRuleReturnedObject");
+	    // extract the datatype from typeTemplates, there should only be one...
+	    Set<String> dataTypeNames = client.typeTemplates.keySet();
+	    if(dataTypeNames.size() == 0){
+		throw new Exception("No TextClient rules were loaded from " + ruleSource +
+				    " - make sure the XSLT follows the naming conventions described at " +
+				    "http://biomoby.open-bio.org/CVS_CONTENT/moby-live/Java/docs/demRules.html");
+	    }
+	    if(dataTypeNames.size() > 1){
+		throw new Exception("Multiple TextClient rules were loaded from " + ruleSource +
+				    ", please separate them out into separate XSLT files to ensure " +
+				    "they can be referenced inidivudually and unambiguously.");
+	    }
+	    MobyDataType type = MobyDataType.getDataType(dataTypeNames.toArray()[0].toString(), client.getRegistry());
+	    template.setDataType(type);
+	    // extract the ns from typeNsRestrictions
+	    Vector<MobyNamespace> nsRestrictions = client.typeNsRestrictions.get(type.getName());
+	    template.setNamespaces(nsRestrictions.toArray(new MobyNamespace[nsRestrictions.size()]));
+	 }
+	return template;
+    }
+    
+    public Registry getRegistry(){
+	return registry;
+    }     
 }




More information about the MOBY-guts mailing list