/**
* Organizes {@link #tags} to the tree structure.
*/
public Tag.Root buildTree() throws IOException {
Reader in = new CharSequenceReader(sourceFile);
long curPos = 0; // chars from input that are written so far
StringBuilder sb = new StringBuilder();
char[] buf = new char[256];
Collections.sort(tags);
TagStack opened = new TagStack();
// start with the root tag
Root root = new Root(sourceFile.length());
opened.push(root);
TagScanner upcoming = new TagScanner(tags);
OUTER:
while(true) {
// determine the next marker position
TagStack.Adder lhs = opened.peek();
Tag rhs = upcoming.peek();
long nextPos = Math.min(
lhs==null ? Long.MAX_VALUE : lhs.endPos(),
rhs==null ? Long.MAX_VALUE : rhs.sp );
// read until nextPos
sb.setLength(0);
while(curPos<nextPos) {
int sz = in.read(buf,0, (int)Math.min(nextPos-curPos,buf.length));
if(sz<0) break OUTER; // all streams read
sb.append(buf,0,sz);
curPos += sz;
}