@@ -38,14 +38,24 @@ <h2>Working with Document Managers</h2>
3838 To identify a document in the database, you use a String with the document URI.
3939</ p >
4040< p >
41- To access document content, you create a < em > handle</ em > object that supports
42- the appropriate representation. This concept of content handles is core to the Java API
43- (making use of the Adapter design pattern so the API can read or write content
44- with a diverse and extensible set of representations).
41+ To access document content, you can create a < em > handle</ em > object that provides
42+ strongly-typed support for an IO representation. This concept of content handles
43+ is core to the Java API (making use of the Adapter design pattern so the API can read
44+ or write content with a diverse and extensible set of representations). You can define
45+ new handle classes to integrate new IO representations into the API.
4546</ p >
4647< p >
47- For instance, consider a binary JPEG file.
48- To write this file to the database, you create a {@link com.marklogic.client.io.FileHandle}
48+ For frequently-used requests, you can also write an IO object or specify an IO class
49+ to read an object directly. Internally, these shortcut methods create the handle for you.
50+ By default, the IO representations supported in shortcut methods include byte[],
51+ DOM Document, File, InputStream, SAX InputSource, Reader, Transformer Source, String,
52+ StAX XMLEventReader, and StAX XMLEventWriter. You can enable shortcut support
53+ for dom4j Document, JDOM Document, GSON JsonElement, Jackson JsonNode, JAXB POJOs, or
54+ XOM Document.
55+ </ p >
56+ < p >
57+ For instance, consider a binary JPEG file. To write this file to the database
58+ using a strongly typed handle, you create a {@link com.marklogic.client.io.FileHandle}
4959 and call the handle's set() method with the File object. You then pass the handle
5060 to the operation that writes the binary content to the database.
5161</ p >
@@ -71,7 +81,16 @@ <h2>Working with Document Managers</h2>
7181
7282docMgr.read(docId, handle);
7383
74- org.w3c.dom.Document document = handle.get();
84+ Document document = handle.get();
85+ </ pre >
86+ < p >
87+ The following example reads the document with the equivalent shortcut method:
88+ </ p >
89+ < pre >
90+ XMLDocumentManager docMgr = client.newXMLDocumentManager();
91+ String docId = "/db/path/to/myDoc.xml";
92+
93+ Document document = docMgr.readAs(docId, Document.class);
7594</ pre >
7695< p >
7796 To write content to a document in the database, set the content in a handle and then pass the
@@ -80,7 +99,7 @@ <h2>Working with Document Managers</h2>
8099 < code > /db/path/to/myDoc.xml</ code > database document from a DOM document:
81100</ p >
82101< pre >
83- org.w3c.dom. Document document = ... < em > create or modify the document</ em > ...;
102+ Document document = ... < em > create or modify the document</ em > ...;
84103
85104XMLDocumentManager docMgr = client.newXMLDocumentManager();
86105String docId = "/db/path/to/myDoc.xml";
@@ -89,6 +108,17 @@ <h2>Working with Document Managers</h2>
89108
90109docMgr.write(docId, handle);
91110</ pre >
111+ < p >
112+ The following example writes the document with the equivalent shortcut method:
113+ </ p >
114+ < pre >
115+ Document document = ... < em > create or modify the document</ em > ...;
116+
117+ XMLDocumentManager docMgr = client.newXMLDocumentManager();
118+ String docId = "/db/path/to/myDoc.xml";
119+
120+ docMgr.writeAs(docId, document);
121+ </ pre >
92122< p >
93123 To learn which handles you can use to read the content of an XML document, look at the classes
94124 that implement {@link com.marklogic.client.io.marker.XMLReadHandle}. To learn which handles
@@ -99,7 +129,9 @@ <h2>Working with Document Managers</h2>
99129 {@link com.marklogic.client.document.TextDocumentManager}, and
100130 {@link com.marklogic.client.document.GenericDocumentManager}
101131 each have corresponding read and write handles that identify the handle classes
102- that can be used when reading and writing documents of that format.
132+ that can be used when reading and writing documents of that format. The shortcut methods
133+ of each document manager work with the same IO objects supported by the handles that are
134+ accepted by the document manager and that implement {@link com.marklogic.client.io.marker.ContentHandle}.
103135</ p >
104136< p >
105137 You can read or write document metadata in the same request as document
@@ -124,19 +156,38 @@ <h2>Working with Query Options</h2>
124156< pre >
125157QueryOptionsManager optionsMgr = client.newQueryOptionsManager();
126158
127- String options =
128- "<search:options>"+
129- "<search:constraint name=\"port\">"+
130- "<search:value>"+
131- "<search:element name=\"port\" ns=\"\"/>"+
132- "</search:value>"+
133- "</search:constraint>"+
134- "</search:options>";
159+ String options = new StringBuilder()
160+ .append("<search:options>")
161+ .append( "<search:constraint name=\"port\">")
162+ .append( "<search:value>")
163+ .append( "<search:element name=\"port\" ns=\"\"/>")
164+ .append( "</search:value>")
165+ .append( "</search:constraint>")
166+ .append("</search:options>")
167+ .toString();
135168
136169StringHandle handle = new StringHandle(options);
137170
138171optionsMgr.writeOptions("shipments", handle);
139172</ pre >
173+ < p >
174+ The following example writes the query options with the equivalent shortcut method:
175+ </ p >
176+ < pre >
177+ QueryOptionsManager optionsMgr = client.newQueryOptionsManager();
178+
179+ String options = new StringBuilder()
180+ .append("<search:options>")
181+ .append( "<search:constraint name=\"port\">")
182+ .append( "<search:value>")
183+ .append( "<search:element name=\"port\" ns=\"\"/>")
184+ .append( "</search:value>")
185+ .append( "</search:constraint>")
186+ .append("</search:options>")
187+ .toString();
188+
189+ optionsMgr.writeOptionsAs("shipments", Format.XML, options);
190+ </ pre >
140191< h2 > Working with the Query Manager</ h2 >
141192< p >
142193 Use {@link com.marklogic.client.query.QueryManager} to search documents or
@@ -163,7 +214,7 @@ <h2>Working with the Query Manager</h2>
163214
164215queryMgr.search(querydef, handle);
165216
166- org.w3c.dom. Document results = handle.get();
217+ Document results = handle.get();
167218</ pre >
168219< p >
169220 Besides XML or JSON, you can also get search results as a Java data structure by using
@@ -215,7 +266,7 @@ <h2>Concise Code with Fluent Interfaces</h2>
215266 returns the handle, you can a write operation in a single line:
216267</ p >
217268< pre >
218- org.w3c.dom. Document document = ... < em > create or modify the document</ em > ...;
269+ Document document = ... < em > create or modify the document</ em > ...;
219270
220271client.newXMLDocumentManager().write(
221272 client.newDocId("/db/path/to/myDoc.xml"),
@@ -226,11 +277,48 @@ <h2>Concise Code with Fluent Interfaces</h2>
226277 When convenient, you can write a search operation in a single line:
227278</ p >
228279< pre >
229- org.w3c.dom.Document results =
230- client.newQueryManager().search(
231- queryMgr.newStringDefinition("shipments").withCriteria("electronics port:Lisbon"),
232- new DOMHandle()
233- ).get();
280+ Document results = client.newQueryManager().search(
281+ queryMgr.newStringDefinition("shipments").withCriteria("electronics port:Lisbon"),
282+ new DOMHandle()
283+ ).get();
284+ </ pre >
285+ < h2 > Registering New IO Representations for Shortcut Methods</ h2 >
286+ < p >
287+ To use extra IO handles like
288+ {@link com.marklogic.client.extra.dom4j.DOM4JHandle},
289+ {@link com.marklogic.client.extra.gson.GSONHandle},
290+ {@link com.marklogic.client.extra.jackson.JacksonHandle},
291+ {@link com.marklogic.client.extra.jdom.JDOMHandle}, or
292+ {@link com.marklogic.client.extra.xom.XOMHandle}, you download the
293+ library integrated by the handle and add the library to the classpath.
294+ You can enable support in shortcut methods for the IO representation
295+ provided by an extra library by registering a factory for the handle
296+ using {@link com.marklogic.client.DatabaseClientFactory}.getHandleRegistry().
297+ </ p >
298+ < p >
299+ You can also register a factory for the built-in JAXB handle to support
300+ IO on your POJO classes in shortcut methods. The following example
301+ registers the JAXB handle for one or more POJO classes and writes
302+ and reads the POJOs as database documents.
303+ </ p >
304+ < pre >
305+ // configure once before creating a client
306+ DatabaseClientFactory.getHandleRegistry().register(
307+ JAXBHandle.newFactory(Product.class, ... < em > other POJO classes</ em > ...)
308+ );
309+
310+ // ... < em > create a database client</ em > ...
311+
312+ XMLDocumentManager docMgr = client.newXMLDocumentManager();
313+ String docId = "/db/path/to/myPOJO.xml";
314+
315+ Product product = ... < em > create or modify the POJO</ em > ...;
316+
317+ docMgr.writeAs(docId, product);
318+
319+ // ... < em > at some other time</ em > ...
320+
321+ Product product = docMgr.readAs(docId, Product.class);
234322</ pre >
235323</ body >
236324</ html >
0 commit comments