// Check module for bad layout DXL

/*
   It is common for modules to contain layout DXL columns that cause
   DXL errors when the view is loaded. This may be due to #included
   files being removed or because of syntax errors in the layout DXL itself.
   
   Most frustrating is when the default view for the module contains
   a column that causes DXL errors. This prevents scripts from running
   unattended.
   
   This function scans a module for layout DXL and reports any errors
   that are found.
   
   Tony Goodman.
*/


/******************************************************************************
	checkModuleForBadLayoutDxl
	
	Checks the given module for bad layout DXL.
******************************************************************************/
void checkModuleForBadLayoutDxl(string modName)
{
	Module m              = null
	string viewName       = ""
	string defViewUser    = ""
	string defViewModule  = ""
	string sRes           = ""
	Column c              = null
	string dxlCode        = ""
	bool   bRes           = false
	int    errors         = 0

	
	print("Module: " modName "\n")
	
	// suspend error reporting and open module in background
	noError

	m = read(modName, false)
	
	// we ignore any errors generated because these will be picked up
	// later when we loop through all the views.
	sRes = lastError
	
	// check that the module was opened successfully
	if (null m)
	{
		print("ERROR opening module: " sRes "\n")
		return
	}

	// get the user's default view
	defViewUser = getDefaultViewForUser(m)
	
	// if there is a default view then clear it to ensure module is opened in "Standard view"
	if (!null defViewUser)
	{
		sRes = clearDefaultViewForUser(m)
		
		if (sRes != "")
		{
			print("ERROR clearing default view for user: " sRes "\n")
			close(m)
			return
		}
	}
	
	// get the module's default view
	defViewModule = getDefaultViewForModule(m)
	
	// if there is a default view then clear it to ensure module is opened in "Standard view"
	if (!null defViewModule)
	{
		sRes = clearDefaultViewForModule(m)
		
		if (sRes != "")
		{
			print("ERROR clearing default view for module: " sRes "\n")
			close(m)
			return
		}
	}
	
	// open module visible so we can load the views
	m = read(modName, true)

	// loop through the views in the module
	for viewName in views(m) do
	{
		print("View \"" viewName "\"...\t")
		
		// error count for this view
		errors = 0
		
		// suspend error reporting
		noError
		
		// load the view
		bRes = load(m, view viewName)
	
		if (!bRes)
		{
			// view was not loaded - continue to next view
			print("ERROR loading view - Columns not checked!\n")
			lastError
			continue
		}
		
		// loop through the columns in the view
		for c in m do
	    {
		    // get the layout DXL - this will return null if this is not a layout dxl column
	        dxlCode = dxl(c)
	        
	        if (dxlCode "" != "")
	        {
		        // check the layout dxl for errors
		        sRes = checkDXL(dxlCode)
		        
		        if (sRes "" != "")
		        {
			        // report bugs
					print("ERROR in Layout DXL Column: \"" title(c) "\"\n")
					errors++
		        }
	        }
	    }
	    
	    // resume error reporting - no need to check for errors because we
	    // already checked the layout dxl
	    sRes = lastError
	    
	    // if there were no errors in this view - report OK to user
	    if (errors == 0)
	    {
		    print("OK\n")
	    }
	}
	
	// reset the user's default view (if there was one)
	if (!null defViewUser)
	{
		setDefaultViewForUser(m, defViewUser)
	}
	
	// reset the module's default view (if there was one)
	if (!null defViewModule)
	{
		setDefaultViewForModule(m, defViewModule)
	}
	
	// close the module
	close(m)

	print("\n")
}