KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method to Capture all 4D Commands with Command Numbers
PRODUCT: 4D | VERSION: 15.1 | PLATFORM: Mac & Win
Published On: December 10, 2015

One of the challenges in writing well formed input templates using 4D Transformation Tags is the appending of command numbers to any 4D commands that are in the input template, see Tech Tip: Where to find 4D Command numbers.

The utility below provides a way to capture in three arrays all 4D commands their ID numbers and an array with the ID number already appended to the command in the ":CXXXX" format. From here a developer can easily create a dialog with a listbox to display the array and integrate a quick lookup.

If (True)
      If (False)
         Begin SQL
            /*
            Name: Get4DCommandsPlusNums
            Path: Get4DCommandsPlusNums
            
            Purpose: Get all 4D command names and IDs.
            
            $1 - Pointer - Pointer to Command Names Plus Number array
            $2 - Pointer - Pointer to Command Names array
            $3 - Pointer - Pointer to Command Numbers array
            */
         End SQL
      End if
      C_TEXT($MethodName_T)
      $MethodName_T:=Current method name
       //===================== Declare Variables ==================================
       //method_parameters_declarations
      C_POINTER($1;$CmdNamesPlusNum_p_aT)
      C_POINTER($2;$CmdNames_p_aT)
      C_POINTER($3;$CmdIDs_p_aT)
       //--------------------------------------------------------------------------
       //method_wide_Commands_declarations
       //--------------------------------------------------------------------------
       //local_variable_declarations
      C_LONGINT($Ndx;$SOA;$RIS;$Params_L;$SAXEvent_L;$Pos_L)
      C_BOOLEAN($TransUnitFound_B;$TargetFound_B)
      C_TEXT($CommandsXLIFFFile_T;$elementName_T;$elementPrefix_T;\
      $CommandName_T;$CommandID_T)
      C_TIME($CommandsXLIFFFile_H)
End if

//====================== Initialize and Setup ================================

$Params_L:=Count parameters
ARRAY TEXT($attributeNames_aT;0)
ARRAY TEXT($attributeVals_aT;0)

$CmdNamesPlusNum_p_aT:=$1
$CmdNames_p_aT:=$2
$CmdIDs_p_aT:=$3

$CommandsXLIFFFile_T:=Application file+Folder separator+\
   "Contents"+Folder separator+\
   "Resources"+Folder separator+\
   "en.lproj"+Folder separator+\
   "4DSyntaxEN.xlf"

