To use this function add it to the class library file named Utilities.ts
GitHub Repository: https://github.com/SimonSparksUK/Orchestrator
Filename: Utilities.ts
Description: Orchestrator Class to Build an XPath Query
Sample Usage: Simple Filtering by Parent vCenter
letobjXPathQuery:XPathQuery=newXPathQuery(XPathLogicalOperator.AND);
objXPathQuery.addXPathExpression(XPathFunction.NONE,XPathOperator.EQUAL,"sdkConnection/id",objVcSdkConnection.id,false);
letstrXPathQuery:string=objXPathQuery.buildXPathQuery();TypeScriptSample Usage: ComplexFiltering by Parent vCenter
letobjXPathQuery:XPathQuery=newXPathQuery(XPathLogicalOperator.AND);
objXPathQuery.addXPathExpression(XPathFunction.NONE,XPathOperator.EQUAL,"sdkConnection/id",objVcSdkConnection.id,false);
objXPathQuery.addXPathExpression(XPathFunction.STARTS_WITH,XPathOperator.EQUAL,"name","dev",false);
letstrXPathQuery:string=objXPathQuery.buildXPathQuery();TypeScriptClass: XPathQuery
exportclassXPathQuery{
private arrXPathExpression:XPathExpression[];
private enumXPathLogicalOperator:XPathLogicalOperator;
publicconstructor(enumXPathLogicalOperator:XPathLogicalOperator,arrXPathExpression?:XPathExpression[]){
if (arrXPathExpression){
this.arrXPathExpression=arrXPathExpression;
}
if (enumXPathLogicalOperator){
this.enumXPathLogicalOperator=enumXPathLogicalOperator;
}
}
}TypeScriptPublic Functions: Getters and Setters
publicgetXPathLogicalOperator():XPathLogicalOperator{
returnthis.enumXPathLogicalOperator;
}
publicsetXPathLogicalOperator(enumXPathLogicalOperator:XPathLogicalOperator):void{
this.enumXPathLogicalOperator=enumXPathLogicalOperator;
}
publicgetXPathExpressions():XPathExpression[]{
returnthis.arrXPathExpression;
}
publicsetXPathExpressions(arrXPathExpression:XPathExpression[]):void{
this.arrXPathExpression=arrXPathExpression;
}TypeScriptPrivate Function: buildXPathExpression
privatebuildXPathExpression(objXPathExpression:XPathExpression):string{
letstrXPathFunction:string=objXPathExpression.enumXPathFunction;
strXPathFunction=strXPathFunction.replace(XPathReplacement.FIELD,objXPathExpression.strFieldName);
strXPathFunction=strXPathFunction.replace(XPathReplacement.FIND,objXPathExpression.strValue);
if (objXPathExpression.intStartPosition){
strXPathFunction=strXPathFunction.replace(XPathReplacement.START_POSITION,objXPathExpression.intStartPosition.toString());
}
if (objXPathExpression.intLength){
strXPathFunction=strXPathFunction.replace(XPathReplacement.LENGTH,objXPathExpression.intLength.toString());
}
letstrXPathExpression:string=`${strXPathFunction}`;
if (objXPathExpression.enumXPathFunction===XPathFunction.CONTAINS||XPathFunction.MATCHES||XPathFunction.STARTS_WITH){
// No Additional Query Parts Required as They Compare to Boolean TRUE
}elseif (objXPathExpression.enumXPathFunction===XPathFunction.TRANSLATE_LOWER_TO_UPPER){
strXPathFunction=strXPathFunction+`${objXPathExpression.strValue.toUpperCase()}`;
}elseif (objXPathExpression.enumXPathFunction===XPathFunction.TRANSLATE_UPPER_TO_LOWER){
strXPathFunction=strXPathFunction+`${objXPathExpression.enumXPathOperator}${objXPathExpression.strValue.toLowerCase()}`;
}else{
strXPathFunction=strXPathFunction+`${objXPathExpression.enumXPathOperator}${objXPathExpression.enumXPathOperator}${objXPathExpression.strValue}`;
}
if (objXPathExpression.blnNegateExpression===true){
strXPathExpression=`not(${strXPathExpression})`;
}
returnstrXPathExpression;
}TypeScriptPublic Function: addXPathExpression
publicaddXPathExpression(enumXPathFunction:XPathFunction,enumXPathOperator:XPathOperator,strFieldName:string,strValue:string,blnNegateExpression:boolean=false,intStartPosition?:number,intLength?:number):boolean{
try{
letobjXPathExpression:XPathExpression={
enumXPathFunction:enumXPathFunction,
enumXPathOperator:enumXPathOperator,
strFieldName:strFieldName,
strValue:strValue,
blnNegateExpression:blnNegateExpression
};
if (intStartPosition){
objXPathExpression.intStartPosition=intStartPosition;
}
if (intLength){
objXPathExpression.intLength=intLength;
}
this.arrXPathExpression.push(objXPathExpression);
returntrue;
}catch (objException){
returnfalse;
}
}TypeScriptPublic Function: buildXPathQuery
publicbuildXPathQuery():string{
letstrXPathQuery:string=``;
this.arrXPathExpression.forEach((objXPathExpression:XPathExpression,intIndex:number):void=>{
letstrXPathExpression:string=this.buildXPathExpression(objXPathExpression);
if (this.arrXPathExpression.length!==intIndex){
strXPathQuery=strXPathQuery+`${this.enumXPathLogicalOperator.toString()}${strXPathExpression}`;
}
});
returnstrXPathQuery;
}TypeScriptInterface: XPathExpression
exportinterfaceXPathExpression{
enumXPathFunction:XPathFunction;
enumXPathOperator:XPathOperator;
strFieldName:string;
strValue:string;
blnNegateExpression:boolean;
intStartPosition?:number;
intLength?:number;
}TypeScriptEnumeration: XPathLogicalOperator
exportenumXPathLogicalOperator{
AND="and",
OR="or"
}TypeScriptEnumeration: XPathOperator
exportenumXPathOperator{
EQUAL="=",
NOT_EQUAL="!=",
LESS_THAN="<",
GREATER_THAN=">",
LESS_THAN_OR_EQUAL="<=",
GREATER_THAN_OR_EQUAL=">="
}Enumeration: XPath
exportenumXPathReplacement{
FIELD="FIELD",
FIND="FIND",
LENGTH="LENGTH",
START_POSITION="START_POSITION"
}TypeScriptEnumeration: XPathTranslate
exportenumXPathTranslate{
ALPHABET_LOWERCASE="abcdefghijklmnopqrstuvwxyz",
ALPHABET_UPPERCASE="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
}TypeScriptEnumeration: XPathFunction
exportenumXPathFunction{
NONE=`${XPathReplacement.FIELD}`,
LAST="last()",
NODE="node()",
POSITION="position()",
STRING_LENGTH=`string-length(${XPathReplacement.FIELD})`,
MATCHES=`matches(${XPathReplacement.FIELD},${XPathReplacement.FIND}) = true`,
CONTAINS=`contains(${XPathReplacement.FIELD},${XPathReplacement.FIND}) = true`,
STARTS_WITH=`starts-with(${XPathReplacement.FIELD},${XPathReplacement.FIND}) = true`,
SUBSTRING_TO_END=`substring(${XPathReplacement.FIELD},${XPathReplacement.START_POSITION})`,
SUBSTRING_TO_LENGTH=`substring(${XPathReplacement.FIELD},${XPathReplacement.START_POSITION},${XPathReplacement.LENGTH})`,
TRANSLATE_LOWER_TO_UPPER=`translate(${XPathReplacement.FIELD},${XPathTranslate.ALPHABET_LOWERCASE},${XPathTranslate.ALPHABET_UPPERCASE})`,
TRANSLATE_UPPER_TO_LOWER=`translate(${XPathReplacement.FIELD},${XPathTranslate.ALPHABET_UPPERCASE},${XPathTranslate.ALPHABET_LOWERCASE})`
}TypeScriptDiscover more from Cloud Build Tools
Subscribe to get the latest posts sent to your email.
