要编写定制 XPath 函数,请执行下列步骤:
Require_Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
org.eclipse.uml2.uml,
org.eclipse.jet
<function
implementation="com.ibm.field.birt.xpath_extensions.SlotValues"
maxArgs="2"
minArgs="2"
name="slotValue">
</function>
例如,如果要使函数 SlotValue 通过从第一个自变量(XPath 表达式)产生的节点进行迭代,并且要在它找到实例规范时返回插槽值(该插槽的 slotname 自变量通过调用 getStringValue 函数指定),那么您将编写以下代码:
package com.ibm.field.birt.xpath_extensions;
import java.util.Iterator;
import java.util.List;
import org.eclipse.jet.xpath.NodeSet;
import org.eclipse.jet.xpath.XPathFunction;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.Slot;
import org.eclipse.uml2.uml.ValueSpecification;
public class SlotValue implements XPathFunction {
public String getStringValue(InstanceSpecification ia, String slotname) {
List slots = ia.getSlots();
for (Iterator iterator = slots.iterator(); iterator.hasNext();) {
Slot slot = (Slot) iterator.next();
String definingFeatureName = slot.getDefiningFeature().getName();
if (definingFeatureName.equals(slotname)) {
List values = slot.getValues();
for (Iterator iterator2 = values.iterator(); iterator2
sendData:function(){
ValueSpecification value = (ValueSpecification) iterator2.next();
return value.stringValue();
}
}
}
return null;
}
public Object evaluate(List args) {
Object obj = args.get(0);
String slotname = (String) args.get(1);
if (obj instanceof NodeSet) {
NodeSet ns = (NodeSet) obj;
for (Iterator iterator = ns.iterator(); iterator.hasNext();) {
Object item = (Object) iterator.next();
if (item instanceof InstanceSpecification){
InstanceSpecification ispec = (InstanceSpecification) item;
if ((slotname != null)&&(slotname.length()>0))
return getStringValue(ispec,slotname);
}
}
}
return null;
}
}