If (Test path name($CommandsXLIFFFile_T)#Is a document)
      ALERT("4DSyntaxEN.xlf file not found!")
Else
      $CommandsXLIFFFile_H:=Open document($CommandsXLIFFFile_T;Read mode)
      
       //======================== Method Actions ==================================
      
      If (OK=1)
         Repeat
            $SAXEvent_L:=SAX Get XML node($CommandsXLIFFFile_H)
            Case of
               : ($SAXEvent_L=XML start element)
                  SAX GET XML ELEMENT($CommandsXLIFFFile_H;$elementName_T;\
                  $elementPrefix_T;$attributeNames_aT;$attributeVals_aT)
                  
                   // Need to locate the "target" element for each Command "trans-unit".
                  Case of
                     : ($elementName_T="trans-unit")
                        If (Find in array($attributeVals_aT;"cmd@")>0)
                           $Pos_L:=Find in array($attributeNames_aT;"id")
                           If ($Pos_L>0)
                              $CommandID_T:=$attributeVals_aT{$Pos_L}
                               // Ignore the first trans-unit in each group, it's not a Command.
                              If ($CommandID_T#"@_0")
                                 $TransUnitFound_B:=True
                              End if
                           End if
                        End if
                        
                     : ($elementName_T="target")
                        If ($TransUnitFound_B)
                           $TargetFound_B:=True
                           $TransUnitFound_B:=False
                        End if
                  End case
                  
               : ($SAXEvent_L=XML DATA)
                   // With the target located, we have the Command name.
                  If ($TargetFound_B)
                     SAX GET XML ELEMENT VALUE($CommandsXLIFFFile_H;$CommandName_T)
                     APPEND TO ARRAY($CmdNamesPlusNum_p_aT->;$CommandName_T)
                     APPEND TO ARRAY($CmdIDs_p_aT->;$CommandID_T)
                     $CommandName_T:=""
                     $CommandID_T:=""
                     $TargetFound_B:=False
                  End if
                  
            End case
         Until ($SAXEvent_L=XML end document)
         CLOSE DOCUMENT($CommandsXLIFFFile_H)
      End if

      For ($Ndx;1;Size of array($CmdNamesPlusNum_p_aT->))
         $Pos:=Position(" (";$CmdNamesPlusNum_p_aT->{$Ndx})
         If ($Pos>0)
            $CmdNamesPlusNum_p_aT->{$Ndx}:=Substring($CmdNamesPlusNum_p_aT->{$Ndx};1;$Pos-1)
         ;Else
            $Pos:=Position(" {";$CmdNamesPlusNum_p_aT->{$Ndx})
            If ($Pos>0)
               $CmdNamesPlusNum_p_aT->{$Ndx}:=Substring($CmdNamesPlusNum_p_aT->{$Ndx};1;$Pos-1)
            ;Else
               $Pos:=Position(" ->";$CmdNamesPlusNum_p_aT->{$Ndx})
               If ($Pos>0)
                  $CmdNamesPlusNum_p_aT->{$Ndx}:=Substring($CmdNamesPlusNum_p_aT->{$Ndx};1;$Pos-1)
               ;Else
                  $Pos:=Position(" ->";$CmdNamesPlusNum_p_aT->{$Ndx})
                  If ($Pos>0)
                     $CmdNamesPlusNum_p_aT->{$Ndx}:=Substring($CmdNamesPlusNum_p_aT->{$Ndx};1;$Pos-1)
                  ;Else
                     $Pos:=Position(" ->";$CmdNamesPlusNum_p_aT->{$Ndx})
                     If ($Pos>0)
                        $CmdNamesPlusNum_p_aT->{$Ndx}:=Substring($CmdNamesPlusNum_p_aT->{$Ndx};1;$Pos-1)
                     ;Else
                        $Pos:=Position(" </";$CmdNamesPlusNum_p_aT->{$Ndx})
                        If ($Pos>0)
                           $CmdNamesPlusNum_p_aT->{$Ndx}:=Substring($CmdNamesPlusNum_p_aT->{$Ndx};1;$Pos-1)
                        ;Else
                           $Pos:=Position("</";$CmdNamesPlusNum_p_aT->{$Ndx})
                           If ($Pos>0)
                              $CmdNamesPlusNum_p_aT->{$Ndx}:=Substring($CmdNamesPlusNum_p_aT->{$Ndx};1;$Pos-1)
                           End if
                        End if
                     End if
                  End if
               End if
            End if
         End if
         $CmdNamesPlusNum_p_aT->{$Ndx}:=RGX_RemoveWhiteSpace ($CmdNamesPlusNum_p_aT->{$Ndx})
         APPEND TO ARRAY($CmdNames_p_aT->;$CmdNamesPlusNum_p_aT->{$Ndx})
         $CmdNamesPlusNum_p_aT->{$Ndx}:=$CmdNamesPlusNum_p_aT->{$Ndx}+":C"+$CmdIDs_p_aT->{$Ndx}
         $Pos:=Position(" :C";$CmdNamesPlusNum_p_aT->{$Ndx})
         If ($Pos>0)
            $CmdNamesPlusNum_p_aT->{$Ndx}:=Replace string($CmdNamesPlusNum_p_aT->{$Ndx};" :C";":C")
         End if
      End for
      
       //================== Clean up and Exit ===========================
      
End if


Below is the code to the utility RGX_RemoveWhiteSpace that is called from the utility above.


If (True)
      If (False)
         Begin SQL
            /*
            Name: RGX_RemoveWhiteSpace
            Path: RGX_RemoveWhiteSpace
            
            Purpose: Remove all leading and trailing white space
            
            $0 - TEXT - Outout text
            $1 - TEXT - Input text
            */
         End SQL
      End if
      C_TEXT($MethodName_T)
      $MethodName_T:=Current method name
       //===================== Declare Variables ==================================
       //method_parameters_declarations
      C_TEXT($myString;$1)
      C_TEXT($0;$resultString)
       //--------------------------------------------------------------------------
       //method_wide_constants_declarations
       //--------------------------------------------------------------------------
       //local_variable_declarations
      C_LONGINT($Ndx;$SOA;$RIS;$Params_L)
      C_TEXT($pattern)
      C_LONGINT($start;$position;$length)
      C_BOOLEAN($foundFlag)
      
End if
//====================== Initialize and Setup ================================

$Params_L:=Count parameters
$myString:=$1

//======================== Method Actions ==================================

$start:=1
$foundFlag:=False

//regex pattern for leading white spaces
//^ -- search the beginging of the string
//\s+ -- search for one or more white spaces
$pattern:="^\\s+"

If (Match regex($pattern;$myString;$start;$position;$length))
      $resultString:=Substring($myString;$length+1)
Else
      $resultString:=$myString
End if

//regex pattern for trailing white spaces
//$ -- search the end of the string
$pattern:="\\s+$"

If (Match regex($pattern;$resultString;$start;$position;$length))
      $resultString:=Substring($resultString;$start;$position-1)
End if

//======================== Clean up and Exit =================================

$0:=$resultString