Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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()
*/
Expand Down Expand Up @@ -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();
Expand Down