[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Mon Jan 14 23:00:44 UTC 2008


gordonp
Mon Jan 14 18:00:44 EST 2008
Update of /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services
In directory dev.open-bio.org:/tmp/cvs-serv8988/src/main/ca/ucalgary/seahawk/services

Modified Files:
	MobyClient.java 
Log Message:
Updates for javax.xml.xpath support rather than Aapache Xalan
moby-live/Java/src/main/ca/ucalgary/seahawk/services MobyClient.java,1.19,1.20
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services/MobyClient.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services/MobyClient.java	2008/01/07 22:07:45	1.19
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services/MobyClient.java	2008/01/14 23:00:44	1.20
@@ -1,6 +1,5 @@
 package ca.ucalgary.seahawk.services;
 
-import ca.ucalgary.seahawk.util.NamespaceContextImpl;
 import ca.ucalgary.seahawk.util.SeahawkOptions;
 
 import org.biomoby.client.*;
@@ -8,14 +7,8 @@
 import org.biomoby.shared.*;
 import org.biomoby.shared.data.*;
 
-import org.apache.xpath.XPath; // compiled xpath statement
-import org.apache.xpath.XPathContext; //runtime environment in which XPath is executed
-import org.apache.xpath.objects.XObject; //format of results of XPath evaluation
-import org.apache.xpath.objects.XNodeSet; //format of node based result lists of XPaths
-import org.apache.xpath.objects.XNodeSetForDOM; //format of node based result lists of XPaths
-import org.apache.xpath.objects.XString;
-import org.apache.xml.utils.PrefixResolver;
-import org.apache.xml.utils.PrefixResolverDefault;
+import javax.xml.xpath.*; // compiled xpath statement
+import javax.xml.namespace.NamespaceContext;
 
 import org.w3c.dom.*;
 
@@ -66,9 +59,10 @@
     public static final String IS_ALIVE_SERVICE_URL = "http://moby.ucalgary.ca/moby/ValidateService";
 
     private NamespaceContextImpl nsContext;
+    private XPath commonXPath;
     private CentralImpl c;
     private Map<String,String> isDeadMap;
-    private Map<XPath,MobyComplexBuilder> xpathMap;
+    private Map<XPathExpression,MobyComplexBuilder> xpathMap;
     private Map<Pattern,MobyComplexBuilder> urlRegexMap;
     private Map<Pattern,MobyComplexBuilder> regexMap;
     private Map<String,MobyComplexBuilder> builderNameMap;
@@ -105,12 +99,14 @@
         else{
             c = new CentralCachedCallsImpl();
         }
-	xpathMap = new HashMap<XPath,MobyComplexBuilder>();
+	xpathMap = new HashMap<XPathExpression,MobyComplexBuilder>();
 	urlRegexMap = new HashMap<Pattern,MobyComplexBuilder>();
 	regexMap = new HashMap<Pattern,MobyComplexBuilder>();
 	builderNameMap = new HashMap<String,MobyComplexBuilder>(); 
 	patternNameMap = new HashMap<String,Pattern>(); 
 	nsContext = new NamespaceContextImpl();
+	commonXPath = XPathFactory.newInstance().newXPath();
+	commonXPath.setNamespaceContext(nsContext);
 
  	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 	dbf.setNamespaceAware(true);	
@@ -462,7 +458,7 @@
 		throw new Exception("Attribute "+MEMBERS_RULE_ATTR+" refers to a rule (" +
 				    membersRuleName+") that does not exist");
 	    }
-	    MobyDataType dataType = MobyDataType.getDataType(dataTypeName);
+	    MobyDataType dataType = MobyDataType.getDataType(dataTypeName, getRegistry());
 	    if(!dataType.inheritsFrom(membersBuilder.getDataType())){
 		throw new Exception("Data type produced by inherited rule (" + membersRuleName + 
 				    ") is not a subtype of the current rule (" + dataType.getName() + ")"); 
@@ -938,28 +934,24 @@
 	    return new MobyDataObject[0];
 	}
 
-        XPathContext xpath_context = new XPathContext();
         Node current_node = n;
 	//for searching (dynamic mapping dependent on doc contents)
-        PrefixResolver doc_prefix_resolver = new PrefixResolverDefault(current_node); 
+        //PrefixResolver doc_prefix_resolver = new PrefixResolverDefault(current_node); nsContextImpl.setCurrentNode?
 
 	// Vector of moby objects we construct based on the xpath mappings found
 	Vector<MobyDataObject> objectVector = new Vector<MobyDataObject>(); 
 
