use of org.gephi.datalab.api.SearchReplaceController.SearchResult in project gephi by gephi.
the class SearchReplaceControllerImpl method matchRegex.
private SearchResult matchRegex(Object value, SearchOptions searchOptions, int rowIndex, int columnIndex, TimeFormat timeFormat, DateTimeZone timeZone) {
boolean found;
String str = value != null ? AttributeUtils.print(value, timeFormat, timeZone) : "";
Matcher matcher = searchOptions.getRegexPattern().matcher(str);
if (str.isEmpty()) {
if (searchOptions.getRegionStart() > 0) {
return null;
}
} else if (searchOptions.getRegionStart() >= str.length()) {
//No more to search in this value, go to next
return null;
}
if (searchOptions.isOnlyMatchWholeAttributeValue()) {
//Try to match the whole value
found = matcher.matches();
} else {
//Try to match a group in the remaining part of the value
matcher.region(searchOptions.getRegionStart(), str.length());
found = matcher.find();
}
if (found) {
//For next search
searchOptions.setStartingRow(rowIndex);
//For next search
searchOptions.setStartingColumn(columnIndex);
int end = matcher.end();
if (matcher.start() == end && !str.isEmpty()) {
//Do not match empty string in not empty values
return null;
}
if (str.isEmpty()) {
//To be able to search on next values when the value matched is empty
end++;
}
//Start next search after this match in this value. (If it is greater than the length of the value, it will be discarded at the beginning of this method next time)
searchOptions.setRegionStart(end);
//Set node or edge values later
return new SearchResult(searchOptions, null, null, rowIndex, columnIndex, matcher.start(), matcher.end());
} else {
return null;
}
}
use of org.gephi.datalab.api.SearchReplaceController.SearchResult in project gephi by gephi.
the class SearchReplaceControllerImpl method replaceAll.
@Override
public int replaceAll(SearchOptions searchOptions, String replacement) {
int replacementsCount = 0;
searchOptions.resetStatus();
//To avoid infinite loop when the replacement parse makes it to match again.
searchOptions.setLoopToBeginning(false);
SearchResult result;
result = findNext(searchOptions);
while (result != null) {
if (canReplace(result)) {
result = replace(result, replacement);
replacementsCount++;
} else {
result = findNext(searchOptions);
}
}
//Restore loop behaviour
searchOptions.setLoopToBeginning(true);
return replacementsCount;
}
use of org.gephi.datalab.api.SearchReplaceController.SearchResult in project gephi by gephi.
the class SearchReplaceControllerImpl method findNext.
@Override
public SearchResult findNext(SearchOptions searchOptions) {
int row = 0;
int column = 0;
if (searchOptions.getStartingRow() != null) {
row = searchOptions.getStartingRow();
}
if (searchOptions.getStartingColumn() != null) {
column = searchOptions.getStartingColumn();
}
SearchResult result = null;
if (searchOptions.isSearchNodes()) {
result = findOnNodes(searchOptions, row, column);
if (result == null && searchOptions.isLoopToBeginning()) {
searchOptions.resetStatus();
//If the end of data is reached with no success, try to search again from the beginning as a loop
return findOnNodes(searchOptions, 0, 0);
} else {
return result;
}
} else {
result = findOnEdges(searchOptions, row, column);
if (result == null && searchOptions.isLoopToBeginning()) {
searchOptions.resetStatus();
//If the end of data is reached with no success, try to search again from the beginning as a loop
return findOnEdges(searchOptions, 0, 0);
} else {
return result;
}
}
}
use of org.gephi.datalab.api.SearchReplaceController.SearchResult in project gephi by gephi.
the class SearchReplaceControllerImpl method findOnNodes.
private SearchResult findOnNodes(SearchOptions searchOptions, int rowIndex, int columnIndex) {
GraphElementsController gec = Lookup.getDefault().lookup(GraphElementsController.class);
SearchResult result = null;
Set<Integer> columnsToSearch = searchOptions.getColumnsToSearch();
boolean searchAllColumns = columnsToSearch.isEmpty();
Table table = Lookup.getDefault().lookup(GraphController.class).getGraphModel().getNodeTable();
Node[] nodes = searchOptions.getNodesToSearch();
Node row;
Column column;
Object value;
TimeFormat timeFormat = table.getGraph().getModel().getTimeFormat();
DateTimeZone timeZone = table.getGraph().getModel().getTimeZone();
for (; rowIndex < nodes.length; rowIndex++) {
if (!gec.isNodeInGraph(nodes[rowIndex])) {
//Make sure node is still in graph when continuing a search
continue;
}
row = nodes[rowIndex];
for (; columnIndex < table.countColumns(); columnIndex++) {
if (searchAllColumns || columnsToSearch.contains(columnIndex)) {
column = table.getColumn(columnIndex);
value = row.getAttribute(column);
result = matchRegex(value, searchOptions, rowIndex, columnIndex, timeFormat, timeZone);
if (result != null) {
result.setFoundNode(nodes[rowIndex]);
return result;
}
}
//Start at the beginning for the next value
searchOptions.setRegionStart(0);
}
//Start at the beginning for the next value
searchOptions.setRegionStart(0);
//Start at the first column for the next row
columnIndex = 0;
}
return result;
}
use of org.gephi.datalab.api.SearchReplaceController.SearchResult in project gephi by gephi.
the class SearchReplaceControllerImpl method findOnEdges.
private SearchResult findOnEdges(SearchOptions searchOptions, int rowIndex, int columnIndex) {
GraphElementsController gec = Lookup.getDefault().lookup(GraphElementsController.class);
SearchResult result = null;
Set<Integer> columnsToSearch = searchOptions.getColumnsToSearch();
boolean searchAllColumns = columnsToSearch.isEmpty();
Table table = Lookup.getDefault().lookup(GraphController.class).getGraphModel().getEdgeTable();
Edge[] edges = searchOptions.getEdgesToSearch();
Edge row;
Column column;
Object value;
TimeFormat timeFormat = table.getGraph().getModel().getTimeFormat();
DateTimeZone timeZone = table.getGraph().getModel().getTimeZone();
for (; rowIndex < edges.length; rowIndex++) {
if (!gec.isEdgeInGraph(edges[rowIndex])) {
//Make sure edge is still in graph when continuing a search
continue;
}
row = edges[rowIndex];
for (; columnIndex < table.countColumns(); columnIndex++) {
if (searchAllColumns || columnsToSearch.contains(columnIndex)) {
column = table.getColumn(columnIndex);
value = row.getAttribute(column);
result = matchRegex(value, searchOptions, rowIndex, columnIndex, timeFormat, timeZone);
if (result != null) {
result.setFoundEdge(edges[rowIndex]);
return result;
}
}
//Start at the beginning for the next value
searchOptions.setRegionStart(0);
}
//Start at the beginning for the next value
searchOptions.setRegionStart(0);
//Start at the first column for the next row
columnIndex = 0;
}
return result;
}
Aggregations