in jelly-tags/xml/src/main/java/org/apache/commons/jelly/tags/xml/SetTag.java [80:174]
public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
if (var == null) {
throw new MissingAttributeException( "var" );
}
if (select == null) {
throw new MissingAttributeException( "select" );
}
Object xpathContext = getXPathContext();
Object value = null;
try {
if ( single != null && single.booleanValue() == true ) {
value = select.selectSingleNode(xpathContext);
} else {
value = select.evaluate(xpathContext);
}
}
catch (JaxenException e) {
throw new JellyTagException(e);
}
if (value instanceof List) {
List list = (List) value;
// sort the list if xpCmp is set.
if (xpCmp != null && (xpCmp.getXpath() != null)) {
Collections.sort(list, xpCmp);
}
if (list.isEmpty()) {
value = null;
}
}
// handle single
if (single!=null) {
if (single.booleanValue() == true) {
if (value instanceof List) {
List l = (List) value;
if (l.isEmpty())
value=null;
else
value=l.get(0);
}
} else { // single == false
if (! (value instanceof List) ) {
List l = null;
if (value==null) {
l = new ArrayList(0);
} else {
l = new ArrayList(1);
l.add(value);
}
value = l;
}
}
}
// now convert the result(s) to string if need
if (asString != null && asString.booleanValue()) {
if (value instanceof Node) {
value = ((Node) value).getStringValue();
} else if (value instanceof List) {
for(ListIterator it = ((List) value).listIterator(); it.hasNext(); ) {
Object v = it.next();
if (v instanceof Node) {
v = ((Node) v).getStringValue();
it.set(v);
}
}
}
}
// finally convert the result to a concatenated string if delimiter is defined
if (delimiter != null && value instanceof List) {
StringBuilder buff = new StringBuilder();
for(Iterator it = ((List) value).iterator(); it.hasNext(); ) {
Object v = it.next();
if (v instanceof Node) {
buff.append( ((Node) v).getStringValue());
} else {
buff.append(v.toString());
}
if (it.hasNext()) {
buff.append(delimiter);
}
}
buff.setLength(buff.length());
value = buff.toString();
}
//log.info( "Evaluated xpath: " + select + " as: " + value + " of type: " + value.getClass().getName() );
context.setVariable(var, value);
}