-	Iterator xpath_keys = xpathMap.keySet().iterator();
-	while(xpath_keys.hasNext()){ //map still has untraversed xpath entry to test
-	    XPath xpath = (XPath) xpath_keys.next();
-
+	for(XPathExpression xpath: xpathMap.keySet()){ //map still has untraversed xpath entry to test
 	    Object result = null;
 	    try{
 		MobyComplexBuilder rule = (MobyComplexBuilder) xpathMap.get(xpath);
 
-                result = xpath.execute(xpath_context, current_node, doc_prefix_resolver);
+		result = xpath.evaluate(current_node, XPathConstants.NODESET);
 
 		// More than one hit?
-		if(result instanceof XNodeSet){
+		if(result != null){
 		    try{
-			NodeList node_list = ((XNodeSet) result).nodelist(); 
+			NodeList node_list = (NodeList) result; 
 			//System.out.println("number of nodes in the nodelist: " + node_list.getLength()); 
 			if(node_list == null || node_list.getLength() == 0)
 			    continue;
@@ -972,36 +964,31 @@
 			    }
 			}
 			
-		    }catch(javax.xml.transform.TransformerException te){
+		    }catch(XPathExpressionException xpe2){
 			System.err.println( "Warning: Cannot access resulting node list "+
-					    "due to exception in its retrieval: " + te);
+					    "due to exception in its retrieval: " + xpe2);
 			return new MobyDataObject[0];
 		    }
 		}
-		else if(result instanceof XString){
-		    if(((XString) result).str() == null || ((XString) result).str().equals("")){
-			continue;  // no useful data
+		else{
+		    result = xpath.evaluate(current_node, XPathConstants.STRING);
+		    if(result != null){
+			if(result.toString().equals("")){
+			    continue;  // no useful data
+			}
+			//System.out.println("Got string result: " + result);
+			MobyDataObject mobyObj = rule.applyXPath(result, nsContext);
+			if(mobyObj != null){
+			    objectVector.add(mobyObj);
+			}
 		    }
-		    //System.out.println("Got string result: " + result);
-		    MobyDataObject mobyObj = rule.applyXPath(result, nsContext);
-		    if(mobyObj != null){
-			objectVector.add(mobyObj);
+		    else{ //not a node sequence
+			System.out.println( "Warning: the XPath expression ("+ xpath +
+					    ") did not return a node set, cannot select document elements."+
+					    " The returned object was of type "+ result.getClass().getName() );
+			continue;
 		    }
-		}
-		else{ //not a node sequence
-		    System.out.println( "Warning: the XPath expression ("+ xpath.getPatternString()+
-					") did not return a node set, cannot select document elements."+
-					" The returned object was of type "+ result.getClass().getName() );
-		    continue;
-		}
-		
-	    }catch(javax.xml.transform.TransformerException te){
-		logger.warn( "Warning: Cannot select nodes due to exception "+
-		"while executing XPath statement (" + xpath.getPatternString() + "):" +te.getMessage());
-                System.err.println( "Warning: Cannot select nodes due to exception "+
-				    "while executing XPath statement (" + 
-				    xpath.getPatternString() + "):" + te);
-                return new MobyDataObject[0];
+		}		
 	    }catch(javax.xml.xpath.XPathExpressionException xpe){
                 System.err.println( "Warning: Cannot select nodes due to exception "+
 				    "in XPath expression:" + xpe);
@@ -1392,10 +1379,7 @@
 				Map<String,String[]> membersMap, String articleName){ //mobyObj<--mobyNamespaces
 	//System.out.println("xpath addMapping: " + xpath_exp);
 	try{
-	    XPath xpath = new XPath(xpath_exp,
-				    null, // SourceLocator locator,
-				    nsContext, //need to use the default setting
-				    XPath.SELECT);	
+	    XPathExpression xpath = commonXPath.compile(xpath_exp);
 
 	    // Base object
 	    if(mobyDataType == null || mobyDataType.length() == 0){
@@ -1445,19 +1429,6 @@
 	urlRegexMap.clear();
     }
 
-    public void deleteXPathMapping(String xpath_exp){
-	//System.out.println("the number of key-value mappings in this map before deleting: " + map.size());
-	Set keys = xpathMap.keySet();
-	Iterator it = keys.iterator();
-	while(it.hasNext()) //map still has untraversed entry
-	{
-	    XPath xpath = (XPath) it.next(); //Xpath obj. fr map //use an iterator
-	    if(xpath.getPatternString().equals(xpath_exp))
-	        xpathMap.remove(xpath);
-	}
-	//System.out.println("the number of key-value mappings in this map: " + map.size());
-    }
-
     /**
      * Indicates whether at least one production rule exists for the data type or one of its children.
      */




More information about the MOBY-guts mailing list