diff --git a/odfdom/src/main/java/org/odftoolkit/odfdom/incubator/search/TextNavigation.java b/odfdom/src/main/java/org/odftoolkit/odfdom/incubator/search/TextNavigation.java index 9e90d27a64..82611c9127 100644 --- a/odfdom/src/main/java/org/odftoolkit/odfdom/incubator/search/TextNavigation.java +++ b/odfdom/src/main/java/org/odftoolkit/odfdom/incubator/search/TextNavigation.java @@ -36,9 +36,9 @@ */ public class TextNavigation extends Navigation { - private String mMatchedElementName = "text:p,text:h"; - private String mPatternText; - private OdfTextDocument mTextDocument; + private static final String mMatchedElementName = "text:p,text:h"; + private final Pattern mPattern; + private final OdfTextDocument mTextDocument; private TextSelection mCurrentSelectedItem; private String mCurrentText; private int mCurrentIndex; @@ -51,10 +51,19 @@ public class TextNavigation extends Navigation { * @param doc the navigation scope */ public TextNavigation(String pattern, OdfTextDocument doc) { - this.mPatternText = pattern; - mTextDocument = doc; - mCurrentSelectedItem = null; - mbFinishFindInHeaderFooter = false; + this(Pattern.compile(pattern), doc); + } + + /** + * Construct TextNavigation with matched condition and navigation scope + * @param pattern the Pattern object to search with + * @param doc the navigation scope + */ + public TextNavigation(Pattern pattern, OdfTextDocument doc){ + this.mPattern = pattern; + mTextDocument = doc; + mCurrentSelectedItem = null; + mbFinishFindInHeaderFooter = false; } // the matched text might exist in header/footer @@ -64,21 +73,7 @@ private TextSelection findInHeaderFooter(TextSelection selected) { OdfElement element = null; if (selected != null) { - OdfElement containerElement = selected.getContainerElement(); - int index = selected.getIndex(); - OdfWhitespaceProcessor textProcessor = new OdfWhitespaceProcessor(); - String content = textProcessor.getText(containerElement); - - int nextIndex = -1; - Pattern pattern = Pattern.compile(mPatternText); - Matcher matcher = pattern.matcher(content); - // start from the end index of the selected item - if (matcher.find(index + selected.getText().length())) { - // here just consider \n\r\t occupy one char - nextIndex = matcher.start(); - int eIndex = matcher.end(); - mCurrentText = content.substring(nextIndex, eIndex); - } + int nextIndex = setCurrentTextAndGetIndex(selected); if (nextIndex != -1) { TextSelection item = new TextSelection(mCurrentText, selected.getContainerElement(), nextIndex); @@ -143,26 +138,13 @@ private TextSelection findnext(TextSelection selected) { } OdfElement containerElement = selected.getContainerElement(); - int index = selected.getIndex(); - OdfWhitespaceProcessor textProcessor = new OdfWhitespaceProcessor(); - String content = textProcessor.getText(containerElement); - - int nextIndex = -1; - Pattern pattern = Pattern.compile(mPatternText); - Matcher matcher = pattern.matcher(content); - // start from the end index of the selected item - if (matcher.find(index + selected.getText().length())) { - // here just consider \n\r\t occupy one char - nextIndex = matcher.start(); - int eIndex = matcher.end(); - mCurrentText = content.substring(nextIndex, eIndex); - } + int nextIndex = setCurrentTextAndGetIndex(selected); if (nextIndex != -1) { TextSelection item = - new TextSelection(mCurrentText, selected.getContainerElement(), nextIndex); + new TextSelection(mCurrentText, containerElement, nextIndex); return item; } else { - OdfElement element = (OdfElement) getNextMatchElement((Node) containerElement); + OdfElement element = (OdfElement) getNextMatchElement(containerElement); if (element != null) { TextSelection item = new TextSelection(mCurrentText, element, mCurrentIndex); return item; @@ -172,6 +154,23 @@ private TextSelection findnext(TextSelection selected) { } } + private int setCurrentTextAndGetIndex(TextSelection selected){ + int index = selected.getIndex(); + OdfWhitespaceProcessor textProcessor = new OdfWhitespaceProcessor(); + String content = textProcessor.getText(selected.getContainerElement()); + + int nextIndex = -1; + Matcher matcher = mPattern.matcher(content); + // start from the end index of the selected item + if (matcher.find(index + selected.getText().length())) { + // here just consider \n\r\t occupy one char + nextIndex = matcher.start(); + int eIndex = matcher.end(); + mCurrentText = content.substring(nextIndex, eIndex); + } + return nextIndex; + } + /* (non-Javadoc) * @see org.odftoolkit.odfdom.incubator.search.Navigation#getCurrentItem() */ @@ -199,13 +198,12 @@ public boolean hasNext() { @Override public boolean match(Node element) { if (element instanceof OdfElement) { - if (mMatchedElementName.indexOf(element.getNodeName()) != -1) { + if (mMatchedElementName.contains(element.getNodeName())) { OdfWhitespaceProcessor textProcessor = new OdfWhitespaceProcessor(); String content = textProcessor.getText(element); - Pattern pattern = Pattern.compile(mPatternText); - Matcher matcher = pattern.matcher(content); - while (matcher.find()) { + Matcher matcher = mPattern.matcher(content); + if (matcher.find()) { // here just consider \n\r\t occupy one char mCurrentIndex = matcher.start(); int eIndex = matcher.end();