Skip to content

Commit

Permalink
0.2.0 use memBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
apb2006 committed May 30, 2016
1 parent 642a9fd commit 3a0a816
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 52 deletions.
2 changes: 1 addition & 1 deletion dist/doc/file-walker.xqm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<xqdoc:xqdoc xmlns:xqdoc="http://www.xqdoc.org/1.0">
<xqdoc:control>
<xqdoc:date>2016-05-18T18:17:23.616+01:00</xqdoc:date>
<xqdoc:date>2016-05-29T22:56:58.097+01:00</xqdoc:date>
<xqdoc:version>1.1</xqdoc:version>
</xqdoc:control>
<xqdoc:module type="library">
Expand Down
Binary file removed dist/quodatum-files-0.1.8.xar
Binary file not shown.
Binary file added dist/quodatum-files-0.2.0.xar
Binary file not shown.
16 changes: 0 additions & 16 deletions makejar.jardesc

This file was deleted.

2 changes: 1 addition & 1 deletion makjar.jardesc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="WINDOWS-1252" standalone="no"?>
<jardesc>
<jar path="file-walker/src/main/content/quodatum-files-0.1.7.jar"/>
<jar path="file-walker/src/main/content/quodatum-files-0.1.9.jar"/>
<options buildIfNeeded="true" compress="true" descriptionLocation="/file-walker/makjar.jardesc" exportErrors="true" exportWarnings="true" includeDirectoryEntries="false" overwrite="true" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/>
<storedRefactorings deprecationInfo="true" structuralOnly="false"/>
<selectedProjects/>
Expand Down
6 changes: 6 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,11 @@
<version num="0.1.8">
<!-- generated: {fn:current-dateTime()} -->
</version>
<version num="0.1.9">
<!-- generated: {fn:current-dateTime()} -->
</version>
<version num="0.2.0">
<!-- generated: {fn:current-dateTime()} -->
</version>
</pkg>
</repo>
71 changes: 42 additions & 29 deletions src/java/com/quodatum/file/Walker.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,76 @@
* file walker
* @see https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileVisitResult.html
*/
import static java.nio.file.FileVisitResult.*;
import static java.nio.file.FileVisitResult.CONTINUE;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayDeque;
import java.util.Deque;

import org.basex.query.value.Value;
import org.basex.query.value.ValueBuilder;
import org.basex.query.value.node.FElem;
import org.basex.build.MemBuilder;
import org.basex.build.SingleParser;
import org.basex.core.MainOptions;
import org.basex.io.IOContent;
import org.basex.query.value.node.ANode;
import org.basex.query.value.node.DBNode;
import org.basex.util.Atts;
import org.basex.util.Token;

public class Walker extends SimpleFileVisitor<Path> {
final String cns = "http://www.w3.org/ns/xproc-step";

Deque<FElem> stack = new ArrayDeque<FElem>();
FElem site;
// builder
final byte[] DIR=Token.token("c:directory");
final byte[] FILE=Token.token("c:file");
final byte[] ERR=Token.token("c:error");
final byte[] NAME=Token.token("name");
final byte[] BASE=Token.token("xml:base");
final Atts NSP = new Atts(Token.token("c"),Token.token(cns));

public Walker(Path startingDir) {
site= new FElem("directory", cns)
.add("name",startingDir.getFileName().toString())
.add("xml:base",startingDir.toUri().toString());
SingleParser singleParser = new SingleParser(new IOContent(""), MainOptions.get())
{
@Override
protected void parse() throws IOException {}
};
MemBuilder memBuilder = new MemBuilder("", singleParser);


public Walker(Path startingDir) throws IOException {
memBuilder.init();
preVisitDirectory(startingDir,null);
}

public Value result() {
ValueBuilder vb = new ValueBuilder();
vb.add(site);
return vb.value();
public ANode result() {
return new DBNode(memBuilder.data());
}

// Print information about
// each type of file.
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
FElem elem = new FElem("file", cns).add("name", file.getFileName()
.toString());
site.add(elem);
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) throws IOException {
Atts atts = new Atts(NAME,Token.token(file.getFileName().toString()));
memBuilder.emptyElem(NAME, atts, NSP);
return CONTINUE;
}

// Print each directory visited.
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
stack.push(site);
site = new FElem("directory", cns)
.add("name",dir.getFileName().toString())
.add("xml:base",dir.toUri().toString());
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {

Atts atts = new Atts();
atts.add(NAME, Token.token(dir.getFileName().toString()));
atts.add(BASE, Token.token(dir.toUri().toString()));
memBuilder.openElem(DIR, atts, NSP);
return CONTINUE;
}

// Print each directory visited.
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
site = stack.pop().add(site);
memBuilder.closeElem();
return CONTINUE;
}

Expand All @@ -70,9 +83,9 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc)
// and an error occurs, an IOException
// is thrown.
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
FElem elem = new FElem("error", cns).add("name", file.toString());
site.add(elem);
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
Atts atts = new Atts(NAME,Token.token(file.getFileName().toString()));
memBuilder.emptyElem(ERR, atts, NSP);
return CONTINUE;
}
}
2 changes: 1 addition & 1 deletion src/main/basex.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<package xmlns="http://www.basex.org/modules/pkg">
<jar>quodatum-files-0.1.7.jar</jar>
<jar>quodatum-files-0.1.9.jar</jar>
<class>com.quodatum.file.Runner</class>
</package>
Binary file removed src/main/content/quodatum-files-0.1.7.jar
Binary file not shown.
Binary file added src/main/content/quodatum-files-0.1.9.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion src/main/expath-pkg.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<package xmlns="http://expath.org/ns/pkg"
name="https://github.com/Quodatum/file-walker"
abbrev="quodatum-files"
version="0.1.8"
version="0.2.0"
spec="1.0">

<title>File list and search in the XProc style</title>
Expand Down
4 changes: 2 additions & 2 deletions src/test/test.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module namespace test = 'http://basex.org/modules/xqunit-tests';
:)
import module namespace fw="quodatum.file.walker";
declare namespace c="http://www.w3.org/ns/xproc-step";
declare variable $test:dir:="\\ODROID-JESSIE\sda1\pictures\Pictures";

declare variable $test:dirx:="\\ODROID-JESSIE\sda1\pictures\Pictures";
declare variable $test:dir:="P:\pictures\Pictures";

(:~ directory-list :)
declare
Expand Down
2 changes: 1 addition & 1 deletion tools/buildx.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ declare namespace pkg="http://expath.org/ns/pkg";
declare function files($src as xs:string) as xs:string*
{
fn:filter(file:list($src,fn:true()),
function ($f){file:is-file($src || $f)}
function ($f){($src || $f)=>fn:translate("\","/")=>file:is-file()}
)
!fn:translate(.,"\","/")
};
Expand Down

0 comments on commit 3a0a816

Please sign in to comment.