Paragraph current = (Paragraph) stack.pop();
if (currentChunk != null) {
current.add(currentChunk);
currentChunk = null;
}
Section previous = (Section) stack.pop();
previous.setTitle(current);
stack.push(previous);
return;
}
// all other endtags
if (currentChunk != null) {
TextElementArray current;
try {
current = (TextElementArray) stack.pop();
} catch (EmptyStackException ese) {
current = new Paragraph();
}
current.add(currentChunk);
stack.push(current);
currentChunk = null;
}
// chunks
if (ElementTags.CHUNK.equals(name)) {
return;
}
// phrases, anchors, lists, tables
if (ElementTags.PHRASE.equals(name) || ElementTags.ANCHOR.equals(name) || ElementTags.LIST.equals(name)
|| ElementTags.PARAGRAPH.equals(name)) {
Element current = (Element) stack.pop();
try {
TextElementArray previous = (TextElementArray) stack.pop();
previous.add(current);
stack.push(previous);
} catch (EmptyStackException ese) {
document.add(current);
}
return;
}
// listitems
if (ElementTags.LISTITEM.equals(name)) {
ListItem listItem = (ListItem) stack.pop();
List list = (List) stack.pop();
list.add(listItem);
stack.push(list);
}
// tables
if (ElementTags.TABLE.equals(name)) {
Table table = (Table) stack.pop();
try {
TextElementArray previous = (TextElementArray) stack.pop();
previous.add(table);
stack.push(previous);
} catch (EmptyStackException ese) {
document.add(table);
}
return;
}
// rows
if (ElementTags.ROW.equals(name)) {
ArrayList cells = new ArrayList();
int columns = 0;
Table table;
Cell cell;
while (true) {
Element element = (Element) stack.pop();
if (element.type() == Element.CELL) {
cell = (Cell) element;
columns += cell.getColspan();
cells.add(cell);
} else {
table = (Table) element;
break;
}
}
if (table.getColumns() < columns) {
table.addColumns(columns - table.getColumns());
}
Collections.reverse(cells);
String width;
float[] cellWidths = new float[columns];
boolean[] cellNulls = new boolean[columns];
for (int i = 0; i < columns; i++) {
cellWidths[i] = 0;
cellNulls[i] = true;
}
float total = 0;
int j = 0;
for (Iterator i = cells.iterator(); i.hasNext();) {
cell = (Cell) i.next();
width = cell.getWidthAsString();
if (cell.getWidth() == 0) {
if (cell.getColspan() == 1 && cellWidths[j] == 0) {
try {
cellWidths[j] = 100f / columns;
total += cellWidths[j];
} catch (Exception e) {
// empty on purpose
}
} else if (cell.getColspan() == 1) {
cellNulls[j] = false;
}
} else if (cell.getColspan() == 1 && width.endsWith("%")) {
try {
cellWidths[j] = Float.parseFloat(
width.substring(0, width.length() - 1)
+ "f");
total += cellWidths[j];
} catch (Exception e) {
// empty on purpose
}
}
j += cell.getColspan();
table.addCell(cell);
}
float widths[] = table.getProportionalWidths();
if (widths.length == columns) {
float left = 0.0f;
for (int i = 0; i < columns; i++) {
if (cellNulls[i] && widths[i] != 0) {
left += widths[i];
cellWidths[i] = widths[i];
}
}
if (100.0 >= total) {
for (int i = 0; i < widths.length; i++) {
if (cellWidths[i] == 0 && widths[i] != 0) {
cellWidths[i] = (widths[i] / left)
* (100.0f - total);
}
}
}
table.setWidths(cellWidths);
}
stack.push(table);
}
// cells
if (ElementTags.CELL.equals(name)) {
return;
}
// sections
if (ElementTags.SECTION.equals(name)) {
stack.pop();
return;
}
// chapters
if (ElementTags.CHAPTER.equals(name)) {
document.add((Element) stack.pop());
return;
}
// the documentroot
if (isDocumentRoot(name)) {
try {
while (true) {
Element element = (Element) stack.pop();
try {
TextElementArray previous = (TextElementArray) stack
.pop();
previous.add(element);
stack.push(previous);
} catch (EmptyStackException es) {
document.add(element);
}
}