Skip to content

Commit 138abb9

Browse files
[MOD] QT4TS: Adaptations for file nodule
1 parent 1bab3bc commit 138abb9

File tree

12 files changed

+83
-22
lines changed

12 files changed

+83
-22
lines changed

basex-core/src/main/java/org/basex/query/QueryResources.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.basex.query.QueryError.*;
44

55
import java.io.*;
6+
import java.nio.file.*;
67
import java.util.*;
78

89
import org.basex.build.*;
@@ -56,6 +57,9 @@ public final class QueryResources {
5657
/** Input references. */
5758
private final ArrayList<InputStream> inputs = new ArrayList<>(1);
5859

60+
/** Current directory for file operations (can be {@null}). */
61+
public Path currentDir;
62+
5963
/**
6064
* Constructor.
6165
* @param qc query context
@@ -404,6 +408,14 @@ public void ftmaps(final HashMap<String, IO> sw, final HashMap<String, IO> th) {
404408
thes = th;
405409
}
406410

411+
/**
412+
* Assigns a sandpit directory.
413+
* @param path path to assign
414+
*/
415+
public void sandpit(final Path path) {
416+
currentDir = path;
417+
}
418+
407419
// PRIVATE METHODS ==============================================================================
408420

409421
/**

basex-core/src/main/java/org/basex/query/func/StandardFunc.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,18 +463,21 @@ protected final Collation toCollation(final byte[] collation, final QueryContext
463463
* @throws QueryException query exception
464464
*/
465465
protected final Path toPath(final Expr expr, final QueryContext qc) throws QueryException {
466-
return toPath(toString(expr, qc));
466+
return toPath(toString(expr, qc), qc);
467467
}
468468

469469
/**
470470
* Converts a path to a file path.
471471
* @param path path string
472+
* @param qc query context
472473
* @return file path
473474
* @throws QueryException query exception
474475
*/
475-
protected final Path toPath(final String path) throws QueryException {
476+
protected final Path toPath(final String path, final QueryContext qc) throws QueryException {
476477
try {
477-
return path.startsWith(IO.FILEPREF) ? Paths.get(new URI(path)) : Paths.get(path);
478+
final Path p = path.startsWith(IO.FILEPREF) ? Paths.get(new URI(path)) : Paths.get(path);
479+
final Path cd = qc.resources.currentDir;
480+
return cd != null ? cd.resolve(p) : p;
478481
} catch(final IllegalArgumentException | URISyntaxException ex) {
479482
Util.debug(ex);
480483
throw FILE_INVALID_PATH_X.get(info, path);

basex-core/src/main/java/org/basex/query/func/file/FileCopy.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.*;
66
import java.nio.file.*;
77

8+
import org.basex.core.jobs.*;
89
import org.basex.query.*;
910
import org.basex.query.value.*;
1011
import org.basex.query.value.seq.*;
@@ -59,18 +60,18 @@ final void relocate(final boolean copy, final QueryContext qc)
5960
* @param src source path
6061
* @param trg target path
6162
* @param copy copy flag
62-
* @param qc query context
63+
* @param job job
6364
* @throws IOException I/O exception
6465
*/
65-
private static void relocate(final Path src, final Path trg, final boolean copy,
66-
final QueryContext qc) throws IOException {
66+
public static void relocate(final Path src, final Path trg, final boolean copy, final Job job)
67+
throws IOException {
6768

6869
if(Files.isDirectory(src)) {
6970
if(!Files.exists(trg)) Files.createDirectory(trg);
7071
try(DirectoryStream<Path> children = Files.newDirectoryStream(src)) {
71-
qc.checkStop();
72+
job.checkStop();
7273
for(final Path child : children) {
73-
relocate(child, trg.resolve(child.getFileName()), copy, qc);
74+
relocate(child, trg.resolve(child.getFileName()), copy, job);
7475
}
7576
}
7677
if(!copy) Files.delete(src);

basex-core/src/main/java/org/basex/query/func/file/FileCreateTempFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final Str createTemp(final boolean directory, final QueryContext qc)
3737
final String prefix = toStringOrNull(arg(0), qc);
3838
final String suffix = toStringOrNull(arg(1), qc);
3939
final String dir = toStringOrNull(arg(2), qc);
40-
final Path root = dir != null ? toPath(dir) : Paths.get(Prop.TEMPDIR);
40+
final Path root = dir != null ? toPath(dir, qc) : Paths.get(Prop.TEMPDIR);
4141

4242
if(Files.isRegularFile(root)) throw FILE_NO_DIR_X.get(info, root);
4343

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.basex.query.func.file;
22

3-
import java.nio.file.*;
4-
53
import org.basex.query.*;
64
import org.basex.query.value.*;
75

@@ -13,7 +11,7 @@
1311
*/
1412
public final class FileCurrentDir extends FileFn {
1513
@Override
16-
public Value eval(final QueryContext qc) {
17-
return get(absolute(Paths.get(".")), true);
14+
public Value eval(final QueryContext qc) throws QueryException {
15+
return get(absolute(toPath(".", qc)), true);
1816
}
1917
}

basex-core/src/main/java/org/basex/query/func/file/FileDelete.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.*;
44
import java.nio.file.*;
55

6+
import org.basex.core.jobs.*;
67
import org.basex.query.*;
78
import org.basex.query.value.*;
89
import org.basex.query.value.seq.*;
@@ -30,15 +31,15 @@ public Value eval(final QueryContext qc) throws QueryException, IOException {
3031
/**
3132
* Recursively deletes a file path.
3233
* @param path path to be deleted
33-
* @param qc query context
34+
* @param job job
3435
* @throws IOException I/O exception
3536
*/
36-
private static void delete(final Path path, final QueryContext qc) throws IOException {
37+
public static void delete(final Path path, final Job job) throws IOException {
3738
if(Files.isDirectory(path)) {
3839
try(DirectoryStream<Path> children = Files.newDirectoryStream(path)) {
3940
for(final Path child : children) {
40-
qc.checkStop();
41-
delete(child, qc);
41+
job.checkStop();
42+
delete(child, job);
4243
}
4344
}
4445
}

basex-core/src/main/java/org/basex/query/func/file/FileResolvePath.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
public final class FileResolvePath extends FileFn {
1818
@Override
1919
public Value eval(final QueryContext qc) throws QueryException {
20-
final Path path = toPath(toString(arg(0), qc)), abs;
20+
final Path path = toPath(toString(arg(0), qc), qc), abs;
2121
final String base = toStringOrNull(arg(1), qc);
2222
if(base != null) {
23-
Path bs = toPath(base);
23+
Path bs = toPath(base, qc);
2424
if(!bs.isAbsolute()) throw FILE_IS_RELATIVE_X.get(info, bs);
2525
if(!endsWith(base, '/') && !endsWith(base, '\\')) bs = bs.getParent();
2626
abs = bs.resolve(path).normalize();

basex-core/src/main/java/org/basex/query/func/proc/ProcFn.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ final ProcResult exec(final QueryContext qc, final boolean fork) throws QueryExc
5959
final ProcResult result = new ProcResult();
6060
final Process proc;
6161
final ProcessBuilder pb = new ProcessBuilder(args.finish());
62-
if(dir != null) pb.directory(toPath(dir).toFile());
62+
if(dir != null) pb.directory(toPath(dir, qc).toFile());
6363
if(!env.isEmpty()) {
6464
pb.environment().clear();
6565
pb.environment().putAll(env);

basex-tests/src/main/java/org/basex/tests/bxapi/XQuery.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package org.basex.tests.bxapi;
22

33
import java.io.*;
4+
import java.nio.file.*;
45
import java.util.*;
56

67
import javax.xml.namespace.*;
78

89
import org.basex.core.*;
10+
import org.basex.core.jobs.*;
911
import org.basex.query.*;
12+
import org.basex.query.func.file.*;
1013
import org.basex.query.iter.*;
1114
import org.basex.query.util.format.*;
1215
import org.basex.query.value.*;
@@ -185,6 +188,25 @@ public XQuery baseURI(final String base) {
185188
return this;
186189
}
187190

191+
/**
192+
* Prepares a sandpit.
193+
* @param source source directory ({@code null} if no files should be copied to the sandpit)
194+
* @param target target directory
195+
* @throws XQueryException exception
196+
*/
197+
public void sandpit(final Path source, final Path target) {
198+
try {
199+
if(source != null) {
200+
final Job job = new Job() { };
201+
if(Files.exists(target)) FileDelete.delete(target, job);
202+
FileCopy.relocate(source, target, true, job);
203+
}
204+
qp.qc.resources.sandpit(target);
205+
} catch(final IOException ex) {
206+
throw new XQueryException(new QueryException(ex));
207+
}
208+
}
209+
188210
/**
189211
* Returns the next item, or {@code null} if all items have been returned.
190212
* @return next result item

basex-tests/src/main/java/org/basex/tests/w3c/QT3Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ interface QT3Constants {
2828
String STATIC_BASE_URI = "static-base-uri";
2929
/** QT3TS String. */
3030
String DECLARED = "declared";
31+
/** QT3TS String. */
32+
String SANDPIT = "sandpit";
3133

3234
/** QT3TS String. */
3335
String FILE = "file";

0 commit comments

Comments
 (0)