Search in sources :

Example 1 with SaxXmlFeeder

use of com.dexels.navajo.document.stream.impl.SaxXmlFeeder in project navajo by Dexels.

the class XML method parseFlowable.

public static FlowableOperator<Flowable<XMLEvent>, byte[]> parseFlowable(int queueSize) {
    return new BaseFlowableOperator<Flowable<XMLEvent>, byte[]>(queueSize) {

        @Override
        public Subscriber<? super byte[]> apply(Subscriber<? super Flowable<XMLEvent>> child) throws Exception {
            return new Subscriber<byte[]>() {

                private final SaxXmlFeeder feeder = new SaxXmlFeeder();

                @Override
                public void onError(Throwable t) {
                    error = t;
                    done = true;
                    drain(child);
                }

                @Override
                public void onComplete() {
                    feeder.endOfInput();
                    done = true;
                    drain(child);
                }

                @Override
                public void onNext(byte[] buffer) {
                    Flowable<XMLEvent> fromIterable;
                    try {
                        List<XMLEvent> x = StreamSupport.stream(feeder.parse(buffer).spliterator(), false).filter(elt -> elt != null).collect(Collectors.toList());
                        fromIterable = Flowable.fromIterable(x);
                        queue.offer(fromIterable);
                        drain(child);
                    } catch (XMLStreamException e) {
                        child.onError(e);
                        return;
                    }
                }

                @Override
                public void onSubscribe(Subscription s) {
                    subscription = s;
                    child.onSubscribe(new Subscription() {

                        @Override
                        public void request(long n) {
                            BackpressureHelper.add(requested, n);
                            drain(child);
                        }

                        @Override
                        public void cancel() {
                            cancelled = true;
                            s.cancel();
                        }
                    });
                    s.request(1);
                }
            };
        }
    };
}
Also used : FlowableOperator(io.reactivex.FlowableOperator) List(java.util.List) BackpressureHelper(io.reactivex.internal.util.BackpressureHelper) Flowable(io.reactivex.Flowable) BaseFlowableOperator(com.dexels.navajo.document.stream.io.BaseFlowableOperator) XMLStreamException(javax.xml.stream.XMLStreamException) Subscription(org.reactivestreams.Subscription) SaxXmlFeeder(com.dexels.navajo.document.stream.impl.SaxXmlFeeder) StreamSupport(java.util.stream.StreamSupport) Collectors(java.util.stream.Collectors) Subscriber(org.reactivestreams.Subscriber) BaseFlowableOperator(com.dexels.navajo.document.stream.io.BaseFlowableOperator) XMLStreamException(javax.xml.stream.XMLStreamException) Subscriber(org.reactivestreams.Subscriber) Subscription(org.reactivestreams.Subscription) SaxXmlFeeder(com.dexels.navajo.document.stream.impl.SaxXmlFeeder) Flowable(io.reactivex.Flowable)

Aggregations

SaxXmlFeeder (com.dexels.navajo.document.stream.impl.SaxXmlFeeder)1 BaseFlowableOperator (com.dexels.navajo.document.stream.io.BaseFlowableOperator)1 Flowable (io.reactivex.Flowable)1 FlowableOperator (io.reactivex.FlowableOperator)1 BackpressureHelper (io.reactivex.internal.util.BackpressureHelper)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 Subscriber (org.reactivestreams.Subscriber)1 Subscription (org.reactivestreams.Subscription)1