Friday, December 3, 2010

Non-simple parameters in spring SCA component

Here's a sample how to handle some non-simple parameters in a spring SCA component: I hand over and return a Base64 encoded document a a byte array - as read by the file/ftp adapter - and a Java HashMap with key/value pairs to do replacements in the doc. Doc content can be read from a ByteArrayInputStream, HashMap results in an XMLElement.

package my;

import java.io.ByteArrayInputStream;

import java.util.HashMap;
import oracle.xml.parser.v2.XMLElement;

public class DocReplacerImpl implements DocReplacer {
public DocReplacerImpl() {
super();
}

public byte[] customize(byte[] doc,HashMap pairs){
String s_doc=null;
ByteArrayInputStream bis=new ByteArrayInputStream(doc);
byte[] in=new byte[doc.length];
bis.read(in, 0, doc.length);
s_doc=new String(in).replaceAll("&", "&");
Object[] keys=pairs.keySet().toArray();
for(int i=0;i<keys.length;i++){
XMLElement xml=(XMLElement)keys[i];
String s_key=xml.getFirstChild().getNodeValue();
String s_value=((XMLElement)pairs.get(keys[i])).getFirstChild().getNodeValue();
s_doc=s_doc.replaceAll(s_key, s_value);
}
return s_doc.getBytes();
}
}

No comments:

Post a Comment