// Fix double headlines
int lastIndex = 0;
StringBuilder buffer = new StringBuilder();
for (int i = 1; i < titles.size(); i++) {
PageElementTitle previousTitle = titles.get(i - 1);
PageElementTitle currentTitle = titles.get(i);
if ((previousTitle.getLevel() == currentTitle.getLevel()) &&
(previousTitle.getTitle().equals(currentTitle.getTitle()))) {
// Analyze if first title can be removed
String betweenTitles = contents.substring(
previousTitle.getEndIndex(), currentTitle.getBeginIndex()).trim();
boolean shouldRemove = false;
if (betweenTitles.length() == 0) {
shouldRemove = true;
} else {
int tmpIndex = currentTitle.getEndIndex();
while ((tmpIndex < contents.length()) &&
(Character.isWhitespace(contents.charAt(tmpIndex)))) {
tmpIndex++;
}
if ((tmpIndex < contents.length()) &&
(contents.startsWith(betweenTitles, tmpIndex))) {
shouldRemove = true;
}
}
if (shouldRemove) {
if (previousTitle.getBeginIndex() > lastIndex) {
buffer.append(contents.substring(lastIndex, previousTitle.getBeginIndex()));
lastIndex = previousTitle.getBeginIndex();
}
lastIndex = currentTitle.getBeginIndex();
} else {
// Analyze if second title can be removed
PageElementTitle nextTitle = (i + 1 < titles.size()) ? titles.get(i + 1) : null;
String afterTitle = contents.substring(
currentTitle.getEndIndex(),
(nextTitle != null) ? nextTitle.getBeginIndex() : contents.length()).trim();
if (afterTitle.length() == 0) {
shouldRemove = true;
} else {
int tmpIndex = previousTitle.getEndIndex();
while ((tmpIndex < contents.length()) &&
(Character.isWhitespace(contents.charAt(tmpIndex)))) {
tmpIndex++;
}
if ((tmpIndex < contents.length()) &&
(contents.startsWith(afterTitle, tmpIndex))) {
shouldRemove = true;
}
}
if (shouldRemove) {
if (currentTitle.getBeginIndex() > lastIndex) {
buffer.append(contents.substring(lastIndex, currentTitle.getBeginIndex()));
lastIndex = currentTitle.getBeginIndex();
}
lastIndex = (nextTitle != null) ? nextTitle.getBeginIndex() : contents.length();
}
}
}
}
if (lastIndex == 0) {