/**
* Write out a loop which will read in a sequence of elements.
*
*/
private void writeMainLoop() {
JBlock b = method.body();
// Add XSI checks
if (!valueType && depth > 1) {
writeXsiChecks(b);
}
// Add the user constructed codeblock and continue from there
b.add(codeBlock);
b = codeBlock;
if (attributes.size() > 0) {
writeAttributeReader(b);
}
JVar depthVar = b.decl(model._ref(int.class), "depth", xsrVar.invoke("getDepth"));
JVar targetDepthVar;
JVar event;
if (depth == 1) {
targetDepthVar = b.decl(model._ref(int.class), "targetDepth", JExpr.lit(depth));
event = b.decl(model._ref(int.class), "event", xsrVar.invoke("getEventType"));
} else {
targetDepthVar = b.decl(model._ref(int.class), "targetDepth", depthVar.plus(JExpr.lit(1)));
event = b.decl(model._ref(int.class), "event", xsrVar.invoke("nextTagIgnoreAll"));
}
b.assign(depthVar, xsrVar.invoke("getDepth"));
// JClass sysType = (JClass) model._ref(System.class);
// if (depth != 1)
// b.add(sysType.staticRef("out").invoke("println").arg(JExpr.lit("TD ").plus(targetDepthVar)
// .plus(JExpr.lit(" Depth: ")).plus(depthVar)
// .plus(JExpr.lit(" Name: " + name).plus(JExpr.lit(" Current: "))
// .plus(xsrVar.invoke("getName")))));
JBlock loop = b._while(depthVar.gte(targetDepthVar.minus(JExpr.lit(1)))).body();
b = loop._if(event.eq(JExpr.lit(XMLStreamConstants.START_ELEMENT)))._then();
JConditional ifDepth = b._if(depthVar.eq(targetDepthVar));
writeElementReader(elements, ifDepth._then(), false);
if (allowUnknown) {
writeElementReader(buildContext.getGlobalElements(), ifDepth._else(), true);
}
JConditional ifHasNext = loop._if(xsrVar.invoke("hasNext"));
ifHasNext._then().assign(event, xsrVar.invoke("next"));
ifHasNext._then().assign(depthVar, xsrVar.invoke("getDepth"));
ifHasNext._else()._break();
}