use of biblemulticonverter.data.FormattedText.Visitor in project BibleMultiConverter by schierlm.
the class Chapter method createVirtualVerses.
public List<VirtualVerse> createVirtualVerses() {
// split up verses to separate headlines
final List<VirtualVerse> tempVerses = new ArrayList<VirtualVerse>();
BitSet numericVerseNumbers = new BitSet(verses.size());
for (final Verse verse : verses) {
int num;
try {
num = Integer.parseInt(verse.getNumber());
numericVerseNumbers.set(num);
} catch (NumberFormatException ex) {
// ignore nonnumeric verse numbers
num = Integer.MAX_VALUE;
}
final int vnum = num;
verse.accept(new VisitorAdapter<RuntimeException>(null) {
VirtualVerse vv = new VirtualVerse(vnum);
boolean hasContent = false;
{
tempVerses.add(vv);
vv.getVerses().add(new Verse(verse.getNumber()));
}
@Override
public Visitor<RuntimeException> visitHeadline(int depth) {
Headline h = new Headline(depth);
if (hasContent) {
vv = new VirtualVerse(vnum);
tempVerses.add(vv);
vv.getVerses().add(new Verse(verse.getNumber()));
hasContent = false;
}
vv.getHeadlines().add(h);
return h.getAppendVisitor();
}
@Override
public int visitElementTypes(String elementTypes) throws RuntimeException {
return 0;
}
@Override
public void visitStart() {
hasContent = true;
}
@Override
public boolean visitEnd() throws RuntimeException {
hasContent = true;
return false;
}
@Override
protected void beforeVisit() {
hasContent = true;
}
@Override
protected Visitor<RuntimeException> getVisitor() {
return vv.getVerses().get(0).getAppendVisitor();
}
});
}
// group verses sensibly
List<VirtualVerse> result = new ArrayList<VirtualVerse>();
VirtualVerse current = null;
int nextverse = 1;
for (VirtualVerse vv : tempVerses) {
for (Headline h : vv.getHeadlines()) h.finished();
for (Verse v : vv.getVerses()) v.finished();
boolean makeNew;
if (current == null || vv.getHeadlines().size() > 0) {
makeNew = true;
if (vv.getNumber() != Integer.MAX_VALUE && vv.getNumber() > nextverse) {
nextverse = vv.getNumber();
}
} else if (vv.getNumber() == Integer.MAX_VALUE) {
makeNew = false;
} else {
// numeric verse without headlines; may be both as new verse and
// as appended one;
// decide based on verse number
int vnum = vv.getNumber();
if (vnum < nextverse) {
makeNew = false;
} else if (vnum > nextverse + 1 && numericVerseNumbers.nextSetBit(nextverse) < vnum) {
makeNew = false;
numericVerseNumbers.clear(vnum);
} else {
makeNew = true;
nextverse = vnum;
}
}
if (makeNew) {
current = new VirtualVerse(nextverse);
current.getHeadlines().addAll(vv.getHeadlines());
for (Verse vvv : vv.getVerses()) {
if (vvv.getElementTypes(1).length() > 0)
current.getVerses().add(vvv);
}
result.add(current);
nextverse++;
} else {
for (Verse vvv : vv.getVerses()) {
if (vvv.getElementTypes(1).length() > 0)
current.getVerses().add(vvv);
}
}
}
return result;
}
Aggregations