javaodfodftoolkit

ODF Toolkit TextNavigation can't find string containing a special character (dollar sign)


I am trying to find a placeholder in an ODT-Document. Therefor I am using the Apache ODF Toolkit - Simple API.

I created an odt-file containing the text $TestString. The following code is supposed to find it:

Document doc = TextDocument.loadDocument("path/to/test.odt");
TextNavigation search = new TextNavigation("$TestString", doc);
while (search.hasNext()) {
    TextSelection item = (TextSelection) search.nextSelection();
    System.out.println(item);
}

My Problem is, that this search doesn't find my String. But it works when I remove the $ from the TextNavigation (new TextNavigation("TestString", doc)).

The code then returns the output:

[TestString] started from 1 in paragraph:$TestString

What causes this error?


Solution

  • It seems like the Problem was, that I have to escape the $-Symbol because it is used as a Regex (Check this question for more detail).

    So changing the code to

    TextNavigation search = new TextNavigation("\\$TestString", templateDoc);
    

    solves the problem