Skip to content

Commit 4373679

Browse files
committed
Testing serialize(XMLStreamWriter)
1 parent 6bdcf57 commit 4373679

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java

+5
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,11 @@ public AbstractStructuredQuery withCriteria(String criteria) {
12311231
return this;
12321232
}
12331233

1234+
@Override
1235+
public void serialize(XMLStreamWriter serializer) throws XMLStreamException {
1236+
innerSerialize(serializer);
1237+
}
1238+
12341239
@Override
12351240
public String serialize() {
12361241
return serializeQueries(this);

marklogic-client-api/src/main/java/com/marklogic/client/query/StructuredQueryDefinition.java

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import com.marklogic.client.pojo.PojoQueryDefinition;
1919

20+
import javax.xml.stream.XMLStreamException;
21+
import javax.xml.stream.XMLStreamWriter;
22+
2023
/**
2124
* A StructuredQueryDefinition represents a structured query.
2225
*
@@ -32,6 +35,8 @@ public interface StructuredQueryDefinition
3235
*/
3336
String serialize();
3437

38+
void serialize(XMLStreamWriter serializer) throws XMLStreamException;
39+
3540
/**
3641
* Returns the query criteria, that is the query string.
3742
* @return The query string.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.marklogic.client;
2+
3+
import com.marklogic.client.query.StructuredQueryBuilder;
4+
import com.marklogic.client.query.StructuredQueryDefinition;
5+
import com.marklogic.rest.util.Fragment;
6+
7+
import javax.xml.stream.XMLOutputFactory;
8+
import javax.xml.stream.XMLStreamWriter;
9+
import java.io.StringWriter;
10+
11+
public class SerializeQueryTest {
12+
13+
public static void main(String[] args) throws Exception {
14+
DatabaseClient client = new DatabaseClientBuilder()
15+
.withHost("localhost")
16+
.withPort(8000)
17+
.withDigestAuth("admin", "admin").build();
18+
19+
StructuredQueryBuilder qb = client.newQueryManager().newStructuredQueryBuilder();
20+
StructuredQueryDefinition query = qb.and(qb.collection("hello"), qb.term("world"));
21+
22+
final String SEARCH_NS = "http://marklogic.com/appservices/search";
23+
24+
XMLOutputFactory factory = XMLOutputFactory.newFactory();
25+
// Required in order for default namespace to be applied.
26+
factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
27+
28+
StringWriter writer = new StringWriter();
29+
XMLStreamWriter serializer = factory.createXMLStreamWriter(writer);
30+
31+
// Must set this in order for the serialized query to be in the correct namespace.
32+
serializer.setDefaultNamespace(SEARCH_NS);
33+
34+
serializer.writeStartElement("someOtherNamespace", "myDocument");
35+
serializer.writeStartElement("someLocalElement");
36+
serializer.writeEndElement();
37+
38+
// In order for query.serialize to work, it appears necessary to wrap the query in an element in the search
39+
// namespace. And because that's the default namespace on the XMLStreamWriter, it will be applied to each of
40+
// the child elements produced by query.serialize.
41+
serializer.writeStartElement(SEARCH_NS, "theMarkLogicQuery");
42+
query.serialize(serializer);
43+
serializer.writeEndElement();
44+
45+
serializer.writeEndElement();
46+
serializer.flush();
47+
serializer.close();
48+
49+
String output = writer.toString();
50+
new Fragment(output).prettyPrint();
51+
}
52+
}

0 commit comments

Comments
 (0)