boolean inComment = false;
final TreeXmlReader builder = (TreeXmlReader) getBuilder();
final StringBuilder waitingBuf = new StringBuilder();
int level = 1;
for (;;) {
final IXMLReader reader = getReader();
char ch = reader.read();
if (inComment) {
waitingBuf.append(ch);
if (ch != '-') {
continue;
}
ch = reader.read();
waitingBuf.append(ch);
if (ch != '-') {
continue;
}
ch = reader.read();
waitingBuf.append(ch);
if (ch != '>') {
continue;
}
inComment = false;
continue;
}
if (ch == '<') {
ch = reader.read();
if (ch == '/') {
level--;
if (level == 0) {
break;
}
}
else if (ch == '!') {
final char read1 = reader.read();
final char read2 = reader.read();
if (read1 != '-' || read2 != '-') {
throw new XMLParseException(reader.getSystemID(), reader.getLineNr(), "Invalid input: <!"
+ read1 + read2);
}
inComment = true;
waitingBuf.append("<!--");
continue;
}
else {
level++;
}
waitingBuf.append('<');
}
else if (ch == '/') {
ch = reader.read();
if (ch == '>') {
level--;
if (level == 0) {
throw new XMLParseException(reader.getSystemID(), reader.getLineNr(), "Invalid input: />");
}
}
else if (ch == '<') {
waitingBuf.append('/');
reader.unread(ch);
continue;
}
waitingBuf.append('/');
}
waitingBuf.append(ch);