To implement your own ElementProcessor, you need to implement the
interface
com.ibm.btt.element.ElementProcessor, besides this
interface, you need also implement
com.ibm.btt.config.Initializer,
which is used to pass configuration into your own ElementProcessor.
Following
is the sample code for ElementProcessor:
public class DefaultValueProcessor implements ElementProcessor, Initializer {
private Map<String, Map<String, String>> values =
new HashMap<String, Map<String, String>>();
public Object afterInitializeElement(Tag tag, Object element)
throws ElementException {
return element;
}
public Tag beforeCreateElement(Tag tag) throws ElementException {
Map<String, String> defaultAttrs = values.get(tag.getname());
if (defaultAttrs != null) {
Set set = defaultAttrs.keySet();
Iterator<String> iter = set.iterator();
while(iter.hasNext()) {
String attrName = iter.next();
if (tag.getAttributeValue(attrName) == null){
TagAttribute attr = new AttributeImp();
attr.setName(attrName);
attr.setValue(defaultAttrs.get(attrName));
tag.addAttribute(attr);
}
}
}
return tag;
}
public Object beforeInitializeElement(Tag tag, Object element)
throws ElementException {
return element;
}
public void cleanup(KeyedCollection config) throws BTTCleanUpException {
}
public void initialize(KeyedCollection config) throws BTTInitException {
try {
IndexedCollection icoll =
(IndexedCollection) config.getElementAt("defaultValues");
for (int i = 0; i < icoll.size(); i ++) {
KeyedCollection kc = (KeyedCollection) icoll.getElementAt(i);
String tagName = (String) kc.getValueAt("tagName");
String attributeName = (String) kc.getValueAt("attributeName");
String defaultValue = (String) kc.getValueAt("defaultValue");
if (values.get(tagName) == null) {
values.put(tagName, new HashMap<String, String>());
}
values.get(tagName).put(attributeName, defaultValue);
}
} catch (DSEObjectNotFoundException e) {
throw new BTTInitException(
"Configuration error to DefaultValueProcessor");
}
}
}