Search in sources :

Example 11 with Binary

use of com.dexels.navajo.document.types.Binary in project navajo by Dexels.

the class BinaryObserver method apply.

@Override
public Subscriber<? super String> apply(Subscriber<? super Binary> outgoing) {
    return new Subscriber<String>() {

        @Override
        public void onComplete() {
            try {
                w.flush();
                w.close();
                fos.flush();
                fos.close();
                Binary binary;
                if (NavajoFactory.getInstance().isSandboxMode()) {
                    binary = new Binary(BinaryObserver.this.inMemory);
                } else {
                    binary = new Binary(dataFile, true);
                }
                binary.setDigest(digest);
                outgoing.onNext(binary);
                outgoing.onComplete();
            } catch (Throwable e) {
                outgoing.onError(e);
            }
        }

        @Override
        public void onError(Throwable t) {
            // propagate
            outgoing.onError(t);
        }

        @Override
        public void onNext(String line) {
            count++;
            try {
                w.write(line);
            } catch (IOException e) {
                outgoing.onError(e);
            }
        }

        @Override
        public void onSubscribe(Subscription s) {
        }
    };
}
Also used : Subscriber(org.reactivestreams.Subscriber) Binary(com.dexels.navajo.document.types.Binary) IOException(java.io.IOException) Subscription(org.reactivestreams.Subscription)

Example 12 with Binary

use of com.dexels.navajo.document.types.Binary in project navajo by Dexels.

the class StreamDocument method gatherBinary.

public static FlowableOperator<Binary, String> gatherBinary() {
    return new BaseFlowableOperator<Binary, String>(1) {

        @Override
        public Subscriber<? super String> apply(Subscriber<? super Binary> child) throws Exception {
            return new Subscriber<String>() {

                Binary result = null;

                @Override
                public void onComplete() {
                    if (result != null) {
                        try {
                            result.finishPushContent();
                            operatorNext("", r -> result, child);
                        } catch (IOException e) {
                            operatorError(e, child);
                        }
                    }
                    operatorComplete(child);
                }

                @Override
                public void onError(Throwable t) {
                    operatorError(t, child);
                }

                @Override
                public void onNext(String e) {
                    try {
                        result.pushContent(e);
                        operatorRequest(1);
                    } catch (IOException ex) {
                        operatorError(ex, child);
                    }
                }

                @Override
                public void onSubscribe(Subscription s) {
                    operatorSubscribe(s, child);
                    result = createBinary();
                }

                private Binary createBinary() {
                    Binary result = new Binary();
                    try {
                        result.startPushRead();
                    } catch (IOException e1) {
                        logger.error("Error: ", e1);
                    }
                    return result;
                }
            };
        }
    };
}
Also used : FlowableSubscriber(io.reactivex.FlowableSubscriber) Subscriber(org.reactivestreams.Subscriber) BaseFlowableOperator(com.dexels.navajo.document.stream.io.BaseFlowableOperator) Binary(com.dexels.navajo.document.types.Binary) IOException(java.io.IOException) Subscription(org.reactivestreams.Subscription)

Example 13 with Binary

use of com.dexels.navajo.document.types.Binary in project navajo by Dexels.

the class StreamDocument method createBinary.

public static ObservableOperator<Binary, byte[]> createBinary() {
    return new ObservableOperator<Binary, byte[]>() {

        @Override
        public Observer<? super byte[]> apply(Observer<? super Binary> out) throws Exception {
            Binary result = new Binary();
            result.startBinaryPush();
            return new Observer<byte[]>() {

                @Override
                public void onComplete() {
                    try {
                        result.finishPushContent();
                    } catch (IOException e) {
                        e.printStackTrace();
                        out.onError(e);
                        return;
                    }
                    out.onNext(result);
                    out.onComplete();
                }

                @Override
                public void onError(Throwable e) {
                    out.onError(e);
                }

                @Override
                public void onNext(byte[] b) {
                    result.pushContent(b);
                }

                @Override
                public void onSubscribe(Disposable d) {
                    out.onSubscribe(new Disposable() {

                        private boolean disposed = false;

                        @Override
                        public void dispose() {
                            disposed = true;
                        }

                        @Override
                        public boolean isDisposed() {
                            return disposed;
                        }
                    });
                }
            };
        }
    };
}
Also used : Disposable(io.reactivex.disposables.Disposable) Observer(io.reactivex.Observer) Binary(com.dexels.navajo.document.types.Binary) IOException(java.io.IOException) ObservableOperator(io.reactivex.ObservableOperator)

