The following JScript example shows how to use the MapForce API to automate tasks pertaining to MapForce projects. Before running the example, make sure to edit the variable strSamplePath so that it points to the following folder of your MapForce installation: C:\Users\<username>\Documents\Altova\MapForce2024\MapForceExamples.
To successfully run all operations in this example below, you will need the Enterprise edition of MapForce. If you have the Professional edition, comment out the lines that insert the WebService project.
// //////////// global variables ///////////////// var objMapForce = null; var objWshShell = null; var objFSO = null; // !!! adapt the following path to your needs. !!! var strSamplePath = "C:\\Users\\<username>\\Documents\\Altova\\MapForce2024\\MapForceExamples\\"; // /////////////////////// Helpers ////////////////////////////// function Exit(strErrorText) { WScript.Echo(strErrorText); WScript.Quit(-1); } function ERROR(strText, objErr) { if (objErr != null) Exit ("ERROR: (" + (objErr.number & 0xffff) + ")" + objErr.description + " - " + strText); else Exit ("ERROR: " + strText); } function CreateGlobalObjects () { // the Shell and FileSystemObject of the windows scripting host often useful try { objWshShell = WScript.CreateObject("WScript.Shell"); objFSO = WScript.CreateObject("Scripting.FileSystemObject"); } catch(err) { Exit("Can't create WScript.Shell object"); } // create the MapForce connection // if there is a running instance of MapForce (that never had a connection) - use it // otherwise, we automatically create a new instance try { objMapForce = WScript.GetObject("", "MapForce.Application"); } catch(err) { { Exit("Can't access or create MapForce.Application"); } } } // ---------------------------------------------------------- // print project tree items and their properties recursively. // ---------------------------------------------------------- function PrintProjectTree( objProjectItemIter, strTab ) { while ( ! objProjectItemIter.atEnd() ) { // get current project item objItem = objProjectItemIter.item(); try { // ----- print common properties strGlobalText += strTab + "[" + objItem.Kind + "]" + objItem.Name + "\n"; // ----- print code generation properties, if available try { if ( objItem.CodeGenSettings_UseDefault ) strGlobalText += strTab + " Use default code generation settings\n"; else strGlobalText += strTab + " code generation language is " + objItem.CodeGenSettings_Language + " output folder is " + objItem.CodeGenSettings_OutputFolder + "\n"; } catch( err ) {} // ----- print WSDL settings, if available try { strGlobalText += strTab + " WSDL File is " + objItem.WSDLFile + " Qualified Name is " + objItem.QualifiedName + "\n"; } catch( err ) {} } catch( ex ) { strGlobalText += strTab + "[" + objItem.Kind + "]\n" } // ---- recurse PrintProjectTree( new Enumerator( objItem ), strTab + ' ' ); objProjectItemIter.moveNext(); } } // ---------------------------------------------------------- // Load example project installed with MapForce. // ---------------------------------------------------------- function LoadSampleProject() { // close open project objProject = objMapForce.ActiveProject; if ( objProject != null ) objProject.Close(); // open sample project and iterate through it. objProject = objMapForce.OpenProject(strSamplePath + "MapForceExamples.mfp"); // dump properties of all project items strGlobalText = ''; PrintProjectTree( new Enumerator (objProject), ' ' ) WScript.Echo( strGlobalText ); objProject.Close(); } // ---------------------------------------------------------- // Create a new project with some folders, mappings and a // Web service project. // ---------------------------------------------------------- function CreateNewProject() { try { // create new project and specify file to store it. objProject = objMapForce.NewProject(strSamplePath + "Sample.mfp"); // create a simple folder structure objProject.CreateFolder( "New Folder 1"); objFolder1 = objProject.Item(1); objFolder1.CreateFolder( "New Folder 2"); objFolder2 = ( new Enumerator( objFolder1 ) ).item(); // an alternative to Item(0) // add two different mappings to folder structure objFolder1.AddFile( strSamplePath + "DB_Altova_SQLXML.mfd"); objMapForce.Documents.OpenDocument(strSamplePath + "InspectionReport.mfd"); objFolder2.AddActiveFile(); // override code generation settings for this folder objFolder2.CodeGenSettings_UseDefault = false; objFolder2.CodeGenSettings_OutputFolder = strSamplePath + "SampleOutput" objFolder2.CodeGenSettings_Language = 1; //C++ // insert Web service project based on a wsdl file from the installed examples objProject.InsertWebService( strSamplePath + "TimeService/TimeService.wsdl", "{http://www.Nanonull.com/TimeService/}TimeService", "TimeServiceSoap", true ); objProject.Save(); if ( ! objProject.Saved ) WScript.Echo("problem occurred when saving project"); // dump project tree strGlobalText = ''; PrintProjectTree( new Enumerator (objProject), ' ' ) WScript.Echo( strGlobalText ); } catch (err) { ERROR("while creating new project", err ); } } // ---------------------------------------------------------- // Generate code for a project's sub-tree. Mix default code // generation parameters and overloaded parameters. // ---------------------------------------------------------- function GenerateCodeForNewProject() { // since the Web service project contains only initial mappings, // we generate code only for our custom folder. // code generation parameters from project are used for Folder1, // whereas Folder2 provides overwritten values. objFolder = objProject.Item(1); objFolder1.GenerateCode(); } // /////////////////////// MAIN ////////////////////////////// CreateGlobalObjects(); objMapForce.Visible = true; LoadSampleProject(); CreateNewProject(); GenerateCodeForNewProject(); // uncomment to shut down application when script ends // objMapForce.Visible = false; |