[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Fri Apr 23 05:16:45 UTC 2010


gordonp
Fri Apr 23 01:16:45 EDT 2010
Update of /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services
In directory dev.open-bio.org:/tmp/cvs-serv24515/src/main/ca/ucalgary/seahawk/services

Modified Files:
	TextClient.java 
Log Message:
Better Dublin Core support, added getPossibleTextTypes() for drag and drop export
moby-live/Java/src/main/ca/ucalgary/seahawk/services TextClient.java,1.7,1.8
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services/TextClient.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services/TextClient.java	2009/06/09 19:22:21	1.7
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services/TextClient.java	2010/04/23 05:16:45	1.8
@@ -8,13 +8,15 @@
 
 import org.w3c.dom.*;
 
-import javax.xml.transform.stream.*;
-import javax.xml.transform.*;
 import javax.xml.parsers.*;
+import javax.xml.transform.*;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.*;
 
 import java.io.*;
 import java.net.URL;
 import java.util.*;
+import java.util.logging.*;
 
 /**
  * This class uses XSLT rules to transform MOBY XML data representation
@@ -24,20 +26,26 @@
  */
 public class TextClient{
 
+    public static final String DATA_MAPPING_XSLT_RESOURCE = "ca/ucalgary/seahawk/resources/mobyRules.xsl";
+    public static final String RESOURCE_SYSTEM_PROPERTY = "seahawk.textrules";
+
     public final static String XSLT_NS = "http://www.w3.org/1999/XSL/Transform";
     public final static String XSLT_MODE_VAR = "selectedACDTypeTarget";
 
     private Vector<Transformer> moby2textConverters;  // XSLT engine
     private TransformerFactory transFactory;
     private DocumentBuilder docBuilder;
+    private URL dataMappingXSLTURL;
     //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 Map<String,String> templateIds; // template name -> URN
     private Registry registry;
 
+    private static Logger logger = Logger.getLogger(TextClient.class.getName());
+
     public TextClient() throws Exception{
 	this(SeahawkOptions.getRegistry() == null ? 
 	     RegistryCache.getDefaultRegistry() :
@@ -60,16 +68,95 @@
 	typeTemplates = new HashMap<String, Vector<String>>();
 	typeNsRestrictions = new HashMap<String, Vector<MobyNamespace>>();
 	templateMode = new HashMap<String, String>();
+	templateIds = new HashMap<String, String>();
+
+	ClassLoader cl = getClass().getClassLoader();
+	if(cl == null){
+	    cl = ClassLoader.getSystemClassLoader();
+	}
+        String rulesResource = System.getProperty(RESOURCE_SYSTEM_PROPERTY);
+        if(rulesResource == null){
+	  dataMappingXSLTURL = cl.getResource(DATA_MAPPING_XSLT_RESOURCE);
+        }
+	else if(rulesResource.length() != 0){
+	    // See if it's a URL
+	    try{
+		dataMappingXSLTURL = new URL(rulesResource);
+	    }
+	    catch(Exception e){
+		dataMappingXSLTURL = cl.getResource(rulesResource);
+	    }
+	}
+	if(dataMappingXSLTURL == null){
+	    if(rulesResource.length() != 0){ // if not left intentionally blank
+		logger.log(Level.WARNING, "Could not find MOBY to text mapping resource '"+
+				          rulesResource+"'");
+	    }
+	}
+	else{
+	    try{
+		addMappingsFromURL(dataMappingXSLTURL);
+	    }
+	    catch(Exception e){
+		logger.log(Level.SEVERE, "Error loading default data mapping rules ("+dataMappingXSLTURL+")", e);
+                e.printStackTrace();
+	    }
+	}
+    }
+
+    public String[] getPossibleTextTypes(MobyPrimaryData dataTemplate, boolean mustHaveURN){
+	Vector<String> possibleTypes = new Vector<String>();
+	// Search for templates matching the given type or one of its parent types,
+	// and see if they create the given text type (indicated by the template's mode attribute)
+	for(MobyDataType type = dataTemplate.getDataType();
+	    type != null;
+	    type = type.getParent()){
+	    if(typeTemplates.containsKey(type.getName())){
+		// These two vectors should be of the same length!
+		Vector<String> templateNames = typeTemplates.get(type.getName());
+		Vector<MobyNamespace> nsRestrictions = typeNsRestrictions.get(type.getName());
+		for(int i = 0; i < templateNames.size(); i++){
+		    String templateName = templateNames.elementAt(i);
+		    if(mustHaveURN && getTemplateURN(templateName) == null){
+			continue;
+		    }
+
+		    // the template input moby datatype is the same type as we have an instance of
+		    String textType = templateMode.get(templateName); // mode and text type name are one and the same
+		    // does set-ness match?
+		    if(dataTemplate instanceof MobyPrimaryDataSet){
+			if(templateName.startsWith("Collection-")){
+			    possibleTypes.add(textType);	
+			}
+		    }
+		    else{
+			if(!templateName.startsWith("Collection-")){
+			    possibleTypes.add(textType);
+			}
+		    }
+		    // Is there a namespace restriction on the transformation rule?
+		    MobyNamespace ns = nsRestrictions.elementAt(i);
+		    if(ns != null){
+			for(MobyNamespace n: dataTemplate.getNamespaces()){
+			    if(ns.equals(n)){
+				possibleTypes.add(textType);
+			    }
+			}
+		    }
+		}
+	    }
+	}
+	return possibleTypes.toArray(new String[possibleTypes.size()]);
+    }
+
+    public String getTemplateURN(String templateName){
+	return templateIds.get(templateName);
     }
 
     /**
      * @return the data format the xslt creates (the last template mode attribute in the file) 
      */
     public synchronized String addMappingsFromURL(URL xsltURL) throws Exception{
-        StreamSource stylesheet = new StreamSource(xsltURL.toString());
-	// Prepend to list, so later rules can override ones specified earlier
-	moby2textConverters.insertElementAt(transFactory.newTransformer(stylesheet), 0);
-
 	// 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.
@@ -80,7 +167,6 @@
             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
 
@@ -93,6 +179,9 @@
 		continue;
 	    }
 	    String templateName = template.getAttribute("name");
+
+	    templateIds.put(templateName, getId(template));
+
 	    String m = template.getAttribute("mode");
 	    if(m != null && m.trim().length() > 0){
 		mode = template.getAttribute("mode");
@@ -150,12 +239,16 @@
 	    typeNsRestrictions.get(templateNameParts[0]).insertElementAt(ns, 0);	    
 	}
 
+        DOMSource stylesheet = new DOMSource(domDoc);
+	// Prepend to list, so later rules can override ones specified earlier
+	moby2textConverters.insertElementAt(transFactory.newTransformer(stylesheet), 0);
+
 	//System.err.println("Mode returned by " + xsltURL + " is " + mode);
 	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){
+    private String getId(Element xsltDOMRoot){
 	NodeList idElements = xsltDOMRoot.getElementsByTagNameNS(MobyPrefixResolver.DUBLIN_CORE_NAMESPACE,
 								 "identifier");
 	if(idElements.getLength() == 0){
@@ -163,13 +256,17 @@
 							     "source");
 	}
 	if(idElements.getLength() == 0){
-	    return;
+	    return null;
 	}
 	if(idElements.getLength() != 1){
 	    System.err.println("More than one Dublin Core identifier was found, cannot disambiguate.");
-	    return;
+	    return null;
 	}
-	id = idElements.item(0).getTextContent();
+	String id = idElements.item(0).getTextContent();
+	// remove child so id doesn't show up in the xslt output!
+	idElements.item(0).getParentNode().removeChild(idElements.item(0));
+	System.err.println("Found and removed "+id);
+	return id;
     }
 
    /**
@@ -374,17 +471,17 @@
 				e);
 	}	 
 	
-	if(client.id == null){
+	if(client.templateIds.isEmpty()){
 	    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)){
+	else if(!client.templateIds.containsValue(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);
+				"was " + client.templateIds.values().iterator().next());
 	}
 	else{
 	    template = new MobyPrimaryDataSimple("templateRuleReturnedObject");
@@ -398,7 +495,7 @@
 	    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.");
+				    "they can be referenced indivudually and unambiguously.");
 	    }
 	    MobyDataType type = MobyDataType.getDataType(dataTypeNames.toArray()[0].toString(), client.getRegistry());
 	    template.setDataType(type);




More information about the MOBY-guts mailing list