Hey All
I have attempted the scenario in which i need to dynamically address the contents of a document.
I found the following code in PSUtilities service name substituteVariables.
// Get input objects from pipeline
IDataCursor idcPipeline = pipeline.getCursor();
String strInputString = null;
if (idcPipeline.first("inputString"))
{
strInputString = (String)idcPipeline.getValue();
}
else
{
// Input string is null
return;
}
// int intInputStringLen = strInputString.length();
String strOutputString = “”;
int intCurrentIndex = 0;
int intStartIndex = 0;
int intEndIndex = 0;
//System.out.println("pipeline = " + pipeline);
while ((intStartIndex = strInputString.indexOf(‘%’,intCurrentIndex)) != -1)
{
// If %, then ignore…don’t perform variable substitution
if ((intStartIndex != 0) &&
(strInputString.charAt(intStartIndex - 1) == ‘\’))
{
strOutputString = strOutputString + strInputString.substring(intCurrentIndex, intStartIndex - 1) + ‘%’;
intCurrentIndex = intStartIndex + 1;
continue;
}
// Find the ending %
intEndIndex = strInputString.indexOf('%', intStartIndex + 1);
if (intEndIndex == -1)
{
break;
}
// Print out everything before the %
strOutputString = strOutputString + strInputString.substring(intCurrentIndex, intStartIndex);
// Find the string to substitute
String strStringToSubstitute = null;
String strVariableName = strInputString.substring(intStartIndex + 1, intEndIndex);
//System.out.println("strVariableName = " + strVariableName);
if (strVariableName.equals(""))
{
// We had occurence of "%%"
intCurrentIndex = intEndIndex + 1;
continue;
}
// Perform variable substitution
StringTokenizer tokenizedString = new StringTokenizer(strVariableName, "/", false);
int maxTokens = tokenizedString.countTokens();
IData currentRecord = null;
int intTokenIndex = 1;
while (tokenizedString.hasMoreTokens())
{
String strCurrentToken = tokenizedString.nextToken();
System.out.println("strCurrentToken = " + strCurrentToken);
IDataCursor idc = null;
if (currentRecord == null)
{
// New search - look in pipeline for record/string
idc = idcPipeline;
//System.out.println(“Searching in pipeline”);
}
else
{
//System.out.println("Searching in record = " + currentRecord);
idc = currentRecord.getCursor();
}
if (idc.first(strCurrentToken))
{
Object o = idc.getValue();
//System.out.println("intTokenIndex = " + intTokenIndex);
//if (o instanceof String)
//{
//System.out.println(“String!”);
//}
if ((intTokenIndex == maxTokens) && (o instanceof String))
{
// This is the last token. Look for a string
// Variable found in pipeline
strStringToSubstitute = (String)o;
strOutputString = strOutputString + strStringToSubstitute;
intCurrentIndex = intEndIndex + 1;
//System.out.println(“String found”);
//System.out.println("new strOutputString = " + strOutputString);
}
else if ((intTokenIndex != maxTokens) && (o instanceof IData))
{
// Look for a IData (record)
currentRecord = (IData)o;
//System.out.println("Record found = " + currentRecord);
}
else
{
// Type mismatch - variable not found in pipeline
strOutputString = strOutputString + strInputString.substring(intStartIndex, intEndIndex + 1);
intCurrentIndex = intEndIndex + 1;
break; // Ignore other tokens
}
}
else
{
// Variable not found in pipeline
strOutputString = strOutputString + strInputString.substring(intStartIndex, intEndIndex + 1);
intCurrentIndex = intEndIndex + 1;
break; // Ignore other tokens
}
intTokenIndex++;
}
}
strOutputString = strOutputString + strInputString.substring(intCurrentIndex);
idcPipeline.insertAfter("outputString", strOutputString);
idcPipeline.destroy();
just pass the input string and it will replace the variables.
Hope this helps.
DO let me know if you have any questions.
#webMethods#Integration-Server-and-ESB#Flow-and-Java-services