Package org.odftoolkit.simple.common.navigation

Examples of org.odftoolkit.simple.common.navigation.TextNavigation


   */
  @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++;
View Full Code Here


   * "http://www.ibm.com" for all the 'delete' word
   */
  @Test
  public void testAddHref() {
    search = null;
    search = new TextNavigation("^delete", doc);
    while (search.hasNext()) {
      TextSelection item = (TextSelection) search.nextSelection();
      try {
        item.addHref(new URL("http://www.ibm.com"));
      } catch (InvalidNavigationException e) {
View Full Code Here

   * text match the pattern "<%([^>]*)%>", and extract the content between
   * "<%" and "%>"
   */
  @Test
  public void testCutPattern() {
    search = new TextNavigation("<%([^>]*)%>", doc);

    while (search.hasNext()) {
      TextSelection item = (TextSelection) search.nextSelection();
      try {
        String text = item.getText();
View Full Code Here

   * or text:h. It means container element can't has child element has the
   * same text content.
   */
  @Test
  public void testTextSelectionContainerElement() {
    search = new TextNavigation("TextSelectionContainer", doc);
    if (search.hasNext()) {
      TextSelection item = (TextSelection) search.nextSelection();
      OdfElement container = item.getContainerElement();
      Node childNode = container.getFirstChild();
      while (childNode != null) {
        String containerText = childNode.getTextContent();
        Assert.assertFalse(container.getTextContent().equals(containerText));
        childNode = childNode.getNextSibling();
      }
    } else {
      Assert.fail("Navigation search nothing.");
    }
    // test selected table cell content in draw:frame.
    search = new TextNavigation("Task", doc);
    if (search.hasNext()) {
      TextSelection item = (TextSelection) search.nextSelection();
      OdfElement container = item.getContainerElement();
      Node childNode = container.getFirstChild();
      while (childNode != null) {
View Full Code Here

    }
  }
 
  @Test
  public void testGetContainerElement() throws Exception {
    search = new TextNavigation("TextSelectionContainer", doc);
    if (search.hasNext()) {
      TextSelection item = (TextSelection) search.nextSelection();
      URL url = new URL("http://www.IBM.com");
      item.addHref(url);
     
View Full Code Here

    Table table1 = Table.newTable(sourcedoc, rownumber3, clmnumber3);
    table1.setTableName(tablename3);
    search = null;
    // 6 Simple, at the middle of original Paragraph, split original
    // Paragraph, insert before the second Paragraph.
    search = new TextNavigation("SIMPLE", doc);
    while (search.hasNext()) {
      TextSelection item = (TextSelection) search.nextSelection();
      table = sourcedoc.getTableByName("Table1");
      Cell cell = table.getCellByPosition(0, 0);
      cell.setStringValue("SIMPLE");
      Table newtable = item.replaceWith(table);
      Assert.assertNotNull(newtable);
      Assert.assertEquals(1, newtable.getHeaderColumnCount());
      Assert.assertEquals(1, newtable.getHeaderRowCount());
      Assert.assertEquals(7 + 1, newtable.getRowCount());
      Assert.assertEquals(5 + 1, newtable.getColumnCount());
      cell = newtable.getCellByPosition(1, 1);
      Assert.assertEquals("string", cell.getValueType());
    }
    // 2 Task1, #1 at the start of original Paragraph, #2 replace original
    // Paragraph
    search = new TextNavigation("Task1", doc);
    while (search.hasNext()) {
      TextSelection item = (TextSelection) search.nextSelection();
      table = sourcedoc.getTableByName("Table2");
      Table newtable = item.replaceWith(table);
      Cell cell = newtable.getCellByPosition(0, 0);
      cell.setStringValue("From Source Table2");
      Assert.assertNotNull(newtable);
      Assert.assertEquals(1, newtable.getHeaderColumnCount());
      Assert.assertEquals(1, newtable.getHeaderRowCount());
      Assert.assertEquals(10 + 1, newtable.getRowCount());
      Assert.assertEquals(4 + 1, newtable.getColumnCount());
     
      cell = newtable.getCellByPosition(1, 1);
      Assert.assertEquals("float", cell.getValueType());
    }
    // 1 RESS%>, #1 at the end of original Paragraph,
    search = new TextNavigation("RESS%>", doc);
   
    while (search.hasNext()) {
      TextSelection item = (TextSelection) search.nextSelection();
      table = sourcedoc.getTableByName("Table3");
      Table newtable = item.replaceWith(table);
      Cell cell = newtable.getCellByPosition(0, 0);
      cell.setStringValue("From Source Table3");
      Assert.assertNotNull(newtable);
      Assert.assertEquals(5, newtable.getRowCount());
      Assert.assertEquals(3, newtable.getColumnCount());
    }
    try {
      doc.save(ResourceUtilities.newTestOutputFile("TextSelectionReplacewithTableResult.odt"));
    } catch (Exception e) {
      Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e);
      Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
    }
    doc = (TextDocument) Document.loadDocument(ResourceUtilities
        .getAbsolutePath(TEST_FILE));
    search = new TextNavigation("<<target>>", doc);
    int i = 0;
    try {
      while (search.hasNext()) {
        i++;
        TextSelection item = (TextSelection) search.nextSelection();
View Full Code Here

    image2.setHyperlink(new URI("http://odftoolkit.org"));
    search = null;
    // 6 Simple, at the middle of original Paragraph, split original
    // Paragraph, insert before the second Paragraph.
    doc = (TextDocument) Document.loadDocument(ResourceUtilities.getAbsolutePath(TEXT_FILE));
    search = new TextNavigation("SIMPLE", doc);
    int i = 0;
    TextSelection item = null;
    while (search.hasNext()) {
      item = (TextSelection) search.nextSelection();
      Paragraph paragraph = sourcedoc.getParagraphByIndex(0, true);
      TextParagraphElementBase textParaEleBase = paragraph.getOdfElement();
      NodeList nodeImages = textParaEleBase.getElementsByTagName("draw:image");
      Node nodeImage = nodeImages.item(0);
      DrawImageElement im = (DrawImageElement) nodeImage;
      Image ima = Image.getInstanceof(im);
      image = item.replaceWith(ima);
      Assert.assertNotNull(image);
      if (image.getName().startsWith("replace")) {
        Assert.assertTrue(true);
      } else {
        Assert.fail();
      }
      String name = "simple" + (i++);
      image.setName(name);
      Assert.assertEquals(name, image.getName());
    }
    try {
      doc.save(ResourceUtilities.newTestOutputFile("TextSelectionReplacewithImageResult.odt"));
    } catch (Exception e) {
      Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e);
      Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
    }
    doc = (TextDocument) Document.loadDocument(ResourceUtilities.getAbsolutePath(TEXT_FILE));
    // 6 Simple, at the middle of original Paragraph, split original
    // Paragraph, insert before the second Paragraph.
    search = new TextNavigation("SIMPLE", doc);
    i = 0;
    try {
      while (search.hasNext()) {
        item = (TextSelection) search.nextSelection();
        URI imageuri = ResourceUtilities.getURI("image_list_item.png");
        image = item.replaceWith(imageuri);
        Assert.assertNotNull(image);
        if (image.getName().startsWith("replace")) {
          Assert.assertTrue(true);
        } else {
          Assert.fail();
        }
        String name = "simple" + (i++);
        image.setName(name);
        Assert.assertEquals(name, image.getName());
      }
      doc.save(ResourceUtilities.newTestOutputFile("TextSelectionReplacewithImageURIResult.odt"));
    } catch (Exception e) {
      Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e);
      Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
    }
    doc = (TextDocument) Document.loadDocument(ResourceUtilities
        .getAbsolutePath(TEST_FILE));
    search = new TextNavigation("<<target>>", doc);
    i = 0;
    try {
      while (search.hasNext()) {
        i++;
        item = (TextSelection) search.nextSelection();
        URI imageuri = ResourceUtilities.getURI("image_list_item.png");
        image = item.replaceWith(imageuri);
        Assert.assertNotNull(image);
        if (image.getName().startsWith("replace")) {
          Assert.assertTrue(true);
        } else {
          Assert.fail();
        }
      }
      Assert.assertEquals(11, i);
      doc.save(ResourceUtilities
          .newTestOutputFile("TextSelectionReplacewithImageResult-URI-BasicCases.odt"));
      verifyReplaceWithImageByBasicCases("TextSelectionReplacewithImageResult-URI-BasicCases.odt");
    } catch (Exception e) {
      Logger.getLogger(TextSelectionTest.class.getName()).log(
          Level.SEVERE, e.getMessage(), e);
      Assert.fail("Failed with " + e.getClass().getName() + ": '"
          + e.getMessage() + "'");
    }
    doc = (TextDocument) Document.loadDocument(ResourceUtilities
        .getAbsolutePath(TEST_FILE));
    search = new TextNavigation("<<target>>", doc);
    i = 0;
    try {
      while (search.hasNext()) {
        i++;
        item = (TextSelection) search.nextSelection();
View Full Code Here

    sourcedoc.addParagraph("Hello3 from source document!");
   
    search = null;
    // 6 Simple, at the middle of original Paragraph, split original
    // Paragraph, insert before the second Paragraph.
    search = new TextNavigation("SIMPLE", doc);
    int i = 0;
    TextSelection item = null;
    while (search.hasNext()) {
      item = (TextSelection) search.nextSelection();
      Paragraph paragraph = sourcedoc.getParagraphByIndex(0, true);
      item.replaceWith(paragraph);
      i++;
    }
    search = new TextNavigation("Hello1 from SIMPLE source document!", doc);
    int j = 0;
    while (search.hasNext()) {
      search.nextSelection();
      j++;
    }
    Assert.assertTrue(i == j);
   
    // 2 Task1, #1 at the start of original Paragraph, #2 replace original
    // Paragraph
    search = new TextNavigation("Task1", doc);
    i = 0;
    while (search.hasNext()) {
      item = (TextSelection) search.nextSelection();
     
      Paragraph paragraph = sourcedoc.getParagraphByIndex(1, true);
      item.replaceWith(paragraph);
      i++;
    }
    search = new TextNavigation("Hello2 from source document!", doc);
    j = 0;
    while (search.hasNext()) {
      search.nextSelection();
      j++;
    }
    Assert.assertTrue(i == j);
   
    // 1 Container, #1 at the end of original Paragraph,
    search = new TextNavigation("Container", doc);
    i = 0;
    while (search.hasNext()) {
      item = (TextSelection) search.nextSelection();
      Paragraph paragraph = sourcedoc.getParagraphByIndex(2, true);
      item.replaceWith(paragraph);
      i++;
    }
    search = new TextNavigation("Hello3 from source document!", doc);
    j = 0;
    while (search.hasNext()) {
      search.nextSelection();
      j++;
    }
    Assert.assertTrue(i == j);
    try {
      doc.save(ResourceUtilities.newTestOutputFile("TextSelectionReplacewithParagraphResult.odt"));
    } catch (Exception e) {
      Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e);
      Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
    }
    doc = (TextDocument) Document.loadDocument(ResourceUtilities
        .getAbsolutePath(TEST_FILE));
    search = new TextNavigation("<<target>>", doc);
    i = 0;
    try {
      while (search.hasNext()) {
        i++;
        item = (TextSelection) search.nextSelection();
View Full Code Here

    newTextSpanElement = sourcedoc.newParagraph("Show Variable Field:").newTextSpanElement();
    variableField.displayField(newTextSpanElement);
    Field orgField = sourcedoc.getVariableFieldByName("test_simple_variable");
    // 6 Simple, at the middle of original Paragraph, split original
    // Paragraph, insert before the second Paragraph.
    search = new TextNavigation("SIMPLE", doc);
    while (search.hasNext()) {
      TextSelection item = (TextSelection) search.nextSelection();
      try {
        Field newField = item.replaceWith(orgField);
      } catch (InvalidNavigationException e) {
View Full Code Here

    // currtenItem with TextDocument, TextNavigation.nextSelection will
    // search from the inserted Content,
    // it will make you into a loop if the Search keyword also can be found
    // in the inserted Content.
    int i = 0;
    search = new TextNavigation("SIMPLE", doc);
    TextSelection currtenTextSelection, nextTextSelection = null;
    while (search.hasNext()) {
      if (nextTextSelection != null) {
        currtenTextSelection = nextTextSelection;
      } else {
        currtenTextSelection = (TextSelection) search.nextSelection();
      }
      nextTextSelection = (TextSelection) search.nextSelection();
      if (currtenTextSelection != null) {
       
        try {
          currtenTextSelection.replaceWith(sourcedoc);
          i++;
        } catch (Exception e) {
          e.printStackTrace();
          Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
        }
      }
    }
    if (nextTextSelection != null) {
      try {
        nextTextSelection.replaceWith(sourcedoc);
        i++;
      } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
      }
    }
    search = new TextNavigation("Hello1 from SIMPLE source document!", doc);
    int j = 0;
    while (search.hasNext()) {
      search.nextSelection();
      j++;
    }
View Full Code Here

TOP

Related Classes of org.odftoolkit.simple.common.navigation.TextNavigation

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.