Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Bugfix) misc XQTS reported issues #3693

Merged
merged 4 commits into from Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -37,8 +37,8 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static org.exist.xquery.FunctionDSL.*;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.exist.xquery.FunctionDSL.*;

public class FunUnparsedText extends BasicFunction {

Expand Down Expand Up @@ -164,10 +164,18 @@ private Sequence readLines(final String uriParam, final String encoding) throws
final Source source = getSource(uriParam);
final Charset charset = getCharset(encoding, source);

try (final BufferedReader reader = new BufferedReader(new InputStreamReader(source.getInputStream(), charset))) {
String line;
while ((line = reader.readLine()) != null) {
result.add(new StringValue(line));
try (final InputStream inputStream = source.getInputStream()) {

// Nested try() as inputStream can be null
if (inputStream == null) {
throw new XPathException(this, ErrorCodes.FOUT1170, "Unable to retrieve bytestream from " + uriParam);
}

try (final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset))) {
String line;
while ((line = reader.readLine()) != null) {
result.add(new StringValue(line));
}
}
}
return result;
Expand Down
Expand Up @@ -178,8 +178,12 @@ private void nodeValueToJson(final NodeValue nodeValue, final Writer writer) thr
jsonGenerator.writeNull();
break;
case "number":
final double tempDouble = Double.parseDouble(tempString);
jsonGenerator.writeNumber(tempDouble);
try{
final double tempDouble = Double.parseDouble(tempString);
jsonGenerator.writeNumber(tempDouble);
} catch (NumberFormatException ex){
throw new XPathException(ErrorCodes.FOJS0006, "Cannot convert '" + tempString + "' to a number.");
}
break;
case "string":
if (elementValueIsEscaped == true) {
Expand Down
Expand Up @@ -149,4 +149,23 @@ public void unparsedTextAvailable_dynamicallyAvailableDocument_relativeUri() thr
assertTrue(result.itemAt(0).toJavaObject(Boolean.class).booleanValue());
}
}

@Test(expected = XPathException.class)
public void unparsedTextLines_noDataStream() throws XPathException, EXistException, PermissionDeniedException {
final BrokerPool pool = BrokerPool.getInstance();

final String text = "hello, the time is: " + System.currentTimeMillis();
final String textUri = "http://from-dynamic-context/doc1";
final String query = "fn:unparsed-text-lines('" + textUri + "')";

try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQueryContext context = new XQueryContext(pool);
context.addDynamicallyAvailableTextResource(textUri, UTF_8,
(broker2, transaction, uri, charset) -> new InputStreamReader(null, charset));

final XQuery xqueryService = pool.getXQueryService();
final CompiledXQuery compiled = xqueryService.compile(broker, context, query);
final Sequence result = xqueryService.execute(broker, compiled, null);
}
}
}
2 changes: 1 addition & 1 deletion exist-core/src/test/xquery/xquery3/xml-to-json.xql
Expand Up @@ -56,7 +56,7 @@ function xtj:xml-to-json-boolean($arg1) {

declare
%test:arg('arg1', '')
%test:assertError('NumberFormatException')
%test:assertError('FOJS0006')
%test:arg('arg1', '0')
%test:assertEquals('0.0')
%test:arg('arg1', '1')
Expand Down