Package org.odftoolkit.odfdom.doc

Examples of org.odftoolkit.odfdom.doc.OdfTextDocument


  @Test
  public void testGetRowList() {
    String tablename = "MyTable";
    String testFileName = "TestGetRowList.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);
      OdfTableRow tmpRow;
      List<OdfTableRow> rows = table.getRowList();
      for (int i = 1; i < rows.size(); i++) {
        tmpRow = rows.get(i);
        //each row's first cell is the row header
View Full Code Here


    cellRange.merge();
    OdfTableCell cell = cellRange.getCellByPosition(0, 0);
    Assert.assertEquals(cell.getDisplayText(), "cell1cell2");
    saveodt("MergeTwoCell");
    try {
      OdfTextDocument saveddoc = (OdfTextDocument) OdfTextDocument.loadDocument(ResourceUtilities.getAbsolutePath(odtfilename + "MergeTwoCell.odt"));
      OdfTable savedTable1 = saveddoc.getTableByName("Table1");
      //get the cell range which the first cell is the covered cell.
      //so the cell range will be enlarged
      OdfTableCellRange savedCellRange = savedTable1.getCellRangeByPosition(1, 0, 2, 0);
      savedCellRange.merge();
      Assert.assertTrue(savedCellRange.getColumnNumber() == 3);
      OdfTableCell savedCell = savedCellRange.getCellByPosition(0, 0);
      NodeList paraList = savedCell.getOdfElement().getChildNodes();
      OdfWhitespaceProcessor textProcessor = new OdfWhitespaceProcessor();
      Assert.assertTrue(paraList.item(2) instanceof OdfTextParagraph);
      Assert.assertEquals(textProcessor.getText(paraList.item(2)),"0.00");
      saveddoc.save(ResourceUtilities.newTestOutputFile(odtfilename + "MergeCoveredCell.odt"));
    } catch (Exception e) {
      Logger.getLogger(TableCellRangeTest.class.getName()).log(Level.SEVERE, e.getMessage(), e);
      Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
    }

    try {
      OdfTextDocument saveddoc = (OdfTextDocument) OdfTextDocument.loadDocument(ResourceUtilities.getAbsolutePath(odtfilename + "MergeTwoCell.odt"));
      OdfTable savedTable1 = saveddoc.getTableByName("Table1");
      //get the cell range which the first cell is the covered cell.
      //so the cell range will be enlarged
      OdfTableCellRange savedCellRange = savedTable1.getCellRangeByPosition(0, 0, 0, 1);
      savedCellRange.merge();
      Assert.assertTrue(savedCellRange.getColumnNumber() == 2);
      Assert.assertTrue(savedCellRange.getRowNumber() == 2);
      OdfTableCell savedCell = savedCellRange.getCellByPosition(0, 1);
      Assert.assertTrue(savedCell.getOdfElement() instanceof TableCoveredTableCellElement);
      saveddoc.save(ResourceUtilities.newTestOutputFile(odtfilename + "MergeCoveredCell2.odt"));
    } catch (Exception e) {
      Logger.getLogger(TableCellRangeTest.class.getName()).log(Level.SEVERE, e.getMessage(), e);
      Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
    }
  }
View Full Code Here

    OdfTableCell cell = firstColumn.getCellByPosition(0, 2);
    OdfTableCell firstCell = firstColumn.getCellByPosition(0, 0);
    Assert.assertTrue(cell.getOwnerTableCell().equals(firstCell));
    saveodt("MergeFirstColumn");
    try {
      OdfTextDocument saveddoc = (OdfTextDocument) OdfTextDocument.loadDocument(ResourceUtilities.getAbsolutePath(odtfilename + "MergeFirstColumn.odt"));
      OdfTable savedTable = saveddoc.getTableByName("Table1");
      OdfTableCellRange firstTwoColumn = savedTable.getCellRangeByPosition(0, 0, 1, savedTable.getRowCount() - 1);
      firstTwoColumn.merge();
      OdfTableCell cell1 = firstTwoColumn.getCellByPosition(0, 2);
      OdfTableCell firstCell1 = firstTwoColumn.getCellByPosition(0, 0);
      Assert.assertTrue(cell1.getOwnerTableCell().equals(firstCell1));
      saveddoc.save(ResourceUtilities.newTestOutputFile(odtfilename + "MergeFirstTwoColumn.odt"));
    } catch (Exception e) {
      Logger.getLogger(TableCellRangeTest.class.getName()).log(Level.SEVERE, e.getMessage(), e);
      Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
    }
  }
