}
public Sequence templates(Sequence contextSequence, Item contextItem) throws XPathException {
boolean matched = false;
Sequence result = new ValueSequence();
Sequence currentSequence = contextSequence;
if (contextItem != null)
currentSequence = contextItem.toSequence();
int pos = context.getContextPosition();
// for (Item item : currentSequence) {
for (SequenceIterator iterInner = currentSequence.iterate(); iterInner.hasNext();) {
Item item = iterInner.nextItem();
// if (currentSequence == item)
// item = null;
//UNDERSTAND: work around
// if (item instanceof org.w3c.dom.Document) {
// org.w3c.dom.Document document = (org.w3c.dom.Document) item;
// item = (Item) document.getDocumentElement();
// }
context.setContextSequencePosition(pos, currentSequence);
if ((contextItem == null) && (rootTemplate != null)) {
context.setContextSequencePosition(0, currentSequence);
Sequence res = rootTemplate.eval(contextSequence, item);
result.addAll(res);
matched = true;
}
for (Template template : templates) {
if (template.matched(contextSequence, item)) { //contextSequence
matched = true;
Sequence res = template.eval(contextSequence, item);
result.addAll(res);
if (res.getItemCount() > 0)
break;
}
}
//XXX: performance !?! how to get subelements sequence?? fast...
if (!matched) {
if (item instanceof ElementAtExist || item instanceof DocumentAtExist) {
NodeAtExist node = (NodeAtExist) item;
NodeList children = node.getChildNodes();
for (int i=0; i<children.getLength(); i++) {
NodeAtExist child = (NodeAtExist)children.item(i);
// if (child instanceof Text) {
// MemTreeBuilder builder = context.getDocumentBuilder();
// builder.characters(item.getStringValue());
// result.add(item);
// } else {
Sequence res = templates((Sequence)node, (Item)child);
if (res != null) {
result.addAll(res);
matched = true;
}
// }
}
}