Example 14 with Binary

use of com.dexels.navajo.document.types.Binary in project navajo by Dexels.

the class Prop method write.

public void write(Writer sw, int indent) throws IOException {
    for (int a = 0; a < indent; a++) {
        sw.write(" ");
    }
    sw.write("<property");
    if (name != null) {
        sw.write(" name=\"" + StringEscapeUtils.escapeXml(name) + "\"");
    }
    if (type != null) {
        sw.write(" type=\"" + type + "\"");
    }
    if (value != null && !isBinary()) {
        String value = valueAsString();
        String escapedValue = StringEscapeUtils.escapeXml(value);
        sw.write(" value=\"" + escapedValue + "\"");
    }
    if (direction != null && direction.isPresent()) {
        sw.write(" direction=\"" + direction().get() + "\"");
    }
    if (description != null && !"".equals(description)) {
        sw.write(" description=\"" + description + "\"");
    }
    if (cardinality != null && cardinality.isPresent() && !"".equals(cardinality.get())) {
        sw.write(" cardinality=\"" + cardinality.get() + "\"");
    }
    if (length > 0) {
        sw.write(" length=\"" + length + "\"");
    }
    if (!isBinary() && (selections == null || selections.isEmpty())) {
        sw.write("/>\n");
    } else {
        sw.write(">\n");
        if (isBinary()) {
            Binary b = binary;
            b.writeBase64(sw);
        } else {
            for (Select select : selections) {
                writeSelection(sw, select, indent + NavajoStreamSerializer.INDENT);
            }
        }
        for (int a = 0; a < indent; a++) {
            sw.write(" ");
        }
        sw.write("</property>\n");
    }
}
Also used : Binary(com.dexels.navajo.document.types.Binary)

Example 15 with Binary

use of com.dexels.navajo.document.types.Binary in project navajo by Dexels.

the class TestBinaries method testGatherBinary.

@Test
public void testGatherBinary() throws Exception {
    Binary nn = Bytes.from(TestRx.class.getClassLoader().getResourceAsStream("tml_with_binary.xml"), 4096).lift(XML.parseFlowable(5)).flatMap(e -> e).lift(StreamDocument.parse()).concatMap(e -> e).lift(StreamDocument.observeBinary("SecureImage/Image")).lift(StreamDocument.gatherBinary()).doOnNext(e -> System.err.println("Event: " + e)).blockingFirst();
    System.err.println("eventcount: " + nn.getLength());
    // nn.write(System.err);
    // System.err.println("RESULT:\n"+new String(baos.toByteArray()));
    Assert.assertTrue(nn.getLength() == 3245);
}
Also used : NavajoFactory(com.dexels.navajo.document.NavajoFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamCompress(com.dexels.navajo.document.stream.StreamCompress) XML(com.dexels.navajo.document.stream.xml.XML) Test(org.junit.Test) StreamDocument(com.dexels.navajo.document.stream.StreamDocument) Schedulers(io.reactivex.schedulers.Schedulers) Assert(org.junit.Assert) Bytes(com.github.davidmoten.rx2.Bytes) Binary(com.dexels.navajo.document.types.Binary) Binary(com.dexels.navajo.document.types.Binary) Test(org.junit.Test)

Aggregations

Binary (com.dexels.navajo.document.types.Binary)139 Test (org.junit.Test)38 IOException (java.io.IOException)32 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)26 File (java.io.File)25 Ignore (org.junit.Ignore)17 Property (com.dexels.navajo.document.Property)16 URL (java.net.URL)16 UserException (com.dexels.navajo.script.api.UserException)14 OutputStream (java.io.OutputStream)13 FileOutputStream (java.io.FileOutputStream)12 Navajo (com.dexels.navajo.document.Navajo)11 MappableException (com.dexels.navajo.script.api.MappableException)11 FileInputStream (java.io.FileInputStream)9 InputStream (java.io.InputStream)9 Message (com.dexels.navajo.document.Message)8 StringWriter (java.io.StringWriter)8 OutputStreamWriter (java.io.OutputStreamWriter)7 NavajoException (com.dexels.navajo.document.NavajoException)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6