Package org.odftoolkit.simple

Examples of org.odftoolkit.simple.TextDocument


  }

  @Test
  public void testReturnChar() {
    try {
      TextDocument textDoc = TextDocument.newTextDocument();
      textDoc.newParagraph();
      OdfTextParagraph graph = textDoc.newParagraph("abc");

      TextExtractor extractor = TextExtractor.newOdfTextExtractor(textDoc.getContentRoot());
      String text = extractor.getText();
      System.out.println(text);

      int count = 0;
      for (int i = 0; i < text.length(); i++)
View Full Code Here


   * "simpleODF should be Simple ODF" for all the 'simpleODF' word
   */
  @Test
  public void testAddComment() {
    try {
      TextDocument textDoc = TextDocument.loadDocument(ResourceUtilities.getAbsolutePath(TEXT_COMMENT_FILE));
      search = new TextNavigation("simpleODF", textDoc);
      int i=0;
      while (search.hasNext()) {
        TextSelection item = (TextSelection) search.nextSelection();
        item.addComment("simpleODF should be replaced by Simple ODF.", "devin-"+i);
        i++;
      }
      // there are 7 simpleODF in this test document.
      Assert.assertEquals(7, i);
      textDoc.save(ResourceUtilities.newTestOutputFile(SAVE_FILE_COMMENT));
    } catch (Exception e) {
      Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e);
      Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
    }
  }
View Full Code Here

  }
 
  @Test
  public void testVariableTypesisplayField() {
    try {
      TextDocument doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream(TEST_DOCUMENT));
      // declare simple variable
      VariableField simpleVariableField = Fields.createSimpleVariableField(doc, "test_simple_variable");
      Assert.assertNotNull(simpleVariableField);
      TextSpanElement newTextSpanElement = doc.newParagraph("Update Simple Variable Field:").newTextSpanElement();
      simpleVariableField.updateField("simple variable content", newTextSpanElement);
     
      FieldType fieldType = simpleVariableField.getFieldType();
      Assert.assertNotNull(fieldType);
      Assert.assertEquals(FieldType.SIMPLE_VARIABLE_FIELD, fieldType);
View Full Code Here

  }

  @Test
  public void testAddRemoveIterateList() {
    try {
      TextDocument doc = TextDocument.newTextDocument();
      Table table = Table.newTable(doc, 3, 3);
      ListDecorator bulletDecorator = new BulletDecorator(doc);
      ListDecorator numberDecorator = new NumberDecorator(doc);
      ListDecorator imageDecorator = new ImageDecorator(doc, ResourceUtilities.getURI("image_list_item.png"));
      String[] numberItemContents = { "number item 1", "number item 2", "number item 3" };
View Full Code Here

  }

  @Test
  public void testGetSetImage() {
    try {
      TextDocument doc = TextDocument.newTextDocument();
      Table table = Table.newTable(doc, 2, 2);
      table.setTableName("ImageTable");
      Cell cell = table.getCellByPosition(0, 0);
      cell.setImage(ResourceUtilities.getURI("image_list_item.png"));
      doc.save(ResourceUtilities.newTestOutputFile("ImageCellTable.odt"));

      // load the document again.
      doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("ImageCellTable.odt"));
      table = doc.getTableByName("ImageTable");
      cell = table.getCellByPosition(0, 0);
      // image height = 34 pixels.
      Assert.assertEquals(34, cell.getBufferedImage().getHeight(null));
    } catch (Exception e) {
      Logger.getLogger(TableCellTest.class.getName()).log(Level.SEVERE, null, e);
View Full Code Here

        { "three trailing spaces ", "*s2" }, { "one", "*t", "tab" }, { "two", "*t", "*t", "tabs" },
        { "*t", "leading tab" }, { "trailing tab", "*t" },
        { "mixed ", "*s2", "*t", " ", "*s2", "spaces and tabs" }, { "line", "*n", "break" } };
    try {
      // test append paragraph
      TextDocument doc = TextDocument.newTextDocument();
      Table table = Table.newTable(doc, 2, 2);
      Cell cell = table.getCellByPosition(0, 0);
      for (int i = 0; i < plainText.length; i++) {
        Paragraph para = cell.addParagraph(plainText[i]);
        compareResults(para.getOdfElement(), plainText[i], elementResult[i]);
      }

      // test set paragraph content
      cell = table.getCellByPosition(0, 1);
      for (int i = 0; i < plainText.length; i++) {
        Paragraph para = cell.addParagraph(plainText[i]);
        compareResults(para.getOdfElement(), plainText[i], elementResult[i]);
        String content = para.getTextContent();
        Assert.assertEquals(plainText[i], content);
      }

      // test remove paragraph content
      cell = table.getCellByPosition(1, 0);
      for (int i = 0; i < plainText.length; i++) {
        Paragraph para = cell.addParagraph(plainText[i]);
        String content = para.getTextContent();
        Assert.assertEquals(plainText[i], content);
        para.removeTextContent();
        content = para.getTextContent();
        Assert.assertEquals("", content);
      }

      // test get paragraph by index
      cell = table.getCellByPosition(1, 1);
      Paragraph paragraph1 = cell.addParagraph("paragraph1");
      Paragraph paragraphE = cell.addParagraph(null);
      Paragraph paragraph2 = cell.addParagraph("p2");

      Paragraph t1 = cell.getParagraphByIndex(0, false);
      Assert.assertEquals(t1, paragraph1);
      t1 = cell.getParagraphByIndex(2, false);
      Assert.assertEquals(t1, paragraph2);
      t1 = cell.getParagraphByIndex(1, true);
      Assert.assertEquals(t1, paragraph2);
      t1 = cell.getParagraphByReverseIndex(0, false);
      Assert.assertEquals(t1, paragraph2);
      t1 = cell.getParagraphByReverseIndex(2, false);
      Assert.assertEquals(t1, paragraph1);
      t1 = cell.getParagraphByReverseIndex(1, true);
      Assert.assertEquals(t1, paragraph1);
      doc.save(ResourceUtilities.newTestOutputFile("testCellParagraph.odt"));
    } catch (Exception e) {
      Logger.getLogger(TableCellTest.class.getName()).log(Level.SEVERE, null, e);
      Assert.fail(e.getMessage());
    }
  }
