Package org.odftoolkit.simple.common.navigation

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


  public void testSpan() {
    try {
      TextDocument doc = TextDocument.newTextDocument();
      doc.addParagraph("This is a test paragraph!");
      TextNavigation navigation = new TextNavigation("test", doc);
      TextSelection sel = (TextSelection) navigation.nextSelection();
      Span span = Span.newSpan(sel);
      TextHyperlink link = span.applyHyperlink(new URI("http://www.ibm.com"));
      DefaultStyleHandler handler = span.getStyleHandler();
      Font font1Base = new Font("Arial", FontStyle.ITALIC, 10, Color.BLACK, TextLinePosition.THROUGH);
      handler.getTextPropertiesForWrite().setFont(font1Base);
View Full Code Here


      }
     
      doc.addParagraph("This is a test paragraph!");
     
      TextNavigation navigation = new TextNavigation("test", doc);
      TextSelection sel = (TextSelection) navigation.nextSelection();
      Span span = Span.newSpan(sel);
     
      span.removeTextContent();
      boolean flag = false;
      Iterator<Paragraph> parai = doc.getParagraphIterator();
View Full Code Here

    try {
      TextDocument doc = TextDocument.newTextDocument();
      doc.addParagraph("This is a test paragraph!");
     
      TextNavigation navigation = new TextNavigation("test", doc);
      TextSelection sel = (TextSelection) navigation.nextSelection();
      Span span = Span.newSpan(sel);
     
      span.appendTextContent("hello world.");
      Assert.assertEquals("testhello world.", span.getTextContent());
View Full Code Here

    try {
      TextDocument doc = TextDocument.newTextDocument();
      doc.addParagraph("This is a test paragraph!");
     
      TextNavigation navigation = new TextNavigation("test", doc);
      TextSelection sel = (TextSelection) navigation.nextSelection();
      Span span = Span.newSpan(sel);
     
      span.appendTextContent("hello world.", true);
      Assert.assertEquals("testhello world.", span.getTextContent());
     
View Full Code Here

    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();
        table = sourcedoc.getTableByName("Table1");
        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());
View Full Code Here

    // 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();
        image = item.replaceWith(image);
        Assert.assertNotNull(image);
        if (image.getName().startsWith("replace")) {
          Assert.assertTrue(true);
        } else {
          Assert.fail();
View Full Code Here

    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();
        Paragraph paragraph = sourcedoc.getParagraphByIndex(1, true);
        paragraph = item.replaceWith(paragraph);
        Assert.assertNotNull(paragraph);
        Assert.assertEquals(
            TextExtractor.getText(paragraph.getOdfElement()),
            "Hello2 from source document!");
      }
View Full Code Here

    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) {
        e.printStackTrace();
      }
    }
    try {
View Full Code Here

    // 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() + "'");
        }
View Full Code Here

  @Test
  public void testGetInstance() {
    //get the text content contains "cell"
    TextNavigation search = new TextNavigation("cell", odtdoc);
    while (search.hasNext()) {
      TextSelection item = (TextSelection) search.nextSelection();
      OdfElement containerEle = item.getContainerElement();
      if (containerEle instanceof OdfTextParagraph) {
        Node ele = containerEle.getParentNode();
        if (ele instanceof TableTableCellElement) {
          Cell cell = Cell.getInstance((TableTableCellElementBase) ele);
          Assert.assertTrue(cell.getStringValue().contains("cell"));
View Full Code Here

TOP

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

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.