int numCData = 0;
for (int i = 0, count = node.getChildCount(); i < count; i++)
{
Node child = (Node) node.getChildAt(i);
if (child instanceof CDATANode)
{
numCData++;
CDATANode cdata = (CDATANode) child;
if (cdata.image.length() > 0)
{
if (cdata.inCDATA)
{
//in CDATA Section, leave exactly as is
if (e4x)
{
((StringWriter) serializer).write("<![CDATA[" + cdata.image + "]]>");
}
else
{
try
{
((XMLStringSerializer) serializer).writeString(cdata.image);
}
catch (IOException e)
{
logError(cdata, e.getLocalizedMessage());
}
}
}
else
{
// We're not in CDATA section so extract bindings and cleanup binding escapes
BindingExpression be = textParser.parseBindingExpression(cdata.image, cdata.beginLine);
if (be != null)
{
if (be.isTwoWayPrimary() && !allowTwoWayBind)
{
log(cdata, new AbstractBuilder.TwoWayBindingNotAllowed());
}
else
{
be.setDestinationLValue(getElementsByLocalName + ".text()[" + (numCData - 1) + "]");
be.setDestinationProperty(destinationProperty + "[" + node.getIndex() + "]" + ".text()[" + i + "]" );
be.setDestination(xml);
be.setDestinationXMLNode(true);
xml.setHasBindings(true);
if (e4x)
{
be.setDestinationE4X(true);
PrefixMapping.pushNamespaces(be, namespaces);
}
// Inject placeholder cdata child so we can target it
// with binding.
((StringWriter) serializer).write("<![CDATA[" + "]]>");
}
}
else if (e4x)
{
((StringWriter) serializer).write(TextParser.replaceBindingEscapesForE4X(cdata.image));
}
else
{
String text = TextParser.cleanupBindingEscapes(cdata.image);
text = TextParser.cleanupAtFunctionEscapes(text);
try
{
((XMLStringSerializer) serializer).writeString(text);
}
catch (IOException e)
{
logError(cdata, e.getLocalizedMessage());
}
}
}
}
}
else if (e4x)
{
PrefixMapping.pushNodeNamespace(child, namespaces);
if (getElementsByLocalName != null)
{
StringBuilder e4xbuffer = new StringBuilder(getElementsByLocalName);
String destProp = child.getLocalPart();
if (child.getNamespace().length() > 0)
{
PrefixMapping pm = namespaces.peek();
destProp = "ns" + pm.getNs() + "::" + destProp;
}
e4xbuffer.append(".").append(destProp).append("[").append(child.getIndex()).append("]");
destinationProperty.push(destProp);
processNode(e4x, child, serializer, e4xbuffer.toString(), destinationProperty, namespaces);
destinationProperty.pop();
}
else
{
processNode(e4x, child, serializer, xml.getId(), destinationProperty, namespaces);
}
PrefixMapping.popNodeNamespace(namespaces);
}
else
{
String classNamespaceUtil = NameFormatter.toDot(standardDefs.CLASS_NAMESPACEUTIL);
document.addImport(classNamespaceUtil, node.beginLine);
StringBuilder buffer = new StringBuilder(classNamespaceUtil + ".getElementsByLocalName(");
buffer.append((getElementsByLocalName == null) ? xml.getId() : getElementsByLocalName);
buffer.append(", \"").append(child.getLocalPart()).append("\")[").append(child.getIndex()).append("]");
destinationProperty.push(child.getLocalPart());
processNode(e4x, child, serializer, buffer.toString(), destinationProperty, null);
destinationProperty.pop();
}
}
}