Im folgenden JScript-Beispiel wird gezeigt, wie Sie ein bestehendes Dokument mit einem einfachen Mapping laden, seine Komponenten aufrufen, Input- und Output-Instanzdateinamen definieren und das Mapping ausführen.
/* This sample file performs the following operations: Load existing MapForce mapping document. Find source and target component. Set input and output instance filenames. Execute the transformation. Works with Windows scripting host. */ // ---- general helpers ------------------------------ function Exit( message ) { WScript.Echo( message ); WScript.Quit(-1); } function ERROR( message, err ) { if( err != null ) Exit( "ERROR: (" + (err.number & 0xffff) + ") " + err.description + " - " + message ); else Exit( "ERROR: " + message ); } // ---- MapForce constants ----------------------- var eComponentUsageKind_Unknown = 0; var eComponentUsageKind_Instance = 1; var eComponentUsageKind_Input = 2; var eComponentUsageKind_Output = 3; // ---- MapForce helpers ----------------------- // Searches in the specified mapping for a component by name and returns it. // If not found, throws an error. function FindComponent( mapping, component_name ) { var components = mapping.Components; for( var i = 0 ; i < components.Count ; ++i ) { var component = components.Item( i + 1 ); if( component.Name == component_name ) return component; } throw new Error( "Cannot find component with name " + component_name ); } // Browses components in a mapping and returns the first one found acting as // source component (i.e. having connections on its right side). function GetFirstSourceComponent( mapping ) { var components = mapping.Components; for( var i = 0 ; i < components.Count ; ++i ) { var component = components.Item( i + 1 ); if( component.UsageKind == eComponentUsageKind_Instance && component.HasOutgoingConnections ) { return component; } } throw new Error( "Cannot find a source component" ); } // Browses components in a mapping and returns the first one found acting as // target component (i.e. having connections on its left side). function GetFirstTargetComponent( mapping ) { var components = mapping.Components; for( var i = 0 ; i < components.Count ; ++i ) { var component = components.Item( i + 1 ); if( component.UsageKind == eComponentUsageKind_Instance && component.HasIncomingConnections ) { return component; } } throw new Error( "Cannot find a target component" ); } function IndentTextLines( s ) { return "\t" + s.replace( /\n/g, "\n\t" ); } function GetAppoutputLineFullText( oAppoutputLine ) { var s = oAppoutputLine.GetLineText(); var oAppoutputChildLines = oAppoutputLine.ChildLines; var i; for( i = 0 ; i < oAppoutputChildLines.Count ; ++i ) { oAppoutputChildLine = oAppoutputChildLines.Item( i + 1 ); sChilds = GetAppoutputLineFullText( oAppoutputChildLine ); s += "\n" + IndentTextLines( sChilds ); } return s; } // Create a nicely formatted string from AppOutputLines function GetResultMessagesString( oAppoutputLines ) { var s1 = "Transformation result messages:\n"; var oAppoutputLine; var i; for( i = 0 ; i < oAppoutputLines.Count ; ++i ) { oAppoutputLine = oAppoutputLines.Item( i + 1 ); s1 += GetAppoutputLineFullText( oAppoutputLine ); s1 += "\n"; } return s1; } // ---- MAIN ------------------------------------- var wshShell; var fso; var mapforce; // create the Shell and FileSystemObject of the windows scripting system try { wshShell = WScript.CreateObject( "WScript.Shell" ); fso = WScript.CreateObject( "Scripting.FileSystemObject" ); } catch( err ) { ERROR( "Can't create windows scripting objects", err ); } // open MapForce or access currently running instance try { mapforce = WScript.GetObject( "", "MapForce.Application" ); } catch( err ) { ERROR( "Can't access or create MapForce.Application", err ); } try { // Make MapForce UI visible. This is an API requirement for output generation. mapforce.Visible = true; // open an existing mapping. // **** adjust the examples path to your needs ! ************** var sMapForceExamplesPath = fso.BuildPath( wshShell.SpecialFolders( "MyDocuments" ), "Altova\\MapForce2025\\MapForceExamples" ); var sDocFilename = fso.BuildPath( sMapForceExamplesPath, "PersonList.mfd" ); var doc = mapforce.OpenDocument( sDocFilename ); // Find existing components by name in the main mapping. // Note, the names of components may not be unique as a schema component's name // is derived from its schema file name. var source_component = FindComponent( doc.MainMapping, "Employees" ); var target_component = FindComponent( doc.MainMapping, "PersonList" ); // If you do not know the names of the components for some reason, you could // use the following functions instead of FindComponent. //var source_component = GetFirstSourceComponent( doc.MainMapping ); //var target_component = GetFirstTargetComponent( doc.MainMapping ); // specify the desired input and output files. source_component.InputInstanceFile = fso.BuildPath( sMapForceExamplesPath, "Employees.xml" ); target_component.OutputInstanceFile = fso.BuildPath( sMapForceExamplesPath, "test_transformation_results.xml" ); // Perform the transformation. // You can use doc.GenerateOutput() if you do not need result messages. // If you have a mapping with more than one target component and you want // to execute the transformation only for one specific target component, // call target_component.GenerateOutput() instead. var result_messages = doc.GenerateOutputEx(); var summary_info = "Transformation performed from " + source_component.InputInstanceFile + "\n" + "to " + target_component.OutputInstanceFile + "\n\n" + GetResultMessagesString( result_messages ); WScript.Echo( summary_info ); } catch( err ) { ERROR( "Failure", err ); } |