TextSelection extends Selection { public void replaceWith(String newText) throws InvalidNavigationException { if (validate() == false) { throw new InvalidNavigationException("No matched string at this position"); } OdfElement parentElement = getContainerElement(); int leftLength = getText().length(); int index = mIndexInContainer; delete(index, leftLength, parentElement); OdfTextSpan textSpan = new OdfTextSpan((OdfFileDom) parentElement.getOwnerDocument()); textSpan.addContentWhitespace(newText); mIsInserted = false; insertOdfElement(textSpan, index, parentElement); optimize(parentElement); int offset = newText.length() - leftLength; SelectionManager.refresh(getContainerElement(), offset, index + getText().length()); mMatchedText = newText; } TextSelection(Navigation search, String text, OdfElement containerElement,
			int index); TextNavigation getTextNavigation(); static TextSelection newTextSelection(Navigation search,
			String text, OdfElement containerElement, int index); @Override OdfElement getElement(); OdfElement getContainerElement(); @Override int getIndex(); String getText(); @Override void cut(); void applyStyle(OdfStyleBase style); void replaceWith(String newText); Table replaceWith(Table newTable); Image replaceWith(Image newImage); Image replaceWith(URI imageUri); Field replaceWith(Field orgField); Paragraph replaceWith(Paragraph newParagraph); void replaceWith(TextDocument newTextDocument); TextSpanElement createSpanElement(); @Override void pasteAtFrontOf(Selection positionItem); @Override void pasteAtEndOf(Selection positionItem); void setSelectionReplaced(boolean b); boolean isSelectionReplaced(); void addHref(URL url); void addComment(String content, String creator); @Override String toString();  }
@Test public void testReplacewith() { search = null; search = new TextNavigation("SIMPLE", doc); TextSelection nextSelect = null; TextNavigation nextsearch = new TextNavigation("next", doc); nextSelect = (TextSelection) nextsearch.nextSelection(); OdfStyle style = new OdfStyle(contentDOM); style.setProperty(StyleTextPropertiesElement.FontWeight, "bold"); style.setStyleFamilyAttribute("text"); int i = 0; while (search.hasNext()) { if (i > 0) { TextSelection item = (TextSelection) search.nextSelection(); try { item.replaceWith("Odf Toolkit"); item.applyStyle(style); } catch (InvalidNavigationException e) { Assert.fail(e.getMessage()); } } i++; } search = new TextNavigation("Odf Toolkit", doc); int j = 0; while (search.hasNext()) { search.nextSelection(); j++; } Assert.assertTrue(i == j); try { nextSelect.replaceWith("bbb"); } catch (InvalidNavigationException e1) { Assert.fail(e1.getMessage()); } try { doc.save(ResourceUtilities.newTestOutputFile(SAVE_FILE_REPLACE)); } catch (Exception e) { Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }
@Test public void testReplacewithTable() throws Exception { doc = (TextDocument) Document.loadDocument(ResourceUtilities.getAbsolutePath(TEXT_FILE)); TextDocument sourcedoc = TextDocument.newTextDocument(); String tablename = "Table1"; int rowcount = 7, columncount = 5; 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; } Table table = Table.newTable(sourcedoc, rowlabels, columnlabels, data); table.setTableName(tablename); String tablename2 = "Table2"; int rowcount2 = 10, columncount2 = 4; double[][] data2 = new double[rowcount2][columncount2]; for (int i = 0; i < rowcount2; i++) { for (int j = 0; j < columncount2; j++) { data2[i][j] = Math.random(); } } String[] rowlabels2 = new String[rowcount2]; for (int i = 0; i < rowcount2; i++) { rowlabels2[i] = "RowHeader" + i; } String[] columnlabels2 = new String[columncount2]; for (int i = 0; i < columncount2; i++) { columnlabels2[i] = "ColumnHeader" + i; } Table table2 = Table.newTable(sourcedoc, rowlabels2, columnlabels2, data2); table2.setTableName(tablename2); String tablename3 = "Table3"; int rownumber3 = 5; int clmnumber3 = 3; Table table1 = Table.newTable(sourcedoc, rownumber3, clmnumber3); table1.setTableName(tablename3); search = null; 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()); } 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()); } 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()); Cell cell = newtable.getCellByPosition(1, 1); Assert.assertEquals("string", cell.getValueType()); } doc.save(ResourceUtilities .newTestOutputFile("TextSelectionReplacewithTableResult-BasicCases.odt")); verifyReplaceWithTableByBasicCases("TextSelectionReplacewithTableResult-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() + "'"); } }
@Test public void testReplacewithImage() throws Exception { TextDocument sourcedoc = TextDocument.newTextDocument(); Paragraph para = sourcedoc.addParagraph("helloImage"); Image image = Image.newImage(para, ResourceUtilities.getURI("image_list_item.png")); image.setName("this image 1"); image.setHyperlink(new URI("http: Paragraph para2 = sourcedoc.addParagraph("helloImage2"); Image image2 = Image.newImage(para2, ResourceUtilities.getURI("testA.jpg")); image2.setName("this image 2"); image2.setHyperlink(new URI("http: search = null; 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)); 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(); } } Assert.assertEquals(11, i); doc.save(ResourceUtilities .newTestOutputFile("TextSelectionReplacewithImageResult-BasicCases.odt")); verifyReplaceWithImageByBasicCases("TextSelectionReplacewithImageResult-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() + "'"); } }
@Test public void testReplacewithField() throws Exception { doc = (TextDocument) Document.loadDocument(ResourceUtilities.getAbsolutePath(TEXT_FILE)); TextDocument sourcedoc = TextDocument.newTextDocument(); sourcedoc.addParagraph("Hello1 from SIMPLE source document!"); sourcedoc.addParagraph("Hello2 from source document!"); sourcedoc.addParagraph("Hello3 from source document!"); VariableField variableField = Fields.createUserVariableField(sourcedoc, "test_simple_variable","testReplacewithField"); Assert.assertNotNull(variableField); TextSpanElement newTextSpanElement = sourcedoc.newParagraph("Update Variable Field:").newTextSpanElement(); variableField.updateField("simple variable content", newTextSpanElement); newTextSpanElement = sourcedoc.newParagraph("Show Variable Field:").newTextSpanElement(); variableField.displayField(newTextSpanElement); Field orgField = sourcedoc.getVariableFieldByName("test_simple_variable"); 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 { doc.save(ResourceUtilities.newTestOutputFile("TextSelectionReplacewithFieldResult.odt")); } catch (Exception e) { Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }
@Test public void testReplacewithTextDocument() throws Exception { doc = (TextDocument) Document.loadDocument(ResourceUtilities.getAbsolutePath(TEXT_FILE)); TextDocument sourcedoc = TextDocument.newTextDocument(); sourcedoc.addParagraph("Hello1 from SIMPLE source document!"); sourcedoc.addParagraph("Hello2 from source document!"); sourcedoc.addParagraph("Hello3 from source document!"); search = null; 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++; } Assert.assertTrue(i == j); try { doc.save(ResourceUtilities.newTestOutputFile("TextSelectionReplacewithTextDocumentResult.odt")); } catch (Exception e) { Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }