static public InputSourceHandle InputSourceToHandle(InputSource value) { return (value == null) ? null : new InputSourceHandle(value); } static public Stream<InputSourceHandle> InputSourceToHandle(Stream<? extends InputSource> values) {
/** * Initializes the handle with a SAX input source for the content. * @param content a SAX input source */ public InputSourceHandle(InputSource content) { this(); set(content); }
/** * Returns the factory for parsing SAX events. * @return the SAX factory */ public SAXParserFactory getFactory() throws SAXException, ParserConfigurationException { if (factory == null) factory = makeSAXParserFactory(); return factory; } /**
assertXMLEqual("DOM buffering",beforeStr,new String(after)); InputSourceHandle inputSourceH = new InputSourceHandle(); inputSourceH.fromBuffer(before); after = inputSourceH.toBuffer(); assertNotNull("InputSource after",after); assertXMLEqual("InputSource buffering",beforeStr,new String(after));
"<root foo='bar'><child/>mixed</root>"; InputSourceHandle saxHandle = new InputSourceHandle(); saxHandle.setErrorHandler(new InputSourceHandle.DraconianErrorHandler()); saxHandle.setDefaultWriteSchema(schema); docMgr.write(docId, saxHandle.with(new InputSource(new StringReader(doc)))); saxHandle.setDefaultWriteSchema(schema); docMgr.write(docId, saxHandle.with(new InputSource(new StringReader(doc)))); } catch(RuntimeException ex) { threwException = true;
assertXMLEqual("Transform result not equivalent to source",domString,docText); InputSourceHandle saxHandle = new InputSourceHandle(); saxHandle.set(new InputSource(new StringReader(domString))); docMgr.write(docId, saxHandle); docText = docMgr.read(docId2, new StringHandle()).get(); docMgr.read(docId, saxHandle).process(handler); assertTrue("Failed to process XML document with SAX", counter.get("elementCount") == 2 && counter.get("attributeCount") == 2);
@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); } } /**
/** * Buffers the SAX input source and returns the buffer * as an XML string. */ @Override public String toString() { try { return new String(toBuffer(),"UTF-8"); } catch (UnsupportedEncodingException e) { throw new MarkLogicIOException(e); } }
@Override public void write(OutputStream out) throws IOException { try { TransformerFactory.newInstance().newTransformer().transform( new SAXSource(makeReader(true), content), new StreamResult(new OutputStreamWriter(out, "UTF-8")) ); } catch (TransformerException e) { logger.error("Failed to transform input source into result",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
/** * 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 InputSourceHandle 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; }
protected XMLReader makeReader(boolean isForWrite) { try { SAXParserFactory factory = getFactory(); if (factory == null) { throw new MarkLogicInternalException("Failed to make SAX parser factory"); } boolean useDefaultSchema = (isForWrite && defaultWriteSchema != null && factory.getSchema() == null); if (useDefaultSchema) { factory.setSchema(defaultWriteSchema); } XMLReader reader = factory.newSAXParser().getXMLReader(); if (useDefaultSchema) { factory.setSchema(null); } if (resolver != null) reader.setEntityResolver(resolver); if (errorHandler != null) reader.setErrorHandler(errorHandler); return reader; } catch (SAXException e) { logger.error("Failed to process input source with SAX content handler",e); throw new MarkLogicInternalException(e); } catch (ParserConfigurationException e) { logger.error("Failed to process input source with SAX content handler",e); throw new MarkLogicInternalException(e); } }
@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); } } /**
/** * Buffers the SAX input source and returns the buffer * as an XML string. */ @Override public String toString() { try { return new String(toBuffer(),"UTF-8"); } catch (UnsupportedEncodingException e) { throw new MarkLogicIOException(e); } }
@Override public void write(OutputStream out) throws IOException { try { TransformerFactory.newInstance().newTransformer().transform( new SAXSource(makeReader(true), content), new StreamResult(new OutputStreamWriter(out, "UTF-8")) ); } catch (TransformerException e) { logger.error("Failed to transform input source into result",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
/** * 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 InputSourceHandle 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(JacksonHandle.newFactory()); registry.register(JacksonParserHandle.newFactory()); registry.register(ReaderHandle.newFactory()); registry.register(SourceHandle.newFactory()); registry.register(StringHandle.newFactory()); registry.register(XMLEventReaderHandle.newFactory()); registry.register(XMLStreamReaderHandle.newFactory()); return registry; }
protected XMLReader makeReader(boolean isForWrite) { try { SAXParserFactory factory = getFactory(); if (factory == null) { throw new MarkLogicInternalException("Failed to make SAX parser factory"); } boolean useDefaultSchema = (isForWrite && defaultWriteSchema != null && factory.getSchema() == null); if (useDefaultSchema) { factory.setSchema(defaultWriteSchema); } XMLReader reader = factory.newSAXParser().getXMLReader(); if (useDefaultSchema) { factory.setSchema(null); } if (resolver != null) reader.setEntityResolver(resolver); if (errorHandler != null) reader.setErrorHandler(errorHandler); return reader; } catch (SAXException e) { logger.error("Failed to process input source with SAX content handler",e); throw new MarkLogicInternalException(e); } catch (ParserConfigurationException e) { logger.error("Failed to process input source with SAX content handler",e); throw new MarkLogicInternalException(e); } }