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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.odftoolkit.odfdom.incubator.search;

import org.w3c.dom.Node;
import org.odftoolkit.odfdom.pkg.OdfElement;

/**
* Abstract class Navigation used to navigate the document and find the matched element by the user
Expand All @@ -36,12 +37,20 @@ public abstract class Navigation {
public abstract boolean hasNext();
// abstract public void gotoPrevious();

/*
* Return the element from the current matching selection.
* Use hasNext() to navigate to the next element.
*
* @return OdfElement of the current item or null if not element exists.
*/
public abstract OdfElement next();

/**
* get the current Selection result
*
* @return the current Selection result
*/
public abstract Selection getCurrentItem();
public abstract Selection getSelection();

/**
* check if the element match the user defined condition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,16 @@ private int setCurrentTextAndGetIndex(TextSelection selected) {
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);
try {
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);
}
} catch (IndexOutOfBoundsException e) {
// can occur in case the text of the selection was manipulated from the client
return -1;
}
return nextIndex;
}
Expand All @@ -175,7 +180,7 @@ private int setCurrentTextAndGetIndex(TextSelection selected) {
* @see org.odftoolkit.odfdom.incubator.search.Navigation#getCurrentItem()
*/
@Override
public Selection getCurrentItem() {
public Selection getSelection() {
Selection.SelectionManager.registerItem(mCurrentSelectedItem);
return mCurrentSelectedItem;
}
Expand All @@ -189,6 +194,20 @@ public boolean hasNext() {
return (mCurrentSelectedItem != null);
}

/*
* Return the element from the current matching selection.
* Use hasNext() to navigate to the next element.
*
* @return OdfElement of the current item or null if not element exists.
*/
@Override
public OdfElement next() {
if (getSelection()!=null) {
return getSelection().getElement();
}
return null;
}

/**
* check if the text content of element match the specified pattern string
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private Node getPHElement(Node node) {
* @see org.odftoolkit.odfdom.incubator.search.Navigation#getCurrentItem()
*/
@Override
public Selection getCurrentItem() {
public Selection getSelection() {
Selection.SelectionManager.registerItem(mCurrentSelectedItem);
return mCurrentSelectedItem;
}
Expand All @@ -128,6 +128,20 @@ public boolean hasNext() {
return (mCurrentSelectedItem != null);
}

/*
* Return the element from the current matching selection.
* Use hasNext() to navigate to the next element.
*
* @return OdfElement of the current item or null if not element exists.
*/
@Override
public OdfElement next() {
if (getSelection()!=null) {
return getSelection().getElement();
}
return null;
}

/**
* check if the element has the specified style properties
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void testGetInstance() {
// get the text content contains "cell"
TextNavigation search = new TextNavigation("cell", odtdoc);
while (search.hasNext()) {
TextSelection item = (TextSelection) search.getCurrentItem();
TextSelection item = (TextSelection) search.getSelection();
OdfElement containerEle = item.getContainerElement();
if (containerEle instanceof OdfTextParagraph) {
Node ele = containerEle.getParentNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testReplaceWith() {

int i = 0;
while (search.hasNext()) {
TextSelection item = (TextSelection) search.getCurrentItem();
TextSelection item = (TextSelection) search.getSelection();
try {
item.replaceWith("success");
i++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void testGotoNext() {
search = new TextNavigation("delete", doc);

while (search.hasNext()) {
TextSelection item = (TextSelection) search.getCurrentItem();
TextSelection item = (TextSelection) search.getSelection();
LOG.info(item.toString());
}
}
Expand Down Expand Up @@ -111,4 +111,52 @@ public void testGetNextMatchElement() {
Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
}
}



/** Test methods hasNext and next of org.odftoolkit.odfdom.incubator.search.TextNavigation */
@Test
public void testHasNextNext() {
String phrase="";
try {
phrase="<%NAME%>";
search = new TextNavigation(phrase, doc);
while (search.hasNext()) {
TextSelection item = (TextSelection) search.getSelection();
LOG.info(item.toString());
OdfElement element =search.next();

String text=element.getTextContent();
Logger logger = Logger.getLogger(TextNavigationTest.class.getName());
logger.log(Level.INFO," Current Item Text="+text);
element.setTextContent("John Doe");
}


// test the phrase 'ODFDOM' which should occur in 4 paragraphs
phrase="ODFDOM";
int countParagraphs=0;
search = new TextNavigation(phrase, doc);
while (search.hasNext()) {

TextSelection item = (TextSelection) search.getSelection();
LOG.info(item.toString());

OdfElement element = search.next();
String text=element.getTextContent();
Logger logger = Logger.getLogger(TextNavigationTest.class.getName());
logger.log(Level.INFO," Current Item Text="+text);
text=text.replace(phrase, "Software Project");
element.setTextContent(text);
countParagraphs++;

}
Assert.assertEquals(4,countParagraphs);

} catch (Exception e) {
Logger.getLogger(TextNavigationTest.class.getName()).log(Level.SEVERE, e.getMessage(), e);
Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ public void testCut() {
TextSelection nextSelect = null;
TextNavigation nextsearch = new TextNavigation("next", doc);
if (nextsearch.hasNext()) {
nextSelect = (TextSelection) nextsearch.getCurrentItem();
nextSelect = (TextSelection) nextsearch.getSelection();
}
int i = 0;
while (search.hasNext()) {
TextSelection item = (TextSelection) search.getCurrentItem();
TextSelection item = (TextSelection) search.getSelection();
i++;
try {
item.cut();
Expand Down Expand Up @@ -140,12 +140,12 @@ public void testPasteAtFrontOf() {

TextNavigation search1 = new TextNavigation("change", doc);
if (search1.hasNext()) {
sel = (TextSelection) search1.getCurrentItem();
sel = (TextSelection) search1.getSelection();
}

int i = 0;
while (search.hasNext()) {
TextSelection item = (TextSelection) search.getCurrentItem();
TextSelection item = (TextSelection) search.getSelection();
i++;
try {
sel.pasteAtFrontOf(item);
Expand Down Expand Up @@ -181,12 +181,12 @@ public void testPasteAtEndOf() {

TextNavigation search1 = new TextNavigation("change", doc);
if (search1.hasNext()) {
sel = (TextSelection) search1.getCurrentItem();
sel = (TextSelection) search1.getSelection();
}

int i = 0;
while (search.hasNext()) {
TextSelection item = (TextSelection) search.getCurrentItem();
TextSelection item = (TextSelection) search.getSelection();
i++;
try {
sel.pasteAtEndOf(item);
Expand Down Expand Up @@ -228,7 +228,7 @@ public void testApplyStyle() {
Assert.assertNotNull(style);

while (search.hasNext()) {
TextSelection item = (TextSelection) search.getCurrentItem();
TextSelection item = (TextSelection) search.getSelection();
try {
item.applyStyle(style);
} catch (InvalidNavigationException e) {
Expand Down Expand Up @@ -256,7 +256,7 @@ public void testReplacewith() {
TextSelection nextSelect = null;
TextNavigation nextsearch = new TextNavigation("next", doc);
if (nextsearch.hasNext()) {
nextSelect = (TextSelection) nextsearch.getCurrentItem();
nextSelect = (TextSelection) nextsearch.getSelection();
}

// replace all the "ODFDOM" to "Odf Toolkit"
Expand All @@ -267,7 +267,7 @@ public void testReplacewith() {
int i = 0;
while (search.hasNext()) {
if (i > 0) {
TextSelection item = (TextSelection) search.getCurrentItem();
TextSelection item = (TextSelection) search.getSelection();
try {
item.replaceWith("Odf Toolkit");
item.applyStyle(style);
Expand Down Expand Up @@ -314,7 +314,7 @@ public void testReplacewithMultispace() {
navigations.forEach(n -> assertTrue("Navigation " + n + " should have a next", n.hasNext()));
final List<TextSelection> selections =
navigations.stream()
.map(n -> (TextSelection) n.getCurrentItem())
.map(n -> (TextSelection) n.getSelection())
.collect(Collectors.toList());
try {
selections.get(0).replaceWith("Xmultiple___X");
Expand Down Expand Up @@ -346,7 +346,7 @@ public void testAddHref() {
search = null;
search = new TextNavigation("^delete", doc);
while (search.hasNext()) {
TextSelection item = (TextSelection) search.getCurrentItem();
TextSelection item = (TextSelection) search.getSelection();
// LOG.info(item);
try {
item.addHref(new URL("http://www.ibm.com"));
Expand Down Expand Up @@ -375,7 +375,7 @@ public void testCutPattern() {
search = new TextNavigation("<%([^>]*)%>", doc);

while (search.hasNext()) {
TextSelection item = (TextSelection) search.getCurrentItem();
TextSelection item = (TextSelection) search.getSelection();
try {
String text = item.getText();
text = text.substring(2, text.length() - 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ public void testPasteAtFrontOf() {

TextSelection itemstyle = null;
if (search1.hasNext()) {
itemstyle = (TextSelection) search1.getCurrentItem();
itemstyle = (TextSelection) search1.getSelection();
LOG.info(itemstyle.toString());
}
int i = 0;
if (itemstyle != null) {
while (search2.hasNext()) {
i++;
TextSelection itemtext = (TextSelection) search2.getCurrentItem();
TextSelection itemtext = (TextSelection) search2.getSelection();
try {
itemstyle.pasteAtFrontOf(itemtext);
} catch (InvalidNavigationException e) {
Expand Down Expand Up @@ -137,14 +137,14 @@ public void testPasteAtEndOf() {
search3 = new TextNavigation("deleteRoman16 Romanl16", doc);
TextSelection itemstyle = null;
if (search1.hasNext()) {
itemstyle = (TextSelection) search1.getCurrentItem();
itemstyle = (TextSelection) search1.getSelection();
LOG.info(itemstyle.toString());
}
int i = 0;
if (itemstyle != null) {
while (search2.hasNext()) {
i++;
TextSelection itemtext = (TextSelection) search2.getCurrentItem();
TextSelection itemtext = (TextSelection) search2.getSelection();
try {
itemstyle.pasteAtEndOf(itemtext);
} catch (InvalidNavigationException e) {
Expand Down Expand Up @@ -181,7 +181,7 @@ public void testCut() {
search2 = new TextNavigation("Century22", doc);

while (search1.hasNext()) {
TextSelection item = (TextSelection) search1.getCurrentItem();
TextSelection item = (TextSelection) search1.getSelection();
try {
item.cut();
} catch (InvalidNavigationException e) {
Expand Down Expand Up @@ -222,7 +222,7 @@ public void testApplyStyle() {
int i = 0;
while (search1.hasNext()) {
i++;
TextSelection item = (TextSelection) search1.getCurrentItem();
TextSelection item = (TextSelection) search1.getSelection();
// LOG.info(item);
try {
item.applyStyle(style);
Expand Down