From 91d17d5b9b575202019996b4bf111e088b88aa97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20T=C3=A2che?= Date: Mon, 18 Jan 2021 11:13:52 +0100 Subject: [PATCH 1/2] Adding Pattern constructor in TextNavigation and compiling pattern only once --- .../incubator/search/TextNavigation.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) 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..61b39a738a 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 @@ -37,7 +37,7 @@ public class TextNavigation extends Navigation { private String mMatchedElementName = "text:p,text:h"; - private String mPatternText; + private Pattern mPattern; private OdfTextDocument mTextDocument; private TextSelection mCurrentSelectedItem; private String mCurrentText; @@ -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 @@ -70,8 +79,7 @@ private TextSelection findInHeaderFooter(TextSelection selected) { String content = textProcessor.getText(containerElement); int nextIndex = -1; - Pattern pattern = Pattern.compile(mPatternText); - Matcher matcher = pattern.matcher(content); + 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 @@ -148,8 +156,7 @@ private TextSelection findnext(TextSelection selected) { String content = textProcessor.getText(containerElement); int nextIndex = -1; - Pattern pattern = Pattern.compile(mPatternText); - Matcher matcher = pattern.matcher(content); + 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 @@ -203,9 +210,8 @@ public boolean match(Node element) { 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(); From 40b5be40afb8da0fb6ce3ed780c17a2ec5aea039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20T=C3=A2che?= Date: Mon, 18 Jan 2021 11:30:15 +0100 Subject: [PATCH 2/2] TextNavigation cleanup --- .../incubator/search/TextNavigation.java | 56 ++++++++----------- 1 file changed, 24 insertions(+), 32 deletions(-) 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 61b39a738a..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 Pattern mPattern; - 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; @@ -73,20 +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; - 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); - } + int nextIndex = setCurrentTextAndGetIndex(selected); if (nextIndex != -1) { TextSelection item = new TextSelection(mCurrentText, selected.getContainerElement(), nextIndex); @@ -151,9 +138,26 @@ private TextSelection findnext(TextSelection selected) { } OdfElement containerElement = selected.getContainerElement(); + int nextIndex = setCurrentTextAndGetIndex(selected); + if (nextIndex != -1) { + TextSelection item = + new TextSelection(mCurrentText, containerElement, nextIndex); + return item; + } else { + OdfElement element = (OdfElement) getNextMatchElement(containerElement); + if (element != null) { + TextSelection item = new TextSelection(mCurrentText, element, mCurrentIndex); + return item; + } else { + return null; + } + } + } + + private int setCurrentTextAndGetIndex(TextSelection selected){ int index = selected.getIndex(); OdfWhitespaceProcessor textProcessor = new OdfWhitespaceProcessor(); - String content = textProcessor.getText(containerElement); + String content = textProcessor.getText(selected.getContainerElement()); int nextIndex = -1; Matcher matcher = mPattern.matcher(content); @@ -164,19 +168,7 @@ private TextSelection findnext(TextSelection selected) { int eIndex = matcher.end(); mCurrentText = content.substring(nextIndex, eIndex); } - if (nextIndex != -1) { - TextSelection item = - new TextSelection(mCurrentText, selected.getContainerElement(), nextIndex); - return item; - } else { - OdfElement element = (OdfElement) getNextMatchElement((Node) containerElement); - if (element != null) { - TextSelection item = new TextSelection(mCurrentText, element, mCurrentIndex); - return item; - } else { - return null; - } - } + return nextIndex; } /* (non-Javadoc) @@ -206,7 +198,7 @@ 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);