TextElementArray current;
try {
current = (TextElementArray) stack.pop();
} catch (EmptyStackException ese) {
if (bf == null) {
current = new Paragraph("", new LwgFont());
}
else {
current = new Paragraph("", new LwgFont(this.bf));
}
}
current.add(currentChunk);
stack.push(current);
currentChunk = null;
}
// chunks
if (ElementTags.CHUNK.equals(name)) {
currentChunk = ElementFactory.getChunk(attributes);
if (bf != null) {
currentChunk.setFont(new LwgFont(this.bf));
}
return;
}
// symbols
if (ElementTags.ENTITY.equals(name)) {
LwgFont f = new LwgFont();
if (currentChunk != null) {
handleEndingTags(ElementTags.CHUNK);
f = currentChunk.getFont();
}
currentChunk = EntitiesToSymbol.get(attributes.getProperty(ElementTags.ID),
f);
return;
}
// phrases
if (ElementTags.PHRASE.equals(name)) {
stack.push(ElementFactory.getPhrase(attributes));
return;
}
// anchors
if (ElementTags.ANCHOR.equals(name)) {
stack.push(ElementFactory.getAnchor(attributes));
return;
}
// paragraphs and titles
if (ElementTags.PARAGRAPH.equals(name) || ElementTags.TITLE.equals(name)) {
stack.push(ElementFactory.getParagraph(attributes));
return;
}
// lists
if (ElementTags.LIST.equals(name)) {
stack.push(ElementFactory.getList(attributes));
return;
}
// listitems
if (ElementTags.LISTITEM.equals(name)) {
stack.push(ElementFactory.getListItem(attributes));
return;
}
// cells
if (ElementTags.CELL.equals(name)) {
stack.push(ElementFactory.getCell(attributes));
return;
}
// tables
if (ElementTags.TABLE.equals(name)) {
Table table = ElementFactory.getTable(attributes);
float widths[] = table.getProportionalWidths();
for (int i = 0; i < widths.length; i++) {
if (widths[i] == 0) {
widths[i] = 100.0f / widths.length;
}
}
try {
table.setWidths(widths);
} catch (BadElementException bee) {
// this shouldn't happen
throw new ExceptionConverter(bee);
}
stack.push(table);
return;
}
// sections
if (ElementTags.SECTION.equals(name)) {
LwgElement previous = (LwgElement) stack.pop();
Section section;
try {
section = ElementFactory.getSection((Section) previous, attributes);
} catch (ClassCastException cce) {
throw new ExceptionConverter(cce);
}
stack.push(previous);
stack.push(section);
return;
}
// chapters
if (ElementTags.CHAPTER.equals(name)) {
stack.push(ElementFactory.getChapter(attributes));
return;
}
// images
if (ElementTags.IMAGE.equals(name)) {
try {
LwgImage img = ElementFactory.getImage(attributes);
try {
addImage(img);
return;
} catch (EmptyStackException ese) {
// if there is no element on the stack, the LwgImage is added
// to the document
try {
document.add(img);
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
return;
}
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
// annotations
if (ElementTags.ANNOTATION.equals(name)) {
Annotation annotation = ElementFactory.getAnnotation(attributes);
TextElementArray current;
try {
try {
current = (TextElementArray) stack.pop();
try {
current.add(annotation);
} catch (Exception e) {
document.add(annotation);
}
stack.push(current);
} catch (EmptyStackException ese) {
document.add(annotation);
}
return;
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
// newlines
if (isNewline(name)) {
TextElementArray current;
try {
current = (TextElementArray) stack.pop();
current.add(Chunk.NEWLINE);
stack.push(current);
} catch (EmptyStackException ese) {
if (currentChunk == null) {
try {
document.add(Chunk.NEWLINE);
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
} else {
currentChunk.append("\n");
}
}
return;
}
// newpage
if (isNewpage(name)) {
TextElementArray current;
try {
current = (TextElementArray) stack.pop();
Chunk newPage = new Chunk("");
newPage.setNewPage();
if (bf != null) {
newPage.setFont(new LwgFont(this.bf));
}
current.add(newPage);
stack.push(current);
} catch (EmptyStackException ese) {
document.newPage();