Reputation: 12347
I am trying to get command output from Linux box
eg: tnsping
and I want to preserve it's newline characters.
Below is the code where I add command(s) to frows
and pass it to a function for execution
def oracleCommand(csm,pluginOutputs)
{
HashMap<String,String> params = IntermediateResults.get("userparams")
Map env=AppContext.get(AppCtxProperties.environmentVariables)
def fClass = new GroovyClassLoader().parseClass( new File( 'plugins/infa9/Infa9CommandExecUtil.groovy' ) )
List<String> frows
frows=["tnsping $params.tns"] //for the time being only one command is here
List<String> resultRows = fClass.newInstance().fetchCommandOutput( params, env, frows )
csm.oracleCommand(){
oracle_input(frows[0]){}
oracle_output(resultRows[0]){}
}
}
In the below code I am reading the result, splitting result based on newlines so all my newlines are gone
public List<String> fetchCommandOutput( Map<String,String> params, Map env, List<String> rows )
{
List<String> outputRows = new ArrayList<String>()
try
{
for(item in rows)
{
String temp=item.toString()
Process proc = Runtime.getRuntime().exec(temp);
InputStream stdin = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
String line = null;
result = new StringBuffer()
line=null
while ((line = br.readLine()) != null)
{
result.append(line+" #10#13 ") //Here i am trying to add the newline again, but it does not reflect in the generated xml. it just come as plain text
}
String tRes=result
tRes=tRes.trim()
outputRows.add(tRes)
int exitVal = proc.waitFor();
}
}
catch (IOException io)
{
io.printStackTrace();
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
return outputRows
}
update
How can i preserve my newlines or carriage returns, such that when the xml is opened with a xsl and when encountered <pre></pre>
it should preserve its formatting?
Upvotes: 0
Views: 12157
Reputation: 171084
If you make your function more Groovy:
public List<String> fetchCommandOutput( Map<String,String> params, Map env, List<String> rows )
{
rows.collect { item ->
def (outtxt, errtxt) = [new StringBuffer(), new StringBuffer()].with { inbuf, errbuf ->
def proc = item.execute()
proc.waitForProcessOutput( inbuf, errbuf )
[ inbuf, errbuf ]*.toString()*.trim()
}
if( errtxt ) {
println "Got error $errtxt running $item"
}
outtxt
}
}
I believe it keeps the output in exactly the same format as the process outputs... And you have less code to maintain...
Upvotes: 1
Reputation: 137312
If I get you right, You should use \r
and \n
:
result.append(line+"\r\n");
or, you can get the line separator from the system:
result.append(line+System.getProperty("line.separator"));
Upvotes: 6