static public ReaderHandle ReaderToHandle(Reader value) { return (value == null) ? null : new ReaderHandle(value); } static public Stream<ReaderHandle> ReaderToHandle(Stream<? extends Reader> values) {
@Override public byte[] toBuffer() { try { if (content == null) return null; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); write(buffer); byte[] b = buffer.toByteArray(); fromBuffer(b); return b; } catch (IOException e) { throw new MarkLogicIOException(e); } } /**
/** * Assigns a character reader as the content and returns the handle * as a fluent convenience. * @param content a reader * @return this handle */ public ReaderHandle with(Reader content) { set(content); return this; }
@Test public void testWrite_rdfxml_FileHandle() throws Exception { BufferedReader buffreader = new BufferedReader(new FileReader(datasource + "semantics.rdf")); ReaderHandle handle = new ReaderHandle(); handle.set(buffreader); gmWriter.write("http://test.reader.handle/bufferreadtrig", handle.withMimetype(RDFMimeTypes.RDFXML)); buffreader.close(); ReaderHandle read = gmWriter.read("http://test.reader.handle/bufferreadtrig", new ReaderHandle().withMimetype(RDFMimeTypes.RDFXML)); Reader readFile = read.get(); String readContent = convertReaderToString(readFile); assertTrue("Did not get receive expected string content, Received:: " + readContent, readContent.contains("http://www.daml.org/2001/12/factbook/vi#A113932")); handle.close(); read.close(); }
params.add("result", applyResult.toString().toLowerCase()); List<String> responseUris = new BufferedReader( services.postResource(null, "internal/apply-transform", null, params, uris, new ReaderHandle()).get() ).lines().collect(Collectors.toList()); QueryBatchImpl processedBatch = new QueryBatchImpl()
/** * Write document to databae using ReaderHandle * * @param client * : the database client connection * @param filename * : the filename * @param uri * : the document uri * @param type * : the document type (XML, Text, JSON, or Binary) * @throws IOException */ public void writeDocumentReaderHandle(DatabaseClient client, String filename, String uri, String type) throws IOException { // create doc manager DocumentManager docMgr = null; docMgr = documentManagerSelector(client, docMgr, type); // acquire the content BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/client/functionaltest/data/" + filename)); // create an identifier for the document String docId = uri + filename; // create a handle on the content ReaderHandle handle = new ReaderHandle(); handle.set(docStream); // write the document content docMgr.write(docId, handle); System.out.println("Write " + docId + " to database"); docStream.close(); }
ReaderHandle handle = new ReaderHandle(); handle.setFormat(Format.JSON); handle.set(docStream);
assertXMLEqual("InputStream buffering",beforeStr,new String(after)); ReaderHandle readerH = new ReaderHandle(); readerH.fromBuffer(before); after = readerH.toBuffer(); assertNotNull("Reader after",after); assertXMLEqual("Reader buffering",beforeStr,new String(after));
@Test public void testWriteMultipleJSONDocs() throws KeyManagementException, NoSuchAlgorithmException, Exception { String docId[] = { "/a.json", "/b.json", "/c.json" }; String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); Reader strReader = new StringReader(json1); JSONDocumentManager docMgr = client.newJSONDocumentManager(); DocumentWriteSet writeset = docMgr.newWriteSet(); writeset.add(docId[0], new ReaderHandle(strReader).withFormat(Format.JSON)); writeset.add(docId[1], new ReaderHandle(new StringReader(json2))); writeset.add(docId[2], new ReaderHandle(new StringReader(json3))); docMgr.write(writeset); ReaderHandle r1 = new ReaderHandle(); docMgr.read(docId[0], r1); BufferedReader bfr = new BufferedReader(r1.get()); assertEquals(json1, bfr.readLine()); docMgr.read(docId[1], r1); assertEquals("Json File Content" + docId[1], json2, new BufferedReader(r1.get()).readLine()); docMgr.read(docId[2], r1); assertEquals("Json File Content" + docId[2], json3, new BufferedReader(r1.get()).readLine()); bfr.close(); }
try (ReaderHandle readerHandle = new ReaderHandle()) { rowMgr.resultDoc(plan, readerHandle.withMimetype("text/csv")); try (LineNumberReader lineReader = new LineNumberReader(readerHandle.get())) { String line = lineReader.readLine(); String[] cols = line.split(","); try (ReaderHandle readerHandle = new ReaderHandle()) { rowMgr.resultDoc(plan, readerHandle.withMimetype("application/json-seq")); try (LineNumberReader lineReader = new LineNumberReader(readerHandle.get())) { int i=0; for (String line = null; (line = lineReader.readLine()) != null; i++) {
@Test public void testWriteMultipleJSONDocsWithDefaultMetadata() throws KeyManagementException, NoSuchAlgorithmException, Exception { String docId[] = { "/a.json", "/b.json", "/c.json" }; String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); Reader strReader = new StringReader(json1); JSONDocumentManager docMgr = client.newJSONDocumentManager(); DocumentWriteSet writeset = docMgr.newWriteSet(); // put metadata DocumentMetadataHandle mh = setMetadata(); writeset.addDefault(mh); writeset.add(docId[0], new ReaderHandle(strReader).withFormat(Format.JSON)); writeset.add(docId[1], new ReaderHandle(new StringReader(json2))); writeset.add(docId[2], new ReaderHandle(new StringReader(json3))); docMgr.write(writeset); DocumentPage page = docMgr.read(docId); while (page.hasNext()) { DocumentRecord rec = page.next(); docMgr.readMetadata(rec.getUri(), mh); System.out.println(rec.getUri()); validateMetadata(mh); } validateMetadata(mh); }
/** * Specifies the format of the content and returns the handle * as a fluent convenience. * @param format the format of the content * @return this handle */ public ReaderHandle withFormat(Format format) { setFormat(format); return this; } /**
/** * Buffers the character stream and returns the buffer as a string. */ @Override public String toString() { try { return new String(toBuffer(),"UTF-8"); } catch (UnsupportedEncodingException e) { throw new MarkLogicIOException(e); } }
/** * Specifies the mime type of the content and returns the handle * as a fluent convenience. * @param mimetype the mime type of the content * @return this handle */ public ReaderHandle withMimetype(String mimetype) { setMimetype(mimetype); return this; }
public static HandleFactoryRegistry registerDefaults(HandleFactoryRegistry registry) { registry.register(BytesHandle.newFactory()); registry.register(DOMHandle.newFactory()); registry.register(FileHandle.newFactory()); registry.register(InputSourceHandle.newFactory()); registry.register(InputStreamHandle.newFactory()); registry.register(ReaderHandle.newFactory()); registry.register(SourceHandle.newFactory()); registry.register(StringHandle.newFactory()); registry.register(XMLEventReaderHandle.newFactory()); registry.register(XMLStreamReaderHandle.newFactory()); return registry; }
@Override public void fromBuffer(byte[] buffer) { if (buffer == null || buffer.length == 0) content = null; else receiveContent(new ByteArrayInputStream(buffer)); } @Override
/** * Zero-argument constructor. */ public ReaderHandle() { super(); setResendable(false); } /**
@Test public void testReadWrite() throws IOException { String docId = "/test/testWrite1.txt"; String text = "A simple text document"; TextDocumentManager docMgr = Common.client.newTextDocumentManager(); docMgr.write(docId, new StringHandle().with(text)); assertEquals("Text document write difference",text,docMgr.read(docId, new StringHandle()).get()); BytesHandle bytesHandle = new BytesHandle(); docMgr.read(docId, bytesHandle); assertEquals("Text document mismatch reading bytes", bytesHandle.get().length,text.length()); InputStreamHandle inputStreamHandle = new InputStreamHandle(); docMgr.read(docId, inputStreamHandle); byte[] b = Common.streamToBytes(inputStreamHandle.get()); assertEquals("Text document mismatch reading input stream",new String(b),text); Reader reader = docMgr.read(docId, new ReaderHandle()).get(); String s = Common.readerToString(reader); assertEquals("Text document mismatch with reader",s,text); File file = docMgr.read(docId, new FileHandle()).get(); assertEquals("Text document mismatch with file",text.length(),file.length()); }
/** * Update document using ReaderHandle * * @param client * @param filename * @param uri * @param type * @throws IOException */ public void updateDocumentReaderHandle(DatabaseClient client, String filename, String uri, String type) throws IOException { // create doc manager DocumentManager docMgr = null; docMgr = documentManagerSelector(client, docMgr, type); // acquire the content BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/client/functionaltest/data/" + filename)); // create an identifier for the document String docId = uri; // create a handle on the content ReaderHandle handle = new ReaderHandle(); handle.set(docStream); // write the document content docMgr.write(docId, handle); System.out.println("Update " + docId + " to database"); docStream.close(); }