Skip to content

Commit

Permalink
Merge pull request #1633 from adamretter/hotfix/doc-available
Browse files Browse the repository at this point in the history
Fix to return value of fn:doc-available on failure
  • Loading branch information
ljo authored Nov 27, 2017
2 parents 4aba29f + 124ea7c commit 5f269d3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/org/exist/xquery/functions/fn/FunDocAvailable.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.Type;
import org.xml.sax.SAXParseException;

import java.net.URI;
import java.net.URISyntaxException;

/**
* Implements the XQuery's fn:doc-available() function.
Expand Down Expand Up @@ -88,17 +90,17 @@ public Sequence eval(final Sequence contextSequence, final Item contextItem)
final Sequence arg = getArgument(0).eval(contextSequence, contextItem);
if (!arg.isEmpty()) {
final String path = arg.itemAt(0).getStringValue();

try {
new URI(path);
} catch (final URISyntaxException e) {
throw new XPathException(this, ErrorCodes.FODC0005, e.getMessage(), arg, e);
}

try {
result = BooleanValue.valueOf(DocUtils.isDocumentAvailable(this.context, path));
} catch (final XPathException e) {
if (e.getCause() instanceof SAXParseException) {
result = BooleanValue.FALSE;
} else if(e.getMessage().contains("is a binary resource, not an XML document")) {
result = BooleanValue.FALSE;
} else {
e.prependMessage(ErrorCodes.FODC0005, "");
throw e;
}
result = BooleanValue.FALSE;
}
}

Expand All @@ -110,7 +112,7 @@ public Sequence eval(final Sequence contextSequence, final Item contextItem)
}

@Override
public void resetState(boolean postOptimization) {
public void resetState(final boolean postOptimization) {
super.resetState(postOptimization);
getArgument(0).resetState(postOptimization);
}
Expand Down
19 changes: 19 additions & 0 deletions test/src/xquery/fn.xql
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ function fnt:doc-available($filename as xs:string) {
fn:doc-available("/db/fn-test/" || $filename)
};

(:
Intentionally marked as pending as it has
an external dependency
:)
declare
%test:pending
%test:args("https://www.kingjamesbibleonline.org/Genesis-Chapter-1_Original-1611-KJV/")
%test:assertEquals("false")
function fnt:doc-available-remote($uri as xs:string) {
fn:doc-available($uri)
};

declare
%test:args("\adamretter")
%test:assertError("FODC0005")
function fnt:doc-available-invalid-uri($uri as xs:string) {
fn:doc-available($uri)
};

declare
%test:args("test.xml")
%test:assertEquals("true")
Expand Down

0 comments on commit 5f269d3

Please sign in to comment.