View Full Code Here

      Assert.assertTrue(table.getColumnCount() == 5);
      Assert.assertTrue(table.getRowCount() == 1);
      table = ods.getTableByName("Sheet2");
      Assert.assertTrue(table.getColumnCount() == 6);
      Assert.assertTrue(table.getRowCount() == 3);
      OdfTextDocument odt = OdfTextDocument.newTextDocument();
      OdfTable swTable = OdfTable.newTable(odt, 1, 5);
      OdfTableCellRange swCellRange = swTable.getCellRangeByPosition("A1", "E2");
      OdfTableCell swCell = swTable.getCellByPosition("E2");
      swCell.setStringValue("Merge A1:E2");
      swCellRange.merge();
      odt.save(ResourceUtilities.newTestOutputFile(odtfilename + "MergeTextExpandCell.odt"));
      swTable = odt.getTableList().get(0);
      Assert.assertTrue(swTable.getColumnCount() == 1);
      Assert.assertTrue(swTable.getRowCount() == 1);
    } catch (Exception ex) {
      Logger.getLogger(TableCellRangeTest.class.getName()).log( Level.SEVERE, ex.getMessage(), ex);
      Assert.fail("Failed with " + ex.getClass().getName() + ": '" + ex.getMessage() + "'");
View Full Code Here

    // TODO Auto-generated constructor stub
  }

  @Test
  public void testGetRDFMetaFromGRDDLXSLT() throws Exception {
    OdfTextDocument odt = (OdfTextDocument) OdfDocument
        .loadDocument(ResourceUtilities.getAbsolutePath(SIMPLE_ODT));
    Model m1 = odt.getManifestRDFMetadata();
    LOG.info("RDF Model - manifest:\n" + m1.toString());       
    long size1 = m1.size();
    TestCase.assertEquals(25, size1);

    Model m2 = odt.getInContentMetadata();
    LOG.info("RDF Model - rood document in-content:\n" + m1.toString());       
    long size2 = m2.size();
    TestCase.assertEquals(20, size2);

    Model m = odt.getRDFMetadata();

    // This triples are duplicated in m1 and m2:
    // (test_rdfmeta.odt rdf:type
    // http://docs.oasis-open.org/ns/office/1.2/meta/pkg#Document)
    // (test_rdfmeta.odt/embeded.odt rdf:type
    // http://docs.oasis-open.org/ns/office/1.2/meta/pkg#Document)
//    int duplicated = 2;
    Model m3 = m1.intersection(m2);
    LOG.info("RDF Model - duplicated (manifest & in-content of root doc):\n" + m3.toString());
// Uncertain about the previuos statement, as the duplicated RDF triple do not have an identical subject..
//    TestCase.assertEquals(duplicated, m3.size());
//    TestCase.assertEquals(size1 + size2 - duplicated, m.size());

    // test the embeded document
    OdfDocument subDoc = odt.loadSubDocument("embeded.odt");
    m1 = subDoc.getManifestRDFMetadata();
    size1 = m1.size();
    TestCase.assertEquals(5, size1);

    m2 = subDoc.getInContentMetadata();
View Full Code Here

  }

  @Test
  public void testGetInContentMetaFromCache() throws Exception {
    OdfTextDocument odt = (OdfTextDocument) OdfDocument
        .loadDocument(ResourceUtilities.getAbsolutePath(SIMPLE_ODT));
    Model m1 = odt.getInContentMetadataFromCache();

    // We have the following 1 triple in cache:
    // (http://dbpedia.org/page/J._R._R._Tolkien
    // http://www.w3.org/2006/vcard/ns#fn 'John Ronald Reuel Tolkien')
    TestCase.assertEquals(1, m1.size());

    OdfContentDom contentDom = odt.getContentDom();
    XPath xpath = contentDom.getXPath();
    TextMetaElement tm = (TextMetaElement) xpath.evaluate(
        "//text:p/text:meta[last()]", contentDom, XPathConstants.NODE);

    tm.setXhtmlAboutAttribute("http://dbpedia.org/page/J._K._Rowling");
    m1 = odt.getInContentMetadataFromCache();
    PrintUtil.printOut(m1.listStatements());
    TestCase.assertEquals("http://dbpedia.org/page/J._K._Rowling", m1
        .listStatements().nextStatement().getSubject().getURI());

    tm.setTextContent("Joanne Kathleen Rowling");
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals("Joanne Kathleen Rowling", m1.listStatements()
        .nextStatement().getObject().toString());

    tm.setXhtmlPropertyAttribute("dc:name");
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals("http://purl.org/dc/elements/1.1/name", m1
        .listStatements().nextStatement().getPredicate().getURI());

    Node parent = tm.getParentNode();
    parent.removeChild(tm);
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(0, m1.size());
    parent.appendChild(tm);
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(1, m1.size());
    parent.removeChild(tm);

    TextPElement pEle = contentDom.newOdfElement(TextPElement.class);
    parent.appendChild(pEle);
    pEle.setXhtmlAboutAttribute("[dbpedia:J._K._Rowling]");
    pEle.setXhtmlPropertyAttribute("dbpprop:birthDate dbpprop:dateOfBirth");
    pEle.setXhtmlDatatypeAttribute("xsd:date");
    pEle.setXhtmlContentAttribute("1965-07-31");
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(2, m1.size());
    StmtIterator iter = m1.listStatements();
    while (iter.hasNext()) {
      Statement stmt = iter.nextStatement();
      TestCase.assertEquals("http://dbpedia.org/page/J._K._Rowling", stmt
          .getSubject().getURI());
      TestCase.assertTrue(stmt.getObject().canAs(Literal.class));
      Literal literal = stmt.getObject().as(Literal.class);
      TestCase.assertEquals("http://www.w3.org/2001/XMLSchema#date",
          literal.getDatatypeURI());
      TestCase.assertEquals("1965-07-31", literal.getLexicalForm());
    }
    pEle.setXhtmlPropertyAttribute("dc:date");
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(1, m1.size());
    TestCase.assertEquals("http://purl.org/dc/elements/1.1/date", m1
        .listStatements().nextStatement().getPredicate().getURI());

    parent.removeChild(pEle);
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(0, m1.size());
    parent.appendChild(pEle);
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(1, m1.size());
    parent.removeChild(pEle);

    TextHElement hEle = contentDom.newOdfElement(TextHElement.class);
    parent.appendChild(hEle);
    hEle.setXhtmlAboutAttribute("[dbpedia:J._K._Rowling]");
    hEle.setXhtmlPropertyAttribute("dbpprop:children");
    hEle.setXhtmlDatatypeAttribute("xsd:integer");
    hEle.setTextContent("2");
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(1, m1.size());
    TestCase.assertEquals("http://dbpedia.org/page/J._K._Rowling", m1
        .listStatements().nextStatement().getSubject().getURI());
    TestCase.assertEquals("http://dbpedia.org/property/children", m1
        .listStatements().nextStatement().getPredicate().getURI());
    TestCase.assertTrue(m1.listStatements().nextStatement().getObject()
        .canAs(Literal.class));
    TestCase.assertEquals("2^^http://www.w3.org/2001/XMLSchema#integer", m1
        .listStatements().nextStatement().getObject().toString());
    hEle.setTextContent("3");
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals("3^^http://www.w3.org/2001/XMLSchema#integer", m1
        .listStatements().nextStatement().getObject().toString());

    parent.removeChild(hEle);
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(0, m1.size());
    parent.appendChild(hEle);
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(1, m1.size());
    parent.removeChild(hEle);

    TableTableCellElement ttce = contentDom
        .newOdfElement(TableTableCellElement.class);
    parent.appendChild(ttce);
    ttce.setXhtmlAboutAttribute("[dbpedia:J._K._Rowling]");
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(0, m1.size());
    ttce.setXhtmlPropertyAttribute("dbpprop:nationality");
    ttce.setTextContent("British");
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(1, m1.size());
    ttce.setXhtmlAboutAttribute("http://dbpedia.org/page/J._R._R._Tolkien");
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(1, m1.size());
    TestCase.assertEquals("http://dbpedia.org/page/J._R._R._Tolkien", m1
        .listStatements().nextStatement().getSubject().getURI());

    TableCoveredTableCellElement tctce = contentDom
        .newOdfElement(TableCoveredTableCellElement.class);
    parent.appendChild(tctce);
    tctce.setXhtmlAboutAttribute("[dbpedia:J._R._R._Tolkien]");
    tctce.setXhtmlPropertyAttribute("dbpprop:shortDescription");
    tctce.setXhtmlContentAttribute("British philologist and author");
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(2, m1.size());

    parent.removeChild(ttce);
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(1, m1.size());
    parent.appendChild(ttce);
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(2, m1.size());
    parent.removeChild(ttce);

    parent.removeChild(tctce);
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(0, m1.size());
    parent.appendChild(tctce);
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(1, m1.size());
    parent.removeChild(tctce);
    m1 = odt.getInContentMetadataFromCache();
    TestCase.assertEquals(0, m1.size());

  }
