Deleting Empty Lines
Hello,
Suppose you want to delete the empty lines from the Text File ...
You can use the JCN .......WOW it woks........
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import com.ibm.broker.javacompute.MbJavaComputeNode;
import com.ibm.broker.plugin.MbBLOB;
import com.ibm.broker.plugin.MbElement;
import com.ibm.broker.plugin.MbException;
import com.ibm.broker.plugin.MbMessage;
import com.ibm.broker.plugin.MbMessageAssembly;
import com.ibm.broker.plugin.MbOutputTerminal;
import com.ibm.broker.plugin.MbUserException;
public class IdiotFlow_JavaCompute extends MbJavaComputeNode {
public void
evaluate(MbMessageAssembly inAssembly) throws MbException {
MbOutputTerminal
out = getOutputTerminal("out");
// MbOutputTerminal alt =
getOutputTerminal("alternate");
MbMessage
inMessage = inAssembly.getMessage();
MbElement
inputRoot=inMessage.getRootElement();
byte[] MessageBodyByteArray = (byte[])(inputRoot.getFirstElementByPath("/BLOB/BLOB").getValue());
byte[]
outdata = createFile(MessageBodyByteArray);
MbMessageAssembly outAssembly = null;
try
{
MbMessage
outMessage = new MbMessage();
copyMessageHeaders(inMessage,
outMessage);
outAssembly
= new MbMessageAssembly(inAssembly, outMessage);
MbElement
outRoot = outMessage.getRootElement();
MbElement
outParser = outRoot.createElementAsLastChild(MbBLOB.PARSER_NAME);
outParser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,
"BLOB", outdata);
}
catch (MbException e) {
//
Re-throw to allow Broker handling of MbException
throw
e;
}
catch (RuntimeException e) {
//
Re-throw to allow Broker handling of RuntimeException
throw
e;
}
catch (Exception e) {
//
Consider replacing Exception with type(s) thrown by user code
//
Example handling ensures all exceptions are re-thrown to be handled in the
flow
throw
new MbUserException(this, "evaluate()", "", "",
e.toString(),
null);
}
//
The following should only be changed
//
if not propagating message to the 'out' terminal
out.propagate(outAssembly);
}
public void copyMessageHeaders(MbMessage
inMessage, MbMessage outMessage)
throws
MbException {
MbElement
outRoot = outMessage.getRootElement();
MbElement
header = inMessage.getRootElement().getFirstChild();
while
(header != null && header.getNextSibling() != null) // stop before
{
outRoot.addAsLastChild(header.copy());
header
= header.getNextSibling();
}
}
@SuppressWarnings("null")
private byte[]
createFile(byte[] MessageBodyByteArray)
{
byte[]
output = null;
InputStream
source = new ByteArrayInputStream(MessageBodyByteArray);
OutputStream
out1= new ByteArrayOutputStream();
PrintStream
out = new PrintStream(out1);
Scanner
scanner=new Scanner(source) ;
try{
while(scanner.hasNextLine()){
String line = scanner.nextLine();
line = line.trim();
if(line.length() > 0)
out.println(line);
} ByteArrayOutputStream bOut = new
ByteArrayOutputStream();
output= ((ByteArrayOutputStream)
out1).toByteArray();
}
catch ( Exception ex ) {
System.out.println(ex);
}
return
output;
}
}
|