Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved navigation interface, added next() method #236

Merged
merged 2 commits into from Jun 23, 2023
Merged

Improved navigation interface, added next() method #236

merged 2 commits into from Jun 23, 2023

Conversation

rsoika
Copy link
Collaborator

@rsoika rsoika commented Jun 22, 2023

This is a smaller improvement of the org.odftoolkit.odfdom.incubator.search.Navigation Interface.

Using this feature in my own project I recognized that the TextNavigation was not working correctly - especially in case the search pattern occur multiple times in one document or paragraph.

The following example shows how to replace all matches of a regex with a text using the new implementation:

	private void replaceODFTextFragment(OdfTextDocument doc, String pattern, String replace)
			throws InvalidNavigationException {
		try {			
			TextNavigation searchPattern = new TextNavigation(pattern, doc);
			while (searchPattern.hasNext()) {
				OdfElement element = searchPattern.next(); //.getCurrentItem().getElement();
				String text=element.getTextContent();
				logger.info(" Current Item Text="+text);
				element.setTextContent(replace);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

The method TextNavigation.next() is new and a convenient method for getCurrentItem().getElement();

An important bugfix is in the method setCurrentTextAndGetIndex. This method runs in a outOfBounds Exception in case the pattern length was equals to the replacement text length. This is now also fixed.

I have tested the new code in my own project and it works now perfect - even with text in embedded table cells.

Copy link
Contributor

@svanteschubert svanteschubert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, Ralph, for this patch fixing the search functionality.
As it was broken earlier the renaming (API change) should not matter to me.

@svanteschubert svanteschubert linked an issue Jun 23, 2023 that may be closed by this pull request
@svanteschubert svanteschubert merged commit edda484 into tdf:master Jun 23, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to replace paragraph in ODF Text Document
2 participants