Document extends OdfSchemaDocument implements TableContainer { public static Document loadDocument(String documentPath, String password) throws Exception { File file = new File(documentPath); return loadDocument(file, password); } protected  Document(OdfPackage pkg, String internalPath, OdfMediaType mediaType); static Document loadDocument(String documentPath, String password); static Document loadDocument(String documentPath); static Document loadDocument(InputStream inStream); static Document loadDocument(File file); static Document loadDocument(File file, String password); static Document loadDocument(OdfPackage odfPackage); static Document loadDocument(OdfPackage odfPackage, String internalPath); void setPassword(String password); Document getEmbeddedDocument(String documentPath); List<Document> getEmbeddedDocuments(); List<Document> getEmbeddedDocuments(OdfMediaType mediaType); void insertDocument(OdfPackageDocument sourceDocument, String documentPath); Meta getOfficeMetadata(); void save(OutputStream out); void save(File file); void save(File file, String password); @Override void close(); OdfElement getContentRoot(); @Override String toString(); String newImage(URI imageUri); void setLocale(Locale locale); static ScriptType getScriptType(Locale locale); void setLocale(Locale locale, ScriptType scriptType); Locale getLocale(ScriptType scriptType); Iterator<Section> getSectionIterator(); Section getSectionByName(String name); boolean removeElementLinkedResource(OdfElement odfElement); Table addTable(); Table addTable(int numRows, int numCols); Table getTableByName(String name); java.util.List<Table> getTableList(); TableBuilder getTableBuilder(); TableTemplate LoadTableTemplateFromForeignTable(InputStream templateFileInputStream,
        String tableName);  }
