incubator-xap-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mtu...@apache.org
Subject svn commit: r427651 - /incubator/xap/trunk/src/xap/Xap.js
Date Tue, 01 Aug 2006 17:14:46 GMT
Author: mturyn
Date: Tue Aug  1 10:14:46 2006
New Revision: 427651

URL: http://svn.apache.org/viewvc?rev=427651&view=rev
Log:
Added a generalised class-tree walker to which you can attach a callback, and a method to
find the dependencies of all classes in the tree.

I will probably break these out into a separate analysis object later.

Modified:
    incubator/xap/trunk/src/xap/Xap.js

Modified: incubator/xap/trunk/src/xap/Xap.js
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/Xap.js?rev=427651&r1=427650&r2=427651&view=diff
==============================================================================
--- incubator/xap/trunk/src/xap/Xap.js (original)
+++ incubator/xap/trunk/src/xap/Xap.js Tue Aug  1 10:14:46 2006
@@ -441,48 +441,160 @@
 }
 
 
-
-Xap.getAllXapClasses = function(obj,name,arr){
+Xap.getAllClasses = function(name,arr){
 	var theArray = null ;
 	if (!arr ){
 		theArray = new Array(0) ;
 	} else {
 		theArray = arr ;
 	}
-	if ( obj.prototype ){
-		theArray.push( name ) ;
-	} else {
-		for (var oo in obj){			
-			Xap.getAllXapClasses(obj[oo],name+"."+oo,theArray );
-		}
-	}
+	var pushClass = function(){ 
+									if(arguments[0][3]){
+											theArray.push(arguments[0][1]) ;
+									} 
+								}
+	// At each node
+	Xap.walkClassTree(name, pushClass) ;
 	return theArray ;
 }
 
 
 
-Xap.getClassTree = function(name,obj,tree){
-	var theTree = null ;
-	if (!tree ){
-		theTree = new Object() ;
+
+
+Xap.walkClassTree = function(name,callbackFunction,level,object){
+	var theLevel = -1 ;
+	if( level ){
+		theLevel = level ;
 	} else {
-		theTree = tree ;
+		theLevel=0 ;
 	}
-	
 	var theObject = null ;
-	if( obj ){
-		theObject = obj ;
+	if( object ){
+		theObject = object ;
 	} else {
-		theObject = eval(name) ;	
+		try {
+			var theObject = eval(name) ;
+		} catch(ee){
+			return ;
+		}
 	}
+	var args = [] ;
+	args.push(this) ;
+	args.push(name) ;
+	args.push(theObject);
 	if ( theObject.prototype ){
-		theTree[name] = theObject ;
+		// Let the callback know that this is a class, not a package node:
+		args.push(true) ;
+		callbackFunction.call(this,args) ;
 	} else {
 		for (var oo in theObject){			
-			Xap.getClassTree(name+"."+oo,theObject[oo],theTree[oo] );
+		// Let the callback know that this is a package node, not a class
+			args.push(false) ;
+	 // Better in a different order?
+ 			callbackFunction.call(this,args) ;
+			Xap.walkClassTree(name+"."+oo,callbackFunction,theLevel+1 );
+		}
+	}
+
+	return ;
+}
+
+// Regular expressions for finding and manipulation Xap.require() or dojo.require()
+// statements in code:
+Xap.require_startRegexp = /^.*require\S\s*['"]/  ;
+Xap.require_endRegexp = /["'].*$/ ;
+// Exclude algorithmic .require() statements that aren't just quoted strings:
+Xap.requireRegexp = /(Xap|dojo)\.require\(\s*['"]\S*?['"]\s*\)/mg ;
+
+Xap.getDependencies = function(className){
+	var requirements = [] ;
+	if( !Xap._requestService ){
+		Xap._requestService = new xap.requestservice.RequestService( this );
+	}
+	
+	// Build a file path from a class name:
+	var filePath = "../../src/"+ className.replace(Xap.allFullstopsRegexp,"/") + ".js" ;
+	
+	// Get the source for the class:
+	var classSource = Xap._requestService.retrieve( filePath ).responseText;
+	
+	// These are the lines that have a Xap.require() or a dojo.require() statement in them:
+	var requireLines = classSource.match(Xap.requireRegexp) ;
+	
+	if(!requireLines){
+		return requirements;
+	}
+	
+	for(var jj=0; jj < requireLines.length; ++jj){
+		// extract the argument to the .require():
+		var req = requireLines[jj].replace(Xap.require_startRegexp,"");
+		req=req.replace(Xap.require_endRegexp,"") ;
+		requirements.push(req) ;
+		var bkp=-1 ;
+	}
+		
+	return requirements ;
+}
+
+
+Xap.getAllDependencies = function(name,holder,level){
+
+	var theLevel = -1 ;
+	if(level){
+		theLevel = level ;
+	} else {
+		theLevel = 0 ;
+	}
+
+	if( holder && holder[name]){
+		return ;
+	}
+    
+   
+	var theHolder= null ;
+	if (!holder ){
+		theHolder = new Object() ;
+	} else {
+		theHolder = holder ;
+	}
+	
+	try {
+		var theObject = eval(name) ;
+	} catch (ee){
+		Xap._logString += ("Problem with getting dependencies for \""+name+"\":\n"+ee) ;
+		return ;
+	}
+	
+	if(!theObject){
+		return ;
+	}
+	
+	if ( theObject.prototype ){
+		theHolder[name] = Xap.getDependencies(name) ;
+		var nextLevel = 1+ theLevel ;
+		for(var ii=0; ii<theHolder[name].length; ++ii ){
+			Xap.getAllDependencies( (theHolder[name])[ii] ,theHolder,nextLevel) ;
+		}
+	} else {
+		if ( theLevel == 0){
+			var clazzes = Xap.getAllClasses ("xap") ;
+			var nextLevel = 1+ theLevel ;			
+			for(var jj=0; jj < clazzes.length; ++jj){
+				Xap.getAllDependencies(clazzes[jj],theHolder,nextLevel) ;
+			}
+			
+/*			var arr = new Array(0) ; 
+ *			for( key in theHolder){ 
+ *				arr.push(key+"::"+theHolder[key].join(","))
+ *			}
+ *			prompt("",arr.join("^")) ;		
+ */			
+			return  theHolder;			
 		}
 	}
-	return theTree ;
 }
+
+
 
 



Mime
View raw message