@Override
public void visitC2Elements(C2ElementVisitor c2ElementVisitor, C2Element ... expected) {
assert(validate(expected));
IterableXMLEventReader reader = getXMLEventReader();
try {
Deque<C2Element> stack = new ArrayDeque<C2Element>();
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (event.isEndElement()) {
stack.pop();
} else if (event.isStartElement()) {
String name = event.asStartElement().getName().getLocalPart();
int i =expected.length;
C2Element found = C2Element.None;
while(--i>=0) {
if (name.equals(expected[i].localElementName())) {
found = expected[i];
break;
}
}
stack.push(found);
} else if (event.isCharacters()) {
if (C2Element.None != stack.peek()) {
c2ElementVisitor.visit(stack.peek(), event.asCharacters().getData().trim());
}
}
}
} catch (XMLStreamException e) {
logger.warn("visitC2Elements: \n"+request.buildURL(),e);
} finally {
try {
reader.close();
} catch (XMLStreamException e) {
logger.warn("Unable to close xml stream", e);
}
}
}