Search in sources :

Example 6 with Binary

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

the class Zip method evaluate.

@Override
public Object evaluate() throws TMLExpressionException {
    if (getOperands().size() != 2) {
        return new TMLExpressionException(this, "Wrong number of arguments");
    }
    if (!(getOperand(0) instanceof Binary)) {
        return new TMLExpressionException(this, "Binary content expected");
    }
    if (!(getOperand(1) instanceof String)) {
        return new TMLExpressionException(this, "String expected");
    }
    Binary i = (Binary) getOperand(0);
    String f = (String) getOperand(1);
    File tempFile = null;
    try {
        tempFile = File.createTempFile("zip_function", "navajo");
        System.err.println("Created tempfile: " + tempFile.getAbsolutePath());
        FileOutputStream fos = new FileOutputStream(tempFile);
        // ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ZipOutputStream zo = new ZipOutputStream(fos);
        ZipEntry entry = new ZipEntry(f);
        zo.putNextEntry(entry);
        // zo.write( i.getData() );
        i.write(zo, false);
        fos.flush();
        zo.closeEntry();
        zo.close();
        // byte [] result = baos.toByteArray();
        fos.close();
        Binary b = new Binary(tempFile, false);
        return b;
    } catch (Exception e) {
        throw new TMLExpressionException(this, e.getMessage(), e);
    } finally {
        if (tempFile != null) {
            tempFile.delete();
        }
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) Binary(com.dexels.navajo.document.types.Binary) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) File(java.io.File) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 7 with Binary

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

the class StandardFunctionsTest method testIsEmpty.

@Test
public void testIsEmpty() {
    FunctionInterface fi = fff.getInstance(cl, "IsEmpty");
    // Empty String.
    fi.reset();
    fi.insertStringOperand("");
    Object o = fi.evaluateWithTypeChecking();
    assertNotNull(o);
    assertEquals(Boolean.TRUE, o);
    // Non Empty String.
    fi.reset();
    fi.insertStringOperand("aap");
    o = fi.evaluateWithTypeChecking();
    assertNotNull(o);
    assertEquals(Boolean.FALSE, o);
    // Null value.
    fi.reset();
    fi.insertOperand(Operand.NULL);
    o = fi.evaluateWithTypeChecking();
    assertNotNull(o);
    assertEquals(Boolean.TRUE, o);
    // Empty list
    fi.reset();
    fi.insertListOperand(new ArrayList<Object>());
    o = fi.evaluateWithTypeChecking();
    assertNotNull(o);
    assertEquals(Boolean.TRUE, o);
    // Non Empty list.
    fi.reset();
    boolean thing = new ArrayList<String>().add("noot");
    fi.insertBooleanOperand(thing);
    o = fi.evaluateWithTypeChecking();
    assertNotNull(o);
    assertEquals(Boolean.FALSE, o);
    // Empty Binary.
    fi.reset();
    fi.insertBinaryOperand(new Binary());
    o = fi.evaluateWithTypeChecking();
    assertNotNull(o);
    assertEquals(Boolean.TRUE, o);
    // Non Empty Binary.
    fi.reset();
    fi.insertBinaryOperand(new Binary("aap".getBytes()));
    o = fi.evaluateWithTypeChecking();
    assertNotNull(o);
    assertEquals(Boolean.FALSE, o);
    // Non empty Clocktime.
    fi.reset();
    fi.insertClockTimeOperand(new ClockTime(new Date()));
    o = fi.evaluateWithTypeChecking();
    assertNotNull(o);
    assertEquals(Boolean.FALSE, o);
}
Also used : FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) CapString(com.dexels.navajo.functions.CapString) Binary(com.dexels.navajo.document.types.Binary) ClockTime(com.dexels.navajo.document.types.ClockTime) Date(java.util.Date) Test(org.junit.Test)

Example 8 with Binary

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

the class StandardFunctionsTest method testZipArchive.

@Test
public void testZipArchive() {
    FunctionInterface fi = fff.getInstance(cl, "ZipArchive");
    fi.reset();
    fi.insertStringOperand("aap");
    Binary actual = (Binary) fi.evaluateWithTypeChecking();
    assertNull(actual);
}
Also used : FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) Binary(com.dexels.navajo.document.types.Binary) Test(org.junit.Test)

Example 9 with Binary

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

the class MappingUtils method setProperty.

