/**
* Write out a loop which will read in a sequence of elements.
*
*/
private void writeMainLoop() {
JBlock b = method.body();
// Add XSI checks
if (returnType != null || !valueType && depth > 1) {
writeXsiChecks(b);
}
// Add the user constructed codeblock and continue from there
b.add(codeBlock);
b = codeBlock;
writeAttributeReader(b);
// Add the user constructed codeblock and continue from there
b.add(preElementBlock);
b = preElementBlock;
if (!elements.isEmpty() || !xsiTypes.isEmpty() || anyElement != null || mixedElement != null || allowUnknown ) {
b.add(new JBlankLine());
b.add(new JLineComment("Read elements"));
if (forEachChildElement != null && !allowUnknown) {
b.add(forEachChildElement);
writeElementReader(elements, forEachChildElement.body(), forEachChildElement.var(), false);
} else {
// declare variables used during element reading
JVar targetDepthVar = b.decl(model._ref(int.class), "targetDepth", xsrVar.invoke("getDepth").plus(JExpr.lit(1)));
JVar event = b.decl(model._ref(int.class), "event", xsrVar.invoke("nextTagIgnoreAll"));
JVar depthVar = b.decl(model._ref(int.class), "depth", 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));
Map<QName, ExpectedElement> globalAndLocalEls = new HashMap<QName, ExpectedElement>();
globalAndLocalEls.putAll(elements);
globalAndLocalEls.putAll(buildContext.getGlobalElements());
writeElementReader(globalAndLocalEls, ifDepth._then(), xsrVar, false);
if (allowUnknown) {
writeElementReader(buildContext.getGlobalElements(), ifDepth._else(), xsrVar, 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();
}
}