Skip to content
Closed
Show file tree
Hide file tree
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
23 changes: 21 additions & 2 deletions exist-core/src/main/java/org/exist/util/DirectoryScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;

public class DirectoryScanner {

Expand Down Expand Up @@ -62,8 +63,26 @@ private final static void scanDir(final List<Path> list, final Path dir, final S
}
}

public final static boolean match(String pattern, String name) {
return SelectorUtils.matchPath(pattern, name);
/**
* Check if the pattern matches the path.
*/
public final static boolean match(final @Nonnull String pattern, final @Nonnull String path) {
return SelectorUtils.matchPath(pattern, path);
}

/**
* Check if any of the patterns matches the path.
* If path starts with File.separator it is removed before the match. (Legacy requirement)
*/
public final static boolean matchAny(final @Nonnull Iterable<String> patterns, final @Nonnull String path) {
final String normalizedPath = path.startsWith(File.separator) ? path.substring(File.separator.length())
: path;
for (final String pattern : patterns) {
if (DirectoryScanner.match(pattern, normalizedPath)) {
return true;
}
}
return false;
}

public final static boolean matchStart(String pattern, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ protected Sequence evalWithCollection(Collection collection, Sequence[] args, Se
String relPath = file.toString().substring(baseDir.toString().length());
final int p = relPath.lastIndexOf(java.io.File.separatorChar);

if (checkExcludes(excludes, relPath)) {
if (DirectoryScanner.matchAny(excludes, relPath)) {
continue;
}

Expand Down Expand Up @@ -205,24 +205,4 @@ protected Sequence evalWithCollection(Collection collection, Sequence[] args, Se

return stored;
}

/**
* Check if path matches any of the exclude patterns.
*/
private static boolean checkExcludes(final List<String> excludes, String path) {
if (excludes == null || excludes.isEmpty()) {
return false;
}
if (path.charAt(0) == java.io.File.separatorChar) {
path = path.substring(1);
}
boolean skip = false;
for (final String exclude : excludes) {
if (DirectoryScanner.match(exclude, path)) {
skip = true;
break;
}
}
return skip;
}
}
29 changes: 29 additions & 0 deletions exist-core/src/main/java/org/exist/xquery/value/Sequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

import javax.annotation.Nullable;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

/**
* This interface represents a sequence as defined in the XPath 2.0 specification.
Expand Down Expand Up @@ -334,4 +336,31 @@ default int getItemCount() {
* @param contextSequence the context sequence
*/
void destroy(XQueryContext context, Sequence contextSequence);

int MAX_ITEM_COUNT_AS_LIST = 100_000;

interface ItemFunction<R> {
R apply(Item i) throws XPathException;
}

/**
* Get all (transformed) items from the sequence as a List
* Caller should take care that item count does not exceed MAX_ITEM_COUNT_AS_LIST
* @param f the transformation to apply on each item
* @return list of (transformed) items
* @throws XPathException
* @throws IllegalArgumentException if the sequence contains more than MAX_ITEM_COUNT_AS_LIST items
*/
default <R> List<R> asList(ItemFunction<R> f) throws XPathException {
long itemCount = getItemCountLong();
if (itemCount > MAX_ITEM_COUNT_AS_LIST) {
throw new IllegalArgumentException("Item count exceeds " + MAX_ITEM_COUNT_AS_LIST);
}

ArrayList<R> res = new ArrayList<>((int)itemCount);
for (final SequenceIterator si = iterate(); si.hasNext(); ) {
res.add(f.apply(si.nextItem()));
}
return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public class FileModule extends AbstractInternalModule
new FunctionDef( FileMove.signatures[0], FileMove.class ),
new FunctionDef( DirectoryCreate.signatures[0], DirectoryCreate.class ),
new FunctionDef( DirectoryCreate.signatures[1], DirectoryCreate.class ),
new FunctionDef( Sync.signature, Sync.class)
new FunctionDef( Sync.signature3, Sync.class),
new FunctionDef( Sync.signature4, Sync.class),
new FunctionDef( Sync.signature5, Sync.class),
};


Expand Down
Loading