static public SourceHandle SourceToHandle(Source value) { return (value == null) ? null : new SourceHandle(value); } static public Stream<SourceHandle> SourceToHandle(Stream<? extends Source> values) {
/** * Specifies a transformer for modifying the content and returns the handle * as a fluent convenience. * @param transformer the transformer * @return this handle */ public SourceHandle withTransformer(Transformer transformer) { setTransformer(transformer); return this; }
@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); } } /**
SourceHandle sh = new SourceHandle(); sh.set(ds); sh.setFormat(Format.XML); sh.close();
DocumentWriteSet writeset = docMgr.newWriteSet(); for (int i = 0; i < 3; i++) { SourceHandle handle = new SourceHandle(); handle.set(s[i]); handle.setTransformer(transformer); writeset.add(docId[i], handle); handle.close();
assertXMLEqual("Reader buffering",beforeStr,new String(after)); SourceHandle sourceH = new SourceHandle(); sourceH.fromBuffer(before); after = sourceH.toBuffer(); assertNotNull("Source after",after); assertXMLEqual("Source buffering",beforeStr,new String(after));
SourceHandle handle = new SourceHandle(); handle.set(source); handle.close();
SourceHandle sourceHandle = new SourceHandle(); sourceHandle.setTransformer(transformer); docMgr.write(docId2, docMgr.read(docId, sourceHandle)); docText = docMgr.read(docId2, new StringHandle()).get();
/** * Initializes the handle with a transform source for the content. * @param content a transform source */ public SourceHandle(Source content) { this(); set(content); }
/** * Buffers the transform source 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); } }
@Override public void fromBuffer(byte[] buffer) { if (buffer == null || buffer.length == 0) content = null; else receiveContent(new ByteArrayInputStream(buffer)); } @Override
/** * Zero-argument constructor. */ public SourceHandle() { super(); super.setFormat(Format.XML); setResendable(false); } /**
/** * 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 SourceHandle 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; }
/** * Transforms the source for the content output to the result. If * the transformer is not specified, an identity transform sends * the source to the result. When writing, the result is stored * in the database * @param result the receiver of the transform output */ public void transform(Result result) { if (logger.isInfoEnabled()) logger.info("Transforming source into result"); try { if (content == null) { throw new IllegalStateException("No source to transform"); } Transformer transformer = null; if (this.transformer != null) { transformer = getTransformer(); } else { if (logger.isWarnEnabled()) logger.warn("No transformer, so using identity transform"); transformer = TransformerFactory.newInstance().newTransformer(); } transformer.transform(content, result); } catch (TransformerException e) { logger.error("Failed to transform source into result",e); throw new MarkLogicIOException(e); } }
SourceHandle sh = new SourceHandle(); sh.set(ds); sh.setFormat(Format.XML); sh.close();
SourceHandle handle = new SourceHandle(); handle.set(source); handle.setTransformer(transformer); handle.close();
/** * Assigns a transform source that produces the content and returns * the handle as a fluent convenience. * @param content the transform source * @return this handle */ public SourceHandle with(Source content) { set(content); return this; }
/** * Buffers the transform source 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); } }
@Override public void fromBuffer(byte[] buffer) { if (buffer == null || buffer.length == 0) content = null; else receiveContent(new ByteArrayInputStream(buffer)); } @Override