Skip to content

Instantly share code, notes, and snippets.

@bertramn
Created January 24, 2019 01:39
Show Gist options
  • Save bertramn/82354774b4cf3dfe0abf0c62ad2fc1cf to your computer and use it in GitHub Desktop.
Save bertramn/82354774b4cf3dfe0abf0c62ad2fc1cf to your computer and use it in GitHub Desktop.
Saxon 9.9 StreamReader is broken
package net.sf.saxon;
import com.saxonica.xqj.SaxonXQDataSource;
import net.sf.saxon.om.IgnorableSpaceStrippingRule;
import org.junit.jupiter.api.Test;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xquery.*;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.StringWriter;
import static org.junit.jupiter.api.Assertions.*;
public class SaxonStreamReaderTest {
private final static TransformerFactory XTF;
private static Configuration CONFIG;
static {
CONFIG = new Configuration();
CONFIG.getParseOptions().setSpaceStrippingRule(IgnorableSpaceStrippingRule.getInstance());
XTF = new BasicTransformerFactory(CONFIG);
}
private XQSequence executeQuery() throws XQException {
SaxonXQDataSource ds = new SaxonXQDataSource(CONFIG);
XQConnection con = ds.getConnection();
InputStream query = getClass().getClassLoader().getResourceAsStream("test.xq");
assertNotNull(query);
XQStaticContext ctx = con.getStaticContext();
ctx.setBindingMode(XQConstants.BINDING_MODE_DEFERRED);
con.setStaticContext(ctx);
XQPreparedExpression expr = con.prepareExpression(query);
XQSequence result = expr.executeQuery();
assertNotNull(result);
return result;
}
@Test
void itShouldWriteToOutputStream() throws Exception {
XQSequence result = executeQuery();
if (result.next()) {
XQItem item = result.getItem();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
item.writeItem(bao, null);
String s = bao.toString("UTF-8");
assertTrue(s.contains("47"));
} else {
fail("should always produce a result");
}
}
@Test
void itShouldStreamResult() throws Exception {
XQSequence result = executeQuery();
if (result.next()) {
XQItem item = result.getItem();
XMLStreamReader streamReader = item.getItemAsStream();
StringWriter stringWriter = new StringWriter();
XTF.newTransformer().transform(new StAXSource(streamReader), new StreamResult(stringWriter));
String s = stringWriter.toString();
assertTrue(s.contains("47"));
} else {
fail("should always produce a result");
}
}
}
<test id="47"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment