Skip to content

Commit f5a799d

Browse files
author
ehennum
committed
Bug:25862 shortcut for raw IO object or class when handle is registered
git-svn-id: svn+ssh://svn.marklogic.com/project/engsvn/client-api/java/branches/b2_0@162258 62cac252-8da6-4816-9e9d-6dc37b19578c
1 parent 9beadff commit f5a799d

File tree

1 file changed

+112
-24
lines changed

1 file changed

+112
-24
lines changed

src/main/javadoc/overview.html

Lines changed: 112 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

7282
docMgr.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

85104
XMLDocumentManager docMgr = client.newXMLDocumentManager();
86105
String docId = "/db/path/to/myDoc.xml";
@@ -89,6 +108,17 @@ <h2>Working with Document Managers</h2>
89108

90109
docMgr.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>
125157
QueryOptionsManager optionsMgr = client.newQueryOptionsManager();
126158

127-
String options =
128-
"&lt;search:options&gt;"+
129-
"&lt;search:constraint name=\"port\"&gt;"+
130-
"&lt;search:value&gt;"+
131-
"&lt;search:element name=\"port\" ns=\"\"/&gt;"+
132-
"&lt;/search:value&gt;"+
133-
"&lt;/search:constraint&gt;"+
134-
"&lt;/search:options&gt;";
159+
String options = new StringBuilder()
160+
.append("&lt;search:options&gt;")
161+
.append( "&lt;search:constraint name=\"port\"&gt;")
162+
.append( "&lt;search:value&gt;")
163+
.append( "&lt;search:element name=\"port\" ns=\"\"/&gt;")
164+
.append( "&lt;/search:value&gt;")
165+
.append( "&lt;/search:constraint&gt;")
166+
.append("&lt;/search:options&gt;")
167+
.toString();
135168

136169
StringHandle handle = new StringHandle(options);
137170

138171
optionsMgr.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("&lt;search:options&gt;")
181+
.append( "&lt;search:constraint name=\"port\"&gt;")
182+
.append( "&lt;search:value&gt;")
183+
.append( "&lt;search:element name=\"port\" ns=\"\"/&gt;")
184+
.append( "&lt;/search:value&gt;")
185+
.append( "&lt;/search:constraint&gt;")
186+
.append("&lt;/search:options&gt;")
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

164215
queryMgr.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

220271
client.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

Comments
 (0)