@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() + "'"); } }
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 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 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); }
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(); }
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();  }