View Full Code Here

  }
 
  @Test
  public void testReadDocumentMeta() throws Exception {
    // create a new empty document
    TextDocument textDoc = TextDocument.newTextDocument();
    textDoc.save(ResourceUtilities.newTestOutputFile("DocForMetaTest.odt"));
    textDoc.close();
    // read empty document meta
    textDoc = (TextDocument) TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("DocForMetaTest.odt"));
    Meta meta = textDoc.getOfficeMetadata();
    Assert.assertNotNull(meta.getGenerator());
    Assert.assertNotNull(meta.getCreationDate());
    Assert.assertNotNull(meta.getCreator());
    Assert.assertNotNull(meta.getDcdate());
    Assert.assertTrue(meta.getEditingCycles()>0);
    Assert.assertNotNull(meta.getEditingDuration());
    Assert.assertNotNull(meta.getLanguage());
    textDoc.close();
  }
View Full Code Here

public class SectionTest {

  @Test
  public void testCopyPasteResource() {
    try {
      TextDocument doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("Sections.odt"));
      Section theSec = doc.getSectionByName("ImageSection");
      String newName = doc.appendSection(theSec, false).getName();
      doc.save(ResourceUtilities.newTestOutputFile("NewSection.odt"));

      TextDocument newDoc = TextDocument
          .loadDocument(ResourceUtilities.getTestResourceAsStream("NewSection.odt"));
      theSec = newDoc.getSectionByName("ImageSection");
      Section newSec = newDoc.getSectionByName(newName);

      XPath xpath = newDoc.getContentDom().getXPath();
      DrawImageElement oldImage = (DrawImageElement) xpath.evaluate(".//draw:image", theSec.getOdfElement(),
          XPathConstants.NODE);
      DrawImageElement newImage = (DrawImageElement) xpath.evaluate(".//draw:image", newSec.getOdfElement(),
          XPathConstants.NODE);
      Assert.assertEquals(oldImage.getXlinkHrefAttribute(), newImage.getXlinkHrefAttribute());

      OdfPackage packageDocument = newDoc.getPackage();
      String imagePathPrefix = "Pictures/";
      int count = 0;
      Iterator<String> filePaths = packageDocument.getFilePaths().iterator();
      while (filePaths.hasNext()) {
        String path = filePaths.next();
        if (path.startsWith(imagePathPrefix) && path.length() > imagePathPrefix.length())
          count++;
        if (count > 2)
          break;
      }
      Assert.assertEquals(1, count);
      // ---------resource copied------
      doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("Sections.odt"));
      theSec = doc.getSectionByName("ImageSection");
      newName = doc.appendSection(theSec, true).getName();
      doc.save(ResourceUtilities.newTestOutputFile("NewSection1.odt"));

      newDoc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("NewSection1.odt"));
      theSec = newDoc.getSectionByName("ImageSection");
      newSec = newDoc.getSectionByName(newName);

      xpath = newDoc.getContentDom().getXPath();
      oldImage = (DrawImageElement) xpath.evaluate(".//draw:image", theSec.getOdfElement(), XPathConstants.NODE);
      newImage = (DrawImageElement) xpath.evaluate(".//draw:image", newSec.getOdfElement(), XPathConstants.NODE);
      if (oldImage.getXlinkHrefAttribute().equals(newImage.getXlinkHrefAttribute()))
        Assert.fail();

      packageDocument = newDoc.getPackage();
      count = 0;
      filePaths = packageDocument.getFilePaths().iterator();
      while (filePaths.hasNext()) {
        String path = filePaths.next();
        if (path.startsWith(imagePathPrefix) && path.length() > imagePathPrefix.length())
View Full Code Here

  }

  @Test
  public void testForeignCopyPaste() {
    try {
      TextDocument newDoc = TextDocument.newTextDocument();

      TextDocument doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("Sections.odt"));
      Iterator<Section> sections = doc.getSectionIterator();

      int count = 0;
      while (sections.hasNext()) {
        Section aSection = sections.next();
        count++;
View Full Code Here

  }

  @Test
  public void testCopyPasteAll() {
    try {
      TextDocument doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("Sections.odt"));
      Iterator<Section> sections = doc.getSectionIterator();

      int count = 0;
      while (sections.hasNext()) {
        Section aSection = sections.next();
        count++;
        doc.newParagraph("----Start of " + aSection.getName() + "---------");
        doc.appendSection(aSection, false);
        doc.newParagraph("----End of " + aSection.getName() + "---------");
        doc.newParagraph();
        doc.newParagraph();
        doc.newParagraph();
      }
      doc.save(ResourceUtilities.newTestOutputFile("NewSections.odt"));

      doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("NewSections.odt"));
      sections = doc.getSectionIterator();
      int i = 0;
      while (sections.hasNext()) {
        sections.next();
        i++;
      }
View Full Code Here

TOP

Related Classes of org.odftoolkit.simple.TextDocument

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.