Ensuring String Length in Policy Builder, using ECMAScript

Outstanding Contributor.
Outstanding Contributor.
0 2 3,783
0 Likes

Problem



During one of my last trainings, a student asked me whether it is possible in Policy Builder to ensure that a string has a certain length. Some think at first sight that the substring verb will extend a string to the length specified by the length parameter, which is not the case. Currently there is no verb offering this capability, so I thought about introducing this capability though ECMAScript.

Solution



The following ECMAScript defines a function named extString, which offers three parameters. The first one specifies the value to be extended, the second the length of the resulting string, and the last parameter defines the character to be used for the concatenation.

Please ensure to specify all three parameters, otherwise the result will not be as expected. Consider using a substring() verb in advance to limit the length of the string, since this function only handles the extension to a given length.

The examples below show a simple ECMAScript and a very simple placement policy calling this function.

Example


// Function to extend the length of a string to a specified size
// To use it all three parameters have to be specified.
// value := Value to extend
// size := length of the resulting string
// fill := character to use for the concatenation

function extString(value,size,fill){

var l = value.length;

while (l < size){
value = value + fill;
l= value.length;
}
return value;
}



Here's a sample policy to demonstrate the use of the script:


<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE policy PUBLIC "policy-builder-dtd" "/home/tschloesser/designer3/designer/eclipse/plugins/com.novell.designer.idm.policybuilder_2.0.0.200711271409/DTD/dirxmlscript.dtd"><policy xmlns:es="http://www.novell.com/nxsl/ecmascript">
<rule>
<description>CreateUniqueCN of 6 characters</description>
<conditions>
<and/>
</conditions>
<actions>
<do-set-local-variable name="temp" scope="policy">
<arg-string>
<token-substring length="6">
<token-op-attr name="Surname"/>
</token-substring>
</arg-string>
</do-set-local-variable>
<do-set-local-variable name="charCN" scope="policy">
<arg-string>
<token-xpath expression='es:extString($temp,5,"0")'/>
</arg-string>
</do-set-local-variable>
<do-set-op-dest-dn>
<arg-dn>
<token-text xml:space="preserve">users\active\</token-text>
<token-local-variable name="temp"/>
</arg-dn>
</do-set-op-dest-dn>
</actions>
</rule>
</policy>

Tags (1)
2 Comments
Absent Member.
Absent Member.
Below are two actions one adds "0" to the end of the surname, and the other adds "0" to be beginning of workforceID. Both pad a variable to a specific length with no scripting needed.


<actions>
<do-reformat-op-attr name="surname">
<arg-value>
<token-substring length="6">
<token-op-attr name="Surname"/>
<token-text xml:space="preserve">000000</token-text>
</token-substring>
</arg-value>
</do-reformat-op-attr>
<do-reformat-op-attr name="workforceID">
<arg-value>
<token-substring start="-9">
<token-text xml:space="preserve">000000000</token-text>
<token-op-attr name="workforceID"/>
</token-substring>
</arg-value>
</do-reformat-op-attr>
</actions>
Absent Member.
Absent Member.
Really a nice way to do this, thanX!
About the Author
I am working as a sen. consultant and technical trainer.
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.