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();
}
}
}
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);
}
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);
}
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;
}
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());
}
}
}
Aggregations