View Full Code Here

  }

  @Test
  public void testGetBookmarkRDFMetadata() throws Exception {
    OdfTextDocument odt = (OdfTextDocument) OdfDocument
        .loadDocument(ResourceUtilities.getAbsolutePath(SIMPLE_ODT));
    Model m = odt.getBookmarkRDFMetadata();
    TestCase.assertEquals(2, m.size());

    OdfContentDom contentDom = odt.getContentDom();
    XPath xpath = contentDom.getXPath();
    TextBookmarkStartElement tm = (TextBookmarkStartElement) xpath
        .evaluate("//text:bookmark-start[last()]", contentDom,
            XPathConstants.NODE);
    BookmarkRDFMetadataExtractor extractor = BookmarkRDFMetadataExtractor
View Full Code Here

   */
  @Test
  public void testReplaceWith() {

    try {
      OdfTextDocument doc = (OdfTextDocument) OdfDocument.loadDocument(ResourceUtilities.getAbsolutePath(TEXT_FILE));

      TextNavigation search = new TextNavigation("mnop", doc);

      int i = 0;
      while (search.hasNext()) {
        TextSelection item = (TextSelection) search.getCurrentItem();
        try {
          item.replaceWith("success");
          i++;
          // item.addHref(new URL("http://www.oracle.com"));
        } catch (InvalidNavigationException e) {
          Assert.fail(e.getMessage());
        }
      }
      Assert.assertTrue(18 == i);
      doc.save(ResourceUtilities.newTestOutputFile(SAVE_FILE));
    } catch (Exception e) {
      Logger.getLogger(MONPTest.class.getName()).log(Level.SEVERE, e.getMessage(), e);
      Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
    }
  }
View Full Code Here

  @Test
  public void testGRDDL()  {
    try {
      OdfXMLHelper helper = new OdfXMLHelper();
      OdfTextDocument odt = (OdfTextDocument) OdfDocument
          .loadDocument(ResourceUtilities.getAbsolutePath(SIMPLE_ODT));
      InputSource inputSource = new InputSource(ResourceUtilities.getURI(
          "grddl/odf2rdf.xsl").toString());
      Templates multiFileAccessTemplate = TransformerFactory.newInstance()
          .newTemplates(new SAXSource(inputSource));
      ByteArrayOutputStream out = new ByteArrayOutputStream();
  
      helper.transform(odt.getPackage(), "content.xml",
          multiFileAccessTemplate, new StreamResult(out));

      Model m1 = createMemModel();
      m1.read(new InputStreamReader(new ByteArrayInputStream(out
          .toByteArray()), "utf-8"), odt.getPackage().getBaseURI());
      LOG.info("RDF Model:\n" + m1.toString());
      TestCase.assertEquals(5, m1.size());
    } catch (Exception ex) {
      Assert.fail(ex.getMessage());
      Logger.getLogger(GRDDLTest.class.getName()).log(Level.SEVERE, null, ex);
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.