@Test public void loadDocument() { try { System.setProperty("org.odftoolkit.odfdom.tmpfile.disable", "true"); LOG.info("Loading an supported ODF Spreadsheet document as an ODF Document!"); try { Document ods = Document.loadDocument(ResourceUtilities.getAbsolutePath(GENERATED_INVALID_SPREADSHEET)); Assert.assertNotNull(ods); } catch (Exception e) { LOG.log(Level.SEVERE, e.getMessage(), e); Assert.fail(); } LOG.info("Loading an empty document as an ODF Document!"); try { Document ods = Document.loadDocument(ResourceUtilities.getAbsolutePath(ZERO_BYTE_SPREADSHEET)); Assert.fail(); } catch (Exception e) { if (!e.getMessage().contains("shall be a ZIP file")) { LOG.log(Level.SEVERE, e.getMessage(), e); Assert.fail(); } } LOG.info("Loading an unsupported ODF Formula document as an ODF Document!"); try { Document.loadDocument(ResourceUtilities.getAbsolutePath(ODF_FORMULAR_TEST_FILE)); Assert.fail(); } catch (IllegalArgumentException e) { if (!e.getMessage().contains("is not yet supported!")) { LOG.log(Level.SEVERE, e.getMessage(), e); Assert.fail(); } } LOG.info("Loading an unsupported image file as an ODF Document!"); try { Document.loadDocument(ResourceUtilities.getAbsolutePath(IMAGE_TEST_FILE)); Assert.fail(); } catch (IllegalArgumentException e) { if (!e.getMessage().contains("shall be a ZIP file")) { LOG.log(Level.SEVERE, e.getMessage(), e); Assert.fail(); } } catch (OdfValidationException e) { if (!e.getMessage().contains("shall be a ZIP file")) { LOG.log(Level.SEVERE, e.getMessage(), e); Assert.fail(); } } } catch (Exception e) { LOG.log(Level.SEVERE, e.getMessage(), e); Assert.fail(e.getMessage()); } }
@Test public void testParser() { try { Document.loadDocument(ResourceUtilities.getAbsolutePath(TEST_FILE)); } catch (Exception e) { LOG.log(Level.SEVERE, e.getMessage(), e); Assert.fail(e.getMessage()); } }
@Test public void testStylesDom() { try { Document odfdoc = Document.loadDocument(ResourceUtilities.getAbsolutePath(TEST_FILE)); OdfStylesDom stylesDom = odfdoc.getStylesDom(); Assert.assertNotNull(stylesDom); OdfOfficeStyles styles = odfdoc.getDocumentStyles(); Assert.assertNotNull(styles); Assert.assertNotNull(styles.getDefaultStyle(OdfStyleFamily.Graphic)); Assert.assertNotNull(styles.getDefaultStyle(OdfStyleFamily.Paragraph)); Assert.assertNotNull(styles.getDefaultStyle(OdfStyleFamily.Table)); Assert.assertNotNull(styles.getDefaultStyle(OdfStyleFamily.TableRow)); OdfStyle style = styles.getStyle("Standard", OdfStyleFamily.Paragraph); Assert.assertNotNull(style); Assert.assertEquals(style.getStyleClassAttribute(), "text"); style = styles.getStyle("List", OdfStyleFamily.Paragraph); Assert.assertNotNull(style); Assert.assertEquals(style.getProperty(StyleTextPropertiesElement.FontNameComplex), "Tahoma1"); Assert.assertTrue(style.hasProperty(StyleTextPropertiesElement.FontNameComplex)); Assert.assertFalse(style.hasProperty(StyleTextPropertiesElement.FontNameAsian)); Assert.assertNull(styles.getStyle("foobar", OdfStyleFamily.Chart)); OdfOfficeAutomaticStyles autostyles = stylesDom.getAutomaticStyles(); Assert.assertNotNull(autostyles); OdfStylePageLayout pageLayout = autostyles.getPageLayout("pm1"); Assert.assertNotNull(pageLayout); Assert.assertEquals(pageLayout.getProperty(StylePageLayoutPropertiesElement.PageWidth), "8.5in"); Assert.assertEquals(pageLayout.getProperty(StylePageLayoutPropertiesElement.PageHeight), "11in"); Assert.assertNull(autostyles.getStyle("foobar", OdfStyleFamily.Chart)); } catch (Exception e) { LOG.log(Level.SEVERE, e.getMessage(), e); Assert.fail(e.getMessage()); } }
@Test public void testContentNode() { try { Document odfdoc = Document.loadDocument(ResourceUtilities.getAbsolutePath(TEST_FILE)); OdfContentDom contentDom = odfdoc.getContentDom(); OdfOfficeAutomaticStyles autoStyles = contentDom.getAutomaticStyles(); Assert.assertNotNull(autoStyles); OdfStyle style = autoStyles.getStyle("P1", OdfStyleFamily.Paragraph); Assert.assertNotNull(style); Assert.assertEquals(style.getStyleNameAttribute(), "P1"); Assert.assertEquals(style.getStyleParentStyleNameAttribute(), "Text_20_body"); Assert.assertEquals(style.getStyleListStyleNameAttribute(), "L1"); style = autoStyles.getStyle("T1", OdfStyleFamily.Text); Assert.assertNotNull(style); Assert.assertEquals(style.getStyleNameAttribute(), "T1"); for (OdfStyle testStyle : autoStyles.getStylesForFamily(OdfStyleFamily.Paragraph)) { testStyle(testStyle); } for (OdfStyle testStyle : autoStyles.getStylesForFamily(OdfStyleFamily.Text)) { testStyle(testStyle); } } catch (Exception e) { LOG.log(Level.SEVERE, e.getMessage(), e); Assert.fail(e.getMessage()); } }
@Test public void testLoadDocumentWithIllegalArgument() throws Exception { String filepath = ResourceUtilities.getAbsolutePath("presentation.odp"); OdfPackage odfpackage = OdfPackage.loadPackage(filepath); try { Document.loadDocument(odfpackage, "odt"); } catch (Exception e) { Assert.assertTrue(e instanceof IllegalArgumentException); Assert.assertEquals("Given internalPath 'odt' is an illegal or inappropriate argument.", e.getMessage()); } try { Document.loadDocument(odfpackage, "meta.xml"); } catch (Exception e) { Assert.assertTrue(e instanceof IllegalArgumentException); Assert.assertEquals("Given mediaType 'text/xml' is either not yet supported or not an ODF mediatype!", e.getMessage()); } }
@Test public void testGetContentRootDoc() throws Exception { try { Document odt = Document.loadDocument(ResourceUtilities.getAbsolutePath(TEST_FILE_WITHOUT_OPT)); OdfContentDom odfcon = odt.getContentDom(); Assert.assertNotNull(odfcon); } catch (Exception e) { LOG.log(Level.SEVERE, e.getMessage(), e); Assert.fail(e.getMessage()); } }