Package org.odftoolkit.odfdom.doc

Examples of org.odftoolkit.odfdom.doc.OdfTextDocument


   */
  @Test
  public void testResolverWithXSLT() {
    try {
      OdfXMLHelper helper = new OdfXMLHelper();
      OdfTextDocument odt = (OdfTextDocument) OdfDocument.loadDocument(ResourceUtilities.getAbsolutePath(SIMPLE_ODT));
      InputSource inputSource = new InputSource(ResourceUtilities.getURI(XSL_CONCAT).toString());
      Templates multiFileAccessTemplate = TransformerFactory.newInstance().newTemplates(new SAXSource(inputSource));
      File xslOut = ResourceUtilities.newTestOutputFile(XSL_OUTPUT);
      helper.transform(odt.getPackage(), "content.xml", multiFileAccessTemplate, new StreamResult(xslOut));
      LOG.info("Transformed ODF document " + SIMPLE_ODT + " to " + xslOut.getAbsolutePath() + "!");
      File testOutputFile = new File(xslOut.getAbsolutePath());
      if (testOutputFile.length() < 100) {
        String errorMsg = "The file " + xslOut.getAbsolutePath() + " is smaller than it should be. \nIt was not created from multiple package files!";
        LOG.severe(errorMsg);
View Full Code Here


   * Test of newImage method, of class OdfDrawImage.
   */
  @Test
  public void testInsertImage_URI() throws Exception {
    LOG.info("insertImage from URI");
    OdfTextDocument odt = OdfTextDocument.newTextDocument();
    OdfTextParagraph para = (OdfTextParagraph) odt.getContentRoot().newTextPElement();
    OdfDrawFrame frame = (OdfDrawFrame) para.newDrawFrameElement();
    OdfDrawImage image = (OdfDrawImage) frame.newDrawImageElement();
    String packagePath = image.newImage(ResourceUtilities.getURI("testA.jpg"));
    assertEquals(image.getXlinkTypeAttribute(), "simple");
    LOG.info(frame.getSvgWidthAttribute());
    LOG.info(frame.getSvgHeightAttribute());
    assert (frame.getSvgWidthAttribute().startsWith("19.") && frame.getSvgWidthAttribute().endsWith("cm"));
    assert (frame.getSvgHeightAttribute().startsWith("6.") && frame.getSvgHeightAttribute().endsWith("cm"));
    assertEquals(odt.getPackage().getFileEntry(packagePath).getMediaTypeString(), "image/jpeg");
  }
View Full Code Here

   * Test of newImage method, of class OdfDrawImage.
   */
  @Test
  public void testInsertImage_InputStream() throws Exception {
    LOG.info("insertImage from InputStream");
    OdfTextDocument odt = OdfTextDocument.newTextDocument();
    OdfTextParagraph para = (OdfTextParagraph) odt.getContentRoot().newTextPElement();
    OdfDrawFrame frame = (OdfDrawFrame) para.newDrawFrameElement();
    OdfDrawImage image = (OdfDrawImage) frame.newDrawImageElement();
    String packagePath = "Pictures/myChosenImageName.jpg";
    String mediaType = "image/jpeg";
    image.newImage(new FileInputStream(ResourceUtilities.getAbsolutePath("testA.jpg")), packagePath, mediaType);
View Full Code Here

   * DOCA/ and DOCB/
   */
  @Test
  public void testembeddedDocumentsLocatedSideBySide() {
    try {
      OdfTextDocument odtRootDoc = OdfTextDocument.newTextDocument();
      odtRootDoc.insertDocument(OdfTextDocument.newTextDocument(), "DOCA/");
      OdfDocument docA = odtRootDoc.loadSubDocument("DOCA");
      docA.newImage(ResourceUtilities.getURI(TEST_PIC));
      docA.insertDocument(OdfSpreadsheetDocument.newSpreadsheetDocument(), "../DOCB/");
      OdfFileDom contentA = docA.getContentDom();
      XPath xpath = contentA.getXPath();
      TextPElement lastPara = (TextPElement) xpath.evaluate("//text:p[last()]", contentA, XPathConstants.NODE);
      addFrameForEmbeddedDoc(contentA, lastPara, "DOCB");
      OdfDocument docB = odtRootDoc.loadSubDocument("DOCB/");
      Assert.assertNotNull(docB);
      Assert.assertNull(odtRootDoc.loadSubDocument("DOCA/DOCB/"));
      docB.newImage(ResourceUtilities.getURI(TEST_PIC_ANOTHER));
      OdfTable table1 = docB.getTableList().get(0);
      table1.setTableName("NewTable");
      updateFrameForEmbeddedDoc(contentA, "./DOCB", "DOCA/DOCB");
      //if user want to save the docA with the side by side embedded document
View Full Code Here

   * 7) DOCA/ is saved in a document for its own  
   */
  @Test
  public void testembeddedDocumentWithSubPath() {
    try {
      OdfTextDocument odtDoc1 = OdfTextDocument.newTextDocument();
      odtDoc1.insertDocument(OdfTextDocument.newTextDocument(), "DOCA/");
      OdfDocument docA = odtDoc1.loadSubDocument("DOCA");
      docA.newImage(ResourceUtilities.getURI(TEST_PIC));
      docA.insertDocument(OdfSpreadsheetDocument.newSpreadsheetDocument(), "DOCB/");
      OdfFileDom contentA = docA.getContentDom();
      XPath xpath = contentA.getXPath();
      TextPElement lastPara = (TextPElement) xpath.evaluate("//text:p[last()]", contentA, XPathConstants.NODE);
      addFrameForEmbeddedDoc(contentA, lastPara, "./DOCB");
      OdfDocument docB = odtDoc1.loadSubDocument("DOCA/DOCB/");
      docB.newImage(ResourceUtilities.getURI(TEST_PIC_ANOTHER));
      OdfTable table1 = docB.getTableList().get(0);
      table1.setTableName("NewTable");
      Assert.assertNotNull(docB);
      Assert.assertNull(odtDoc1.loadSubDocument("DOCB/"));
      docA.save(TEST_FILE_FOLDER + TEST_FILE_EMBEDDED_INCLUDED_SAVE_OUT);
      OdfDocument testLoad = OdfDocument.loadDocument(TEST_FILE_FOLDER + TEST_FILE_EMBEDDED_INCLUDED_SAVE_OUT);
      OdfFileEntry imageEntry = testLoad.getPackage().getFileEntry(OdfPackage.OdfFile.IMAGE_DIRECTORY.getPath() + SLASH + TEST_PIC);
      Assert.assertNotNull(imageEntry);
      Map<String, OdfDocument> embDocs = testLoad.loadSubDocuments(OdfDocument.OdfMediaType.SPREADSHEET);
View Full Code Here

    }
  }

  private OdfTextDocument loadODTDocument(String name) {
    try {
      OdfTextDocument odtdoc = (OdfTextDocument) OdfTextDocument.loadDocument(ResourceUtilities.getAbsolutePath(name));
      return odtdoc;
    } catch (Exception e) {
      Logger.getLogger(TableTest.class.getName()).log(Level.SEVERE, null, e);
      Assert.fail(e.getMessage());
    }
View Full Code Here

  }

  @Test
  public void testNewTable() {
    try {
      OdfTextDocument document = OdfTextDocument.newTextDocument();
      document.newParagraph("Empty table:");
      createEmptyTable(document);

      document.newParagraph();
      document.newParagraph("Table with float values:");
      createTableWithData(document);
      document.newParagraph();
      document.newParagraph("Table with string values:");
      createTableWithString(document);

      document.save(ResourceUtilities.newTestOutputFile("CreateTableCase.odt"));
    } catch (Exception e) {
      Logger.getLogger(TableTest.class.getName()).log(Level.SEVERE, null, e);
      Assert.fail(e.getMessage());
    }
  }
View Full Code Here

  @Test
  public void testSetGetWidth() {
    long width = 500;
    try {
      OdfTextDocument document = OdfTextDocument.newTextDocument();
      document.newParagraph("Empty table:");
      OdfTable table = createEmptyTable(document);
      table.setWidth(width);
      Assert.assertTrue(Math.abs(width - table.getWidth()) < 3);

      document.save(ResourceUtilities.newTestOutputFile("TestSetGetWidth.odt"));

    } catch (Exception e) {
      Logger.getLogger(TableTest.class.getName()).log(Level.SEVERE, null, e);
      Assert.fail(e.getMessage());
    }
View Full Code Here

  }

  @Test
  public void testGetSetTablename() {
    String tablename = "My Table";
    OdfTextDocument document = null;
    try {
      document = OdfTextDocument.newTextDocument();
      document.newParagraph("Empty table:");
      OdfTable table = createEmptyTable(document);
      table.setTableName(tablename);
      Assert.assertEquals(tablename, table.getTableName());

      document.save(ResourceUtilities.newTestOutputFile("TestGetSetName.odt"));
      document.close();
      document = loadODTDocument("TestGetSetName.odt");
      table = document.getTableByName(tablename);
      Assert.assertNotNull(table);
      String tablename2 = table.getTableName();
      Assert.assertEquals(tablename, tablename2);
    } catch (Exception e) {
      Assert.fail(e.getMessage());
    }

    try {
      //new another table with the same name
      //an exception will be thrown
      OdfTable table2 = OdfTable.newTable(document);
      table2.setTableName(tablename);
      document.save(ResourceUtilities.newTestOutputFile("TestGetSetName.odt"));
      Assert.fail("should not save the tables with the same table name.");
    } catch (Exception e) {
      if (!e.getMessage().startsWith("The table name is duplicate")) {
        Assert.fail(e.getMessage());
      }
View Full Code Here

  @Test
  public void testGetColumnList() {
    String tablename = "MyTable";
    String testFileName = "TestGetColumnList.odt";
    try {
      OdfTextDocument document = OdfTextDocument.newTextDocument();

      int rowcount = 3, columncount = 3;
      String[][] data = new String[rowcount][columncount];
      for (int i = 0; i < rowcount; i++) {
        for (int j = 0; j < columncount; j++) {
          data[i][j] = "string" + (i * columncount + j);
        }
      }

      String[] rowlabels = new String[rowcount];
      for (int i = 0; i < rowcount; i++) {
        rowlabels[i] = "RowHeader" + i;
      }

      String[] columnlabels = new String[columncount];
      for (int i = 0; i < columncount; i++) {
        columnlabels[i] = "ColumnHeader" + i;
      }

      OdfTable table3 = OdfTable.newTable(document, rowlabels, columnlabels, data);
      table3.setTableName(tablename);

      document.save(ResourceUtilities.newTestOutputFile(testFileName));
      document = loadODTDocument(testFileName);
      OdfTable table = document.getTableByName(tablename);
      OdfTableColumn tmpColumn;
      List<OdfTableColumn> columns = table.getColumnList();

      //the code below prints the column value,it shows that the first columns value is the same with the last column
      for (int i = 0; i < columns.size(); i++) {
View Full Code Here

TOP

Related Classes of org.odftoolkit.odfdom.doc.OdfTextDocument

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.