| 1 | |
package org.jbehave.core.io.odf; |
| 2 | |
|
| 3 | |
import static org.apache.commons.lang.StringUtils.join; |
| 4 | |
import static org.odftoolkit.simple.common.TextExtractor.newOdfTextExtractor; |
| 5 | |
|
| 6 | |
import java.io.InputStream; |
| 7 | |
import java.util.ArrayList; |
| 8 | |
import java.util.Collection; |
| 9 | |
import java.util.List; |
| 10 | |
|
| 11 | |
import org.odftoolkit.odfdom.dom.element.table.TableTableElement; |
| 12 | |
import org.odftoolkit.odfdom.dom.element.text.TextParagraphElementBase; |
| 13 | |
import org.odftoolkit.simple.Document; |
| 14 | |
import org.odftoolkit.simple.TextDocument; |
| 15 | |
import org.odftoolkit.simple.table.Cell; |
| 16 | |
import org.odftoolkit.simple.table.Row; |
| 17 | |
import org.odftoolkit.simple.table.Table; |
| 18 | |
import org.w3c.dom.Node; |
| 19 | |
import org.w3c.dom.NodeList; |
| 20 | |
|
| 21 | 1 | public class OdfUtils { |
| 22 | |
|
| 23 | |
public static TextDocument loadOdt(InputStream resourceAsStream) { |
| 24 | |
try { |
| 25 | 3 | return TextDocument.loadDocument(resourceAsStream); |
| 26 | 1 | } catch (Exception cause) { |
| 27 | 1 | throw new OdfDocumentLoadingFailed(resourceAsStream, cause); |
| 28 | |
} |
| 29 | |
} |
| 30 | |
|
| 31 | |
public static String parseOdt(TextDocument document) { |
| 32 | 3 | List<String> lines = new ArrayList<String>(); |
| 33 | |
|
| 34 | |
try { |
| 35 | 3 | NodeList list = document.getContentRoot().getChildNodes(); |
| 36 | 56 | for (int i = 0; i < list.getLength(); i++) { |
| 37 | 54 | Node item = list.item(i); |
| 38 | 54 | if (isTextNode(item)) { |
| 39 | 52 | lines.add(parseTextNode(item)); |
| 40 | 2 | } else if (isTableNode(item)) { |
| 41 | 2 | lines.addAll(parseTable(item)); |
| 42 | |
} |
| 43 | |
} |
| 44 | 1 | } catch (Exception e) { |
| 45 | 1 | throw new OdfDocumentParsingFailed(document, e); |
| 46 | 2 | } |
| 47 | |
|
| 48 | 2 | return join(lines, System.getProperty("line.separator")); |
| 49 | |
} |
| 50 | |
|
| 51 | |
private static Collection<String> parseTable(Node item) { |
| 52 | 2 | ArrayList<String> lines = new ArrayList<String>(); |
| 53 | 2 | Table table = Table.getInstance((TableTableElement) item); |
| 54 | 2 | for (Row row : table.getRowList()) { |
| 55 | 6 | lines.add(parseTableRow(row)); |
| 56 | 6 | } |
| 57 | 2 | return lines; |
| 58 | |
} |
| 59 | |
|
| 60 | |
private static String parseTableRow(Row row) { |
| 61 | 6 | String line = "|"; |
| 62 | 18 | for (int i = 0; i < row.getCellCount(); i++) { |
| 63 | 12 | Cell cell = row.getCellByIndex(i); |
| 64 | 12 | line += cell.getDisplayText() + "|"; |
| 65 | |
} |
| 66 | 6 | return line; |
| 67 | |
} |
| 68 | |
|
| 69 | |
private static boolean isTableNode(Node item) { |
| 70 | 2 | return item instanceof TableTableElement; |
| 71 | |
} |
| 72 | |
|
| 73 | |
private static String parseTextNode(Node item) { |
| 74 | 52 | TextParagraphElementBase textItem = (TextParagraphElementBase) item; |
| 75 | 52 | return newOdfTextExtractor(textItem).getText(); |
| 76 | |
} |
| 77 | |
|
| 78 | |
private static boolean isTextNode(Node item) { |
| 79 | 54 | return item instanceof TextParagraphElementBase; |
| 80 | |
} |
| 81 | |
|
| 82 | |
@SuppressWarnings("serial") |
| 83 | |
public static class OdfDocumentLoadingFailed extends RuntimeException { |
| 84 | |
|
| 85 | |
public OdfDocumentLoadingFailed(InputStream stream, Throwable cause) { |
| 86 | 1 | super("Failed to load ODF document from stream " + stream, cause); |
| 87 | 1 | } |
| 88 | |
|
| 89 | |
} |
| 90 | |
|
| 91 | 1 | @SuppressWarnings("serial") |
| 92 | |
public static class OdfDocumentParsingFailed extends RuntimeException { |
| 93 | |
|
| 94 | |
public OdfDocumentParsingFailed(Document document, Throwable cause) { |
| 95 | 1 | super("Failed to parse ODF document " + document, cause); |
| 96 | 1 | } |
| 97 | |
|
| 98 | |
} |
| 99 | |
|
| 100 | |
} |