Coverage Report - org.jbehave.core.io.odf.OdfUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
OdfUtils
58%
18/31
30%
3/10
2.111
OdfUtils$OdfDocumentLoadingFailed
100%
2/2
N/A
2.111
OdfUtils$OdfDocumentParsingFailed
100%
2/2
N/A
2.111
 
 1  
 package org.jbehave.core.io.odf;
 2  
 
 3  
 import static org.apache.commons.lang.StringUtils.join;
 4  
 import static org.odftoolkit.odfdom.incubator.doc.text.OdfTextExtractor.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.doc.OdfDocument;
 12  
 import org.odftoolkit.odfdom.doc.OdfTextDocument;
 13  
 import org.odftoolkit.odfdom.doc.table.OdfTable;
 14  
 import org.odftoolkit.odfdom.doc.table.OdfTableCell;
 15  
 import org.odftoolkit.odfdom.doc.table.OdfTableRow;
 16  
 import org.odftoolkit.odfdom.dom.element.table.TableTableElement;
 17  
 import org.odftoolkit.odfdom.dom.element.text.TextParagraphElementBase;
 18  
 import org.w3c.dom.Node;
 19  
 import org.w3c.dom.NodeList;
 20  
 
 21  1
 public class OdfUtils {
 22  
 
 23  
     public static OdfTextDocument loadOdt(InputStream resourceAsStream) {
 24  
         try {
 25  3
             return (OdfTextDocument) OdfTextDocument.loadDocument(resourceAsStream);
 26  1
         } catch (Exception cause) {
 27  1
             throw new OdfDocumentLoadingFailed(resourceAsStream, cause);
 28  
         }
 29  
     }
 30  
 
 31  
     public static String parseOdt(OdfTextDocument document) {
 32  3
         List<String> lines = new ArrayList<String>();
 33  
 
 34  
         try {
 35  3
             NodeList list = document.getContentRoot().getChildNodes();
 36  58
             for (int i = 0; i < list.getLength(); i++) {
 37  56
                 Node item = list.item(i);
 38  56
                 if (isTextNode(item)) {
 39  56
                     lines.add(parseTextNode(item));
 40  0
                 } else if (isTableNode(item)) {
 41  0
                     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  0
         ArrayList<String> lines = new ArrayList<String>();
 53  0
         OdfTable table = OdfTable.getInstance((TableTableElement) item);
 54  0
         for (OdfTableRow row : table.getRowList()) {
 55  0
             lines.add(parseTableRow(row));
 56  
         }
 57  0
         return lines;
 58  
     }
 59  
 
 60  
     private static String parseTableRow(OdfTableRow row) {
 61  0
         String line = "|";
 62  0
         for (int i = 0; i < row.getCellCount(); i++) {
 63  0
             OdfTableCell cell = row.getCellByIndex(i);
 64  0
             line += cell.getDisplayText() + "|";
 65  
         }
 66  0
         return line;
 67  
     }
 68  
 
 69  
     private static boolean isTableNode(Node item) {
 70  0
         return item instanceof TableTableElement;
 71  
     }
 72  
 
 73  
     private static String parseTextNode(Node item) {
 74  56
         TextParagraphElementBase textItem = (TextParagraphElementBase) item;
 75  56
         return newOdfTextExtractor(textItem).getText();
 76  
     }
 77  
 
 78  
     private static boolean isTextNode(Node item) {
 79  56
         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(OdfDocument document, Throwable cause) {
 95  1
             super("Failed to parse ODF document " + document, cause);
 96  1
         }
 97  
 
 98  
     }
 99  
 
 100  
 }