public static final Property setProperty(boolean parameter, Message msg, String name, Object value, String type, String subtype, String direction, String description, int length, Navajo outputDoc, Navajo tmlDoc, boolean remove) throws MappingException {
    Message ref = null;
    if (parameter) {
        if (msg == null) {
            msg = tmlDoc.getMessage("__parms__");
            if (msg == null) {
                // Create __parms__ message.
                msg = NavajoFactory.getInstance().createMessage(tmlDoc, "__parms__");
                tmlDoc.addMessage(msg);
            }
        }
        ref = getMessageObject(name, msg, false, tmlDoc, false, "", -1);
        if (ref == null) {
            // Can be null due to absolute param name (starting with '/'). In this case use
            // __parms__ as parent.
            ref = tmlDoc.getMessage("__parms__");
        }
    } else {
        ref = getMessageObject(name, msg, false, outputDoc, false, "", -1);
    }
    if (ref == null) {
        ref = msg;
    }
    String actualName = getStrippedPropertyName(name);
    if (ref == null) {
        throw new MappingException("Property can only be created under a message");
    }
    // with ../ constructions in the name of the new property, it is possible to create a property at the rootMessage of a NavajoDoc but that will never be serialized. Logger message to see how often it happens
    if (ref.equals(ref.getRootDoc().getRootMessage())) {
        logger.warn("WARNING - Adding property to rootMessage of NavajoDoc - property will not be findable");
    }
    Property prop = ref.getProperty(actualName);
    // Remove a parameter if remove flag is set.
    if (remove && prop != null && parameter) {
        ref.removeProperty(prop);
        return null;
    }
    if (prop == null && remove && parameter) {
        return null;
    }
    if (prop == null) {
        // Property does not exist.
        if (!parameter) {
            if (value instanceof Property) {
                // Value is a property itself!
                prop = (Property) ((Property) value).clone(name);
            } else if (Property.SELECTION_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(outputDoc, actualName, "1", description, direction);
                if (value instanceof Selection[]) {
                    prop.setCardinality("+");
                    prop.setValue((Selection[]) value);
                }
            } else if (Property.BINARY_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(outputDoc, actualName, type, "", length, description, direction);
                if (value instanceof Binary) {
                    prop.setValue((Binary) value);
                }
            } else {
                // Legacy mode hack, many scripts do not expect null valued string properties.
                if (Property.STRING_PROPERTY.equals(type) && value == null) {
                    value = "";
                }
                prop = ref.getRootDoc().getNavajoFactory().createProperty(outputDoc, actualName, type, "", length, description, direction);
                if ((value instanceof StringLiteral)) {
                    value = value.toString();
                }
                prop.setAnyValue(value);
                prop.setType(type);
            }
        } else {
            if (Property.EXPRESSION_LITERAL_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, type, "", length, description, direction);
                prop.setValue(new NavajoExpression(value.toString()));
                prop.setType(type);
            } else if (Property.SELECTION_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, "1", description, direction);
                if (value instanceof Selection[]) {
                    prop.setCardinality("+");
                    prop.setValue((Selection[]) value);
                }
            } else if (Property.BINARY_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, type, "", length, description, direction);
                if (value instanceof Binary) {
                    prop.setValue((Binary) value);
                }
            } else {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, type, "", length, description, direction);
                if ((value instanceof StringLiteral)) {
                    value = value.toString();
                }
                prop.setAnyValue(value);
                prop.setType(type);
            }
        }
        ref.addProperty(prop);
    } else {
        // Existing property.
        prop.clearValue();
        if (value instanceof Property) {
            // Value is a property itself!
            prop = (Property) ((Property) value).clone(name);
        } else if (Property.BINARY_PROPERTY.equals(type)) {
            if (value instanceof Binary) {
                prop.setValue((Binary) value);
            } else {
                prop.clearValue();
            }
        } else if (Property.SELECTION_PROPERTY.equals(type) && value != null && value instanceof Selection[]) {
            prop.setCardinality("+");
            prop.setValue((Selection[]) value);
        } else if (!Property.SELECTION_PROPERTY.equals(type)) {
            if (value != null) {
                if ((value instanceof StringLiteral)) {
                    prop.setUnCheckedStringAsValue(((StringLiteral) value).toString());
                } else {
                    prop.setAnyValue(value);
                }
            } else {
                prop.clearValue();
            }
        }
        if (Property.DIR_IN.equals(direction) || Property.DIR_OUT.equals(direction)) {
            prop.setDirection(direction);
        }
        prop.setType(type);
        // Should not matter ;)
        prop.setName(actualName);
    }
    // Set subtype if not empty.
    if (subtype != null && !subtype.equals("")) {
        prop.setSubType(subtype);
    }
    // Set description if not empty.
    if (description != null && !description.equals("")) {
        prop.setDescription(description);
    }
    // Set length if not empty ( = -1) .
    if (length != -1) {
        prop.setLength(length);
    }
    return prop;
}
Also used : NavajoExpression(com.dexels.navajo.document.types.NavajoExpression) Message(com.dexels.navajo.document.Message) Selection(com.dexels.navajo.document.Selection) Binary(com.dexels.navajo.document.types.Binary) Property(com.dexels.navajo.document.Property) MappingException(com.dexels.navajo.script.api.MappingException)

Example 10 with Binary

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

the class NavajoDomStreamer method emitBinaryProperties.

private static void emitBinaryProperties(Message m, List<NavajoStreamEvent> list) {
    for (Property p : m.getAllProperties()) {
        if (Property.BINARY_PROPERTY.equals(p.getType())) {
            list.add(Events.binaryStarted(p.getName(), p.getLength(), Optional.ofNullable(p.getDescription()), Optional.ofNullable(p.getDirection()), Optional.ofNullable(p.getSubType())));
            Binary b = (Binary) p.getTypedValue();
            if (b != null) {
                try {
                    b.writeBase64(new Writer() {

                        @Override
                        public void write(char[] cbuf, int off, int len) throws IOException {
                            list.add(Events.binaryContent(new String(cbuf, off, len)));
                        }

                        @Override
                        public void flush() throws IOException {
                        }

                        @Override
                        public void close() throws IOException {
                        }
                    });
                } catch (IOException e) {
                    logger.error("Error: ", e);
                }
            }
            list.add(Events.binaryDone());
        }
    }
}
Also used : Binary(com.dexels.navajo.document.types.Binary) IOException(java.io.IOException) Property(com.dexels.navajo.document.Property) Writer(java.io.Writer)

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