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